/// <summary>
        /// VMP message to 'sample'
        /// </summary>
        /// <param name="probsTrue">Incoming message from 'probsTrue'. Must be a proper distribution.  If any element is uniform, the result will be uniform.</param>
        /// <returns>The outgoing VMP message to the 'sample' argument</returns>
        /// <remarks><para>
        /// The outgoing message is the exponential of the average log-factor value, where the average is over all arguments except 'sample'.
        /// The formula is <c>exp(sum_(probsTrue) p(probsTrue) log(factor(sample,probsTrue)))</c>.
        /// </para></remarks>
        /// <exception cref="ImproperMessageException"><paramref name="probsTrue"/> is not a proper distribution</exception>
        public static BernoulliIntegerSubset SampleAverageLogarithm([SkipIfUniform] SparseBetaList probsTrue)
        {
            var logOdds = SparseVector.Zero(probsTrue.Count);

            logOdds.SetToFunction(probsTrue.TrueCounts, probsTrue.FalseCounts, SparseBernoulliFromBetaOp.ComputeLogOdds);
            return(new BernoulliIntegerSubset(logOdds));
        }
        /// <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);
        }
        //-- VMP -------------------------------------------------------------------------------------------

        /// <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">Incoming message from 'probsTrue'. Must be a proper distribution.  If any element is uniform, the result will be uniform.</param>
        /// <param name="MeanLog">Buffer 'MeanLog'.</param>
        /// <param name="MeanLogOneMinus">Buffer '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_(sample,probsTrue) p(sample,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="sample"/> is not a proper distribution</exception>
        /// <exception cref="ImproperMessageException"><paramref name="probsTrue"/> is not a proper distribution</exception>
        public static double AverageLogFactor([Proper] SparseBernoulliListBase sample,
                                              [Proper] SparseBetaList probsTrue, SparseVector MeanLog, SparseVector MeanLogOneMinus)
        {
            //var MeanLogOneMinus = probsTrue.GetMeanLogOneMinus();
            var p   = sample.GetProbTrueVector();
            var res = p * MeanLog + (1 - p) * MeanLogOneMinus;

            return(res.Sum());
        }
        public void SparseDistributionListTolerances()
        {
            var    bern  = SparseBernoulliList.FromSize(1);
            var    beta  = SparseBetaList.FromSize(1);
            var    gamma = SparseGammaList.FromSize(1);
            var    gauss = SparseGaussianList.FromSize(1);
            double origBernDefaultTolerance  = SparseBernoulliList.DefaultTolerance;
            double origBetaDefaultTolerance  = SparseBetaList.DefaultTolerance;
            double origGammaDefaultTolerance = SparseGammaList.DefaultTolerance;
            double origGaussDefaultTolerance = SparseGaussianList.DefaultTolerance;

            Assert.Equal(origBernDefaultTolerance, bern.Tolerance);
            Assert.Equal(origBetaDefaultTolerance, beta.Tolerance);
            Assert.Equal(origGammaDefaultTolerance, gamma.Tolerance);
            Assert.Equal(origGaussDefaultTolerance, gauss.Tolerance);

            double newBernDefaultTolerance  = 0.1;
            double newBetaDefaultTolerance  = 0.2;
            double newGammaDefaultTolerance = 0.3;
            double newGaussDefaultTolerance = 0.4;

            try
            {
                // Checks that we can maintain different default tolerances
                // on different specializations of the generic base class
                SparseBernoulliList.DefaultTolerance = newBernDefaultTolerance;
                SparseBetaList.DefaultTolerance      = newBetaDefaultTolerance;
                SparseGammaList.DefaultTolerance     = newGammaDefaultTolerance;
                SparseGaussianList.DefaultTolerance  = newGaussDefaultTolerance;

                Assert.Equal(newBernDefaultTolerance, SparseBernoulliList.DefaultTolerance);
                Assert.Equal(newBetaDefaultTolerance, SparseBetaList.DefaultTolerance);
                Assert.Equal(newGammaDefaultTolerance, SparseGammaList.DefaultTolerance);
                Assert.Equal(newGaussDefaultTolerance, SparseGaussianList.DefaultTolerance);

                // Now check that the default tolerance gets picked up by the factory methods.
                bern  = SparseBernoulliList.FromSize(1);
                beta  = SparseBetaList.FromSize(1);
                gamma = SparseGammaList.FromSize(1);
                gauss = SparseGaussianList.FromSize(1);

                Assert.Equal(newBernDefaultTolerance, bern.Tolerance);
                Assert.Equal(newBetaDefaultTolerance, beta.Tolerance);
                Assert.Equal(newGammaDefaultTolerance, gamma.Tolerance);
                Assert.Equal(newGaussDefaultTolerance, gauss.Tolerance);
            }

            finally
            {
                // Now revert back so that we don't spoil the other tests
                SparseBernoulliList.DefaultTolerance = origBernDefaultTolerance;
                SparseBetaList.DefaultTolerance      = origBetaDefaultTolerance;
                SparseGammaList.DefaultTolerance     = origGammaDefaultTolerance;
                SparseGaussianList.DefaultTolerance  = origGaussDefaultTolerance;
            }
        }
        /// <summary>
        /// Evidence message for VMP
        /// </summary>
        /// <param name="sample">Constant value for 'sample'.</param>
        /// <param name="probsTrue">Incoming message from 'probsTrue'. Must be a proper distribution.  If any element is uniform, the result will be uniform.</param>
        /// <param name="SumMeanLogOneMinus"></param>
        /// <param name="MeanLogMinusMeanLogOneMinus">Buffer 'MeanLogMinusMeanLogOneMinus'.</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(IList <int> sample, [Proper] SparseBetaList probsTrue, double SumMeanLogOneMinus, SparseVector MeanLogMinusMeanLogOneMinus)
        {
            //var MeanLogOneMinus = probsTrue.GetMeanLogOneMinus();
            double ev = SumMeanLogOneMinus;

            // assume 'sample' is very sparse
            foreach (int i in sample)
            {
                ev += MeanLogMinusMeanLogOneMinus[i];
            }
            return(ev);
        }
        /// <summary>
        /// VMP message to 'probsTrue'
        /// </summary>
        /// <param name="sample">Constant value for 'sample'.</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 'probsTrue' conditioned on the given values.
        /// </para></remarks>
        public static SparseBetaList ProbsTrueAverageLogarithm(IList <int> sample, SparseBetaList result)
        {
            var trueCounts  = result.TrueCounts;
            var falseCounts = result.FalseCounts;

            trueCounts.SetAllElementsTo(1.0);
            falseCounts.SetAllElementsTo(2.0);
            foreach (int i in sample)
            {
                trueCounts[i]  = 2;
                falseCounts[i] = 1;
            }
            return(result);
        }
        /// <summary>
        /// Evidence message for VMP
        /// </summary>
        /// <param name="sample">Constant value for 'sample'.</param>
        /// <param name="probsTrue">Incoming message from 'probsTrue'. Must be a proper distribution.  If any element is uniform, the result will be uniform.</param>
        /// <param name="MeanLogOneMinus">Buffer '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(IList <bool> sample, [Proper] SparseBetaList probsTrue, SparseVector MeanLogOneMinus)
        {
            //var MeanLogOneMinus = probsTrue.GetMeanLogOneMinus();
            double ev    = MeanLogOneMinus.Sum();
            int    count = sample.Count;

            // assume 'sample' is very sparse
            bool[] sampleAsArray = (bool[])sample;
            for (int i = 0; i < count; i++)
            {
                if (!sampleAsArray[i])
                {
                    continue;
                }
                ev -= MeanLogOneMinus[i];
                ev += SparseBetaList.ComputeMeanLog(probsTrue.TrueCounts[i], probsTrue.FalseCounts[i]);
            }
            return(ev);
        }
        /// <summary>Evidence message for VMP.</summary>
        /// <param name="sample">Incoming message from <c>sample</c>.</param>
        /// <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>
        /// <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,probTrue) p(sample,probTrue) log(factor(sample,probTrue))</c>. Adding up these values across all factors and variables gives the log-evidence estimate for VMP.</para>
        /// </remarks>
        /// <exception cref="ImproperMessageException">
        ///   <paramref name="probTrue" /> is not a proper distribution.</exception>
        public static double AverageLogFactor(BernoulliIntegerSubset sample, [Proper] SparseBetaList probTrue)
        {
            Func <Bernoulli, Beta, double> f = BernoulliFromBetaOp.AverageLogFactor;

            return(f.Map(sample.SparseBernoulliList, probTrue).EnumerableSum(x => x));
        }
        /// <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);
        }
