/// <summary>
        /// Gets a vector of log P(false) values.
        /// </summary>
        /// <returns></returns>
        public SparseVector GetLogProbFalseVector()
        {
            SparseVector sv = SparseVector.Zero(Count);

            sv.SetToFunction(LogOddsVector, x => MMath.LogisticLn(-x));
            return(sv);
        }
Beispiel #2
0
 /// <summary>Evidence message for VMP.</summary>
 /// <param name="sample">Constant value for <c>sample</c>.</param>
 /// <param name="logOdds">Constant value for <c>logOdds</c>.</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,logOdds))</c>. Adding up these values across all factors and variables gives the log-evidence estimate for VMP.</para>
 /// </remarks>
 public static double AverageLogFactor(bool sample, double logOdds)
 {
     if (sample)
     {
         return(MMath.LogisticLn(logOdds));
     }
     else
     {
         return(MMath.LogisticLn(-logOdds));
     }
 }
Beispiel #3
0
        /// <summary>
        /// Evidence message for VMP.
        /// </summary>
        /// <param name="sample">Incoming message from sample</param>
        /// <param name="logOdds">Incoming message from logOdds</param>
        /// <returns><c>sum_x marginal(x)*log(factor(x))</c></returns>
        /// <remarks><para>
        /// The formula for the result is <c>int log(f(x)) q(x) dx</c>
        /// where <c>x = (sample,logOdds)</c>.
        /// </para></remarks>
        public static double AverageLogFactor(Bernoulli sample, [Proper, SkipIfUniform] Gaussian logOdds)
        {
            if (logOdds.IsUniform())
            {
                return(0.0);
            }
            double m, v;

            logOdds.GetMeanAndVariance(out m, out v);
            double t = Math.Sqrt(m * m + v);
            double s = 2 * sample.GetProbTrue() - 1;              // probTrue - probFalse

            return(MMath.LogisticLn(t) + (s * m - t) / 2);
        }
Beispiel #4
0
        /// <summary>
        /// Evidence message for VMP.
        /// </summary>
        /// <param name="sample">Fixed value for sample</param>
        /// <param name="logOdds">Incoming message from logOdds</param>
        /// <returns><c>sum_x marginal(x)*log(factor(x))</c></returns>
        /// <remarks><para>
        /// The formula for the result is <c>int log(f(x)) q(x) dx</c>
        /// where <c>x = (sample,logOdds)</c>.
        /// </para></remarks>
        public static double AverageLogFactor(bool sample, [Proper, SkipIfUniform] Gaussian logOdds)
        {
            if (logOdds.IsUniform())
            {
                return(0.0);
            }
            double m, v;

            logOdds.GetMeanAndVariance(out m, out v);
            double t = Math.Sqrt(m * m + v);
            double s = sample ? 1 : -1;

            return(MMath.LogisticLn(t) + (s * m - t) / 2);
        }
        /// <summary>
        /// Gets the log probability of the given value under this distribution
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public double GetLogProb(IList <int> value)
        {
            SparseVector tv = SparseVector.Zero(LogOddsVector.Count);

            tv.SetToFunction(LogOddsVector, x => MMath.LogisticLn(-x));
            double logprob = tv.Sum();

            foreach (int index in value)
            {
                double logOdds = LogOddsVector[index];
                logprob -= MMath.LogisticLn(-logOdds);
                logprob += MMath.LogisticLn(logOdds);
            }
            return(logprob);
        }
        /// <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);

            // for consistency with XAverageLogarithm
            double t             = Math.Sqrt(m * m + v);
            double s             = -1;
            double eLogOneMinusP = MMath.LogisticLn(t) + (s * m - t) / 2;
            // E[log (1-sigma(x))] = E[log sigma(-x)] = -E[log(1+exp(x))]
            // 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));
        }
        /// <summary>
        /// Evidence message for VMP.
        /// </summary>
        /// <param name="sample">Incoming message from sample</param>
        /// <param name="logOdds">Incoming message from logOdds</param>
        /// <returns><c>sum_x marginal(x)*log(factor(x))</c></returns>
        /// <remarks><para>
        /// The formula for the result is <c>int log(f(x)) q(x) dx</c>
        /// where <c>x = (sample,logOdds)</c>.
        /// </para></remarks>
        public static double AverageLogFactor(Beta logistic, [Proper, SkipIfUniform] Gaussian x, Beta to_logistic)
        {
            double a     = logistic.TrueCount;
            double b     = logistic.FalseCount;
            double scale = a + b - 2;
            double shift = -(b - 1);
            // sigma(x) >= sigma(t) exp((x-t)/2 - a/2*(x^2 - t^2))
            double m, v;

            x.GetMeanAndVariance(out m, out v);
            double t               = Math.Sqrt(m * m + v);
            double lambda          = (t == 0) ? 0.25 : Math.Tanh(t / 2) / (2 * t);
            double boundOnLogSigma = MMath.LogisticLn(t) + (m - t) / 2.0 - .5 * lambda * (m * m + v - t * t);

            return(scale * boundOnLogSigma + shift * m - logistic.GetLogNormalizer() - to_logistic.GetAverageLog(logistic));
        }
