Example #1
0
        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 mode   = distribution.Mode;                                         // 3

            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, mode);
            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, 1e-15);
            Assert.AreEqual(-1.7067405350495708, lpdf);
            Assert.AreEqual(1.6331065212852196, hf, 1e-15);
            Assert.AreEqual(0.11111111111111116, ccdf);
            Assert.AreEqual(4.1999999999999993, icdf);
            Assert.AreEqual("Fn(x; S)", str);

            var range1 = distribution.GetRange(0.95);
            var range2 = distribution.GetRange(0.99);
            var range3 = distribution.GetRange(0.01);

            Assert.AreEqual(0.99999947547912593, range1.Min);
            Assert.AreEqual(5.0000002464240794, range1.Max);
            Assert.AreEqual(0.99999913215637204, range2.Min);
            Assert.AreEqual(5.0000004605903117, range2.Max);
            Assert.AreEqual(0.99999913215637204, range3.Min);
            Assert.AreEqual(5.0000004605903117, range3.Max);
        }
Example #2
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);
        }