Exemple #10
0
 public static double LogEvidenceRatio(SparseBernoulliList sample, SparseBetaList probTrue)
 {
     return(0.0);
 }
Exemple #11
0
 /// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="SparseBernoulliFromBetaOp"]/message_doc[@name="ProbTrueAverageLogarithm(SparseBernoulliList, SparseBetaList)"]/*'/>
 public static SparseBetaList ProbTrueAverageLogarithm(SparseBernoulliList sample, SparseBetaList result)
 {
     result.SetToFunction(sample, s => BernoulliFromBetaOp.ProbTrueAverageLogarithm(s));
     return(result);
 }
Exemple #12
0
        /// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="SparseBernoulliFromBetaOp"]/message_doc[@name="AverageLogFactor(ISparseList{bool}, SparseBetaList)"]/*'/>
        public static double AverageLogFactor(ISparseList <bool> sample, [Proper] SparseBetaList probTrue)
        {
            Func <bool, Beta, double> f = BernoulliFromBetaOp.AverageLogFactor;

            return(f.Map(sample, probTrue).EnumerableSum(x => x));
        }
 /// <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);
 }
 /// <summary>
 /// Update the buffer 'MeanLogMinusMeanLogOneMinus'
 /// </summary>
 /// <param name="probsTrue">Incoming message from 'probsTrue'. Must be a proper distribution.  If any element is uniform, the result will be uniform.</param>
 /// <returns>New value of buffer 'MeanLogMinusMeanLogOneMinus'</returns>
 /// <remarks><para>
 ///
 /// </para></remarks>
 /// <exception cref="ImproperMessageException"><paramref name="probsTrue"/> is not a proper distribution</exception>
 public static SparseVector MeanLogMinusMeanLogOneMinus([Proper] SparseBetaList probsTrue)
 {
     return((SparseVector)(probsTrue.GetMeanLog() - probsTrue.GetMeanLogOneMinus()));
 }
 /// <summary>
 /// Update the buffer 'SumMeanLogOneMinus'
 /// </summary>
 /// <param name="probsTrue">Incoming message from 'probsTrue'. Must be a proper distribution.  If any element is uniform, the result will be uniform.</param>
 /// <returns>New value of buffer 'SumMeanLogOneMinus'</returns>
 /// <remarks><para>
 ///
 /// </para></remarks>
 /// <exception cref="ImproperMessageException"><paramref name="probsTrue"/> is not a proper distribution</exception>
 public static double SumMeanLogOneMinus([Proper] SparseBetaList probsTrue)
 {
     return(probsTrue.GetMeanLogOneMinus().Sum());
 }
        //-- VMP -------------------------------------------------------------------------------------------

        /// <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">Incoming message from 'probsTrue'. Must be a proper distribution.  If any element is uniform, the result will be uniform.</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, [Proper] SparseBetaList probsTrue, SparseVector MeanLog, SparseVector MeanLogOneMinus)
        {
            return(SparseBernoulliFromBetaOp.AverageLogFactor(sample, probsTrue, MeanLog, MeanLogOneMinus));
        }
        /// <summary>Evidence message for VMP.</summary>
        /// <param name="sample">Constant value for <c>sample</c>.</param>
        /// <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>
        /// <returns>Average of the factor's log-value across the given argument distributions.</returns>
        /// <remarks>
        ///   <para>The formula for the result is <c>sum_(probTrue) p(probTrue) log(factor(sample,probTrue))</c>. Adding up these values across all factors and variables gives the log-evidence estimate for VMP.</para>
        /// </remarks>
        /// <exception cref="ImproperMessageException">
        ///   <paramref name="probTrue" /> is not a proper distribution.</exception>
        public static double AverageLogFactor(IList <int> sample, [Proper] SparseBetaList probTrue)
        {
            Func <bool, Beta, double> f = BernoulliFromBetaOp.AverageLogFactor;

            return(f.Map(BernoulliIntegerSubset.SubsetToList(sample, probTrue.Count), probTrue).EnumerableSum(x => x));
        }
