/// <summary>EP message to <c>probTrue</c>.</summary>
        /// <param name="sample">Incoming message from <c>sample</c>. Must be a proper distribution. If any element is uniform, the result will be uniform.</param>
        /// <param name="probTrue">Incoming message from <c>probTrue</c>.</param>
        /// <param name="result">Modified to contain the outgoing message.</param>
        /// <returns>
        ///   <paramref name="result" />
        /// </returns>
        /// <remarks>
        ///   <para>The outgoing message is a distribution matching the moments of <c>probTrue</c> as the random arguments are varied. The formula is <c>proj[p(probTrue) sum_(sample) p(sample) factor(sample,probTrue)]/p(probTrue)</c>.</para>
        /// </remarks>
        /// <exception cref="ImproperMessageException">
        ///   <paramref name="sample" /> is not a proper distribution.</exception>
        public static SparseBetaList ProbTrueAverageConditional([SkipIfUniform] BernoulliIntegerSubset sample, SparseBetaList probTrue, SparseBetaList result)
        {
            Func <Bernoulli, Beta, Beta> f = BernoulliFromBetaOp.ProbTrueAverageConditional;

            result.SetTo(f.Map(sample.SparseBernoulliList, probTrue));
            return(result);
        }
        /// <summary>
        /// The expected logarithm of that distribution under this distribution.
        /// </summary>
        /// <param name="that">The distribution to take the logarithm of.</param>
        /// <returns><c>sum_x this.Evaluate(x)*Math.Log(that.Evaluate(x))</c></returns>
        /// <remarks>This is also known as the cross entropy.</remarks>
        public double GetAverageLog(BernoulliIntegerSubset that)
        {
            var res  = SparseVector.Zero(LogOddsVector.Count);
            var res2 = SparseVector.Zero(LogOddsVector.Count);

            res.SetToFunction(LogOddsVector, that.GetLogProbTrueVector(), (logpr, logProbTrue) => double.IsNegativeInfinity(logpr) ?0 :MMath.Logistic(logpr) * logProbTrue);
            res2.SetToFunction(LogOddsVector, that.GetLogProbFalseVector(), (logpr, logProbFalse) => double.IsPositiveInfinity(logpr) ?0 :MMath.Logistic(-logpr) * logProbFalse);
            return(res.Sum() + res2.Sum());
        }
 /// <summary>
 /// Sets this BernoulliIntegerSubset distribution to the weighted sum of two other such distributions
 /// </summary>
 /// <param name="weight1"></param>
 /// <param name="value1"></param>
 /// <param name="weight2"></param>
 /// <param name="value2"></param>
 /// <remarks>Not yet implemented</remarks>
 public void SetToSum(double weight1, BernoulliIntegerSubset value1, double weight2, BernoulliIntegerSubset value2)
 {
     if (weight1 + weight2 == 0)
     {
         SetToUniform();
     }
     else
     {
         LogOddsVector.SetToFunction(value1.GetProbTrueVector(), value2.GetProbTrueVector(), (x, y) => MMath.Logit((weight1 * x + weight2 * y) / (weight1 + weight2)));
     }
 }
        public void BernoulliIntegerSubsetArithmetic()
        {
            double tolerance     = 1e-10;
            var    commonValue1  = new Bernoulli(0.1);
            var    commonValue2  = new Bernoulli(0.2);
            var    specialValue1 = new Bernoulli(0.7);
            var    specialValue2 = new Bernoulli(0.8);
            var    specialValue3 = new Bernoulli(0.9);

            var listSize             = 100;
            var sparseBernoulliList1 = SparseBernoulliList.Constant(listSize, commonValue1);
            var sparseBernoulliList2 = SparseBernoulliList.Constant(listSize, commonValue2);

            sparseBernoulliList1[20] = specialValue1;
            sparseBernoulliList1[55] = specialValue2;
            sparseBernoulliList2[25] = specialValue2;
            sparseBernoulliList2[55] = specialValue3;
            var bernoulliIntegerSubset1 = BernoulliIntegerSubset.FromSparseList(sparseBernoulliList1);
            var bernoulliIntegerSubset2 = BernoulliIntegerSubset.FromSparseList(sparseBernoulliList2);

            // Product
            var product = bernoulliIntegerSubset1 * bernoulliIntegerSubset2;

            Assert.Equal(3, product.SparseBernoulliList.SparseValues.Count);
            Assert.Equal(commonValue1 * commonValue2, product.SparseBernoulliList.CommonValue);
            Assert.Equal(specialValue1 * commonValue2, product.SparseBernoulliList[20]);
            Assert.Equal(commonValue1 * specialValue2, product.SparseBernoulliList[25]);
            Assert.Equal(specialValue2 * specialValue3, product.SparseBernoulliList[55]);

            // Ratio
            var ratio = bernoulliIntegerSubset1 / bernoulliIntegerSubset2;

            Assert.Equal(2, ratio.SparseBernoulliList.SparseValues.Count);
            Assert.Equal((commonValue1 / commonValue2).GetProbTrue(), ratio.SparseBernoulliList.CommonValue.GetProbTrue(), tolerance);
            Assert.Equal((specialValue1 / commonValue2).GetProbTrue(), ratio.SparseBernoulliList[20].GetProbTrue(), tolerance);
            Assert.Equal((commonValue1 / specialValue2).GetProbTrue(), ratio.SparseBernoulliList[25].GetProbTrue(), tolerance);
            Assert.Equal((specialValue2 / specialValue3).GetProbTrue(), ratio.SparseBernoulliList[55].GetProbTrue(), tolerance);

            // Power
            var exponent = 1.2;
            var power    = bernoulliIntegerSubset1 ^ exponent;

            Assert.Equal(2, power.SparseBernoulliList.SparseValues.Count);
            Assert.Equal(commonValue1 ^ exponent, power.SparseBernoulliList.CommonValue);
            Assert.Equal(specialValue1 ^ exponent, power.SparseBernoulliList[20]);
            Assert.Equal(specialValue2 ^ exponent, power.SparseBernoulliList[55]);
        }
        /// <summary>VMP message to <c>sample</c>.</summary>
        /// <param name="probTrue">Incoming message from <c>probTrue</c>. Must be a proper distribution. If any element is uniform, the result will be uniform.</param>
        /// <param name="result">Modified to contain the outgoing message.</param>
        /// <returns>
        ///   <paramref name="result" />
        /// </returns>
        /// <remarks>
        ///   <para>The outgoing message is the exponential of the average log-factor value, where the average is over all arguments except <c>sample</c>. The formula is <c>exp(sum_(probTrue) p(probTrue) log(factor(sample,probTrue)))</c>.</para>
        /// </remarks>
        /// <exception cref="ImproperMessageException">
        ///   <paramref name="probTrue" /> is not a proper distribution.</exception>
        public static BernoulliIntegerSubset SampleAverageLogarithm([SkipIfUniform] SparseBetaList probTrue, BernoulliIntegerSubset result)
        {
            Func <Beta, Bernoulli> f = BernoulliFromBetaOp.SampleAverageLogarithm;

            result.SetTo(f.Map(probTrue));
            return(result);
        }
        /// <summary>VMP message to <c>sample</c>.</summary>
        /// <param name="probTrue">Constant value for <c>probTrue</c>.</param>
        /// <param name="result">Modified to contain the outgoing message.</param>
        /// <returns>
        ///   <paramref name="result" />
        /// </returns>
        /// <remarks>
        ///   <para>The outgoing message is the factor viewed as a function of <c>sample</c> conditioned on the given values.</para>
        /// </remarks>
        public static BernoulliIntegerSubset SampleAverageLogarithm(ISparseList <double> probTrue, BernoulliIntegerSubset result)
        {
            Func <double, Bernoulli> f = BernoulliFromBetaOp.SampleAverageLogarithm;

            result.SetTo(f.Map(probTrue));
            return(result);
        }
        /// <summary>Evidence message for VMP.</summary>
        /// <param name="sample">Incoming message from <c>sample</c>.</param>
        /// <param name="probTrue">Constant value for <c>probTrue</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>sum_(sample) p(sample) log(factor(sample,probTrue))</c>. Adding up these values across all factors and variables gives the log-evidence estimate for VMP.</para>
        /// </remarks>
        public static double AverageLogFactor(BernoulliIntegerSubset sample, ISparseList <double> probTrue)
        {
            Func <Bernoulli, double, double> f = BernoulliFromBetaOp.AverageLogFactor;

            return(f.Map(sample.SparseBernoulliList, probTrue).EnumerableSum(x => x));
        }
