Ejemplo n.º 1
0
        public void TestDistributionPoisson()
        {
            const double        EPSILON = 0.000001;
            IDoubleDistribution dist    = new PoissonDistribution(m_model, "PoissonDistribution", Guid.NewGuid(), 5.0);

            Assert.AreEqual(5.0, dist.GetValueWithCumulativeProbability(0.50), EPSILON);
            dist.SetCDFInterval(0.5, 0.5);
            Assert.AreEqual(5.0, dist.GetNext(), EPSILON);
            dist.SetCDFInterval(0.0, 1.0);

            System.IO.StreamWriter tw = new System.IO.StreamWriter(Environment.GetEnvironmentVariable("TEMP") + "\\DistributionPoisson.csv");
            Debug.WriteLine("Generating raw data.");
            const int DATASETSIZE = 1500000;

            double[] rawData = new double[DATASETSIZE];
            for (int x = 0; x < DATASETSIZE; x++)
            {
                rawData[x] = dist.GetNext();
                //tw.WriteLine(rawData[x]);
            }

            Debug.WriteLine("Performing histogram analysis.");
            Histogram1D_Double hist = new Histogram1D_Double(rawData, 0, 25, 25, "distribution");

            hist.LabelProvider = new LabelProvider(((Histogram1D_Double)hist).DefaultLabelProvider);
            hist.Recalculate();

            List <double> expected = new List <double>()// From Excel.
            {
                10107, 50535, 126337, 210561, 263201, 263201, 219334, 156667, 97917, 54398, 27199, 12363, 5151, 1981,
                708, 236, 74, 22, 6, 2, 0, 0, 0, 0, 0
            };
            IEnumerable <double> ied    = new List <int>((int[])hist.Bins).Select(n => (double)n);
            List <double>        actual = new List <double>((IEnumerable <double>)ied);
            double rmsError             = Mathematics.RMSErrorCalculator.Calculate(expected, actual);

            Assert.IsTrue(rmsError < 75, "Poisson distribution at lambda = 5 does not follow the expected curve.");

            if (m_visuallyVerify)
            {
                Debug.WriteLine("Writing data dump file.");
                int[] bins = (int[])hist.Bins;
                for (int i = 0; i < bins.Length; i++)
                {
                    //Debug.WriteLine(hist.GetLabel(new int[]{i}) + ", " + bins[i]);
                    tw.WriteLine(hist.GetLabel(new int[] { i }) + ", " + bins[i] + ", " + expected[i]);
                }
                tw.Flush();
                tw.Close();

                System.Diagnostics.Process.Start("excel.exe",
                                                 Environment.GetEnvironmentVariable("TEMP") + "\\DistributionPoisson.csv");
            }
        }