public void TestMethod1() { Distribution n0 = new TransformedDistribution(new NormalDistribution(), -2.0, 3.0); Distribution n1 = new NormalDistribution(-2.0, 3.0); Assert.IsTrue(TestUtilities.IsNearlyEqual(n0.Mean, n1.Mean)); Assert.IsTrue(TestUtilities.IsNearlyEqual(n0.Variance, n1.Variance)); Assert.IsTrue(TestUtilities.IsNearlyEqual(n0.StandardDeviation, n1.StandardDeviation)); Assert.IsTrue(TestUtilities.IsNearlyEqual(n0.Skewness, n1.Skewness)); for (int k = 0; k < 8; k++) { Assert.IsTrue(TestUtilities.IsNearlyEqual(n0.Moment(k), n1.Moment(k))); Assert.IsTrue(TestUtilities.IsNearlyEqual(n0.MomentAboutMean(k), n1.MomentAboutMean(k))); } foreach (double x in TestUtilities.GenerateUniformRealValues(-8.0, 8.0, 8)) { Assert.IsTrue(TestUtilities.IsNearlyEqual(n0.ProbabilityDensity(x), n1.ProbabilityDensity(x))); Assert.IsTrue(TestUtilities.IsNearlyEqual(n0.LeftProbability(x), n1.LeftProbability(x))); Assert.IsTrue(TestUtilities.IsNearlyEqual(n0.RightProbability(x), n1.RightProbability(x))); } foreach (double P in TestUtilities.GenerateRealValues(1.0E-4, 1.0, 4)) { Assert.IsTrue(TestUtilities.IsNearlyEqual(n0.InverseLeftProbability(P), n1.InverseLeftProbability(P))); Assert.IsTrue(TestUtilities.IsNearlyEqual(n0.InverseRightProbability(P), n1.InverseRightProbability(P))); } }
/// <summary> /// Tests whether the sample is compatible with the given distribution. /// </summary> /// <param name="sample">The sample.</param> /// <param name="distribution">The distribution.</param> /// <returns>The test result. The test statistic is the V statistic and the chance to obtain such a large /// value of V under the assumption that the sample is drawn from the given distribution.</returns> /// <remarks> /// <para>Like the Kolmogorov-Smirnov test (<see cref="Univariate.KolmogorovSmirnovTest(IReadOnlyList{Double},ContinuousDistribution)"/>), /// Kuiper's test compares the EDF of the sample to the CDF of the given distribution.</para> /// <para>For small sample sizes, we compute the null distribution of V exactly. For large sample sizes, we use an accurate /// asympototic approximation. Therefore it is safe to use this method for all sample sizes.</para> /// </remarks> /// <exception cref="ArgumentNullException"><paramref name="distribution"/> is <see langword="null"/>.</exception> /// <exception cref="InsufficientDataException">There is no data in the sample.</exception> /// <seealso href="http://en.wikipedia.org/wiki/Kuiper%27s_test"/> public static TestResult KuiperTest(this IReadOnlyList <double> sample, ContinuousDistribution distribution) { if (sample == null) { throw new ArgumentNullException(nameof(sample)); } if (distribution == null) { throw new ArgumentNullException(nameof(distribution)); } int n = sample.Count; if (n < 1) { throw new InsufficientDataException(); } // Compute the V statistic, which is the sum of the D+ and D- statistics. double DP, DM; ComputeDStatistics(sample, distribution, out DP, out DM); double V = DP + DM; ContinuousDistribution VDistribution; if (n < 32) { VDistribution = new TransformedDistribution(new KuiperExactDistribution(n), 0.0, 1.0 / n); } else { VDistribution = new TransformedDistribution(new KuiperAsymptoticDistribution(n), 0.0, 1.0 / Math.Sqrt(n)); } return(new TestResult("V", V, TestType.RightTailed, VDistribution)); }
/// <summary> /// Tests whether the sample is compatible with the given distribution. /// </summary> /// <param name="sample">The sample.</param> /// <param name="distribution">The distribution.</param> /// <returns>The test result. The test statistic is the D statistic and the probability is the chance of /// obtaining such a large value of D under the assumption that the sample is drawn from the given distribution.</returns> /// <remarks> /// <para>The null hypothesis of the Kolmogorov-Smirnov (KS) test is that the sample is drawn from the given continuous distribution. /// The test statsitic D is the maximum deviation of the sample's empirical distribution function (EDF) from /// the distribution's cumulative distribution function (CDF). A high value of the test statistic, corresponding /// to a low right tail probability, indicates that the sample distribution disagrees with the given distribution /// to a degree unlikely to arise from statistical fluctuations.</para> /// <para>For small sample sizes, we compute the null distribution of D exactly. For large sample sizes, we use an accurate /// asympototic approximation. Therefore it is safe to use this method for all sample sizes.</para> /// <para>A variant of this test, <see cref="Sample.KolmogorovSmirnovTest(Sample, Sample)"/>, allows you to non-parametrically /// test whether two samples are drawn from the same underlying distribution, without having to specify that distribution.</para> /// </remarks> /// <exception cref="ArgumentNullException"><paramref name="distribution"/> is <see langword="null"/>.</exception> /// <exception cref="InsufficientDataException">There is no data in the sample.</exception> /// <seealso cref="KolmogorovDistribution"/> /// <seealso href="http://en.wikipedia.org/wiki/Kolmogorov-Smirnov_test"/> public static TestResult KolmogorovSmirnovTest(this IReadOnlyList <double> sample, ContinuousDistribution distribution) { if (sample == null) { throw new ArgumentNullException(nameof(sample)); } if (distribution == null) { throw new ArgumentNullException(nameof(distribution)); } int n = sample.Count; if (n < 1) { throw new InsufficientDataException(); } // Compute the D statistic, which is the maximum of the D+ and D- statistics. double DP, DM; ComputeDStatistics(sample, distribution, out DP, out DM); double D = Math.Max(DP, DM); ContinuousDistribution DDistribution; if (n < 32) { DDistribution = new TransformedDistribution(new KolmogorovExactDistribution(n), 0.0, 1.0 / n); } else { DDistribution = new TransformedDistribution(new KolmogorovAsymptoticDistribution(n), 0.0, 1.0 / Math.Sqrt(n)); } return(new TestResult("D", D, TestType.RightTailed, DDistribution)); }