public virtual void Test(double tolerance) { // a0 and a1 are the distribution parameters // Test quantile double x1 = _dist.Quantile(firstProb); Assert.AreEqual(_a[2], x1, Math.Abs(_a[2] * tolerance), "Unexpected result of Quantile firstProb"); double p1 = _dist.CDF(_a[2]); Assert.AreEqual(firstProb, p1, Math.Abs(firstProb * tolerance), "Unexpected result of CDF firstQuantile"); double d1 = _dist.PDF(_a[2]); Assert.AreEqual(_a[3], d1, Math.Abs(_a[3] * tolerance), "Unexpected result of PDF firstQuantile"); double x2 = _dist.Quantile(secondProb); Assert.AreEqual(_a[4], x2, Math.Abs(_a[4] * tolerance), "Unexpected result of Quantile secondProb"); double p2 = _dist.CDF(_a[4]); Assert.AreEqual(secondProb, p2, Math.Abs(secondProb * tolerance), "Unexpected result of CDF secondQuantile"); double d2 = _dist.PDF(_a[4]); Assert.AreEqual(_a[5], d2, Math.Abs(_a[5] * tolerance), "Unexpected result of PDF secondQuantile"); int len = 1000000; int suml = 0; for (int i = 0; i < len; i++) { if (_dist.NextDouble() < _a[2]) { suml++; } } double p1_tested = suml / (double)len; double confidence = BinomialDistribution.CDF(suml, firstProb, len); Assert.Greater(confidence, 0.01, string.Format("Unexpected result of distribution of generated values : outside (left) the 1% confidence limits, expected p={0}, actual p={1}, confidence={2}", firstProb, p1_tested, confidence)); Assert.Less(confidence, 0.99, string.Format("Unexpected result of distribution of generated values : outside (right) the 1% confidence limits, expected p={0}, actual p={1}, confidence={2}", firstProb, p1_tested, confidence)); }
private void TestContinuousDistributionShape( ContinuousDistribution distribution, double min, double max, double[] expectedShape, double expectedUnderflow, double expectedOverflow, int avgSamplesPerBucket, double absoluteAccuracy, string message) { DistributionShape shape = DistributionShape.CreateMinMax(expectedShape.Length, min, max); int sampleCount = expectedShape.Length * avgSamplesPerBucket; for (int i = 0; i < sampleCount; i++) { shape.Push(distribution.NextDouble()); } double scale = 1.0 / (avgSamplesPerBucket * expectedShape.Length); Assert.AreEqual(expectedUnderflow, shape.Underflow * scale, absoluteAccuracy, message + " Underflow"); Assert.AreEqual(expectedOverflow, shape.Overflow * scale, absoluteAccuracy, message + " Overflow"); for (int i = 0; i < expectedShape.Length; i++) { Assert.AreEqual(expectedShape[i], shape[i] * scale, absoluteAccuracy, message + " Bucket " + i.ToString()); } }