Пример #1
0
        public void EmpiricalDistributionConstructorTest5()
        {
            double[] samples = { 5, 5, 1, 4, 1, 2, 2, 3, 3, 3, 4, 3, 3, 3, 4, 3, 2, 3 };
            EmpiricalDistribution distribution = new EmpiricalDistribution(samples, FaultySmoothingRule(samples));


            double mean      = distribution.Mean;                                      // 3
            double median    = distribution.Median;                                    // 2.9999993064186787
            double var       = distribution.Variance;                                  // 1.2941176470588236
            double chf       = distribution.CumulativeHazardFunction(x: 4.2);          // 2.1972245773362191
            double cdf       = distribution.DistributionFunction(x: 4.2);              // 0.88888888888888884
            double pdf       = distribution.ProbabilityDensityFunction(x: 4.2);        // 0.15552784414141974
            double lpdf      = distribution.LogProbabilityDensityFunction(x: 4.2);     // -1.8609305013898356
            double hf        = distribution.HazardFunction(x: 4.2);                    // 1.3997505972727771
            double ccdf      = distribution.ComplementaryDistributionFunction(x: 4.2); //0.11111111111111116
            double icdf      = distribution.InverseDistributionFunction(p: cdf);       // 4.1999999999999993
            double smoothing = distribution.Smoothing;                                 // 1.9144923416414432

            string str = distribution.ToString();                                      // Fn(x; S)

            Assert.AreEqual(samples, distribution.Samples);
            Assert.AreEqual(1.9144923416414432, smoothing, 1.0e-15);
            Assert.AreEqual(3.0, mean);
            Assert.AreEqual(2.9999993064186787, median);
            Assert.AreEqual(1.2941176470588236, var);
            Assert.AreEqual(2.1972245773362191, chf);
            Assert.AreEqual(0.88888888888888884, cdf);
            Assert.AreEqual(0.15552784414141974, pdf, 1e-15);
            Assert.AreEqual(-1.8609305013898356, lpdf);
            Assert.AreEqual(1.3997505972727771, hf, 1e-15);
            Assert.AreEqual(0.11111111111111116, ccdf);
            Assert.AreEqual(4.1999999999999993, icdf);
            Assert.AreEqual("Fn(x; S)", str);
        }
        public void EmpiricalDistributionConstructorTest3()
        {
            double[] samples = { 5, 5, 1, 4, 1, 2, 2, 3, 3, 3, 4, 3, 3, 3, 4, 3, 2, 3 };
            EmpiricalDistribution distribution = new EmpiricalDistribution(samples);

            double mean      = distribution.Mean;                                      // 3
            double median    = distribution.Median;                                    // 2.9999993064186787
            double var       = distribution.Variance;                                  // 1.2941176470588236
            double chf       = distribution.CumulativeHazardFunction(x: 4.2);          // 2.1972245773362191
            double cdf       = distribution.DistributionFunction(x: 4.2);              // 0.88888888888888884
            double pdf       = distribution.ProbabilityDensityFunction(x: 4.2);        // 0.181456280142802
            double lpdf      = distribution.LogProbabilityDensityFunction(x: 4.2);     // -1.7067405350495708
            double hf        = distribution.HazardFunction(x: 4.2);                    // 1.6331065212852196
            double ccdf      = distribution.ComplementaryDistributionFunction(x: 4.2); //0.11111111111111116
            double icdf      = distribution.InverseDistributionFunction(p: cdf);       // 4.1999999999999993
            double smoothing = distribution.Smoothing;                                 // 0.67595864392399474

            string str = distribution.ToString();                                      // Fn(x; S)

            Assert.AreEqual(samples, distribution.Samples);
            Assert.AreEqual(0.67595864392399474, smoothing);
            Assert.AreEqual(3.0, mean);
            Assert.AreEqual(2.9999993064186787, median);
            Assert.AreEqual(1.2941176470588236, var);
            Assert.AreEqual(2.1972245773362191, chf);
            Assert.AreEqual(0.88888888888888884, cdf);
            Assert.AreEqual(0.18145628014280227, pdf);
            Assert.AreEqual(-1.7067405350495708, lpdf);
            Assert.AreEqual(1.6331065212852196, hf);
            Assert.AreEqual(0.11111111111111116, ccdf);
            Assert.AreEqual(4.1999999999999993, icdf);
            Assert.AreEqual("Fn(x; S)", str);
        }
