Ejemplo n.º 1
0
        // Commented out since this test is very slow and does not have any assertions
        //[Fact]
        internal void GaussianFromMeanAndVarianceVaryTruePrec()
        {
            double mean = 2.0;

            var Nvar = Variable.Observed <int>(50);
            var n    = new Range(Nvar);

            var noisePrecision = 10;

            var variancePriors = new double[] { 1, 5, 10 }.Select(i => Gamma.FromShapeAndRate(i, i)).ToArray();

            var varPriorVar = Variable.Observed(new Gamma());

            var variance_EP = Variable <double> .Random(varPriorVar);

            var x_ep  = GaussianFromMeanAndVarianceSampleSizeModel(variance_EP, n, true, noisePrecision);
            var ie_EP = new InferenceEngine();

            ie_EP.ShowWarnings = false;
            ie_EP.ShowProgress = false;

            var ev          = Variable.Bernoulli(.5);
            var modelBlock  = Variable.If(ev);
            var variance_MC = Variable <double> .Random(varPriorVar);

            var x_mc = GaussianFromMeanAndVarianceSampleSizeModel(variance_MC, n, true, noisePrecision);

            modelBlock.CloseBlock();
            var ie_MC = new InferenceEngine();

            ie_MC.ShowWarnings = false;
            ie_MC.ShowProgress = false;

            var precision_VMP = Variable.GammaFromShapeAndRate(1, 1);
            var x_vmp         = GaussianFromMeanAndVarianceSampleSizeModel(precision_VMP, n, false, noisePrecision);
            var ie_VMP        = new InferenceEngine(new VariationalMessagePassing());

            ie_VMP.ShowWarnings = false;
            ie_VMP.ShowProgress = false;

            var ie_EP_prec = new InferenceEngine();

            ie_EP_prec.ShowWarnings = false;
            ie_EP_prec.ShowProgress = false;

            Console.WriteLine("var " + variancePriors.Select(i => "EP" + i.Shape).Aggregate((i, j) => i + " " + j) + " "
                              + variancePriors.Select(i => "MC" + i.Shape).Aggregate((i, j) => i + " " + j) + " EPp VMP EMP");
            for (int j = 0; j < 10; j++)
            {
                Rand.Restart(2);
                //int N = (int)Math.Pow(10, j + 1);
                double variance = System.Math.Exp(-5 + j);

                var data = Enumerable.Range(0, Nvar.ObservedValue).Select(i => Gaussian.Sample(mean, 1.0 / variance) + Gaussian.Sample(0, noisePrecision)).ToArray();

                x_ep.ObservedValue  = data;
                x_mc.ObservedValue  = data;
                x_vmp.ObservedValue = data;

                var epMeans = variancePriors.Select(i =>
                {
                    double res = double.NaN;
                    varPriorVar.ObservedValue = i;
                    try
                    {
                        res = ie_EP.Infer <Gamma>(variance_EP).GetMean();
                    }
                    catch
                    {
                    }
                    ;
                    return(res);
                }).ToArray();

                var mcMeans = variancePriors.Select(p =>
                {
                    double varianceSample = 2;
                    var ge = new GammaEstimator();
                    varPriorVar.ObservedValue    = p;
                    Converter <double, double> f = vari =>
                    {
                        variance_MC.ObservedValue = vari;
                        return(ie_MC.Infer <Bernoulli>(ev).LogOdds);
                    };
                    int burnin  = 1000, thin = 5, numSamples = 1000;
                    var samples = new double[numSamples];
                    for (int i = 0; i < burnin; i++)
                    {
                        varianceSample = NonconjugateVMP2Tests.SliceSampleUnivariate(varianceSample, f, lower_bound: 0);
                    }
                    for (int i = 0; i < numSamples; i++)
                    {
                        for (int k = 0; k < thin; k++)
                        {
                            varianceSample = NonconjugateVMP2Tests.SliceSampleUnivariate(varianceSample, f, lower_bound: 0);
                        }
                        samples[i] = varianceSample;
                        ge.Add(varianceSample);
                    }
                    return(samples.Sum() / numSamples);
                }).ToArray();


                double epMean2 = double.NaN;
                try
                {
                    epMean2 = ie_EP_prec.Infer <Gamma>(precision_VMP).GetMeanInverse();
                }
                catch
                {
                }
                ;

                double vmpMean = ie_VMP.Infer <Gamma>(precision_VMP).GetMeanInverse();

                var empiricalMean = data.Sum() / data.Length;
                var empVar        = data.Sum(o => o * o) / data.Length - empiricalMean * empiricalMean;
                Console.Write("{0:G3} ", variance);
                foreach (var i in epMeans)
                {
                    Console.Write("{0:G3} ", i);
                }
                foreach (var i in mcMeans)
                {
                    Console.Write("{0:G3} ", i);
                }
                Console.WriteLine("{0:G3} {1:G3} {2:G3}", epMean2, vmpMean, empVar);
            }
        }
