internal double Probability(TestType type) { switch (type) { case TestType.LeftTailed: return(distribution.LeftExclusiveProbability(value) + 0.5 * distribution.ProbabilityMass(value)); case TestType.RightTailed: return(distribution.RightExclusiveProbability(value) + 0.5 * distribution.ProbabilityMass(value)); case TestType.TwoTailed: if (distribution.Skewness == 0.0) { if (value < distribution.Mean) { return(2.0 * (distribution.LeftExclusiveProbability(value) + 0.5 * distribution.ProbabilityMass(value))); } else { return(2.0 * (distribution.RightExclusiveProbability(value) + 0.5 * distribution.ProbabilityMass(value))); } } else { throw new NotImplementedException(); } default: throw new NotSupportedException(); } }
/// <summary> /// Performs a χ<sup>2</sup> test comparing the histogram to the given distribution. /// </summary> /// <param name="distribution">The distribution against which to test the histogram.</param> /// <returns>The test result.</returns> public TestResult ChiSquaredTest(DiscreteDistribution distribution) { if (distribution == null) { throw new ArgumentNullException(nameof(distribution)); } double chi2 = 0.0; int dof = 0; //int lastObservedCounts = extraCounts; //double lastExpectedCounts = (distribution.LeftExclusiveProbability(0) + distribution.RightExclusiveProbability(counts.Length - 1)) * total; int lastObservedCounts = 0; double lastExpectedCounts = 0.0; int observedCounts = 0; double expectedCounts = 0.0; for (int i = 0; i < storage.Count; i++) { observedCounts += storage.GetCounts(i + 1); expectedCounts += distribution.ProbabilityMass(i) * storage.Total; if (expectedCounts > 4.0) { if (lastExpectedCounts > 0.0) { chi2 += MoreMath.Sqr(lastObservedCounts - lastExpectedCounts) / lastExpectedCounts; dof++; } lastObservedCounts = observedCounts; lastExpectedCounts = expectedCounts; observedCounts = 0; expectedCounts = 0.0; } } lastObservedCounts += observedCounts; lastExpectedCounts += expectedCounts; if (lastExpectedCounts > 0.0) { chi2 += MoreMath.Sqr(lastObservedCounts - lastExpectedCounts) / lastExpectedCounts; } ContinuousDistribution nullDistribution = new ChiSquaredDistribution(dof); return(new TestResult("χ²", chi2, nullDistribution, TestType.RightTailed)); }