Exemple #1
0
        public void SampleTest()
        {
            double Mean = 5 * rnd.NextDouble();
            double Sdv  = 5 * rnd.NextDouble();

            Normal             dis    = new Normal(Mean, Sdv);
            UnivariateHybridMC Hybrid = new UnivariateHybridMC(0, dis.DensityLn, 10, 0.1, 1000);

            Hybrid.BurnInterval = 0;

            double[] Sample = Hybrid.Sample(10000);

            double Effective = MCMCDiagnostics.EffectiveSize(Sample, x => x);

            DescriptiveStatistics Stats = new DescriptiveStatistics(Sample);

            //Testing the mean using the normal distribution.
            double MeanConvergence = 3 * Sdv / Math.Sqrt(Effective);

            Assert.AreEqual(Stats.Mean, Mean, MeanConvergence, "Mean");

            double DeviationRation = Math.Pow(Stats.StandardDeviation / Sdv, 2);

            //Approximating chi-square with normal distribution. (Degree of freedom is large)
            double DeviationConvergence = 3 * Math.Sqrt(2) / Math.Sqrt(Effective);

            Assert.AreEqual(DeviationRation, 1, DeviationConvergence, "Standard Deivation");
        }
Exemple #2
0
        public void SampleTest(double mean, double sdv, int seed)
        {
            var dis    = new Normal(mean, sdv);
            var hybrid = new UnivariateHybridMC(0, dis.DensityLn, 10, 0.1, 1000, 1, new System.Random(seed))
            {
                BurnInterval = 0
            };

            double[] sample = hybrid.Sample(10000);

            double effective = MCMCDiagnostics.EffectiveSize(sample, x => x);

            var stats = new DescriptiveStatistics(sample);

            //Testing the mean using the normal distribution.
            double meanConvergence = 3 * sdv / Math.Sqrt(effective);

            Assert.AreEqual(stats.Mean, mean, meanConvergence, "Mean");

            double deviationRation = Math.Pow(stats.StandardDeviation / sdv, 2);

            //Approximating chi-square with normal distribution. (Degree of freedom is large)
            double deviationConvergence = 3 * Math.Sqrt(2) / Math.Sqrt(effective);

            Assert.AreEqual(deviationRation, 1, deviationConvergence, "Standard Deivation");
        }
        public void SampleTest()
        {
            //Variances and Means
            double[] Sdv  = new double[2];
            double[] Mean = new double[2];
            for (int i = 0; i < 2; i++)
            {
                Sdv[i]  = RandomVar();
                Mean[i] = 5 * rnd.NextDouble();
            }

            //Cross Variance
            double rho = 1.0 - RandomVar();

            DensityLn <double[]> pdfLn = new DensityLn <double[]>(x => LogDen(x, Sdv, Mean, rho));


            HybridMC Hybrid = new HybridMC(new double[2] {
                0, 0
            }, pdfLn, 10, 0.1, 1000);

            Hybrid.BurnInterval = 0;

            int SampleSize = 10000;

            double[][] Sample     = Hybrid.Sample(SampleSize);
            double[][] NewSamples = ArrangeSamples(SampleSize, Sample);

            double[] Convergence = new double[2];
            double[] SampleMean  = new double[2];
            double[] SampleSdv   = new double[2];


            for (int i = 0; i < 2; i++)
            {
                Convergence[i] = 1 / Math.Sqrt(MCMCDiagnostics.EffectiveSize(Sample, x => x[i]));
                DescriptiveStatistics Stats = new DescriptiveStatistics(NewSamples[i]);
                SampleMean[i] = Stats.Mean;
                SampleSdv[i]  = Stats.StandardDeviation;
            }
            double SampleRho = Correlation.Pearson(NewSamples[0], NewSamples[1]);

            for (int i = 0; i < 2; i++)
            {
                string index = i.ToString();
                Assert.AreEqual(Mean[i], SampleMean[i], 10 * Convergence[i], index + "Mean");
                Assert.AreEqual(SampleSdv[i] * SampleSdv[i], Sdv[i] * Sdv[i], 10 * Convergence[i], index + "Standard Deviation");
            }

            double ConvergenceRho = 1 / Math.Sqrt(MCMCDiagnostics.EffectiveSize(Sample, x => (x[0] - SampleMean[0]) * (x[1] - SampleMean[1])));

            Assert.AreEqual(SampleRho * SampleSdv[0] * SampleSdv[1], rho * Sdv[0] * Sdv[1], 10 * ConvergenceRho, "Rho");
        }
Exemple #4
0
        public void EffectiveSizeTest()
        {
            const int length = 10;
            var       series = new double[length];

            for (int i = 0; i < length; i++)
            {
                series[i] = RandomSeries();
            }

            double rho = MCMCDiagnostics.ACF(series, 1, x => x * x);
            double ess = (1 - rho) / (1 + rho) * length;

            Assert.AreEqual(ess, MCMCDiagnostics.EffectiveSize(series, x => x * x), 10e-13);
        }
Exemple #5
0
        public void EffectiveSizeTest()
        {
            int Length = 10;

            double[] Series = new double[Length];
            for (int i = 0; i < Length; i++)
            {
                Series[i] = RandomSeries();
            }

            double rho = MCMCDiagnostics.ACF(Series, 1, x => x * x);
            double ESS = (1 - rho) / (1 + rho) * Length;

            Assert.AreEqual(ESS, MCMCDiagnostics.EffectiveSize(Series, x => x * x), 10e-13);
        }
Exemple #6
0
        public void SampleTest(double[] sdv, double[] mean, double rho, int seed)
        {
            var pdfLn  = new DensityLn <double[]>(x => LogDen(x, sdv, mean, rho));
            var hybrid = new HybridMC(new double[2] {
                0, 0
            }, pdfLn, 10, 0.1, 1000, new double[] { 1, 1 }, new System.Random(seed))
            {
                BurnInterval = 0
            };

            const int sampleSize = 10000;

            double[][] sample     = hybrid.Sample(sampleSize);
            double[][] newSamples = ArrangeSamples(sampleSize, sample);

            var convergence = new double[2];
            var sampleMean  = new double[2];
            var sampleSdv   = new double[2];

            for (int i = 0; i < 2; i++)
            {
                convergence[i] = 1 / Math.Sqrt(MCMCDiagnostics.EffectiveSize(sample, x => x[i]));
                var stats = new DescriptiveStatistics(newSamples[i]);
                sampleMean[i] = stats.Mean;
                sampleSdv[i]  = stats.StandardDeviation;
            }

            double sampleRho = Correlation.Pearson(newSamples[0], newSamples[1]);

            for (int i = 0; i < 2; i++)
            {
                string index = i.ToString();
                Assert.AreEqual(mean[i], sampleMean[i], 10 * convergence[i], index + "Mean");
                Assert.AreEqual(sampleSdv[i] * sampleSdv[i], sdv[i] * sdv[i], 10 * convergence[i], index + "Standard Deviation");
            }

            double convergenceRho = 1 / Math.Sqrt(MCMCDiagnostics.EffectiveSize(sample, x => (x[0] - sampleMean[0]) * (x[1] - sampleMean[1])));

            Assert.AreEqual(sampleRho * sampleSdv[0] * sampleSdv[1], rho * sdv[0] * sdv[1], 10 * convergenceRho, "Rho");
        }