public void TestMethod1()
        {
            Distribution n0 = new TransformedDistribution(new NormalDistribution(), -2.0, 3.0);
            Distribution n1 = new NormalDistribution(-2.0, 3.0);

            Assert.IsTrue(TestUtilities.IsNearlyEqual(n0.Mean, n1.Mean));
            Assert.IsTrue(TestUtilities.IsNearlyEqual(n0.Variance, n1.Variance));
            Assert.IsTrue(TestUtilities.IsNearlyEqual(n0.StandardDeviation, n1.StandardDeviation));
            Assert.IsTrue(TestUtilities.IsNearlyEqual(n0.Skewness, n1.Skewness));

            for (int k = 0; k < 8; k++) {
                Assert.IsTrue(TestUtilities.IsNearlyEqual(n0.Moment(k), n1.Moment(k)));
                Assert.IsTrue(TestUtilities.IsNearlyEqual(n0.MomentAboutMean(k), n1.MomentAboutMean(k)));
            }

            foreach (double x in TestUtilities.GenerateUniformRealValues(-8.0, 8.0, 8)) {
                Assert.IsTrue(TestUtilities.IsNearlyEqual(n0.ProbabilityDensity(x), n1.ProbabilityDensity(x)));
                Assert.IsTrue(TestUtilities.IsNearlyEqual(n0.LeftProbability(x), n1.LeftProbability(x)));
                Assert.IsTrue(TestUtilities.IsNearlyEqual(n0.RightProbability(x), n1.RightProbability(x)));
            }

            foreach (double P in TestUtilities.GenerateRealValues(1.0E-4, 1.0, 4)) {
                Assert.IsTrue(TestUtilities.IsNearlyEqual(n0.InverseLeftProbability(P), n1.InverseLeftProbability(P)));
                Assert.IsTrue(TestUtilities.IsNearlyEqual(n0.InverseRightProbability(P), n1.InverseRightProbability(P)));
            }
        }
        public MultivariateSample CreateMultivariateNormalSample(ColumnVector M, SymmetricMatrix C, int n)
        {
            int d = M.Dimension;

            MultivariateSample S = new MultivariateSample(d);

            SquareMatrix A = C.CholeskyDecomposition().SquareRootMatrix();

            Random rng = new Random(1);
            Distribution normal = new NormalDistribution();

            for (int i = 0; i < n; i++) {

                // create a vector of normal deviates
                ColumnVector V = new ColumnVector(d);
                for (int j = 0; j < d; j++) {
                    double y = rng.NextDouble();
                    double z = normal.InverseLeftProbability(y);
                    V[j] = z;
                }

                // form the multivariate distributed vector
                ColumnVector X = M + A * V;

                // add it to the sample
                S.Add(X);

            }

            return (S);
        }
Exemple #3
0
 /// <inheritdoc />
 public override double InverseLeftProbability(double P)
 {
     if ((P < 0.0) || (P > 1.0))
     {
         throw new ArgumentOutOfRangeException(nameof(P));
     }
     return(Math.Exp(normal.InverseLeftProbability(P)));
 }