Exemple #18
0
        public void SparseBernoulliFromBetaFactor()
        {
            var calcSuffix     = ": calculation differs between sparse and dense";
            var sparsitySuffix = ": result is not sparse as expected";
            var calcErrMsg     = "";
            var sparsityErrMsg = "";
            var tolerance      = 1e-10;

            Rand.Restart(12347);
            int listSize           = 50;
            var sparseProbTrueDist = SparseBetaList.Constant(listSize, new Beta(1, 2));

            sparseProbTrueDist[3] = new Beta(4, 5);
            sparseProbTrueDist[6] = new Beta(7, 8);
            var probTrueDist        = sparseProbTrueDist.ToArray();
            var sparseProbTruePoint = SparseList <double> .Constant(listSize, 0.1);

            sparseProbTruePoint[3] = 0.7;
            sparseProbTruePoint[6] = 0.8;
            var probTruePoint = sparseProbTruePoint.ToArray();

            var sparseSampleDist = SparseBernoulliList.Constant(listSize, new Bernoulli(0.1));

            sparseSampleDist[3] = new Bernoulli(0.8);
            sparseSampleDist[9] = new Bernoulli(0.9);
            var sampleDist        = sparseSampleDist.ToArray();
            var sparseSamplePoint = SparseList <bool> .Constant(listSize, false);

            sparseSamplePoint[3] = true;
            sparseSamplePoint[9] = true;
            var samplePoint = sparseSamplePoint.ToArray();

            var toSparseSampleDist = SparseBernoulliList.Constant(listSize, new Bernoulli(0.1));

            toSparseSampleDist[3] = new Bernoulli(0.4);
            toSparseSampleDist[4] = new Bernoulli(0.8);
            var toSampleDist = toSparseSampleDist.ToArray();

            // ---------------------------
            // Check average log factor
            // ---------------------------
            calcErrMsg = "Average log factor" + calcSuffix;
            // Dist, dist
            var sparseAvgLog = SparseBernoulliFromBetaOp.AverageLogFactor(sparseSampleDist, sparseProbTrueDist);
            var avgLog       = Util.ArrayInit(listSize, i => BernoulliFromBetaOp.AverageLogFactor(sampleDist[i], probTrueDist[i])).Sum();

            TAssert.True(System.Math.Abs(avgLog - sparseAvgLog) < tolerance, calcErrMsg);
            // Dist, point
            sparseAvgLog = SparseBernoulliFromBetaOp.AverageLogFactor(sparseSampleDist, sparseProbTruePoint);
            avgLog       = Util.ArrayInit(listSize, i => BernoulliFromBetaOp.AverageLogFactor(sampleDist[i], probTruePoint[i])).Sum();
            TAssert.True(System.Math.Abs(avgLog - sparseAvgLog) < tolerance, calcErrMsg);
            // Point, dist
            sparseAvgLog = SparseBernoulliFromBetaOp.AverageLogFactor(sparseSamplePoint, sparseProbTrueDist);
            avgLog       = Util.ArrayInit(listSize, i => BernoulliFromBetaOp.AverageLogFactor(samplePoint[i], probTrueDist[i])).Sum();
            TAssert.True(System.Math.Abs(avgLog - sparseAvgLog) < tolerance, calcErrMsg);
            // Point, point
            sparseAvgLog = SparseBernoulliFromBetaOp.AverageLogFactor(sparseSamplePoint, sparseProbTruePoint);
            avgLog       = Util.ArrayInit(listSize, i => BernoulliFromBetaOp.AverageLogFactor(samplePoint[i], probTruePoint[i])).Sum();
            TAssert.True(System.Math.Abs(avgLog - sparseAvgLog) < tolerance, calcErrMsg);

            // ---------------------------
            // Check log average factor
            // ---------------------------
            calcErrMsg = "Log average factor" + calcSuffix;
            var sparseLogAvg = SparseBernoulliFromBetaOp.LogAverageFactor(sparseSampleDist, toSparseSampleDist);
            var logAvg       = Util.ArrayInit(listSize, i => BernoulliFromBetaOp.LogAverageFactor(sampleDist[i], toSampleDist[i])).Sum();

            TAssert.True(System.Math.Abs(logAvg - sparseLogAvg) < tolerance, calcErrMsg);
            sparseLogAvg = SparseBernoulliFromBetaOp.LogAverageFactor(sparseSamplePoint, sparseProbTrueDist);
            logAvg       = Util.ArrayInit(listSize, i => BernoulliFromBetaOp.LogAverageFactor(samplePoint[i], probTrueDist[i])).Sum();
            TAssert.True(System.Math.Abs(logAvg - sparseLogAvg) < tolerance, calcErrMsg);
            sparseLogAvg = SparseBernoulliFromBetaOp.LogAverageFactor(sparseSamplePoint, sparseProbTruePoint);
            logAvg       = Util.ArrayInit(listSize, i => BernoulliFromBetaOp.LogAverageFactor(samplePoint[i], probTruePoint[i])).Sum();
            TAssert.True(System.Math.Abs(logAvg - sparseLogAvg) < tolerance, calcErrMsg);

            // ---------------------------
            // Check log evidence ratio
            // ---------------------------
            calcErrMsg = "Log evidence ratio" + calcSuffix;
            // Dist, dist
            var sparseEvidRat = SparseBernoulliFromBetaOp.LogEvidenceRatio(sparseSampleDist, sparseProbTrueDist);
            var evidRat       = Util.ArrayInit(listSize, i => BernoulliFromBetaOp.LogEvidenceRatio(sampleDist[i], probTrueDist[i])).Sum();

            TAssert.True(System.Math.Abs(evidRat - sparseEvidRat) < tolerance, calcErrMsg);
            // Dist, point
            sparseEvidRat = SparseBernoulliFromBetaOp.LogEvidenceRatio(sparseSampleDist, sparseProbTruePoint);
            evidRat       = Util.ArrayInit(listSize, i => BernoulliFromBetaOp.LogEvidenceRatio(sampleDist[i], probTruePoint[i])).Sum();
            TAssert.True(System.Math.Abs(evidRat - sparseEvidRat) < tolerance, calcErrMsg);
            // Point, dist
            sparseEvidRat = SparseBernoulliFromBetaOp.LogEvidenceRatio(sparseSamplePoint, sparseProbTrueDist);
            evidRat       = Util.ArrayInit(listSize, i => BernoulliFromBetaOp.LogEvidenceRatio(samplePoint[i], probTrueDist[i])).Sum();
            TAssert.True(System.Math.Abs(evidRat - sparseEvidRat) < tolerance, calcErrMsg);
            // Point, point
            sparseEvidRat = SparseBernoulliFromBetaOp.LogEvidenceRatio(sparseSamplePoint, sparseProbTruePoint);
            evidRat       = Util.ArrayInit(listSize, i => BernoulliFromBetaOp.LogEvidenceRatio(samplePoint[i], probTruePoint[i])).Sum();
            TAssert.True(System.Math.Abs(evidRat - sparseEvidRat) < tolerance, calcErrMsg);

            // ---------------------------
            // Check SampleConditional
            // ---------------------------
            calcErrMsg     = "SampleConditional" + calcSuffix;
            sparsityErrMsg = "SampleConditional" + sparsitySuffix;
            // Use different common value to ensure this gets properly set
            var sparseSampleConditional = SparseBernoulliList.Constant(listSize, new Bernoulli(0.5));

            sparseSampleConditional = SparseBernoulliFromBetaOp.SampleConditional(sparseProbTruePoint, sparseSampleConditional);
            var sampleConditional = Util.ArrayInit(listSize, i => BernoulliFromBetaOp.SampleConditional(probTruePoint[i]));

            TAssert.True(2 == sparseSampleConditional.SparseCount, sparsityErrMsg);
            TAssert.True(sparseSampleConditional.MaxDiff(sampleConditional) < tolerance, calcErrMsg);

            // ---------------------------
            // Check SampleAverageConditional
            // ---------------------------
            calcErrMsg     = "SampleAverageConditional" + calcSuffix;
            sparsityErrMsg = "SampleAverageConditional" + sparsitySuffix;
            // Use different common value to ensure this gets properly set
            var sparseSampleAvgConditional = SparseBernoulliList.Constant(listSize, new Bernoulli(0.5));

            sparseSampleAvgConditional = SparseBernoulliFromBetaOp.SampleAverageConditional(sparseProbTrueDist, sparseSampleAvgConditional);
            var sampleAvgConditional = Util.ArrayInit(listSize, i => BernoulliFromBetaOp.SampleAverageConditional(probTrueDist[i]));

            TAssert.True(2 == sparseSampleAvgConditional.SparseCount, sparsityErrMsg);
            TAssert.True(sparseSampleAvgConditional.MaxDiff(sampleAvgConditional) < tolerance, calcErrMsg);
            sparseSampleAvgConditional = SparseBernoulliList.Constant(listSize, new Bernoulli(0.5));
            sparseSampleAvgConditional = SparseBernoulliFromBetaOp.SampleAverageConditional(sparseProbTruePoint, sparseSampleAvgConditional);
            sampleAvgConditional       = Util.ArrayInit(listSize, i => BernoulliFromBetaOp.SampleAverageConditional(probTruePoint[i]));
            TAssert.True(2 == sparseSampleAvgConditional.SparseCount, sparsityErrMsg);
            TAssert.True(sparseSampleAvgConditional.MaxDiff(sampleAvgConditional) < tolerance, calcErrMsg);

            // ---------------------------
            // Check ProbTrueConditional
            // ---------------------------
            calcErrMsg     = "ProbTrueConditional" + calcSuffix;
            sparsityErrMsg = "ProbTrueConditional" + sparsitySuffix;
            // Use different common value to ensure this gets properly set
            var sparseProbTrueConditional = SparseBetaList.Constant(listSize, new Beta(1.1, 2.2));

            sparseProbTrueConditional = SparseBernoulliFromBetaOp.ProbTrueConditional(sparseSamplePoint, sparseProbTrueConditional);
            var probTrueConditional = Util.ArrayInit(listSize, i => BernoulliFromBetaOp.ProbTrueConditional(samplePoint[i]));

            TAssert.True(2 == sparseProbTrueConditional.SparseCount, sparsityErrMsg);
            TAssert.True(sparseProbTrueConditional.MaxDiff(probTrueConditional) < tolerance, calcErrMsg);

            // ---------------------------
            // Check ProbTrueAverageConditional
            // ---------------------------
            calcErrMsg     = "ProbTrueAverageConditional" + calcSuffix;
            sparsityErrMsg = "ProbTrueAverageConditional" + sparsitySuffix;
            // Use different common value to ensure this gets properly set
            var sparseProbTrueAvgConditional = SparseBetaList.Constant(listSize, new Beta(1.1, 2.2));

            sparseProbTrueAvgConditional = SparseBernoulliFromBetaOp.ProbTrueAverageConditional(sparseSampleDist, sparseProbTrueDist, sparseProbTrueAvgConditional);
            var probTrueAvgConditional = Util.ArrayInit(listSize, i => BernoulliFromBetaOp.ProbTrueAverageConditional(sampleDist[i], probTrueDist[i]));

            TAssert.True(2 == sparseProbTrueAvgConditional.SparseCount, sparsityErrMsg);
            TAssert.True(sparseProbTrueAvgConditional.MaxDiff(probTrueAvgConditional) < tolerance, calcErrMsg);
            sparseProbTrueAvgConditional = SparseBetaList.Constant(listSize, new Beta(1.1, 2.2));
            sparseProbTrueAvgConditional = SparseBernoulliFromBetaOp.ProbTrueAverageConditional(sparseSamplePoint, sparseProbTrueAvgConditional);
            probTrueAvgConditional       = Util.ArrayInit(listSize, i => BernoulliFromBetaOp.ProbTrueAverageConditional(samplePoint[i]));
            TAssert.True(2 == sparseProbTrueAvgConditional.SparseCount, sparsityErrMsg);
            TAssert.True(sparseProbTrueAvgConditional.MaxDiff(probTrueAvgConditional) < tolerance, calcErrMsg);

            // ---------------------------
            // Check SampleAverageLogarithm
            // ---------------------------
            calcErrMsg     = "SampleAverageLogarithm" + calcSuffix;
            sparsityErrMsg = "SampleAverageLogarithm" + sparsitySuffix;
            // Use different common value to ensure this gets properly set
            var sparseSampleAvgLogarithm = SparseBernoulliList.Constant(listSize, new Bernoulli(0.5));

            sparseSampleAvgLogarithm = SparseBernoulliFromBetaOp.SampleAverageLogarithm(sparseProbTrueDist, sparseSampleAvgLogarithm);
            var sampleAvgLogarithm = Util.ArrayInit(listSize, i => BernoulliFromBetaOp.SampleAverageLogarithm(probTrueDist[i]));

            TAssert.True(2 == sparseSampleAvgLogarithm.SparseCount, sparsityErrMsg);
            TAssert.True(sparseSampleAvgLogarithm.MaxDiff(sampleAvgLogarithm) < tolerance, calcErrMsg);
            sparseSampleAvgLogarithm = SparseBernoulliList.Constant(listSize, new Bernoulli(0.5));
            sparseSampleAvgLogarithm = SparseBernoulliFromBetaOp.SampleAverageLogarithm(sparseProbTruePoint, sparseSampleAvgLogarithm);
            sampleAvgLogarithm       = Util.ArrayInit(listSize, i => BernoulliFromBetaOp.SampleAverageLogarithm(probTruePoint[i]));
            TAssert.True(2 == sparseSampleAvgLogarithm.SparseCount, sparsityErrMsg);
            TAssert.True(sparseSampleAvgLogarithm.MaxDiff(sampleAvgLogarithm) < tolerance, calcErrMsg);

            // ---------------------------
            // Check ProbTrueAverageLogarithm
            // ---------------------------
            calcErrMsg     = "ProbTrueAverageLogarithm" + calcSuffix;
            sparsityErrMsg = "ProbTrueAverageLogarithm" + sparsitySuffix;
            // Use different common value to ensure this gets properly set
            var sparseProbTrueAvgLogarithm = SparseBetaList.Constant(listSize, new Beta(1.1, 2.2));

            sparseProbTrueAvgLogarithm = SparseBernoulliFromBetaOp.ProbTrueAverageLogarithm(sparseSampleDist, sparseProbTrueAvgLogarithm);
            var probTrueAvgLogarithm = Util.ArrayInit(listSize, i => BernoulliFromBetaOp.ProbTrueAverageLogarithm(sampleDist[i]));

            TAssert.True(2 == sparseProbTrueAvgLogarithm.SparseCount, sparsityErrMsg);
            TAssert.True(sparseProbTrueAvgLogarithm.MaxDiff(probTrueAvgLogarithm) < tolerance, calcErrMsg);
            sparseProbTrueAvgLogarithm = SparseBetaList.Constant(listSize, new Beta(1.1, 2.2));
            sparseProbTrueAvgLogarithm = SparseBernoulliFromBetaOp.ProbTrueAverageLogarithm(sparseSamplePoint, sparseProbTrueAvgLogarithm);
            probTrueAvgLogarithm       = Util.ArrayInit(listSize, i => BernoulliFromBetaOp.ProbTrueAverageLogarithm(samplePoint[i]));
            TAssert.True(2 == sparseProbTrueAvgLogarithm.SparseCount, sparsityErrMsg);
            TAssert.True(sparseProbTrueAvgLogarithm.MaxDiff(probTrueAvgLogarithm) < tolerance, calcErrMsg);
        }
		/// <summary>
		/// VMP message to 'probsTrue'
		/// </summary>
		/// <param name="sample">Constant value for 'sample'.</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 'probsTrue' conditioned on the given values.
		/// </para></remarks>
		public static SparseBetaList ProbsTrueAverageLogarithm(IList<int> sample, SparseBetaList result)
		{
			var trueCounts =result.TrueCounts;
			var falseCounts =result.FalseCounts;
			trueCounts.SetAllElementsTo(1.0);
			falseCounts.SetAllElementsTo(2.0);
			foreach(int i in sample)
			{
				trueCounts[i] = 2;
				falseCounts[i] = 1;
			}
			return result;
		}
        /// <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 contribution the EP model evidence.</returns>
        /// <remarks>
        ///   <para>The formula for the result is <c>log(sum_(probTrue) p(probTrue) 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, SparseBetaList probTrue)
        {
            Func <bool, Beta, double> f = BernoulliFromBetaOp.LogEvidenceRatio;

            return(f.Map(BernoulliIntegerSubset.SubsetToList(sample, probTrue.Count), probTrue).EnumerableSum(x => x));
        }
 /// <summary>
 /// Update the buffer 'MeanLog'
 /// </summary>
 /// <param name="probsTrue">Incoming message from 'probsTrue'. Must be a proper distribution.  If any element is uniform, the result will be uniform.</param>
 /// <returns>New value of buffer 'MeanLog'</returns>
 /// <remarks><para>
 ///
 /// </para></remarks>
 /// <exception cref="ImproperMessageException"><paramref name="probsTrue"/> is not a proper distribution</exception>
 public static SparseVector MeanLog([Proper] SparseBetaList probsTrue)
 {
     return(probsTrue.GetMeanLog());
 }