Beispiel #8
0
 public double GetLogProb(int value)
 {
     if (value < 0 || value > TrialCount)
     {
         return(double.NegativeInfinity);
     }
     if (IsPointMass)
     {
         return((value == Point) ? 1.0 : 0.0);
     }
     return(MMath.GammaLn(TrialCount + 1) - A * MMath.GammaLn(value + 1) - B * MMath.GammaLn(TrialCount - value + 1) + value * LogOdds + TrialCount * MMath.LogisticLn(-LogOdds));
 }
Beispiel #9
0
 /// <summary>
 /// Gets the log probability of the binary variable being true
 /// </summary>
 /// <returns>log(p(x=true))</returns>
 public double GetLogProbTrue()
 {
     return(MMath.LogisticLn(LogOdds));
 }
Beispiel #10
0
 /// <summary>
 /// Computes the log-probability that A==B where p(A)=Logistic(x), p(B)=Logistic(y).
 /// </summary>
 /// <param name="x">The log-odds of variable A, which can be any real number from -Inf to Inf.</param>
 /// <param name="y">The log-odds of variable B, which can be any real number from -Inf to Inf.</param>
 /// <returns>The log-probability that A==B.</returns>
 /// <remarks>
 /// The result is Math.Log(0.5) if x=0 or y=0.
 /// </remarks>
 public static double LogProbEqual(double x, double y)
 {
     return(MMath.LogisticLn(LogitProbEqual(x, y)));
 }
Beispiel #11
0
 /// <summary>
 /// Evaluates the logarithm of the density function
 /// </summary>
 /// <param name="x">true or false</param>
 /// <returns>Log of the probability density for the given event</returns>
 public double GetLogProb(bool x)
 {
     return(MMath.LogisticLn(x ? LogOdds : -LogOdds));
 }
Beispiel #12
0
 /// <summary>
 /// Gets the log probability of the binary variable being false
 /// </summary>
 /// <returns>log(p(x=false))</returns>
 public double GetLogProbFalse()
 {
     return(MMath.LogisticLn(-LogOdds));
 }
Beispiel #13
0
 /// <summary>Evidence message for EP.</summary>
 /// <param name="sample">Constant value for <c>sample</c>.</param>
 /// <param name="logOdds">Constant value for <c>logOdds</c>.</param>
 /// <returns>Logarithm of the factor's average value across the given argument distributions.</returns>
 /// <remarks>
 ///   <para>The formula for the result is <c>log(factor(sample,logOdds))</c>.</para>
 /// </remarks>
 public static double LogAverageFactor(bool sample, double logOdds)
 {
     return(MMath.LogisticLn(sample ? logOdds : -logOdds));
 }