public void GaussianOpX() { Gaussian uniform = Gaussian.Uniform(); Gaussian X0 = Gaussian.FromMeanAndVariance(3, 0.5); Gaussian Mean0 = Gaussian.FromMeanAndVariance(7, 1.0 / 3); double Precision0 = 3; Gaussian X, Mean; Gamma Precision, to_precision; Gaussian xActual, xExpected; bool testImproper = false; if (testImproper) { // Test the case where precisionIsBetween = false X = Gaussian.FromNatural(1, 2); Mean = Gaussian.FromNatural(3, -1); Precision = Gamma.FromShapeAndRate(4, 5); to_precision = Gamma.FromShapeAndRate(6, 7); xActual = GaussianOp.SampleAverageConditional(X, Mean, Precision, to_precision); } X = Gaussian.FromNatural(-2.7793306963303595, 0.050822473645365768); Mean = Gaussian.FromNatural(-5.9447032851878134E-09, 3.2975231004586637E-204); Precision = Gamma.FromShapeAndRate(318.50907574398883, 9.6226982361933746E+205); to_precision = Gamma.PointMass(0); xActual = GaussianOp.SampleAverageConditional(X, Mean, Precision, to_precision); X = Gaussian.FromNatural(0.1559599323109816, 8.5162535450918462); Mean = Gaussian.PointMass(0.57957597647840942); Precision = Gamma.FromShapeAndRate(7.8308812008325587E+30, 8.2854255911709925E+30); to_precision = Gamma.FromShapeAndRate(1.4709139487775529, 0.14968339171493822); xActual = GaussianOp.SampleAverageConditional(X, Mean, Precision, to_precision); X = Gaussian.FromNatural(0.15595993233964134, 8.5162535466550349); Mean = Gaussian.PointMass(0.57957597647840942); Precision = Gamma.FromShapeAndRate(3.9206259406339067E+20, 4.1481991194547565E+20); to_precision = Gamma.FromShapeAndRate(1.4709139487806249, 0.14968339171413536); xActual = GaussianOp.SampleAverageConditional(X, Mean, Precision, to_precision); X = Gaussian.FromNatural(0.15595993261634511, 8.5162535617468418); Mean = Gaussian.PointMass(0.57957597647840942); Precision = Gamma.FromShapeAndRate(1.825759224425317E+19, 1.9317356258150703E+19); to_precision = Gamma.FromShapeAndRate(1.4709139487887679, 0.14968339176002607); xActual = GaussianOp.SampleAverageConditional(X, Mean, Precision, to_precision); X = Gaussian.FromNatural(0.16501264432785923, 9.01); Mean = Gaussian.PointMass(0.57957597647840942); Precision = Gamma.FromShapeAndRate(1.6965139612477539E+21, 1.6965139612889427E+21); to_precision = Gamma.FromShapeAndRate(1.4695136363119978, 0.14707291154227081); xActual = GaussianOp.SampleAverageConditional(X, Mean, Precision, to_precision); // initialized in a bad place, gets stuck in a flat region X = Gaussian.FromNatural(3.9112579392580757, 11.631097473681082); Mean = Gaussian.FromNatural(10.449696977834144, 5.5617978202886995); Precision = Gamma.FromShapeAndRate(1.0112702817305146, 0.026480506235719053); to_precision = Gamma.FromShapeAndRate(1, 0.029622790537514355); xActual = GaussianOp.SampleAverageConditional(X, Mean, Precision, to_precision); X = Gaussian.FromNatural(57788.170908674481, 50207.150004827061); Mean = Gaussian.PointMass(0); Precision = Gamma.FromShapeAndRate(19764.051194189466, 0.97190264412377791); xActual = GaussianOp.SampleAverageConditional_slow(X, Mean, Precision); // integration bounds should be [-36,4] X = Gaussian.FromNatural(1.696828485456396, 0.71980672726406147); Mean = Gaussian.PointMass(-1); Precision = new Gamma(1, 1); xActual = GaussianOp.SampleAverageConditional_slow(X, Mean, Precision); xExpected = GaussianOp_Slow.SampleAverageConditional(X, Mean, Precision); Assert.True(xExpected.MaxDiff(xActual) < 1e-4); X = new Gaussian(-1.565, 0.8466); Mean = new Gaussian(0.0682, 0.3629); Precision = new Gamma(103.2, 0.009786); xActual = GaussianOp.SampleAverageConditional_slow(X, Mean, Precision); xExpected = GaussianOp_Slow.SampleAverageConditional(X, Mean, Precision); Assert.True(xExpected.MaxDiff(xActual) < 1e-4); // Fixed precision X = X0; Mean = uniform; Assert.True(GaussianOp.SampleAverageConditional(Mean, Precision0).MaxDiff(uniform) < 1e-10); Mean = Mean0; Assert.True(GaussianOp.SampleAverageConditional(Mean, Precision0).MaxDiff(new Gaussian(Mean.GetMean(), Mean.GetVariance() + 1 / Precision0)) < 1e-10); Mean = Gaussian.PointMass(Mean0.GetMean()); Assert.True(GaussianOp.SampleAverageConditional(Mean, Precision0).MaxDiff(new Gaussian(Mean.GetMean(), 1 / Precision0)) < 1e-10); // Uniform precision // the answer should always be uniform Precision = Gamma.Uniform(); Assert.True(GaussianOp.SampleAverageConditional_slow(X, Mean, Precision).MaxDiff(uniform) < 1e-10); // Unknown precision Precision = Gamma.FromShapeAndScale(3, 3); // Known X X = Gaussian.PointMass(X0.GetMean()); Mean = uniform; Assert.True(GaussianOp.SampleAverageConditional_slow(X, Mean, Precision).MaxDiff(uniform) < 1e-10); // Unknown X X = X0; Mean = uniform; //Console.WriteLine(XAverageConditional2(result)); Assert.True(GaussianOp.SampleAverageConditional_slow(X, Mean, Precision).MaxDiff(uniform) < 1e-10); X = X0; Mean = Mean0; // converge the precision message. (only matters if KeepLastMessage is set). //for (int i = 0; i < 10; i++) GaussianOp.PrecisionAverageConditional(X, Mean, Precision, precisionMessage); // in matlab: test_t_msg if (GaussianOp.ForceProper) { Assert.True(GaussianOp.SampleAverageConditional_slow(X, Mean, Precision).MaxDiff(Gaussian.FromNatural(3.1495, 0)) < 1e-4); } else { Assert.True(GaussianOp.SampleAverageConditional_slow(X, Mean, Precision).MaxDiff(new Gaussian(-9.9121, -4.5998)) < 1e-4); } X = X0; Mean = Gaussian.PointMass(Mean0.GetMean()); // converge the precision message. (only matters if KeepLastMessage is set). //for (int i = 0; i < 10; i++) GaussianOp.PrecisionAverageConditional(X, Mean, Precision, precisionMessage); // in matlab: test_t_msg if (GaussianOp.ForceProper) { Assert.True(GaussianOp.SampleAverageConditional_slow(X, Mean, Precision).MaxDiff(Gaussian.FromNatural(2.443, 0)) < 1e-4); } else { Assert.True(GaussianOp.SampleAverageConditional_slow(X, Mean, Precision).MaxDiff(new Gaussian(0.81394, -1.3948)) < 1e-4); } // Uniform X X = uniform; Mean = Mean0; // converge the precision message. (only matters if KeepLastMessage is set). //for (int i = 0; i < 10; i++) GaussianOp.PrecisionAverageConditional(X, Mean, Precision, precisionMessage); Assert.True(GaussianOp.SampleAverageConditional_slow(X, Mean, Precision).MaxDiff(new Gaussian(7, 0.5)) < 1e-10); X = uniform; Mean = Gaussian.PointMass(Mean0.GetMean()); // converge the precision message. (only matters if KeepLastMessage is set). //for (int i = 0; i < 10; i++) GaussianOp.PrecisionAverageConditional(X, Mean, Precision, precisionMessage); Assert.True(GaussianOp.SampleAverageConditional_slow(X, Mean, Precision).MaxDiff(new Gaussian(7, 1.0 / 6)) < 1e-10); }
public void SparseGaussianListFactor() { SparseGaussianList.DefaultTolerance = 1e-10; 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; // True distribution for the means var sparseMeanDist = SparseGaussianList.Constant(listSize, Gaussian.FromMeanAndPrecision(1, 2), tolerance); sparseMeanDist[3] = Gaussian.FromMeanAndPrecision(4, 5); sparseMeanDist[6] = Gaussian.FromMeanAndPrecision(7, 8); var meanDist = sparseMeanDist.ToArray(); var sparseMeanPoint = SparseList <double> .Constant(listSize, 0.1); sparseMeanPoint[3] = 0.7; sparseMeanPoint[6] = 0.8; var meanPoint = sparseMeanPoint.ToArray(); // True distribution for the precisions var sparsePrecDist = SparseGammaList.Constant(listSize, Gamma.FromShapeAndRate(1.1, 1.2), tolerance); sparsePrecDist[3] = Gamma.FromShapeAndRate(2.3, 2.4); sparsePrecDist[6] = Gamma.FromShapeAndRate(3.4, 4.5); var precDist = sparsePrecDist.ToArray(); var sparsePrecPoint = SparseList <double> .Constant(listSize, 0.1); sparsePrecPoint[3] = 5.6; sparsePrecPoint[6] = 0.5; var precPoint = sparsePrecPoint.ToArray(); var sparseSampleDist = SparseGaussianList.Constant(listSize, Gaussian.FromMeanAndPrecision(0.5, 1.5), tolerance); sparseSampleDist[3] = Gaussian.FromMeanAndPrecision(-0.5, 2.0); sparseSampleDist[9] = Gaussian.FromMeanAndPrecision(1.6, 0.4); var sampleDist = sparseSampleDist.ToArray(); var sparseSamplePoint = SparseList <double> .Constant(listSize, 0.5); sparseSamplePoint[3] = 0.1; sparseSamplePoint[9] = 2.3; var samplePoint = sparseSamplePoint.ToArray(); var toSparseSampleDist = SparseGaussianList.Constant(listSize, Gaussian.FromMeanAndPrecision(-0.2, 0.3), tolerance); toSparseSampleDist[3] = Gaussian.FromMeanAndPrecision(2.1, 3.2); toSparseSampleDist[4] = Gaussian.FromMeanAndPrecision(1.3, 0.7); var toSampleDist = toSparseSampleDist.ToArray(); var toSparsePrecDist = SparseGammaList.Constant(listSize, Gamma.FromShapeAndRate(2.3, 3.4), tolerance); toSparsePrecDist[3] = Gamma.FromShapeAndRate(3.4, 4.5); toSparsePrecDist[4] = Gamma.FromShapeAndRate(5.6, 6.7); var toPrecDist = toSparsePrecDist.ToArray(); // --------------------------- // Check average log factor // --------------------------- calcErrMsg = "Average log factor" + calcSuffix; // Dist, dist, dist var sparseAvgLog = SparseGaussianListOp.AverageLogFactor(sparseSampleDist, sparseMeanDist, sparsePrecDist); var avgLog = Util.ArrayInit(listSize, i => GaussianOp.AverageLogFactor(sampleDist[i], meanDist[i], precDist[i])).Sum(); TAssert.True(System.Math.Abs(avgLog - sparseAvgLog) < tolerance, calcErrMsg); // Dist, dist, point sparseAvgLog = SparseGaussianListOp.AverageLogFactor(sparseSampleDist, sparseMeanDist, sparsePrecPoint); avgLog = Util.ArrayInit(listSize, i => GaussianOp.AverageLogFactor(sampleDist[i], meanDist[i], precPoint[i])).Sum(); TAssert.True(System.Math.Abs(avgLog - sparseAvgLog) < tolerance, calcErrMsg); // Dist, point, dist sparseAvgLog = SparseGaussianListOp.AverageLogFactor(sparseSampleDist, sparseMeanPoint, sparsePrecDist); avgLog = Util.ArrayInit(listSize, i => GaussianOp.AverageLogFactor(sampleDist[i], meanPoint[i], precDist[i])).Sum(); TAssert.True(System.Math.Abs(avgLog - sparseAvgLog) < tolerance, calcErrMsg); // Dist, point, point sparseAvgLog = SparseGaussianListOp.AverageLogFactor(sparseSampleDist, sparseMeanPoint, sparsePrecPoint); avgLog = Util.ArrayInit(listSize, i => GaussianOp.AverageLogFactor(sampleDist[i], meanPoint[i], precPoint[i])).Sum(); TAssert.True(System.Math.Abs(avgLog - sparseAvgLog) < tolerance, calcErrMsg); // Point, dist, dist sparseAvgLog = SparseGaussianListOp.AverageLogFactor(sparseSamplePoint, sparseMeanDist, sparsePrecDist); avgLog = Util.ArrayInit(listSize, i => GaussianOp.AverageLogFactor(samplePoint[i], meanDist[i], precDist[i])).Sum(); TAssert.True(System.Math.Abs(avgLog - sparseAvgLog) < tolerance, calcErrMsg); // Point, dist, point sparseAvgLog = SparseGaussianListOp.AverageLogFactor(sparseSamplePoint, sparseMeanDist, sparsePrecPoint); avgLog = Util.ArrayInit(listSize, i => GaussianOp.AverageLogFactor(samplePoint[i], meanDist[i], precPoint[i])).Sum(); TAssert.True(System.Math.Abs(avgLog - sparseAvgLog) < tolerance, calcErrMsg); // Point, point, dist sparseAvgLog = SparseGaussianListOp.AverageLogFactor(sparseSamplePoint, sparseMeanPoint, sparsePrecDist); avgLog = Util.ArrayInit(listSize, i => GaussianOp.AverageLogFactor(samplePoint[i], meanPoint[i], precDist[i])).Sum(); TAssert.True(System.Math.Abs(avgLog - sparseAvgLog) < tolerance, calcErrMsg); // Point, point, point sparseAvgLog = SparseGaussianListOp.AverageLogFactor(sparseSamplePoint, sparseMeanPoint, sparsePrecPoint); avgLog = Util.ArrayInit(listSize, i => GaussianOp.AverageLogFactor(samplePoint[i], meanPoint[i], precPoint[i])).Sum(); TAssert.True(System.Math.Abs(avgLog - sparseAvgLog) < tolerance, calcErrMsg); // --------------------------- // Check log average factor // --------------------------- calcErrMsg = "Log average factor" + calcSuffix; var sparseLogAvg = SparseGaussianListOp.LogAverageFactor(sparseSampleDist, sparseMeanDist, sparsePrecDist, toSparsePrecDist); var logAvg = Util.ArrayInit(listSize, i => GaussianOp.LogAverageFactor(sampleDist[i], meanDist[i], precDist[i], toPrecDist[i])).Sum(); TAssert.True(System.Math.Abs(logAvg - sparseLogAvg) < tolerance, calcErrMsg); // Dist, dist, point sparseLogAvg = SparseGaussianListOp.LogAverageFactor(sparseSampleDist, sparseMeanDist, sparsePrecPoint); logAvg = Util.ArrayInit(listSize, i => GaussianOp.LogAverageFactor(sampleDist[i], meanDist[i], precPoint[i])).Sum(); TAssert.True(System.Math.Abs(logAvg - sparseLogAvg) < tolerance, calcErrMsg); // Dist, point, dist sparseLogAvg = SparseGaussianListOp.LogAverageFactor(sparseSampleDist, sparseMeanPoint, sparsePrecDist, toSparsePrecDist); logAvg = Util.ArrayInit(listSize, i => GaussianOp.LogAverageFactor(sampleDist[i], meanPoint[i], precDist[i], toPrecDist[i])).Sum(); TAssert.True(System.Math.Abs(logAvg - sparseLogAvg) < tolerance, calcErrMsg); // Dist, point, point sparseLogAvg = SparseGaussianListOp.LogAverageFactor(sparseSampleDist, sparseMeanPoint, sparsePrecPoint); logAvg = Util.ArrayInit(listSize, i => GaussianOp.LogAverageFactor(sampleDist[i], meanPoint[i], precPoint[i])).Sum(); TAssert.True(System.Math.Abs(logAvg - sparseLogAvg) < tolerance, calcErrMsg); // Point, dist, dist sparseLogAvg = SparseGaussianListOp.LogAverageFactor(sparseSamplePoint, sparseMeanDist, sparsePrecDist, toSparsePrecDist); logAvg = Util.ArrayInit(listSize, i => GaussianOp.LogAverageFactor(samplePoint[i], meanDist[i], precDist[i], toPrecDist[i])).Sum(); TAssert.True(System.Math.Abs(logAvg - sparseLogAvg) < tolerance, calcErrMsg); // Point, dist, point sparseLogAvg = SparseGaussianListOp.LogAverageFactor(sparseSamplePoint, sparseMeanDist, sparsePrecPoint); logAvg = Util.ArrayInit(listSize, i => GaussianOp.LogAverageFactor(samplePoint[i], meanDist[i], precPoint[i])).Sum(); TAssert.True(System.Math.Abs(logAvg - sparseLogAvg) < tolerance, calcErrMsg); // Point, point, dist sparseLogAvg = SparseGaussianListOp.LogAverageFactor(sparseSamplePoint, sparseMeanPoint, sparsePrecDist); logAvg = Util.ArrayInit(listSize, i => GaussianOp.LogAverageFactor(samplePoint[i], meanPoint[i], precDist[i])).Sum(); TAssert.True(System.Math.Abs(logAvg - sparseLogAvg) < tolerance, calcErrMsg); // Point, point, point sparseLogAvg = SparseGaussianListOp.LogAverageFactor(sparseSamplePoint, sparseMeanPoint, sparsePrecPoint); logAvg = Util.ArrayInit(listSize, i => GaussianOp.LogAverageFactor(samplePoint[i], meanPoint[i], precPoint[i])).Sum(); TAssert.True(System.Math.Abs(logAvg - sparseLogAvg) < tolerance, calcErrMsg); // --------------------------- // Check log evidence ratio // --------------------------- calcErrMsg = "Log evidence ratio" + calcSuffix; var sparseEvidRat = SparseGaussianListOp.LogEvidenceRatio(sparseSampleDist, sparseMeanDist, sparsePrecDist, toSparseSampleDist, toSparsePrecDist); var evidRat = Util.ArrayInit(listSize, i => GaussianOp.LogEvidenceRatio(sampleDist[i], meanDist[i], precDist[i], toSampleDist[i], toPrecDist[i])).Sum(); TAssert.True(System.Math.Abs(evidRat - sparseEvidRat) < tolerance, calcErrMsg); // Dist, dist, point sparseEvidRat = SparseGaussianListOp.LogEvidenceRatio(sparseSampleDist, sparseMeanDist, sparsePrecPoint); evidRat = Util.ArrayInit(listSize, i => GaussianOp.LogEvidenceRatio(sampleDist[i], meanDist[i], precPoint[i])).Sum(); TAssert.True(System.Math.Abs(evidRat - sparseEvidRat) < tolerance, calcErrMsg); // Dist, point, dist sparseEvidRat = SparseGaussianListOp.LogEvidenceRatio(sparseSampleDist, sparseMeanPoint, sparsePrecDist, toSparseSampleDist, toSparsePrecDist); evidRat = Util.ArrayInit(listSize, i => GaussianOp.LogEvidenceRatio(sampleDist[i], meanPoint[i], precDist[i], toSampleDist[i], toPrecDist[i])).Sum(); TAssert.True(System.Math.Abs(evidRat - sparseEvidRat) < tolerance, calcErrMsg); // Dist, point, point sparseEvidRat = SparseGaussianListOp.LogEvidenceRatio(sparseSampleDist, sparseMeanPoint, sparsePrecPoint); evidRat = Util.ArrayInit(listSize, i => GaussianOp.LogEvidenceRatio(sampleDist[i], meanPoint[i], precPoint[i])).Sum(); TAssert.True(System.Math.Abs(evidRat - sparseEvidRat) < tolerance, calcErrMsg); // Point, dist, dist sparseEvidRat = SparseGaussianListOp.LogEvidenceRatio(sparseSamplePoint, sparseMeanDist, sparsePrecDist, toSparsePrecDist); evidRat = Util.ArrayInit(listSize, i => GaussianOp.LogEvidenceRatio(samplePoint[i], meanDist[i], precDist[i], toPrecDist[i])).Sum(); TAssert.True(System.Math.Abs(evidRat - sparseEvidRat) < tolerance, calcErrMsg); // Point, dist, point sparseEvidRat = SparseGaussianListOp.LogEvidenceRatio(sparseSamplePoint, sparseMeanDist, sparsePrecPoint); evidRat = Util.ArrayInit(listSize, i => GaussianOp.LogEvidenceRatio(samplePoint[i], meanDist[i], precPoint[i])).Sum(); TAssert.True(System.Math.Abs(evidRat - sparseEvidRat) < tolerance, calcErrMsg); // Point, point, dist sparseEvidRat = SparseGaussianListOp.LogEvidenceRatio(sparseSamplePoint, sparseMeanPoint, sparsePrecDist); evidRat = Util.ArrayInit(listSize, i => GaussianOp.LogEvidenceRatio(samplePoint[i], meanPoint[i], precDist[i])).Sum(); TAssert.True(System.Math.Abs(evidRat - sparseEvidRat) < tolerance, calcErrMsg); // Point, point, point sparseEvidRat = SparseGaussianListOp.LogEvidenceRatio(sparseSamplePoint, sparseMeanPoint, sparsePrecPoint); evidRat = Util.ArrayInit(listSize, i => GaussianOp.LogEvidenceRatio(samplePoint[i], meanPoint[i], precPoint[i])).Sum(); TAssert.True(System.Math.Abs(evidRat - sparseEvidRat) < tolerance, calcErrMsg); // --------------------------- // Check SampleAverageConditional // --------------------------- calcErrMsg = "SampleAverageConditional" + calcSuffix; sparsityErrMsg = "SampleAverageConditional" + sparsitySuffix; // Use different common value to ensure this gets properly set var sparseSampleAvgConditional = SparseGaussianList.Constant(listSize, Gaussian.FromMeanAndPrecision(0.5, 0.6), tolerance); sparseSampleAvgConditional = SparseGaussianListOp.SampleAverageConditional(sparseSampleDist, sparseMeanDist, sparsePrecDist, toSparsePrecDist, sparseSampleAvgConditional); var sampleAvgConditional = Util.ArrayInit(listSize, i => GaussianOp.SampleAverageConditional(sampleDist[i], meanDist[i], precDist[i], toPrecDist[i])); TAssert.True(3 == sparseSampleAvgConditional.SparseCount, sparsityErrMsg); TAssert.True(sparseSampleAvgConditional.MaxDiff(sampleAvgConditional) < tolerance, calcErrMsg); sparseSampleAvgConditional = SparseGaussianList.Constant(listSize, Gaussian.FromMeanAndPrecision(0.5, 0.6), tolerance); sparseSampleAvgConditional = SparseGaussianListOp.SampleAverageConditional(sparseSampleDist, sparseMeanPoint, sparsePrecDist, toSparsePrecDist, sparseSampleAvgConditional); sampleAvgConditional = Util.ArrayInit(listSize, i => GaussianOp.SampleAverageConditional(sampleDist[i], meanPoint[i], precDist[i], toPrecDist[i])); TAssert.True(3 == sparseSampleAvgConditional.SparseCount, sparsityErrMsg); TAssert.True(sparseSampleAvgConditional.MaxDiff(sampleAvgConditional) < tolerance, calcErrMsg); sparseSampleAvgConditional = SparseGaussianList.Constant(listSize, Gaussian.FromMeanAndPrecision(0.5, 0.6), tolerance); sparseSampleAvgConditional = SparseGaussianListOp.SampleAverageConditional(sparseMeanDist, sparsePrecPoint, sparseSampleAvgConditional); sampleAvgConditional = Util.ArrayInit(listSize, i => GaussianOp.SampleAverageConditional(meanDist[i], precPoint[i])); TAssert.True(2 == sparseSampleAvgConditional.SparseCount, sparsityErrMsg); TAssert.True(sparseSampleAvgConditional.MaxDiff(sampleAvgConditional) < tolerance, calcErrMsg); sparseSampleAvgConditional = SparseGaussianList.Constant(listSize, Gaussian.FromMeanAndPrecision(0.5, 0.6), tolerance); sparseSampleAvgConditional = SparseGaussianListOp.SampleAverageConditional(sparseMeanPoint, sparsePrecPoint, sparseSampleAvgConditional); sampleAvgConditional = Util.ArrayInit(listSize, i => GaussianOp.SampleAverageConditional(meanPoint[i], precPoint[i])); TAssert.True(2 == sparseSampleAvgConditional.SparseCount, sparsityErrMsg); TAssert.True(sparseSampleAvgConditional.MaxDiff(sampleAvgConditional) < tolerance, calcErrMsg); // --------------------------- // Check MeanAverageConditional // --------------------------- calcErrMsg = "MeanAverageConditional" + calcSuffix; sparsityErrMsg = "MeanAverageConditional" + sparsitySuffix; // Use different common value to ensure this gets properly set var sparseMeanAvgConditional = SparseGaussianList.Constant(listSize, Gaussian.FromMeanAndPrecision(0.5, 0.6), tolerance); sparseMeanAvgConditional = SparseGaussianListOp.MeanAverageConditional(sparseSampleDist, sparseMeanDist, sparsePrecDist, toSparsePrecDist, sparseMeanAvgConditional); var meanAvgConditional = Util.ArrayInit(listSize, i => GaussianOp.MeanAverageConditional(sampleDist[i], meanDist[i], precDist[i], toPrecDist[i])); TAssert.True(3 == sparseMeanAvgConditional.SparseCount, sparsityErrMsg); TAssert.True(sparseMeanAvgConditional.MaxDiff(meanAvgConditional) < tolerance, calcErrMsg); sparseMeanAvgConditional = SparseGaussianList.Constant(listSize, Gaussian.FromMeanAndPrecision(0.5, 0.6), tolerance); sparseMeanAvgConditional = SparseGaussianListOp.MeanAverageConditional(sparseSamplePoint, sparseMeanDist, sparsePrecDist, toSparsePrecDist, sparseMeanAvgConditional); meanAvgConditional = Util.ArrayInit(listSize, i => GaussianOp.MeanAverageConditional(samplePoint[i], meanDist[i], precDist[i], toPrecDist[i])); TAssert.True(3 == sparseMeanAvgConditional.SparseCount, sparsityErrMsg); TAssert.True(sparseMeanAvgConditional.MaxDiff(meanAvgConditional) < tolerance, calcErrMsg); sparseMeanAvgConditional = SparseGaussianList.Constant(listSize, Gaussian.FromMeanAndPrecision(0.5, 0.6), tolerance); sparseMeanAvgConditional = SparseGaussianListOp.MeanAverageConditional(sparseSampleDist, sparsePrecPoint, sparseMeanAvgConditional); meanAvgConditional = Util.ArrayInit(listSize, i => GaussianOp.MeanAverageConditional(sampleDist[i], precPoint[i])); TAssert.True(3 == sparseMeanAvgConditional.SparseCount, sparsityErrMsg); TAssert.True(sparseMeanAvgConditional.MaxDiff(meanAvgConditional) < tolerance, calcErrMsg); sparseMeanAvgConditional = SparseGaussianList.Constant(listSize, Gaussian.FromMeanAndPrecision(0.5, 0.6), tolerance); sparseMeanAvgConditional = SparseGaussianListOp.MeanAverageConditional(sparseSamplePoint, sparsePrecPoint, sparseMeanAvgConditional); meanAvgConditional = Util.ArrayInit(listSize, i => GaussianOp.MeanAverageConditional(samplePoint[i], precPoint[i])); TAssert.True(3 == sparseMeanAvgConditional.SparseCount, sparsityErrMsg); TAssert.True(sparseMeanAvgConditional.MaxDiff(meanAvgConditional) < tolerance, calcErrMsg); // --------------------------- // Check PrecisionAverageConditional // --------------------------- calcErrMsg = "PrecisionAverageConditional" + calcSuffix; sparsityErrMsg = "PrecisionAverageConditional" + sparsitySuffix; // Use different common value to ensure this gets properly set var sparsePrecAvgConditional = SparseGammaList.Constant(listSize, Gamma.FromShapeAndRate(2.1, 3.2), tolerance); sparsePrecAvgConditional = SparseGaussianListOp.PrecisionAverageConditional(sparseSampleDist, sparseMeanDist, sparsePrecDist, sparsePrecAvgConditional); var precAvgConditional = Util.ArrayInit(listSize, i => GaussianOp.PrecisionAverageConditional(sampleDist[i], meanDist[i], precDist[i])); TAssert.True(3 == sparsePrecAvgConditional.SparseCount, sparsityErrMsg); TAssert.True(sparsePrecAvgConditional.MaxDiff(precAvgConditional) < tolerance, calcErrMsg); sparsePrecAvgConditional = SparseGammaList.Constant(listSize, Gamma.FromShapeAndRate(2.1, 3.2), tolerance); sparsePrecAvgConditional = SparseGaussianListOp.PrecisionAverageConditional(sparseSamplePoint, sparseMeanDist, sparsePrecDist, sparsePrecAvgConditional); precAvgConditional = Util.ArrayInit(listSize, i => GaussianOp.PrecisionAverageConditional(Gaussian.PointMass(samplePoint[i]), meanDist[i], precDist[i])); TAssert.True(3 == sparsePrecAvgConditional.SparseCount, sparsityErrMsg); TAssert.True(sparsePrecAvgConditional.MaxDiff(precAvgConditional) < tolerance, calcErrMsg); sparsePrecAvgConditional = SparseGammaList.Constant(listSize, Gamma.FromShapeAndRate(2.1, 3.2), tolerance); sparsePrecAvgConditional = SparseGaussianListOp.PrecisionAverageConditional(sparseSampleDist, sparseMeanPoint, sparsePrecDist, sparsePrecAvgConditional); precAvgConditional = Util.ArrayInit(listSize, i => GaussianOp.PrecisionAverageConditional(sampleDist[i], Gaussian.PointMass(meanPoint[i]), precDist[i])); TAssert.True(3 == sparsePrecAvgConditional.SparseCount, sparsityErrMsg); TAssert.True(sparsePrecAvgConditional.MaxDiff(precAvgConditional) < tolerance, calcErrMsg); sparsePrecAvgConditional = SparseGammaList.Constant(listSize, Gamma.FromShapeAndRate(2.1, 3.2), tolerance); sparsePrecAvgConditional = SparseGaussianListOp.PrecisionAverageConditional(sparseSamplePoint, sparseMeanPoint, sparsePrecAvgConditional); precAvgConditional = Util.ArrayInit(listSize, i => GaussianOp.PrecisionAverageConditional(samplePoint[i], meanPoint[i])); TAssert.True(3 == sparsePrecAvgConditional.SparseCount, sparsityErrMsg); TAssert.True(sparsePrecAvgConditional.MaxDiff(precAvgConditional) < tolerance, calcErrMsg); }
/// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="SparseGaussianListOp"]/message_doc[@name="SampleAverageConditional(SparseGaussianList, ISparseList{double}, SparseGammaList, SparseGammaList, SparseGaussianList)"]/*'/> public static SparseGaussianList SampleAverageConditional( SparseGaussianList sample, ISparseList <double> mean, [SkipIfUniform] SparseGammaList precision, SparseGammaList to_precision, SparseGaussianList result) { result.SetToFunction(sample, mean, precision, to_precision, (s, m, p, tp) => GaussianOp.SampleAverageConditional(s, m, p, tp)); return(result); }
/// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="GaussianFromMeanAndVarianceOp"]/message_doc[@name="SampleAverageConditional(Gaussian, double)"]/*'/> public static Gaussian SampleAverageConditional([SkipIfUniform] Gaussian mean, double variance) { return(GaussianOp.SampleAverageConditional(mean, 1 / variance)); }
/// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="SparseGaussianListOp"]/message_doc[@name="SampleAverageConditional(SparseGaussianList, ISparseList{double}, SparseGaussianList)"]/*'/> public static SparseGaussianList SampleAverageConditional([SkipIfUniform] SparseGaussianList mean, ISparseList <double> precision, SparseGaussianList result) { result.SetToFunction(mean, precision, (m, p) => GaussianOp.SampleAverageConditional(m, p)); return(result); }