internal void GaussianOpQ_Timing() { Gaussian X, Mean; Gamma Precision; Gamma q; int n = 1; X = Gaussian.FromNatural(3.9112579392580757, 11.631097473681082); Mean = Gaussian.FromNatural(10.449696977834144, 5.5617978202886995); Precision = Gamma.FromShapeAndRate(1.0112702817305146, 0.026480506235719053); q = Gamma.FromMeanAndVariance(1, 1); Stopwatch watch = new Stopwatch(); watch.Start(); for (int i = 0; i < n; i++) { GaussianOp_Laplace.Q(X, Mean, Precision, q); } watch.Stop(); Console.WriteLine("Q = {0}", watch.ElapsedTicks); watch.Restart(); for (int i = 0; i < n; i++) { GaussianOp_Laplace.Q_Slow(X, Mean, Precision); } watch.Stop(); Console.WriteLine("Q2 = {0}", watch.ElapsedTicks); }
internal void StudentIsPositiveTest4() { double shape = 1; Gamma precPrior = Gamma.FromShapeAndRate(shape, shape); // mean=-1 causes improper messages double mean = -1; Gaussian meanPrior = Gaussian.PointMass(mean); double evExpected; Gaussian xExpected = StudentIsPositiveExact(mean, precPrior, out evExpected); GaussianOp.ForceProper = false; GaussianOp_Laplace.modified = true; GaussianOp_Laplace.modified2 = true; Gaussian xF = Gaussian.Uniform(); Gaussian xB = Gaussian.Uniform(); Gamma q = GaussianOp_Laplace.QInit(); double r0 = 0.38; r0 = 0.1; for (int iter = 0; iter < 20; iter++) { q = GaussianOp_Laplace.Q(xB, meanPrior, precPrior, q); //xF = GaussianOp_Laplace.SampleAverageConditional(xB, meanPrior, precPrior, q); xF = Gaussian.FromMeanAndPrecision(mean, r0); xB = IsPositiveOp.XAverageConditional(true, xF); Console.WriteLine("xF = {0} xB = {1}", xF, xB); } Console.WriteLine("x = {0} should be {1}", xF * xB, xExpected); double[] precs = EpTests.linspace(1e-3, 5, 100); double[] evTrue = new double[precs.Length]; double[] evApprox = new double[precs.Length]; double[] evApprox2 = new double[precs.Length]; //r0 = q.GetMean(); double sum = 0, sum2 = 0; for (int i = 0; i < precs.Length; i++) { double r = precs[i]; Gaussian xFt = Gaussian.FromMeanAndPrecision(mean, r); evTrue[i] = IsPositiveOp.LogAverageFactor(true, xFt) + precPrior.GetLogProb(r); evApprox[i] = IsPositiveOp.LogAverageFactor(true, xF) + precPrior.GetLogProb(r) + xB.GetLogAverageOf(xFt) - xB.GetLogAverageOf(xF); evApprox2[i] = IsPositiveOp.LogAverageFactor(true, xF) + precPrior.GetLogProb(r0) + q.GetLogProb(r) - q.GetLogProb(r0); sum += System.Math.Exp(evApprox[i]); sum2 += System.Math.Exp(evApprox2[i]); } Console.WriteLine("r0 = {0}: {1} {2} {3}", r0, sum, sum2, q.GetVariance() + System.Math.Pow(r0 - q.GetMean(), 2)); //TODO: change path for cross platform using using (var writer = new MatlabWriter(@"..\..\..\Tests\student.mat")) { writer.Write("z", evTrue); writer.Write("z2", evApprox); writer.Write("z3", evApprox2); writer.Write("precs", precs); } }