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));
                });
            }
        }
Exemple #2
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);
        }