public void GaussianOpMean() { Gaussian result = new Gaussian(); Gaussian X0 = Gaussian.FromMeanAndVariance(7, 1.0 / 3); Gaussian Mean0 = Gaussian.FromMeanAndVariance(3, 0.5); Gamma Precision0 = Gamma.FromShapeAndScale(3, 3); // Unknown precision Gamma Precision = Gamma.FromShapeAndScale(3, 3); Gaussian X = X0; Gaussian Mean = Mean0; // in matlab: test_t_msg result = GaussianOp.MeanAverageConditional_slow(X, Mean, Precision); Console.WriteLine(result); Assert.True(GaussianOp.MeanAverageConditional_slow(X, Mean, Precision).MaxDiff(new Gaussian(-9.9121, -4.5998)) < 1e-0); X = Gaussian.FromMeanAndVariance(1, 2); Mean = Gaussian.PointMass(0); Precision = Gamma.FromShapeAndRate(3, 1); Gaussian xPostExpected = Gaussian.FromMeanAndVariance(0.178378819440295, 0.365796599498963); Console.WriteLine(GaussianOp.SampleAverageConditional_slow(X, Mean, Precision) * X); Assert.True(GaussianOp.SampleAverageConditional_slow(X, Mean, Precision).MaxDiff(xPostExpected / X) < 5e-7); Console.WriteLine(GaussianOp_Slow.SampleAverageConditional(X, Mean, Precision) * X); Assert.True(GaussianOp_Slow.SampleAverageConditional(X, Mean, Precision).MaxDiff(xPostExpected / X) < 5e-7); }
private void GaussianOpX2(Gaussian mean, Gamma precision) { Gaussian sample, result, result2; sample = Gaussian.PointMass(2); result = GaussianOp.SampleAverageConditional_slow(sample, mean, precision); result2 = GaussianOp_Slow.SampleAverageConditional(sample, mean, precision); Console.WriteLine("{0}: {1} {2}", sample, result, result2); Assert.True(result.MaxDiff(result2) < 1e-8); double prevDiff = double.PositiveInfinity; for (int i = 8; i < 30; i++) { double v = System.Math.Pow(0.1, i); sample = Gaussian.FromMeanAndVariance(2, v); result2 = GaussianOp_Slow.SampleAverageConditional(sample, mean, precision); double diff = result.MaxDiff(result2); Console.WriteLine("{0}: {1} diff={2}", sample, result2, diff.ToString("g4")); Assert.True(diff <= prevDiff || diff < 1e-6); result2 = GaussianOp.SampleAverageConditional_slow(sample, mean, precision); diff = result.MaxDiff(result2); Console.WriteLine("{0}: {1} diff={2}", sample, result2, diff.ToString("g4")); Assert.True(diff <= prevDiff || diff < 1e-6); prevDiff = diff; } }
internal void StudentIsPositiveTest3() { double shape = 1; Gamma precPrior = Gamma.FromShapeAndRate(shape, shape); Gaussian meanPrior = Gaussian.PointMass(0); Gaussian xB = Gaussian.Uniform(); Gaussian xF = GaussianOp.SampleAverageConditional_slow(xB, meanPrior, precPrior); for (int iter = 0; iter < 100; iter++) { xB = IsPositiveOp.XAverageConditional(true, xF); xF = GetConstrainedMessage(xB, meanPrior, precPrior, xF); } Console.WriteLine("xF = {0} x = {1}", xF, xB * xF); }
private static Gaussian GetConstrainedMessage1(Gaussian sample, Gaussian mean, Gamma precision, Gaussian to_sample) { Gaussian sampleMarginal = sample * to_sample; double m1, v1; to_sample.GetMeanAndVariance(out m1, out v1); double m, v; sampleMarginal.GetMeanAndVariance(out m, out v); double moment2 = m * m + v; // vq < moment2 implies 1/vq > 1/moment2 // implies 1/v2 > 1/moment2 - to_sample.Precision double v2max = 1 / (1 / moment2 - to_sample.Precision); double v2min = 1e-2; double[] v2s = EpTests.linspace(v2min, v2max, 100); double p2min = 1 / moment2 - to_sample.Precision; if (p2min < 0.0) { return(to_sample); } double p2max = sample.Precision * 10; double[] p2s = EpTests.linspace(p2min, p2max, 100); Gaussian bestResult = to_sample; double bestScore = double.PositiveInfinity; for (int i = 0; i < p2s.Length; i++) { double p2 = p2s[i]; double vq = 1 / (to_sample.Precision + p2); double m2 = (System.Math.Sqrt(moment2 - vq) / vq - to_sample.MeanTimesPrecision) / p2; // check double mq = vq * (to_sample.MeanTimesPrecision + m2 * p2); Assert.True(MMath.AbsDiff(mq * mq + vq, moment2) < 1e-10); Gaussian sample2 = Gaussian.FromMeanAndPrecision(m2, p2); Gaussian result = GaussianOp.SampleAverageConditional_slow(sample2, mean, precision); double score = System.Math.Abs(result.MeanTimesPrecision); if (score < bestScore) { bestScore = score; bestResult = result; } } return(bestResult); }
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); }