Exemple #22
0
 /// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="SparseBernoulliFromBetaOp"]/message_doc[@name="ProbTrueAverageConditional(SparseBernoulliList, SparseBetaList, SparseBetaList)"]/*'/>
 public static SparseBetaList ProbTrueAverageConditional([SkipIfUniform] SparseBernoulliList sample, SparseBetaList probTrue, SparseBetaList result)
 {
     result.SetToFunction(sample, probTrue, (s, pt) => BernoulliFromBetaOp.ProbTrueAverageConditional(s, pt));
     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 for 'probsTrue'.</param>
 /// <param name="MeanLog">Buffer 'MeanLog'.</param>
 /// <param name="MeanLogOneMinus">Buffer '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_(sample) p(sample) 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="sample"/> is not a proper distribution</exception>
 public static double AverageLogFactor([Proper] SparseBernoulliListBase sample, SparseVector probsTrue, SparseVector MeanLog, SparseVector MeanLogOneMinus)
 {
     return(AverageLogFactor(sample, SparseBetaList.PointMass(probsTrue), MeanLog, MeanLogOneMinus));
 }
Exemple #24
0
 /// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="SparseBernoulliFromBetaOp"]/message_doc[@name="SampleAverageLogarithm(SparseBetaList, SparseBernoulliList)"]/*'/>
 public static SparseBernoulliList SampleAverageLogarithm([SkipIfUniform] SparseBetaList probTrue, SparseBernoulliList result)
 {
     result.SetToFunction(probTrue, pt => BernoulliFromBetaOp.SampleAverageLogarithm(pt));
     return(result);
 }
Exemple #25
0
            public void Initialize(bool skipStringDistributions = false)
            {
                // DO NOT make this a constructor, because it makes the test not notice complete lack of serialization as an empty object is set up exactly as the thing
                // you are trying to deserialize.
                this.pareto  = new Pareto(1.2, 3.5);
                this.poisson = new Poisson(2.3);
                this.wishart = new Wishart(20, new PositiveDefiniteMatrix(new double[, ] {
                    { 22, 21 }, { 21, 23 }
                }));
                this.vectorGaussian = new VectorGaussian(Vector.FromArray(13, 14), new PositiveDefiniteMatrix(new double[, ] {
                    { 16, 15 }, { 15, 17 }
                }));
                this.unnormalizedDiscrete = UnnormalizedDiscrete.FromLogProbs(DenseVector.FromArray(5.1, 5.2, 5.3));
                this.pointMass            = PointMass <double> .Create(1.1);

                this.gaussian             = new Gaussian(11.0, 12.0);
                this.nonconjugateGaussian = new NonconjugateGaussian(1.2, 2.3, 3.4, 4.5);
                this.gamma              = new Gamma(9.0, 10.0);
                this.gammaPower         = new GammaPower(5.6, 2.8, 3.4);
                this.discrete           = new Discrete(6.0, 7.0, 8.0);
                this.conjugateDirichlet = new ConjugateDirichlet(1.2, 2.3, 3.4, 4.5);
                this.dirichlet          = new Dirichlet(3.0, 4.0, 5.0);
                this.beta      = new Beta(2.0, 1.0);
                this.binomial  = new Binomial(5, 0.8);
                this.bernoulli = new Bernoulli(0.6);

                this.sparseBernoulliList    = SparseBernoulliList.Constant(4, new Bernoulli(0.1));
                this.sparseBernoulliList[1] = new Bernoulli(0.9);
                this.sparseBernoulliList[3] = new Bernoulli(0.7);

                this.sparseBetaList    = SparseBetaList.Constant(5, new Beta(2.0, 2.0));
                this.sparseBetaList[0] = new Beta(3.0, 4.0);
                this.sparseBetaList[1] = new Beta(5.0, 6.0);

                this.sparseGaussianList    = SparseGaussianList.Constant(6, Gaussian.FromMeanAndPrecision(0.1, 0.2));
                this.sparseGaussianList[4] = Gaussian.FromMeanAndPrecision(0.3, 0.4);
                this.sparseGaussianList[5] = Gaussian.FromMeanAndPrecision(0.5, 0.6);

                this.sparseGammaList = SparseGammaList.Constant(1, Gamma.FromShapeAndRate(1.0, 2.0));

                this.truncatedGamma    = new TruncatedGamma(1.2, 2.3, 3.4, 4.5);
                this.truncatedGaussian = new TruncatedGaussian(1.2, 3.4, 5.6, 7.8);
                this.wrappedGaussian   = new WrappedGaussian(1.2, 2.3, 3.4);

                ga = Distribution <double> .Array(new[] { this.gaussian, this.gaussian });

                vga = Distribution <Vector> .Array(new[] { this.vectorGaussian, this.vectorGaussian });

                ga2D = Distribution <double> .Array(new[, ] {
                    { this.gaussian, this.gaussian }, { this.gaussian, this.gaussian }
                });

                vga2D = Distribution <Vector> .Array(new[, ] {
                    { this.vectorGaussian, this.vectorGaussian }, { this.vectorGaussian, this.vectorGaussian }
                });

                gaJ = Distribution <double> .Array(new[] { new[] { this.gaussian, this.gaussian }, new[] { this.gaussian, this.gaussian } });

                vgaJ = Distribution <Vector> .Array(new[] { new[] { this.vectorGaussian, this.vectorGaussian }, new[] { this.vectorGaussian, this.vectorGaussian } });

                var gp    = new GaussianProcess(new ConstantFunction(0), new SquaredExponential(0));
                var basis = Util.ArrayInit(2, i => Vector.FromArray(1.0 * i));

                this.sparseGp = new SparseGP(new SparseGPFixed(gp, basis));

                this.quantileEstimator = new QuantileEstimator(0.01);
                this.quantileEstimator.Add(5);
                this.outerQuantiles = OuterQuantiles.FromDistribution(3, this.quantileEstimator);
                this.innerQuantiles = InnerQuantiles.FromDistribution(3, this.outerQuantiles);

                if (!skipStringDistributions)
                {
                    // String distributions can not be serialized by some formatters (namely BinaryFormatter)
                    // That is fine because this combination is never used in practice
                    this.stringDistribution1 = StringDistribution.String("aa")
                                               .Append(StringDistribution.OneOf("b", "ccc")).Append("dddd");
                    this.stringDistribution2 = new StringDistribution();
                    this.stringDistribution2.SetToProduct(StringDistribution.OneOf("a", "b"),
                                                          StringDistribution.OneOf("b", "c"));
                }
            }
Exemple #26
0
        /// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="SparseBernoulliFromBetaOp"]/message_doc[@name="LogEvidenceRatio(ISparseList{bool}, SparseBetaList)"]/*'/>
        public static double LogEvidenceRatio(ISparseList <bool> sample, SparseBetaList probTrue)
        {
            Func <bool, Beta, double> f = BernoulliFromBetaOp.LogEvidenceRatio;

            return(f.Map(sample, probTrue).EnumerableSum(x => x));
        }
 public static double LogEvidenceRatio(BernoulliIntegerSubset sample, SparseBetaList probTrue)
 {
     return(0.0);
 }
Exemple #28
0
 /// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="SparseBernoulliFromBetaOp"]/message_doc[@name="ProbTrueAverageConditional(ISparseList{bool}, SparseBetaList)"]/*'/>
 public static SparseBetaList ProbTrueAverageConditional(ISparseList <bool> sample, SparseBetaList result)
 {
     result.SetToFunction(sample, s => BernoulliFromBetaOp.ProbTrueAverageConditional(s));
     return(result);
 }
Exemple #29
0
            public void Initialize()
            {
                // DO NOT make this a constructor, because it makes the test not notice complete lack of serialization as an empty object is set up exactly as the thing
                // you are trying to deserialize.
                this.pareto  = new Pareto(1.2, 3.5);
                this.poisson = new Poisson(2.3);
                this.wishart = new Wishart(20, new PositiveDefiniteMatrix(new double[, ] {
                    { 22, 21 }, { 21, 23 }
                }));
                this.vectorGaussian = new VectorGaussian(Vector.FromArray(13, 14), new PositiveDefiniteMatrix(new double[, ] {
                    { 16, 15 }, { 15, 17 }
                }));
                this.unnormalizedDiscrete = UnnormalizedDiscrete.FromLogProbs(DenseVector.FromArray(5.1, 5.2, 5.3));
                this.pointMass            = PointMass <double> .Create(1.1);

                this.gaussian             = new Gaussian(11.0, 12.0);
                this.nonconjugateGaussian = new NonconjugateGaussian(1.2, 2.3, 3.4, 4.5);
                this.gamma              = new Gamma(9.0, 10.0);
                this.gammaPower         = new GammaPower(5.6, 2.8, 3.4);
                this.discrete           = new Discrete(6.0, 7.0, 8.0);
                this.conjugateDirichlet = new ConjugateDirichlet(1.2, 2.3, 3.4, 4.5);
                this.dirichlet          = new Dirichlet(3.0, 4.0, 5.0);
                this.beta      = new Beta(2.0, 1.0);
                this.binomial  = new Binomial(5, 0.8);
                this.bernoulli = new Bernoulli(0.6);

                this.sparseBernoulliList    = SparseBernoulliList.Constant(4, new Bernoulli(0.1));
                this.sparseBernoulliList[1] = new Bernoulli(0.9);
                this.sparseBernoulliList[3] = new Bernoulli(0.7);

                this.sparseBetaList    = SparseBetaList.Constant(5, new Beta(2.0, 2.0));
                this.sparseBetaList[0] = new Beta(3.0, 4.0);
                this.sparseBetaList[1] = new Beta(5.0, 6.0);

                this.sparseGaussianList    = SparseGaussianList.Constant(6, Gaussian.FromMeanAndPrecision(0.1, 0.2));
                this.sparseGaussianList[4] = Gaussian.FromMeanAndPrecision(0.3, 0.4);
                this.sparseGaussianList[5] = Gaussian.FromMeanAndPrecision(0.5, 0.6);

                this.sparseGammaList = SparseGammaList.Constant(1, Gamma.FromShapeAndRate(1.0, 2.0));

                this.truncatedGamma    = new TruncatedGamma(1.2, 2.3, 3.4, 4.5);
                this.truncatedGaussian = new TruncatedGaussian(1.2, 3.4, 5.6, 7.8);
                this.wrappedGaussian   = new WrappedGaussian(1.2, 2.3, 3.4);

                ga = Distribution <double> .Array(new[] { this.gaussian, this.gaussian });

                vga = Distribution <Vector> .Array(new[] { this.vectorGaussian, this.vectorGaussian });

                ga2D = Distribution <double> .Array(new[, ] {
                    { this.gaussian, this.gaussian }, { this.gaussian, this.gaussian }
                });

                vga2D = Distribution <Vector> .Array(new[, ] {
                    { this.vectorGaussian, this.vectorGaussian }, { this.vectorGaussian, this.vectorGaussian }
                });

                gaJ = Distribution <double> .Array(new[] { new[] { this.gaussian, this.gaussian }, new[] { this.gaussian, this.gaussian } });

                vgaJ = Distribution <Vector> .Array(new[] { new[] { this.vectorGaussian, this.vectorGaussian }, new[] { this.vectorGaussian, this.vectorGaussian } });

                var gp    = new GaussianProcess(new ConstantFunction(0), new SquaredExponential(0));
                var basis = Util.ArrayInit(2, i => Vector.FromArray(1.0 * i));

                this.sparseGp = new SparseGP(new SparseGPFixed(gp, basis));

                this.quantileEstimator = new QuantileEstimator(0.01);
                this.quantileEstimator.Add(5);
            }