/// <summary>
        /// VMP message to 'logistic'
        /// </summary>
        /// <param name="x">Incoming message from 'x'. Must be a proper distribution.  If uniform, the result will be uniform.</param>
        /// <returns>The outgoing VMP message to the 'logistic' argument</returns>
        /// <remarks><para>
        /// The outgoing message is a distribution matching the moments of 'logistic' as the random arguments are varied.
        /// The formula is <c>proj[sum_(x) p(x) factor(logistic,x)]</c>.
        /// </para></remarks>
        /// <exception cref="ImproperMessageException"><paramref name="x"/> is not a proper distribution</exception>
        public static Beta LogisticAverageLogarithm([Proper] Gaussian x)
        {
            double m, v;

            x.GetMeanAndVariance(out m, out v);

#if true
            // for consistency with XAverageLogarithm
            double eLogOneMinusP = BernoulliFromLogOddsOp.AverageLogFactor(false, x);
#else
            // E[log (1-sigma(x))] = E[log sigma(-x)] = -E[log(1+exp(x))]
            double eLogOneMinusP = -MMath.Log1PlusExpGaussian(m, v);
#endif
            // E[log sigma(x)] = -E[log(1+exp(-x))] = -E[log(1+exp(x))-x] = -E[log(1+exp(x))] + E[x]
            double eLogP = eLogOneMinusP + m;
            return(Beta.FromMeanLogs(eLogP, eLogOneMinusP));
        }
예제 #2
0
        /// <summary>
        /// Evidence message for VMP
        /// </summary>
        /// <param name="sample">Constant value for 'sample'.</param>
        /// <param name="logProbs">Incoming message from 'logProbs'. Must be a proper distribution.  If any element is uniform, the result will be uniform.</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_(logProbs) p(logProbs) log(factor(sample,logProbs))</c>.
        /// Adding up these values across all factors and variables gives the log-evidence estimate for VMP.
        /// </para></remarks>
        /// <exception cref="ImproperMessageException"><paramref name="logProbs"/> is not a proper distribution</exception>
        public static double AverageLogFactor(int sample, [Proper] IList <Gaussian> logProbs)
        {
            double result = 0;
            double ms, vs;

            logProbs[sample].GetMeanAndVariance(out ms, out vs);
            for (int k = 0; k < logProbs.Count; k++)
            {
                if (k == sample)
                {
                    continue;
                }
                double m, v;
                logProbs[k].GetMeanAndVariance(out m, out v);
                Gaussian logProb = new Gaussian(ms - m, vs + v);
                result += BernoulliFromLogOddsOp.AverageLogFactor(true, logProb);
            }
            return(result);
        }