Ejemplo n.º 1
0
        public void DistributionMomentTranslations()
        {
            int n = 4;

            foreach (ContinuousDistribution distribution in distributions)
            {
                if (Double.IsNaN(distribution.Mean))
                {
                    continue;
                }
                Console.Write(distribution.GetType().Name);

                // Convert central moments to raw moments
                double[] centralInputs = new double[n];
                for (int k = 0; k < n; k++)
                {
                    centralInputs[k] = distribution.CentralMoment(k);
                }
                double[] rawOutputs = MomentMath.CentralToRaw(distribution.Mean, centralInputs);
                Assert.IsTrue(rawOutputs.Length == n);
                for (int k = 0; k < n; k++)
                {
                    Assert.IsTrue(TestUtilities.IsNearlyEqual(rawOutputs[k], distribution.RawMoment(k)));
                }

                // Convert cumulants to central moments
            }
        }
Ejemplo n.º 2
0
        public void MomentMathConsistency()
        {
            // We can't be too demanding here, since there can be strong cancelations.
            // We take a low number of simple integer cumulants and try to verify consistency.

            double[] K0 = new double[] { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0 };
            double   mu = K0[1];

            double[] C1fromK0 = MomentMath.CumulantToCentral(K0);
            Assert.IsTrue(C1fromK0[0] == 1.0);
            Assert.IsTrue(C1fromK0[1] == 0.0);
            Assert.IsTrue(C1fromK0[2] == K0[2]);

            double[] M1fromK0 = MomentMath.CumulantToRaw(K0);
            Assert.IsTrue(M1fromK0[0] == 1.0);
            Assert.IsTrue(M1fromK0[1] == K0[1]);

            double[] K2fromC1 = MomentMath.CentralToCumulant(mu, C1fromK0);
            Assert.IsTrue(TestUtilities.IsNearlyEqual(K2fromC1, K0));

            double[] M2fromC1 = MomentMath.CentralToRaw(mu, C1fromK0);
            Assert.IsTrue(TestUtilities.IsNearlyEqual(M2fromC1, M1fromK0));

            double[] K2fromM1 = MomentMath.RawToCumulant(M1fromK0);
            Assert.IsTrue(TestUtilities.IsNearlyEqual(K2fromM1, K0));

            double[] C2fromM1 = MomentMath.RawToCentral(M1fromK0);
            Assert.IsTrue(TestUtilities.IsNearlyEqual(C2fromM1, C1fromK0));
        }
Ejemplo n.º 3
0
        public void MomentMapTest()
        {
            Distribution d = new NormalDistribution();

            for (int n = 1; n < 11; n++)
            {
                double[] K = new double[n + 1];
                K[0] = 1.0;
                if (K.Length > 1)
                {
                    K[1] = 0.0;
                }
                if (K.Length > 2)
                {
                    K[2] = 1.0;
                }
                //for (int m = 1; m < K.Length; m++) {
                //    K[m] = AdvancedIntegerMath.Factorial(m - 1);
                //}

                double M = MomentMath.RawMomentFromCumulants(K);
                Console.WriteLine("{0} {1}", d.Moment(n), M);
            }
        }
Ejemplo n.º 4
0
        public void MomentMathOrderOne()
        {
            double mu = 2.0;

            double[] M = new double[] { 1.0, mu };
            double[] C = MomentMath.RawToCentral(M);
            Assert.IsTrue(C[0] == 1.0);
            Assert.IsTrue(C[1] == 0.0);
            double[] K = MomentMath.RawToCumulant(M);
            Assert.IsTrue(K[1] == mu);

            M = MomentMath.CentralToRaw(mu, C);
            Assert.IsTrue(M[0] == 1.0);
            Assert.IsTrue(M[1] == mu);
            K = MomentMath.CentralToCumulant(mu, C);
            Assert.IsTrue(K[1] == mu);

            M = MomentMath.CumulantToRaw(K);
            Assert.IsTrue(M[0] == 1.0);
            Assert.IsTrue(M[1] == mu);
            C = MomentMath.CumulantToCentral(K);
            Assert.IsTrue(C[0] == 1.0);
            Assert.IsTrue(C[1] == 0.0);
        }
Ejemplo n.º 5
0
        public void CumulantToCentralAndRaw()
        {
            int n = 8;

            foreach (UnivariateDistribution d in Distributions)
            {
                Console.WriteLine(d.GetType().Name);

                // Problems with NaN/Infinity
                if (d is CauchyDistribution)
                {
                    continue;
                }
                if (d is StudentDistribution)
                {
                    continue;
                }
                if (d is FisherDistribution)
                {
                    continue;
                }
                if (d is ParetoDistribution)
                {
                    continue;
                }

                // Real problems
                if (d is KolmogorovDistribution)
                {
                    continue;
                }
                if (d is KuiperDistribution)
                {
                    continue;
                }

                // From cumulants to central and raw moments

                double[] inK = new double[n];
                for (int r = 0; r < n; r++)
                {
                    inK[r] = d.Cumulant(r);
                }

                double[] outC = MomentMath.CumulantToCentral(inK);
                for (int r = 0; r < n; r++)
                {
                    Console.WriteLine("r={0} K={1} -> C={2} v C={3}", r, inK[r], outC[r], d.CentralMoment(r));
                }
                for (int r = 0; r < n; r++)
                {
                    Assert.IsTrue(TestUtilities.IsNearlyEqual(outC[r], d.CentralMoment(r)));
                }

                double[] outM = MomentMath.CumulantToRaw(inK);
                for (int r = 0; r < n; r++)
                {
                    Console.WriteLine("r={0} K={1} -> M={2} v M={3}", r, inK[r], outM[r], d.RawMoment(r));
                }
                for (int r = 0; r < n; r++)
                {
                    Assert.IsTrue(TestUtilities.IsNearlyEqual(outM[r], d.RawMoment(r)));
                }
            }
        }