Exemple #1
0
        public void WilcoxonNullDistribution()
        {
            // Pick a very non-normal distribution
            ContinuousDistribution d = new ExponentialDistribution();

            Random rng = new Random(271828);

            foreach (int n in TestUtilities.GenerateIntegerValues(4, 64, 4))
            {
                Sample wSample = new Sample();
                ContinuousDistribution wDistribution = null;
                for (int i = 0; i < 128; i++)
                {
                    BivariateSample sample = new BivariateSample();
                    for (int j = 0; j < n; j++)
                    {
                        double x = d.GetRandomValue(rng);
                        double y = d.GetRandomValue(rng);
                        sample.Add(x, y);
                    }
                    TestResult wilcoxon = sample.WilcoxonSignedRankTest();
                    wSample.Add(wilcoxon.Statistic);
                    wDistribution = wilcoxon.Distribution;
                }

                TestResult ks = wSample.KolmogorovSmirnovTest(wDistribution);
                Assert.IsTrue(ks.Probability > 0.05);

                Assert.IsTrue(wSample.PopulationMean.ConfidenceInterval(0.99).ClosedContains(wDistribution.Mean));
                Assert.IsTrue(wSample.PopulationStandardDeviation.ConfidenceInterval(0.99).ClosedContains(wDistribution.StandardDeviation));
            }
        }
Exemple #2
0
        public void WilcoxonNullDistribution()
        {
            // Pick a very non-normal distribution
            ContinuousDistribution d = new ExponentialDistribution();

            Random rng = new Random(271828);

            foreach (int n in TestUtilities.GenerateIntegerValues(4, 64, 4))
            {
                Sample wContinuousSample = new Sample();
                ContinuousDistribution wContinuousDistribution = null;

                List <int>           wDiscreteSample       = new List <int>();
                DiscreteDistribution wDiscreteDistribution = null;

                for (int i = 0; i < 256; i++)
                {
                    BivariateSample sample = new BivariateSample();
                    for (int j = 0; j < n; j++)
                    {
                        double x = d.GetRandomValue(rng);
                        double y = d.GetRandomValue(rng);
                        sample.Add(x, y);
                    }
                    TestResult wilcoxon = sample.WilcoxonSignedRankTest();
                    if (wilcoxon.UnderlyingStatistic != null)
                    {
                        wDiscreteSample.Add(wilcoxon.UnderlyingStatistic.Value);
                        wDiscreteDistribution = wilcoxon.UnderlyingStatistic.Distribution;
                    }
                    else
                    {
                        wContinuousSample.Add(wilcoxon.Statistic.Value);
                        wContinuousDistribution = wilcoxon.Statistic.Distribution;
                    }
                }

                if (wDiscreteDistribution != null)
                {
                    TestResult chi2 = wDiscreteSample.ChiSquaredTest(wDiscreteDistribution);
                    Assert.IsTrue(chi2.Probability > 0.01);
                }
                else
                {
                    TestResult ks = wContinuousSample.KolmogorovSmirnovTest(wContinuousDistribution);
                    Assert.IsTrue(ks.Probability > 0.01);
                    Assert.IsTrue(wContinuousSample.PopulationMean.ConfidenceInterval(0.99).ClosedContains(wContinuousDistribution.Mean));
                    Assert.IsTrue(wContinuousSample.PopulationStandardDeviation.ConfidenceInterval(0.99).ClosedContains(wContinuousDistribution.StandardDeviation));
                }
            }
        }
        public void GammaFromExponential()
        {
            // test that x_1 + x_2 + ... + x_n ~ Gamma(n) when z ~ Exponential()

            Random rng = new Random(1);
            ExponentialDistribution eDistribution = new ExponentialDistribution();

            // pick some low values of n so distribution is not simply normal
            foreach (int n in new int[] { 2, 3, 4, 5 })
            {
                Sample gSample = new Sample();
                for (int i = 0; i < 100; i++)
                {
                    double sum = 0.0;
                    for (int j = 0; j < n; j++)
                    {
                        sum += eDistribution.GetRandomValue(rng);
                    }
                    gSample.Add(sum);
                }

                GammaDistribution gDistribution = new GammaDistribution(n);
                TestResult        result        = gSample.KolmogorovSmirnovTest(gDistribution);
                Assert.IsTrue(result.Probability > 0.05);
            }
        }
        public void MultivariateMoments()
        {
            // create a random sample
            MultivariateSample     M  = new MultivariateSample(3);
            ContinuousDistribution d0 = new NormalDistribution();
            ContinuousDistribution d1 = new ExponentialDistribution();
            ContinuousDistribution d2 = new UniformDistribution();
            Random rng = new Random(1);
            int    n   = 10;

            for (int i = 0; i < n; i++)
            {
                M.Add(d0.GetRandomValue(rng), d1.GetRandomValue(rng), d2.GetRandomValue(rng));
            }

            // test that moments agree
            for (int i = 0; i < 3; i++)
            {
                int[] p = new int[3];
                p[i] = 1;
                Assert.IsTrue(TestUtilities.IsNearlyEqual(M.Column(i).Mean, M.RawMoment(p)));
                p[i] = 2;
                Assert.IsTrue(TestUtilities.IsNearlyEqual(M.Column(i).Variance, M.CentralMoment(p)));
                for (int j = 0; j < i; j++)
                {
                    int[] q = new int[3];
                    q[i] = 1;
                    q[j] = 1;
                    Assert.IsTrue(TestUtilities.IsNearlyEqual(M.TwoColumns(i, j).Covariance, M.CentralMoment(q)));
                }
            }
        }
