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"); } }
public void TestDistributionEmpirical() { double[] binBounds = new double[] { 4.0, 7.0, 8.0, 10.0, 13.0, 14.0 }; double[] heights = new double[] { 2.0, 4.0, 3.0, 6.0, 4.0 }; // Note - one less than in intervals. IDoubleDistribution dist = new EmpiricalDistribution(m_model, "EmpiricalDistributionFromHistogram", Guid.NewGuid(), binBounds, heights); Assert.IsTrue(dist.GetValueWithCumulativeProbability(0.50) == 10.5); dist.SetCDFInterval(0.5, 0.5); Assert.IsTrue(dist.GetNext() == 10.5); dist.SetCDFInterval(0.0, 1.0); System.IO.StreamWriter tw = new System.IO.StreamWriter(Environment.GetEnvironmentVariable("TEMP") + "\\DistributionEmpiricalFromHistogram.csv"); Debug.WriteLine("Generating raw data."); 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, 4, 14, 100, "distribution"); hist.LabelProvider = new LabelProvider(((Histogram1D_Double)hist).DefaultLabelProvider); hist.Recalculate(); 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]); } tw.Flush(); tw.Close(); if (m_visuallyVerify) { System.Diagnostics.Process.Start("excel.exe", Environment.GetEnvironmentVariable("TEMP") + "\\DistributionEmpiricalFromHistogram.csv"); } }
public void TestDistributionTimeSpanExponential() { IDoubleDistribution dist = new ExponentialDistribution(m_model, "ExponentialDistribution", Guid.NewGuid(), 3.0, 3.0); ITimeSpanDistribution tsd = new TimeSpanDistribution(m_model, "TSD:" + dist.Name, Guid.NewGuid(), dist, TimeSpanDistribution.Units.Minutes); tsd.SetCDFInterval(0.5, 0.5); Assert.IsTrue(tsd.GetNext().Equals(TimeSpan.FromMinutes(5.0794415416798362))); tsd.SetCDFInterval(0.0, 1.0); System.IO.StreamWriter tw = new System.IO.StreamWriter(Environment.GetEnvironmentVariable("TEMP") + "\\TimeSpanDistributionExponential.csv"); Debug.WriteLine("Generating raw data."); int DATASETSIZE = 1500000; double[] rawData = new double[DATASETSIZE]; for (int x = 0; x < DATASETSIZE; x++) { rawData[x] = tsd.GetNext().TotalMinutes; //tw.WriteLine(rawData[x]); } Debug.WriteLine("Performing histogram analysis."); Histogram1D_Double hist = new Histogram1D_Double(rawData, 0, 120.0, 100, "distribution"); hist.LabelProvider = new LabelProvider(((Histogram1D_Double)hist).DefaultLabelProvider); hist.Recalculate(); 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]); } tw.WriteLine("Sum of off-scale-high : " + (((double)hist.SumEntries(HistogramBinCategory.OffScaleHigh)))); tw.WriteLine("Average value : " + (((double)hist.SumEntries(HistogramBinCategory.All)) / ((double)hist.RawData.Length))); tw.Flush(); tw.Close(); if (m_visuallyVerify) { System.Diagnostics.Process.Start("excel.exe", Environment.GetEnvironmentVariable("TEMP") + "\\TimeSpanDistributionExponential.csv"); } }
public void TestDistributionExponential() { IDoubleDistribution dist = new ExponentialDistribution(m_model, "ExponentialDistribution", Guid.NewGuid(), 3.0, 3.0); _Debug.Assert(dist.GetValueWithCumulativeProbability(0.50) == 5.0794415416798362, "Failure in TestDistributionExponential()"); dist.SetCDFInterval(0.5, 0.5); _Debug.Assert(dist.GetNext() == 5.0794415416798362); dist.SetCDFInterval(0.0, 1.0); System.IO.StreamWriter tw = new System.IO.StreamWriter(Environment.GetEnvironmentVariable("TEMP") + "\\DistributionExponential.csv"); _Debug.WriteLine("Generating raw data."); 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, 30.0, 100, "distribution"); hist.LabelProvider = new LabelProvider(((Histogram1D_Double)hist).DefaultLabelProvider); hist.Recalculate(); _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]); } tw.WriteLine("Sum of off-scale-high : " + (((double)hist.SumEntries(HistogramBinCategory.OffScaleHigh)))); tw.WriteLine("Average value : " + (((double)hist.SumEntries(HistogramBinCategory.All)) / ((double)hist.RawData.Length))); tw.Flush(); tw.Close(); if (m_visuallyVerify) { System.Diagnostics.Process.Start("excel.exe", Environment.GetEnvironmentVariable("TEMP") + "\\DistributionExponential.csv"); } }
private void _TestDoubleHistogram(IDoubleDistribution dist, int nDataPoints, double low, double high, int nBins) { double[] rawData = new double[nDataPoints]; for (int x = 0; x < nDataPoints; x++) { rawData[x] = dist.GetNext(); } IHistogram hist = new Histogram1D_Double(rawData, low, high, nBins, "Test Histogram"); hist.LabelProvider = new LabelProvider(((Histogram1D_Double)hist).DefaultLabelProviderWithError); hist.Recalculate(); int[] bins = (int[])hist.Bins; for (int i = 0; i < bins.Length; i++) { Debug.WriteLine(hist.GetLabel(new int[] { i }) + ", " + bins[i]); } }
public void TestDistributionNormal() { IDoubleDistribution dist = new NormalDistribution(m_model, "NormalDistribution", Guid.NewGuid(), 5, 1); _Debug.Assert(dist.GetValueWithCumulativeProbability(0.50) == 5.0); dist.SetCDFInterval(0.5, 0.5); _Debug.Assert(dist.GetNext() == 5.0); dist.SetCDFInterval(0.0, 1.0); System.IO.StreamWriter tw = new System.IO.StreamWriter(Environment.GetEnvironmentVariable("TEMP") + "\\DistributionNormal.csv"); _Debug.WriteLine("Generating raw data."); 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, 7.5, 100, "distribution"); hist.LabelProvider = new LabelProvider(((Histogram1D_Double)hist).DefaultLabelProvider); hist.Recalculate(); _Debug.WriteLine("Writing data dump file."); int[] bins = (int[])hist.Bins; for (int i = 0; i < bins.Length; i++) { tw.WriteLine(hist.GetLabel(new int[] { i }) + ", " + bins[i]); } tw.Flush(); tw.Close(); if (m_visuallyVerify) { System.Diagnostics.Process.Start("excel.exe", Environment.GetEnvironmentVariable("TEMP") + "\\DistributionNormal.csv"); } }