/// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="GammaRatioOp_Laplace"]/message_doc[@name="AAverageConditional(Gamma, Gamma, Gamma, Gamma)"]/*'/> public static Gamma AAverageConditional(Gamma ratio, Gamma A, [SkipIfUniform] Gamma B, Gamma q) { if (ratio.IsPointMass) { return(GammaRatioOp.AAverageConditional(ratio.Point, B)); } if (B.IsPointMass) { return(GammaRatioOp.AAverageConditional(ratio, B.Point)); } if (A.IsPointMass) { throw new NotImplementedException(); } double aMean, aVariance; double x = q.GetMean(); double x2 = x * x; double p = 1 / (ratio.Rate + A.Rate * x); double p2 = p * p; double shape2 = GammaFromShapeAndRateOp_Slow.AddShapesMinus1(ratio.Shape, A.Shape); // aMean = shape2/(y_r/b + a_r) // aVariance = E[shape2*(shape2+1)/(y_r/b + a_r)^2] - aMean^2 = var(shape2/(y_r/b + a_r)) + E[shape2/(y_r/b + a_r)^2] // = shape2^2*var(1/(y_r/b + a_r)) + shape2*(var(1/(y_r/b + a_r)) + (aMean/shape2)^2) double[] g = new double[] { x *p, ratio.Rate *p2, -2 *p2 *p *ratio.Rate *A.Rate, 6 *p2 *p2 *ratio.Rate *A.Rate *A.Rate }; double pMean, pVariance; GaussianOp_Laplace.LaplaceMoments(q, g, dlogfs(x, ratio, A), out pMean, out pVariance); aMean = shape2 * pMean; aVariance = shape2 * shape2 * pVariance + shape2 * (pVariance + pMean * pMean); Gamma aMarginal = Gamma.FromMeanAndVariance(aMean, aVariance); Gamma result = new Gamma(); result.SetToRatio(aMarginal, A, GammaProductOp_Laplace.ForceProper); if (double.IsNaN(result.Shape) || double.IsNaN(result.Rate)) { throw new InferRuntimeException("result is nan"); } return(result); }
/// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="GammaRatioVmpOp"]/message_doc[@name="AAverageLogarithm(double, double)"]/*'/> public static Gamma AAverageLogarithm(double ratio, double B) { return(GammaRatioOp.AAverageConditional(ratio, B)); }
/// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="GammaRatioVmpOp"]/message_doc[@name="AAverageLogarithm(Gamma, double)"]/*'/> public static Gamma AAverageLogarithm([SkipIfUniform] Gamma ratio, double B) { return(GammaRatioOp.AAverageConditional(ratio, B)); }