Ejemplo n.º 2
0
        // Commented out since this test is very slow and does not have any assertions
        //[Fact]
        internal void GaussianFromMeanAndVarianceVaryNoise()
        {
            double mean = 2.0, variance = 2.0;

            var Nvar = Variable.Observed <int>(1);
            var n    = new Range(Nvar);

            var noisePrecision = Variable.Array <double>(n);

            var variance_EP = Variable.GammaFromShapeAndRate(1, 1);
            var x_ep        = GaussianPlusNoiseModel(variance_EP, n, true, noisePrecision);
            var ie_EP       = new InferenceEngine();

            ie_EP.ShowWarnings = false;
            ie_EP.ShowProgress = false;

            var ev          = Variable.Bernoulli(.5);
            var modelBlock  = Variable.If(ev);
            var variance_MC = Variable.GammaFromShapeAndRate(1, 1);
            var x_mc        = GaussianPlusNoiseModel(variance_MC, n, true, noisePrecision);

            modelBlock.CloseBlock();
            var ie_MC = new InferenceEngine();

            ie_MC.ShowWarnings = false;
            ie_MC.ShowProgress = false;

            var precision_VMP = Variable.GammaFromShapeAndRate(1, 1);
            var x_vmp         = GaussianPlusNoiseModel(precision_VMP, n, false, noisePrecision);
            var ie_VMP        = new InferenceEngine(new VariationalMessagePassing());

            ie_VMP.ShowWarnings = false;
            ie_VMP.ShowProgress = false;

            Console.WriteLine("N EP MC VMP EMP");
            for (int j = 0; j < 10; j++)
            {
                Rand.Restart(2);
                //int N = (int)Math.Pow(10, j + 1);
                int N = 10 * (j + 1);

                var noiseP = Enumerable.Range(0, N).Select(i =>
                {
                    if (i % 3 == 0)
                    {
                        return(.1);
                    }
                    if (i % 3 == 1)
                    {
                        return(1);
                    }
                    else
                    {
                        return(10);
                    }
                }).ToArray();

                var data = noiseP.Select(i => Gaussian.Sample(mean, 1.0 / variance) + Gaussian.Sample(0, i)).ToArray();

                Nvar.ObservedValue           = N;
                noisePrecision.ObservedValue = noiseP;
                x_ep.ObservedValue           = data;
                x_mc.ObservedValue           = data;
                x_vmp.ObservedValue          = data;

                double epMean = double.NaN;
                try
                {
                    epMean = ie_EP.Infer <Gamma>(variance_EP).GetMean();
                }
                catch
                {
                }
                ;

                double vmpMean        = ie_VMP.Infer <Gamma>(precision_VMP).GetMeanInverse();
                double varianceSample = 2;
                var    ge             = new GammaEstimator();

                Converter <double, double> f = vari =>
                {
                    variance_MC.ObservedValue = vari;
                    return(ie_MC.Infer <Bernoulli>(ev).LogOdds);
                };
                int burnin = 1000, thin = 5, numSamples = 1000;
                var samples = new double[numSamples];
                for (int i = 0; i < burnin; i++)
                {
                    varianceSample = NonconjugateVMP2Tests.SliceSampleUnivariate(varianceSample, f, lower_bound: 0);
                }
                for (int i = 0; i < numSamples; i++)
                {
                    for (int k = 0; k < thin; k++)
                    {
                        varianceSample = NonconjugateVMP2Tests.SliceSampleUnivariate(varianceSample, f, lower_bound: 0);
                    }
                    samples[i] = varianceSample;
                    ge.Add(varianceSample);
                }
                double mcMean        = samples.Sum() / numSamples;
                var    empiricalMean = data.Sum() / data.Length;
                var    empVar        = data.Sum(o => o * o) / data.Length - empiricalMean * empiricalMean;
                Console.WriteLine(N + " " + epMean + " " + mcMean + " " + vmpMean + " " + empVar);
            }
        }