Exemple #5
0
        public void KendallNullDistributionTest()
        {
            // Pick independent distributions for x and y, which needn't be normal and needn't be related.
            ContinuousDistribution xDistrubtion  = new LogisticDistribution();
            ContinuousDistribution yDistribution = new ExponentialDistribution();
            Random rng = new Random(314159265);

            // generate bivariate samples of various sizes
            foreach (int n in TestUtilities.GenerateIntegerValues(8, 64, 4))
            {
                Sample testStatistics = new Sample();
                ContinuousDistribution testDistribution = null;

                for (int i = 0; i < 128; i++)
                {
                    BivariateSample sample = new BivariateSample();
                    for (int j = 0; j < n; j++)
                    {
                        sample.Add(xDistrubtion.GetRandomValue(rng), yDistribution.GetRandomValue(rng));
                    }

                    TestResult result = sample.KendallTauTest();
                    testStatistics.Add(result.Statistic);
                    testDistribution = result.Distribution;
                }

                TestResult r2 = testStatistics.KolmogorovSmirnovTest(testDistribution);
                Assert.IsTrue(r2.RightProbability > 0.05);

                Assert.IsTrue(testStatistics.PopulationMean.ConfidenceInterval(0.99).ClosedContains(testDistribution.Mean));
                Assert.IsTrue(testStatistics.PopulationVariance.ConfidenceInterval(0.99).ClosedContains(testDistribution.Variance));
            }
        }
        public void BivariateNonlinearFitVariances()
        {
            // Verify that we can fit a non-linear function,
            // that the estimated parameters do cluster around the true values,
            // and that the estimated parameter covariances do reflect the actually observed covariances

            double a = 2.7;
            double b = 3.1;

            ContinuousDistribution xDistribution = new ExponentialDistribution(2.0);
            ContinuousDistribution eDistribution = new NormalDistribution(0.0, 4.0);

            FrameTable parameters = new FrameTable();

            parameters.AddColumns <double>("a", "b");
            MultivariateSample covariances = new MultivariateSample(3);

            for (int i = 0; i < 64; i++)
            {
                BivariateSample sample = new BivariateSample();
                Random          rng    = new Random(i);
                for (int j = 0; j < 8; j++)
                {
                    double x = xDistribution.GetRandomValue(rng);
                    double y = a * Math.Pow(x, b) + eDistribution.GetRandomValue(rng);
                    sample.Add(x, y);
                }

                NonlinearRegressionResult fit = sample.NonlinearRegression(
                    (IReadOnlyList <double> p, double x) => p[0] * Math.Pow(x, p[1]),
                    new double[] { 1.0, 1.0 }
                    );

                parameters.AddRow(fit.Parameters.ValuesVector);
                covariances.Add(fit.Parameters.CovarianceMatrix[0, 0], fit.Parameters.CovarianceMatrix[1, 1], fit.Parameters.CovarianceMatrix[0, 1]);
            }

            Assert.IsTrue(parameters["a"].As <double>().PopulationMean().ConfidenceInterval(0.99).ClosedContains(a));
            Assert.IsTrue(parameters["b"].As <double>().PopulationMean().ConfidenceInterval(0.99).ClosedContains(b));

            Assert.IsTrue(parameters["a"].As <double>().PopulationVariance().ConfidenceInterval(0.99).ClosedContains(covariances.Column(0).Mean));
            Assert.IsTrue(parameters["b"].As <double>().PopulationVariance().ConfidenceInterval(0.99).ClosedContains(covariances.Column(1).Mean));
            Assert.IsTrue(parameters["a"].As <double>().PopulationCovariance(parameters["b"].As <double>()).ConfidenceInterval(0.99).ClosedContains(covariances.Column(2).Mean));
            Assert.IsTrue(Bivariate.PopulationCovariance(parameters["a"].As <double>(), parameters["b"].As <double>()).ConfidenceInterval(0.99).ClosedContains(covariances.Column(2).Mean));
        }
        public void MultivariateLinearLogisticRegressionVariances()
        {
            // define model y = a + b0 * x0 + b1 * x1 + noise
            double a  = -3.0;
            double b0 = 2.0;
            double b1 = 1.0;
            ContinuousDistribution x0distribution = new ExponentialDistribution();
            ContinuousDistribution x1distribution = new LognormalDistribution();

            FrameTable data = new FrameTable();

            data.AddColumns <double>("a", "da", "b0", "db0", "b1", "db1", "p", "dp");

            // draw a sample from the model
            Random rng = new Random(2);

            for (int j = 0; j < 32; j++)
            {
                List <double> x0s = new List <double>();
                List <double> x1s = new List <double>();
                List <bool>   ys  = new List <bool>();

                FrameTable table = new FrameTable();
                table.AddColumn <double>("x0");
                table.AddColumn <double>("x1");
                table.AddColumn <bool>("y");

                for (int i = 0; i < 32; i++)
                {
                    double x0 = x0distribution.GetRandomValue(rng);
                    double x1 = x1distribution.GetRandomValue(rng);
                    double t  = a + b0 * x0 + b1 * x1;
                    double p  = 1.0 / (1.0 + Math.Exp(-t));
                    bool   y  = (rng.NextDouble() < p);
                    x0s.Add(x0);
                    x1s.Add(x1);
                    ys.Add(y);
                }

                // do a linear regression fit on the model
                MultiLinearLogisticRegressionResult result = ys.MultiLinearLogisticRegression(
                    new Dictionary <string, IReadOnlyList <double> > {
                    { "x0", x0s }, { "x1", x1s }
                }
                    );
                UncertainValue pp = result.Predict(0.0, 1.0);

                data.AddRow(
                    result.Intercept.Value, result.Intercept.Uncertainty,
                    result.CoefficientOf("x0").Value, result.CoefficientOf("x0").Uncertainty,
                    result.CoefficientOf("x1").Value, result.CoefficientOf("x1").Uncertainty,
                    pp.Value, pp.Uncertainty
                    );
            }

            // The estimated parameters should agree with the model that generated the data.

            // The variances of the estimates should agree with the claimed variances
            Assert.IsTrue(data["a"].As <double>().PopulationStandardDeviation().ConfidenceInterval(0.99).ClosedContains(data["da"].As <double>().Mean()));
            Assert.IsTrue(data["b0"].As <double>().PopulationStandardDeviation().ConfidenceInterval(0.99).ClosedContains(data["db0"].As <double>().Mean()));
            Assert.IsTrue(data["b1"].As <double>().PopulationStandardDeviation().ConfidenceInterval(0.99).ClosedContains(data["db1"].As <double>().Mean()));
            Assert.IsTrue(data["p"].As <double>().PopulationStandardDeviation().ConfidenceInterval(0.99).ClosedContains(data["dp"].As <double>().Mean()));
        }