public void DiscreteFromDirichletOpTest()
        {
            Dirichlet probs4  = new Dirichlet(1.0, 2, 3, 4);
            Discrete  sample4 = new Discrete(0.4, 0.6, 0, 0);
            Dirichlet result4 = DiscreteFromDirichletOp.ProbsAverageConditional(sample4, probs4, Dirichlet.Uniform(4));

            Dirichlet probs3  = new Dirichlet(1.0, 2, 7);
            Discrete  sample3 = new Discrete(0.4, 0.6, 0);
            Dirichlet result3 = DiscreteFromDirichletOp.ProbsAverageConditional(sample3, probs3, Dirichlet.Uniform(3));

            for (int i = 0; i < 3; i++)
            {
                Assert.True(MMath.AbsDiff(result4.PseudoCount[i], result3.PseudoCount[i], 1e-6) < 1e-10);
            }

            Dirichlet probs2  = new Dirichlet(1.0, 2);
            Discrete  sample2 = new Discrete(0.4, 0.6);
            Dirichlet result2 = DiscreteFromDirichletOp.ProbsAverageConditional(sample2, probs2, Dirichlet.Uniform(2));

            Beta      beta      = new Beta(1.0, 2);
            Bernoulli bernoulli = new Bernoulli(0.4);
            Beta      result1   = BernoulliFromBetaOp.ProbTrueAverageConditional(bernoulli, beta);

            Assert.Equal(result2.PseudoCount[0], result1.TrueCount, 1e-10);
            Assert.Equal(result2.PseudoCount[1], result1.FalseCount, 1e-10);

            // test handling of small alphas
            Discrete sample = DiscreteFromDirichletOp.SampleAverageLogarithm(Dirichlet.Symmetric(2, 1e-8), Discrete.Uniform(2));

            Assert.True(sample.IsUniform());
        }
        public void BernoulliFromBetaOpTest2()
        {
            Bernoulli sampleDist = new Bernoulli(2.0 / 3);

            using (TestUtils.TemporarilyAllowBetaImproperSums)
            {
                for (int i = 10; i <= 10; i++)
                {
                    double s            = System.Math.Exp(i);
                    Beta   probTrueDist = new Beta(s, 2 * s);
                    Beta   expected     = BernoulliFromBetaOp.ProbTrueAverageConditional(sampleDist, probTrueDist);
                    if (i == 10)
                    {
                        probTrueDist.Point = probTrueDist.GetMean();
                    }
                    Beta actual = BernoulliFromBetaOp.ProbTrueAverageConditional(sampleDist, probTrueDist);
                    Console.WriteLine("{0} {1}", probTrueDist, actual);
                    Assert.True(expected.MaxDiff(actual) < 1e-4);
                }
            }
            for (int i = 10; i <= 10; i++)
            {
                Beta probTrueDist = new Beta(System.Math.Exp(i), 1);
                Beta expected     = BernoulliFromBetaOp.ProbTrueAverageConditional(sampleDist, probTrueDist);
                if (i == 10)
                {
                    probTrueDist.Point = 1;
                }
                Beta actual = BernoulliFromBetaOp.ProbTrueAverageConditional(sampleDist, probTrueDist);
                Console.WriteLine("{0} {1}", probTrueDist, actual);
                Assert.True(expected.MaxDiff(actual) < 1e-4);
            }
        }
        /// <summary>Computations that depend on the observed value of vbool1</summary>
        private void Changed_vbool1()
        {
            if (this.Changed_vbool1_isDone)
            {
                return;
            }
            this.vbool1_marginal = Bernoulli.Uniform();
            this.vbool1_marginal = Distribution.SetPoint <Bernoulli, bool>(this.vbool1_marginal, this.Vbool1);
            Beta vBeta0 = Beta.Uniform();

            this.vdouble14_marginal_F = Beta.Uniform();
            Beta vdouble14_use_B = default(Beta);

            // Message to 'vdouble14_use' from Bernoulli factor
            vdouble14_use_B = BernoulliFromBetaOp.ProbTrueAverageConditional(this.Vbool1);
            // Message to 'vdouble14_marginal' from Variable factor
            this.vdouble14_marginal_F  = VariableOp.MarginalAverageConditional <Beta>(vdouble14_use_B, vBeta0, this.vdouble14_marginal_F);
            this.Changed_vbool1_isDone = true;
        }
        public void BernoulliFromBetaOpTest()
        {
            Assert.True(System.Math.Abs(BernoulliFromBetaOp.LogEvidenceRatio(new Bernoulli(3e-5), Beta.PointMass(1)) - 0) < 1e-10);

            using (TestUtils.TemporarilyAllowBetaImproperSums)
            {
                Beta      probTrueDist = new Beta(3, 2);
                Bernoulli sampleDist   = new Bernoulli();
                Assert.True(new Beta(1, 1).MaxDiff(BernoulliFromBetaOp.ProbTrueAverageConditional(sampleDist, probTrueDist)) < 1e-4);
                sampleDist = Bernoulli.PointMass(true);
                Assert.True(new Beta(2, 1).MaxDiff(BernoulliFromBetaOp.ProbTrueAverageConditional(sampleDist, probTrueDist)) < 1e-4);
                sampleDist = Bernoulli.PointMass(false);
                Assert.True(new Beta(1, 2).MaxDiff(BernoulliFromBetaOp.ProbTrueAverageConditional(sampleDist, probTrueDist)) < 1e-4);
                sampleDist = new Bernoulli(0.9);
                Assert.True(new Beta(1.724, 0.9598).MaxDiff(BernoulliFromBetaOp.ProbTrueAverageConditional(sampleDist, probTrueDist)) < 1e-3);
                Assert.Throws <ImproperMessageException>(() =>
                {
                    BernoulliFromBetaOp.ProbTrueAverageConditional(sampleDist, new Beta(1, -2));
                });
            }
        }
Beispiel #5
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);
        }