Beispiel #8
0
 /// <summary>VMP message to <c>set</c>.</summary>
 /// <param name="i">Constant value for <c>i</c>.</param>
 /// <param name="result">Modified to contain the outgoing message.</param>
 /// <returns>
 ///   <paramref name="result" />
 /// </returns>
 /// <remarks>
 ///   <para>The outgoing message is the factor viewed as a function of <c>set</c> conditioned on the given values.</para>
 /// </remarks>
 public static BernoulliIntegerSubset SetAverageLogarithm(int i, BernoulliIntegerSubset result)
 {
     result.SetToUniform();
     result.SparseBernoulliList[i] = Bernoulli.PointMass(true);
     return(result);
 }
        /// <summary>Gibbs message to <c>sample</c>.</summary>
        /// <param name="probTrue">Constant value for <c>probTrue</c>.</param>
        /// <param name="result">Modified to contain the outgoing message.</param>
        /// <returns>
        ///   <paramref name="result" />
        /// </returns>
        /// <remarks>
        ///   <para>The outgoing message is the factor viewed as a function of <c>sample</c> conditioned on the given values.</para>
        /// </remarks>
        public static BernoulliIntegerSubset SampleConditional(ISparseList <double> probTrue, BernoulliIntegerSubset result)
        {
            Func <double, Bernoulli> f = BernoulliFromBetaOp.SampleConditional;

            result.SetTo(f.Map(probTrue));
            return(result);
        }
        /// <summary>Evidence message for EP.</summary>
        /// <param name="sample">Incoming message from <c>sample</c>.</param>
        /// <param name="to_sample">Outgoing message to <c>sample</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(sum_(sample) p(sample) factor(sample,probTrue))</c>.</para>
        /// </remarks>
        public static double LogAverageFactor(BernoulliIntegerSubset sample, [Fresh] BernoulliIntegerSubset to_sample)
        {
            Func <Bernoulli, Bernoulli, double> f = BernoulliFromBetaOp.LogAverageFactor;

            return(f.Map(sample.SparseBernoulliList, to_sample.SparseBernoulliList).EnumerableSum(x => x));
        }
 /// <summary>
 /// VMP message to 'probsTrue'
 /// </summary>
 /// <param name="sample">Incoming message from 'sample'. Must be a proper distribution.  If any element is uniform, the result will be uniform.</param>
 /// <returns>The outgoing VMP message to the 'probsTrue' argument</returns>
 /// <remarks><para>
 /// The outgoing message is the exponential of the average log-factor value, where the average is over all arguments except 'probsTrue'.
 /// The formula is <c>exp(sum_(sample) p(sample) log(factor(sample,probsTrue)))</c>.
 /// </para></remarks>
 /// <exception cref="ImproperMessageException"><paramref name="sample"/> is not a proper distribution</exception>
 public static SparseBetaList ProbsTrueAverageLogarithm([Proper] BernoulliIntegerSubset sample)
 {
     return(SparseBernoulliFromBetaOp.ProbsTrueAverageLogarithm(sample));
 }
 /// <summary>
 /// Sets this BernoulliIntegerSubset distribution to the power another such distribution
 /// </summary>
 /// <param name="value"></param>
 /// <param name="exponent"></param>
 public void SetToPower(BernoulliIntegerSubset value, double exponent)
 {
     throw new NotImplementedException();
 }
 /// <summary>
 /// Sets this BernoulliIntegerSubset distribution to the ratio of two other such distributions
 /// </summary>
 /// <param name="numerator"></param>
 /// <param name="denominator"></param>
 public void SetToRatio(BernoulliIntegerSubset numerator, BernoulliIntegerSubset denominator)
 {
     LogOddsVector.SetToDifference(numerator.LogOddsVector, denominator.LogOddsVector);
 }
 /// <summary>
 /// Sets this BernoulliIntegerSubset distribution to another such distribution
 /// </summary>
 /// <param name="value"></param>
 public void SetTo(BernoulliIntegerSubset value)
 {
     LogOddsVector.SetTo(value.LogOddsVector);
 }
 /// <summary>
 /// Sets this BernoulliIntegerSubset distribution to a product of two other such distributions
 /// </summary>
 /// <param name="a"></param>
 /// <param name="b"></param>
 public void SetToProduct(BernoulliIntegerSubset a, BernoulliIntegerSubset b)
 {
     LogOddsVector.SetToSum(a.LogOddsVector, b.LogOddsVector);
 }
        /// <summary>
        /// Clones this object.
        /// </summary>
        /// <returns></returns>
        public object Clone()
        {
            BernoulliIntegerSubset clone = new BernoulliIntegerSubset((SparseVector)LogOddsVector.Clone());

            return(clone);
        }
 /// <summary>VMP message to <c>probTrue</c>.</summary>
 /// <param name="sample">Constant value for <c>sample</c>.</param>
 /// <param name="result">Modified to contain the outgoing message.</param>
 /// <returns>
 ///   <paramref name="result" />
 /// </returns>
 /// <remarks>
 ///   <para>The outgoing message is the factor viewed as a function of <c>probTrue</c> conditioned on the given values.</para>
 /// </remarks>
 public static SparseBetaList ProbTrueAverageLogarithm(IList <int> sample, SparseBetaList result)
 {
     result.SetToFunction(BernoulliIntegerSubset.SubsetToList(sample, result.Count), s => BernoulliFromBetaOp.ProbTrueAverageLogarithm(s));
     return(result);
 }
 public static double LogEvidenceRatio(BernoulliIntegerSubset sample, ISparseList <double> probTrue)
 {
     return(0.0);
 }
 /// <summary>VMP message to <c>probTrue</c>.</summary>
 /// <param name="sample">Incoming message from <c>sample</c>.</param>
 /// <param name="result">Modified to contain the outgoing message.</param>
 /// <returns>
 ///   <paramref name="result" />
 /// </returns>
 /// <remarks>
 ///   <para>The outgoing message is the exponential of the average log-factor value, where the average is over all arguments except <c>probTrue</c>. The formula is <c>exp(sum_(sample) p(sample) log(factor(sample,probTrue)))</c>.</para>
 /// </remarks>
 public static SparseBetaList ProbTrueAverageLogarithm(BernoulliIntegerSubset sample, SparseBetaList result)
 {
     result.SetToFunction(sample.SparseBernoulliList, s => BernoulliFromBetaOp.ProbTrueAverageLogarithm(s));
     return(result);
 }
 /// <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="probsTrue">Constant value 'probsTrue'.</param>
 /// <param name="MeanLog"></param>
 /// <param name="MeanLogOneMinus"></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_(probsTrue) p(probsTrue) log(factor(sample,probsTrue))</c>.
 /// Adding up these values across all factors and variables gives the log-evidence estimate for VMP.
 /// </para></remarks>
 /// <exception cref="ImproperMessageException"><paramref name="probsTrue"/> is not a proper distribution</exception>
 public static double AverageLogFactor([Proper] BernoulliIntegerSubset sample, SparseVector probsTrue, SparseVector MeanLog, SparseVector MeanLogOneMinus)
 {
     return(SparseBernoulliFromBetaOp.AverageLogFactor(sample, probsTrue, MeanLog, MeanLogOneMinus));
 }
        /// <summary>Evidence message for EP.</summary>
        /// <param name="sample">Constant value for <c>sample</c>.</param>
        /// <param name="probTrue">Incoming message from <c>probTrue</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(sum_(probTrue) p(probTrue) factor(sample,probTrue))</c>.</para>
        /// </remarks>
        public static double LogAverageFactor(IList <int> sample, SparseBetaList probTrue)
        {
            Func <bool, Beta, double> f = BernoulliFromBetaOp.LogAverageFactor;

            return(f.Map(BernoulliIntegerSubset.SubsetToList(sample, probTrue.Count), probTrue).EnumerableSum(x => x));
        }
 /// <summary>
 /// Gets the log of the integral of the product of this BernoulliIntegerSubset distribution and another such distribution
 /// </summary>
 /// <param name="that"></param>
 /// <returns></returns>
 /// <remarks>Not yet implemented</remarks>
 public double GetLogAverageOf(BernoulliIntegerSubset that)
 {
     throw new NotImplementedException();
 }
        /// <summary>Evidence message for EP.</summary>
        /// <param name="sample">Constant value for <c>sample</c>.</param>
        /// <param name="probTrue">Constant value for <c>probTrue</c>.</param>
        /// <returns>Logarithm of the factor's contribution the EP model evidence.</returns>
        /// <remarks>
        ///   <para>The formula for the result is <c>log(factor(sample,probTrue))</c>. Adding up these values across all factors and variables gives the log-evidence estimate for EP.</para>
        /// </remarks>
        public static double LogEvidenceRatio(IList <int> sample, ISparseList <double> probTrue)
        {
            Func <bool, double, double> f = BernoulliFromBetaOp.LogEvidenceRatio;

            return(f.Map(BernoulliIntegerSubset.SubsetToList(sample, probTrue.Count), probTrue).EnumerableSum(x => x));
        }
Beispiel #24
0
 /// <summary>
 /// VMP message to 'set'
 /// </summary>
 /// <param name="i">Constant value for 'i'.</param>
 /// <param name="result">Modified to contain the outgoing message</param>
 /// <returns><paramref name="result"/></returns>
 /// <remarks><para>
 /// The outgoing message is the factor viewed as a function of 'set' conditioned on the given values.
 /// </para></remarks>
 public static BernoulliIntegerSubset SetAverageLogarithm(int i, BernoulliIntegerSubset result)
 {
     result.SetToUniform();
     result.LogOddsVector[i] = double.PositiveInfinity;
     return(result);
 }