Пример #3
0
        public void WeightedEmpiricalDistributionConstructorTest()
        {
            double[] original     = { 5, 5, 1, 4, 1, 2, 2, 3, 3, 3, 4, 3, 3, 3, 4, 3, 2, 3 };
            var      distribution = new EmpiricalDistribution(original);

            int[]    weights = { 2, 1, 1, 1, 2, 3, 1, 3, 1, 1, 1, 1 };
            double[] samples = { 5, 1, 4, 1, 2, 3, 4, 3, 4, 3, 2, 3 };
            var      target  = new EmpiricalDistribution(samples, weights);

            Assert.AreEqual(distribution.Entropy, target.Entropy, 1e-10);
            Assert.AreEqual(distribution.Mean, target.Mean);
            Assert.AreEqual(distribution.Median, target.Median);
            Assert.AreEqual(distribution.Mode, target.Mode);
            Assert.AreEqual(distribution.Quartiles.Min, target.Quartiles.Min);
            Assert.AreEqual(distribution.Quartiles.Max, target.Quartiles.Max);
            Assert.AreEqual(distribution.Smoothing, target.Smoothing);
            Assert.AreEqual(distribution.StandardDeviation, target.StandardDeviation);
            Assert.AreEqual(distribution.Support.Min, target.Support.Min);
            Assert.AreEqual(distribution.Support.Max, target.Support.Max);
            Assert.AreEqual(distribution.Variance, target.Variance);
            Assert.IsTrue(target.Weights.IsEqual(weights.Divide(weights.Sum())));
            Assert.AreEqual(target.Samples, samples);

            for (double x = 0; x < 6; x += 0.1)
            {
                double actual, expected;
                expected = distribution.ComplementaryDistributionFunction(x);
                actual   = target.ComplementaryDistributionFunction(x);
                Assert.AreEqual(expected, actual);

                expected = distribution.CumulativeHazardFunction(x);
                actual   = target.CumulativeHazardFunction(x);
                Assert.AreEqual(expected, actual);

                expected = distribution.DistributionFunction(x);
                actual   = target.DistributionFunction(x);
                Assert.AreEqual(expected, actual);

                expected = distribution.HazardFunction(x);
                actual   = target.HazardFunction(x);
                Assert.AreEqual(expected, actual, 1e-15);

                expected = distribution.InverseDistributionFunction(Accord.Math.Tools.Scale(0, 6, 0, 1, x));
                actual   = target.InverseDistributionFunction(Accord.Math.Tools.Scale(0, 6, 0, 1, x));
                Assert.AreEqual(expected, actual);

                expected = distribution.LogProbabilityDensityFunction(x);
                actual   = target.LogProbabilityDensityFunction(x);
                Assert.AreEqual(expected, actual, 1e-15);

                expected = distribution.ProbabilityDensityFunction(x);
                actual   = target.ProbabilityDensityFunction(x);
                Assert.AreEqual(expected, actual, 1e-15);

                expected = distribution.QuantileDensityFunction(Accord.Math.Tools.Scale(0, 6, 0, 1, x));
                actual   = target.QuantileDensityFunction(Accord.Math.Tools.Scale(0, 6, 0, 1, x));
                Assert.AreEqual(expected, actual, 1e-10);
            }
        }