Exemple #4
0
        /// <inheritdoc />
        public override double InverseLeftProbability(double P)
        {
            if ((P < 0.0) || (P > 1.0))
            {
                throw new ArgumentOutOfRangeException("P");
            }
            return(Math.Exp(normal.InverseLeftProbability(P)));

            /*
             * double z = Global.SqrtTwo * AdvancedMath.InverseErf(2.0 * P - 1.0);
             * return (Math.Exp(mu + sigma * z));
             */
        }
        public void StudentTest2()
        {
            // make sure Student t is consistent with its definition

            // we are going to take a sample that we expect to be t-distributed
            Sample tSample = new Sample();

            // begin with an underlying normal distribution
            Distribution xDistribution = new NormalDistribution();

            // compute a bunch of t satistics from the distribution
            for (int i = 0; i < 100000; i++) {

                // take a small sample from the underlying distribution
                // (as the sample gets large, the t distribution becomes normal)
                Random rng = new Random(314159+i);

                double p = xDistribution.InverseLeftProbability(rng.NextDouble());
                double q = 0.0;
                for (int j = 0; j < 5; j++) {
                    double x = xDistribution.InverseLeftProbability(rng.NextDouble());
                    q += x * x;
                }
                q = q / 5;

                double t = p / Math.Sqrt(q);
                tSample.Add(t);

            }

            Distribution tDistribution = new StudentDistribution(5);
            TestResult result = tSample.KolmogorovSmirnovTest(tDistribution);
            Console.WriteLine(result.LeftProbability);
        }
        public void StudentTest()
        {
            // make sure Student t is consistent with its definition

            // we are going to take a sample that we expect to be t-distributed
            Sample tSample = new Sample();

            // begin with an underlying normal distribution
            Distribution xDistribution = new NormalDistribution(1.0, 2.0);

            // compute a bunch of t satistics from the distribution
            for (int i = 0; i < 200000; i++) {

                // take a small sample from the underlying distribution
                // (as the sample gets large, the t distribution becomes normal)
                Random rng = new Random(i);
                Sample xSample = new Sample();
                for (int j = 0; j < 5; j++) {
                    double x = xDistribution.InverseLeftProbability(rng.NextDouble());
                    xSample.Add(x);
                }

                // compute t for the sample
                double t = (xSample.Mean - xDistribution.Mean) / (xSample.PopulationStandardDeviation.Value / Math.Sqrt(xSample.Count));
                tSample.Add(t);
                //Console.WriteLine(t);

            }

            // t's should be t-distrubuted; use a KS test to check this
            Distribution tDistribution = new StudentDistribution(4);
            TestResult result = tSample.KolmogorovSmirnovTest(tDistribution);
            Console.WriteLine(result.LeftProbability);
            //Assert.IsTrue(result.LeftProbability < 0.95);

            // t's should be demonstrably not normally distributed
            Console.WriteLine(tSample.KolmogorovSmirnovTest(new NormalDistribution()).LeftProbability);
            //Assert.IsTrue(tSample.KolmogorovSmirnovTest(new NormalDistribution()).LeftProbability > 0.95);
        }
 public void TestNormal()
 {
     NormalDistribution n = new NormalDistribution();
     Console.WriteLine(n.InverseLeftProbability(1.0E-10));
     Console.WriteLine(n.InverseRightProbability(1.0E-10));
     Console.WriteLine(n.InverseLeftProbability(1.0E-300));
     Console.WriteLine(n.InverseRightProbability(1.0E-300));
     Console.WriteLine(n.InverseLeftProbability(1.0));
     //Console.WriteLine(n.InverseLeftProbability(0.26));
 }
        public void TestBeta()
        {
            double a = 200.0; double b = 200.0; double P = 1.0E-5;
            double x1 = ApproximateInverseBetaSeries(a, b, P);
            if ((0.0 < x1) && (x1 < 1.0)) {
                Console.WriteLine("x1 {0} {1}", x1, AdvancedMath.LeftRegularizedBeta(a, b, x1));
            }

            double x2 = 1.0 - ApproximateInverseBetaSeries(b, a, 1.0 - P);
            if ((0.0 < x2) && (x2 < 1.0)) {
                Console.WriteLine("x2 {0} {1}", x2, AdvancedMath.LeftRegularizedBeta(a, b, x2));
            }

            //x1 = RefineInverseBeta(a, b, P, x1);
            //Console.WriteLine("{0} {1}", x1, AdvancedMath.LeftRegularizedBeta(a, b, x1));

            NormalDistribution N = new NormalDistribution();
            double m = a / (a + b); double s = Math.Sqrt(a * b / (a + b + 1.0)) / (a + b);
            double x3 = m + s * N.InverseLeftProbability(P);
            if ((0.0 < x3) && (x3 < 1.0)) {
                Console.WriteLine("x3 {0} {1}", x3, AdvancedMath.LeftRegularizedBeta(a, b, x3));
            }

            //Console.WriteLine(AdvancedMath.Beta(a, b, 0.35) / AdvancedMath.Beta(a, b));
            //Console.WriteLine(AdvancedMath.Beta(a, b, 0.40) / AdvancedMath.Beta(a, b));
            //Console.WriteLine(AdvancedMath.Beta(a, b, 0.45) / AdvancedMath.Beta(a, b));
        }
        public void MultivariateLinearRegressionTest()
        {
            // define model y = a + b0 * x0 + b1 * x1 + noise
            double a = 1.0;
            double b0 = -2.0;
            double b1 = 3.0;
            Distribution noise = new NormalDistribution(0.0, 10.0);

            // draw a sample from the model
            Random rng = new Random(1);
            MultivariateSample sample = new MultivariateSample(3);
            for (int i = 0; i < 100; i++) {
                double x0 = -10.0 + 20.0 * rng.NextDouble();
                double x1 = -10.0 + 20.0 * rng.NextDouble();
                double eps = noise.InverseLeftProbability(rng.NextDouble());
                double y = a + b0 * x0 + b1 * x1 + eps;
                sample.Add(x0, x1, y);
            }

            // do a linear regression fit on the model
            FitResult result = sample.LinearRegression(2);

            // the result should have the appropriate dimension
            Assert.IsTrue(result.Dimension == 3);

            // the result should be significant
            Console.WriteLine("{0} {1}", result.GoodnessOfFit.Statistic, result.GoodnessOfFit.LeftProbability);
            Assert.IsTrue(result.GoodnessOfFit.LeftProbability > 0.95);

            // the parameters should match the model
            Console.WriteLine(result.Parameter(0));
            Assert.IsTrue(result.Parameter(0).ConfidenceInterval(0.90).ClosedContains(b0));
            Console.WriteLine(result.Parameter(1));
            Assert.IsTrue(result.Parameter(1).ConfidenceInterval(0.90).ClosedContains(b1));
            Console.WriteLine(result.Parameter(2));
            Assert.IsTrue(result.Parameter(2).ConfidenceInterval(0.90).ClosedContains(a));
        }
        public void TimeNormalGenerators()
        {
            Random rng = new Random(1);
            //IDeviateGenerator nRng = new BoxMullerNormalGenerator();
            //IDeviateGenerator nRng = new PolarRejectionNormalDeviateGenerator();
            //IDeviateGenerator nRng = new RatioOfUniformsNormalGenerator();
            IDeviateGenerator nRng = new LevaNormalGenerator();

            //Sample sample = new Sample();
            Distribution nrm = new NormalDistribution();

            Stopwatch timer = Stopwatch.StartNew();
            double sum = 0.0;
            for (int i = 0; i < 10000000; i++) {
                sum += nrm.InverseLeftProbability(rng.NextDouble());
                //sum += nRng.GetNext(rng);
                //sample.Add(nRng.GetNext(rng));
            }
            timer.Stop();

            //Console.WriteLine(sample.KolmogorovSmirnovTest(new NormalDistribution()).RightProbability);
            Console.WriteLine(sum);
            Console.WriteLine(timer.ElapsedMilliseconds);
        }