/// <summary> /// Evidence message for VMP /// </summary> /// <param name="sample">Incoming message from 'sample'. Must be a proper distribution. If any element is uniform, the result will be uniform.</param> /// <param name="SampleMean">Buffer 'SampleMean'.</param> /// <param name="SampleVariance">Buffer 'SampleVariance'.</param> /// <param name="mean">Incoming message from 'mean'. Must be a proper distribution. If any element is uniform, the result will be uniform.</param> /// <param name="MeanMean">Buffer 'MeanMean'.</param> /// <param name="MeanVariance">Buffer 'MeanVariance'.</param> /// <param name="precision">Constant value for 'precision'.</param> /// <returns>Average of the factor's log-value across the given argument distributions</returns> /// <remarks><para> /// The formula for the result is <c>sum_(sample,mean) p(sample,mean) log(factor(sample,mean,precision))</c>. /// Adding up these values across all factors and variables gives the log-evidence estimate for VMP. /// </para></remarks> /// <exception cref="ImproperMessageException"><paramref name="sample"/> is not a proper distribution</exception> /// <exception cref="ImproperMessageException"><paramref name="mean"/> is not a proper distribution</exception> public static double AverageLogFactor( [Proper] VectorGaussian sample, [Fresh] Vector SampleMean, [Fresh] PositiveDefiniteMatrix SampleVariance, [Proper] VectorGaussian mean, [Fresh] Vector MeanMean, [Fresh] PositiveDefiniteMatrix MeanVariance, PositiveDefiniteMatrix precision) { if (sample.IsPointMass) return AverageLogFactor(sample.Point, mean, MeanMean, MeanVariance, precision); if (mean.IsPointMass) return AverageLogFactor(sample, SampleMean, SampleVariance, mean.Point, precision); return ComputeAverageLogFactor(SampleMean, SampleVariance, MeanMean, MeanVariance, precision.LogDeterminant(ignoreInfinity: true), precision); }
/// <summary> /// Evidence message for VMP /// </summary> /// <param name="sample">Constant value for 'sample'.</param> /// <param name="mean">Constant value for 'mean'.</param> /// <param name="precision">Constant value for 'precision'.</param> /// <returns>Average of the factor's log-value across the given argument distributions</returns> /// <remarks><para> /// The formula for the result is <c>log(factor(sample,mean,precision))</c>. /// Adding up these values across all factors and variables gives the log-evidence estimate for VMP. /// </para></remarks> public static double AverageLogFactor(Vector sample, Vector mean, PositiveDefiniteMatrix precision) { return ComputeAverageLogFactor(sample, mean, precision.LogDeterminant(ignoreInfinity: true), precision); }
/// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="WishartFromShapeAndRateOp_Laplace2"]/message_doc[@name="LogAverageFactor(Wishart, double, Wishart, Wishart)"]/*'/> public static double LogAverageFactor(Wishart sample, double shape, Wishart rate, [Fresh] Wishart to_sample) { // int_R f(Y,R) p(R) dR = |Y|^(a-c) |Y+B_r|^-(a+a_r) Gamma_d(a+a_r)/Gamma_d(a)/Gamma_d(a_r) |B_r|^a_r int dim = sample.Dimension; double c = 0.5 * (dim + 1); double shape2 = shape + rate.Shape; Wishart samplePost = sample * to_sample; PositiveDefiniteMatrix y = samplePost.GetMean(); PositiveDefiniteMatrix yPlusBr = y + rate.Rate; double result = (shape - c) * y.LogDeterminant() - shape2 * yPlusBr.LogDeterminant() + sample.GetLogProb(y) - samplePost.GetLogProb(y); result += MMath.GammaLn(shape2, dim) - MMath.GammaLn(shape, dim) - MMath.GammaLn(rate.Shape, dim); result += rate.Shape * rate.Rate.LogDeterminant(); return(result); }
/// <summary> /// VMP message to 'product' /// </summary> /// <param name="B">Incoming message from 'a'. Must be a proper distribution. If uniform, the result will be uniform.</param> /// <param name="A">Incoming message from 'b'. Must be a proper distribution. If uniform, the result will be uniform.</param> /// <returns>The outgoing VMP message to the 'product' argument</returns> /// <remarks><para> /// The outgoing message is a distribution matching the moments of 'product' as the random arguments are varied. /// The formula is <c>proj[sum_(a,b) p(a,b) factor(product,a,b)]</c>. /// </para></remarks> /// <exception cref="ImproperMessageException"><paramref name="B"/> is not a proper distribution</exception> /// <exception cref="ImproperMessageException"><paramref name="A"/> is not a proper distribution</exception> public static Wishart ProductAverageLogarithm([SkipIfUniform] Wishart A, [SkipIfUniform] Gamma B, Wishart result) { if (B.IsPointMass) return ProductAverageLogarithm(A, B.Point, result); // E[x] = E[a]*E[b] // E[log(x)] = E[log(a)]+E[log(b)] PositiveDefiniteMatrix m = new PositiveDefiniteMatrix(A.Dimension, A.Dimension); A.GetMean(m); m.Scale(B.GetMean()); double meanLogDet = A.Dimension*B.GetMeanLog() + A.GetMeanLogDeterminant(); if (m.LogDeterminant() < meanLogDet) throw new MatrixSingularException(m); return Wishart.FromMeanAndMeanLogDeterminant(m, meanLogDet, result); }
/// <summary> /// Update the buffer 'PrecisionMeanLogDet' /// </summary> /// <param name="Precision">Incoming message from 'precision'.</param> /// <returns>New value of buffer 'PrecisionMeanLogDet'</returns> /// <remarks><para> /// /// </para></remarks> public static double PrecisionMeanLogDet([Proper] PositiveDefiniteMatrix Precision) { return(Precision.LogDeterminant()); }