Пример #4
0
        public void DistributionFunctionTest()
        {
            double[] samples             = { 1, 5, 2, 5, 1, 7, 1, 9 };
            EmpiricalDistribution target = new EmpiricalDistribution(samples);

            Assert.AreEqual(0.000, target.DistributionFunction(0));
            Assert.AreEqual(0.375, target.DistributionFunction(1));
            Assert.AreEqual(0.500, target.DistributionFunction(2));
            Assert.AreEqual(0.750, target.DistributionFunction(5));
            Assert.AreEqual(0.875, target.DistributionFunction(7));
            Assert.AreEqual(1.000, target.DistributionFunction(9));
        }
Пример #5
0
        private List <RecordMongo> typicalDay(List <List <RecordMongo> > possDayValues, string vcode)
        {
            List <double> longTermValues = new List <double>();
            //list of all candidate days cdfs
            List <EmpiricalDistribution> dayCDFS = new List <EmpiricalDistribution>();

            foreach (List <RecordMongo> day in possDayValues)
            {
                List <double> dayValues = new List <double>();
                foreach (RecordMongo rm in day)
                {
                    if (rm.value != -999.9)
                    {
                        //only actual values in the cdfs
                        longTermValues.Add(rm.value);
                        dayValues.Add(rm.value);
                    }
                }
                dayCDFS.Add(new EmpiricalDistribution(dayValues.ToArray()));
            }
            //longterm cdf all days found
            EmpiricalDistribution longterm  = new EmpiricalDistribution(longTermValues.ToArray());
            List <double>         finkelSch = new List <double>();
            var    range = longterm.GetRange(0.9);
            double inc   = (range.Max - range.Min) / 20;

            foreach (EmpiricalDistribution candDay in dayCDFS)
            {
                double sample = range.Min;
                double fs     = 0;
                while (sample <= range.Max)
                {
                    fs     += Math.Abs(candDay.DistributionFunction(sample) - longterm.DistributionFunction(sample));
                    sample += inc;
                }
                //24 is the n values per day
                finkelSch.Add(fs / 24);
            }
            int minindex = finkelSch.IndexOf(finkelSch.Min());
            List <RecordMongo> selectedday = possDayValues[minindex];

            return(selectedday);
        }
Пример #6
0
        /// <summary>
        ///   Constructs a Chi-Square Test.
        /// </summary>
        ///
        public ChiSquareTest(double[] observations, IUnivariateDistribution hypothesizedDistribution)
        {
            int n = observations.Length;
            var E = new EmpiricalDistribution(observations);
            var F = hypothesizedDistribution;


            // Create bins with the observations
            int bins = (int)Math.Ceiling(1 + Math.Log(observations.Length));

            double[] ebins = new double[bins + 1];

            for (int i = 0; i <= bins; i++)
            {
                double p = i / (double)bins;
                ebins[i] = F.InverseDistributionFunction(p);
            }


            double[] expected = new double[bins - 1];

            int size = expected.Length;

            for (int i = 0; i < expected.Length; i++)
            {
                double a = ebins[i];
                double b = ebins[i + 1];

                if (Double.IsPositiveInfinity(b))
                {
                    break;
                }

                double Fa      = F.DistributionFunction(a);
                double Fb      = F.DistributionFunction(b);
                double samples = Math.Abs(Fb - Fa) * n;

                expected[i] = samples;

                if (samples < 5)
                {
                    size = i + 1;
                    for (int j = i + 1; j < ebins.Length - 1; j++)
                    {
                        ebins[j] = ebins[j + 1];
                    }
                    ebins[ebins.Length - 1] = Double.PositiveInfinity;

                    i--;
                }
            }


            ebins    = ebins.Submatrix(size + 2);
            expected = expected.Submatrix(ebins.Length - 2);

            double[] observed = new double[expected.Length];
            for (int i = 0; i < observed.Length; i++)
            {
                double a = ebins[i];
                double b = ebins[i + 1];
                observed[i] = E.DistributionFunction(a, b) * n;
            }



            double sum = compute(expected, observed);

            Compute(sum, bins - 1);
        }