Testing Goodness-Of-Fit
It is often necessary to verify whether the distribution of a variable fits a certain theoretical distribution. Goodness-of-fit tests can be used to perform this verification. Goodness of fit tests require all sample values. They can't be performed using only the summary statistics.
The Chi-Square Test for Goodness-of-Fit
The chi-square (
Definition
The null hypothesis (
The test statistic (
where
The distribution of the test statistic
Assumptions
The chi-square test assumes that the variable is categorical in nature. When the variable is continuous, the chi-square test cannot be used directly. It is possible to group the data into cells and use the categorized data in the test. The sample must be randomly selected from the population, and the expected frequency of each cell should be large enough, typically at least 5.
Applications
The chi-square goodness-of-fit test is used in various fields such as genetics, marketing, and quality control to determine if a sample comes from a population with a specific distribution.
The ChiSquareGoodnessOfFitTest class
The chi-square goodness-of-fit test is implemented by the ChiSquareGoodnessOfFitTest class. This class provides multiple constructors to accommodate different types of input data.
Samples can be provided as a vector, a categorical vector, or as histogram data. The expected distribution can be provided as a histogram or a distribution. In the case of a distribution, you can supply the number of estimated parameters, which affects the degrees of freedom of the test.
For instance, you can use the constructor that takes a categorical vector and an expected vector to perform the test on categorical data. Alternatively, you can use the constructor that takes a sample vector and a distribution to test if the sample comes from a specified continuous or discrete distribution. These techniques are illustrated below.
Example 1 - Fitting a Discrete Distribution
In a gambling game, the payout is directly proportional to the number of sixes that are thrown. A very successful customer has the following results: She threw 3 sixes twice, 2 sixes eleven times, 1 six thirty-five times, and no sixes fifty-two times.
The casino management suspects that the customer may be using weighted dice. The significance level for this test is 0.01.
The number of sixes thrown follows a binomial distribution with
var sixesDistribution = new BinomialDistribution(3, 1 / 6.0);
var expected = sixesDistribution.GetExpectedHistogram(100);
var actual = Vector.Create<double>(51, 35, 12, 2);
var chiSquare = new ChiSquareGoodnessOfFitTest(actual, expected);
chiSquare.SignificanceLevel = 0.01;
Console.WriteLine("Test statistic: {0:F4}", chiSquare.Statistic);
Console.WriteLine("P-value: {0:F4}", chiSquare.PValue);
Console.WriteLine("Reject null hypothesis? {0}",
chiSquare.Reject() ? "yes" : "no");The value of the chi-square statistic is 9.6013 giving a p-value of 0.0223. As a result, the hypothesis that the dice are weighted is rejected at the 0.01 level.
Example 2 - Fitting a Continuous Distribution
A store manager wants to check if the waiting times at the store follow a gamma distribution. The manager collected waiting times (in minutes) from a sample of 100 customers.
We put all the observations in an array and fit the Gamma distribution using Maximum Likelihood Estimation (MLE). We do this by calling the GammaDistribution constructor with our sample, and print the parameters of the fitted distribution.
We then compare the observed frequencies to the expected frequencies using the chi-square test. We don't have to worry about binning the data. All we need to do is supply the distribution and specify that we estimated 2 distribution parameters using the appropriate ChiSquareGoodnessOfFitTest constructor. We finally print the test statistic and p-value.
The complete code looks as follows:
double[] waitingTimes = new double[]
{
12.9, 5.3, 17.6, 19.0, 5.7, 20.5, 23.7, 20.0, 16.5, 27.9,
4.1, 18.4, 8.0, 8.6, 9.4, 11.6, 19.8, 40.6, 0.3, 7.3, 13.2,
14.0, 5.6, 24.7, 32.0, 3.2, 27.6, 6.0, 6.7, 19.2, 12.0,
16.5, 3.5, 8.8, 9.8, 11.0, 14.5, 6.4, 21.9, 16.2, 8.7,
14.3, 5.8, 19.9, 13.0, 3.8, 5.5, 13.4, 21.5, 21.3, 8.4,
4.8, 9.4, 12.7, 14.0, 5.0, 18.5, 10.1, 5.8, 11.0, 4.7,
17.7, 2.4, 12.8, 7.5, 18.3, 7.0, 16.9, 19.8, 10.3, 7.3,
19.6, 0.1, 15.4, 9.6, 4.3, 9.6, 6.1, 17.2, 14.3, 3.7, 2.3,
21.7, 22.4, 4.1, 7.5, 5.2, 17.3, 7.1, 8.3, 23.6, 19.0,
20.4, 8.6, 15.3, 12.1, 6.3, 10.1, 14.2, 4.8
};
// The manager suspects that the waiting times follow a gamma distribution.
// We fit the distribution using Maximum Likelihood Estimation:
var gamma = new GammaDistribution(waitingTimes);
Console.WriteLine("Estimated gamma distribution:");
Console.WriteLine($" Shape: {gamma.ShapeParameter:F3}");
Console.WriteLine($" Scale: {gamma.ScaleParameter:F3}");
// Perform the chi-square goodness-of-fit test. We specify that
// we estimated two parameters of the distribution.
var x2Test = new ChiSquareGoodnessOfFitTest(waitingTimes, gamma, 2);
Console.WriteLine($"Chi-Square Statistic: {x2Test.Statistic:F3}");
Console.WriteLine($"P-Value: {x2Test.PValue:F4}");
Console.WriteLine($"Reject? {(x2Test.Reject(0.05) ? "yes" : "no")}");The fitted gamma distribution has shape parameter 2.582 and scale parameter 3.705. Running the Chi square test gives a test statistic of 7.414 with a corresponding p-value of 0.1917. The manager concludes that he can't reject the hypothesis that the waiting times follow a gamma distribution at the 0.05 level.
The One Sample Kolmogorov-Smirnov Test
The one sample Kolmogorov-Smirnov test (KS test) is used to test the hypothesis that a given sample was taken from a proposed continuous distribution.
Definition
The null hypothesis (
The test statistic (
where
The distribution of the test statistic
Assumptions
The KS test can be applied to any continuous distribution. However, it can't be applied to discrete distributions and is more sensitive near the center of the distribution than at the tails. The distribution must be completely specified. If one or more of the distribution's parameters is estimated, the distribution of the test statistic is different from the Kolmogorov-Smirnov distribution.
Applications
The KS test is used in various fields such as finance, biology, and engineering to test if a sample comes from a specific continuous distribution.
The OneSampleKolmogorovSmirnovTest class
The one sample Kolmogorov-Smirnov test is implemented by the OneSampleKolmogorovSmirnovTest class. It has three constructors. The first constructor takes no arguments. All test parameters must be specified through properties of the test object. The second constructor takes two arguments. The first is a Vector<T> object that specifies the sample. The second is a Func<T, TResult> delegate, which specifies the cumulative distribution function of the distribution being tested. The third constructor also takes two arguments. The first argument is once again vector. The second argument must be of a type derived from ContinuousDistribution.
Example
In this example, we take samples of a lognormal distribution, and test whether it could come from a similar looking Weibull distribution.
var weibull = new WeibullDistribution(2, 1);
var logNormal = new LognormalDistribution(0, 1);
var logNormalSample = logNormal.Sample(25);
var ksTest = new OneSampleKolmogorovSmirnovTest(logNormalSample, weibull);
Console.WriteLine("Test statistic: {0:F4}", ksTest.Statistic);
Console.WriteLine("P-value: {0:F4}", ksTest.PValue);
Console.WriteLine("Reject null hypothesis? {0}",
ksTest.Reject() ? "yes" : "no");First we create a Weibull and a lognormal distribution. We then get 25 random samples from the lognormal distribution using its Sample(Int32) method.
Because we use random samples, the results of the test are different on each run. The trend is that the p-value is anywhere from 0.03 to 0.3. We can conclude from this that it is not possible to distinguish a lognormal distribution from a Weibull distribution using only 25 sample points.
The Two Sample Kolmogorov-Smirnov Test
The two sample Kolmogorov-Smirnov test is used to test the hypothesis that two samples come from a population with the same, unknown distribution.
Definition
The null hypothesis (
The test statistic (
where
The distribution of the test statistic
Assumptions
The two sample KS test assumes that the samples are independent and that the distributions are continuous.
Applications
The two sample KS test is used in various fields such as medicine, economics, and environmental science to compare two samples and determine if they come from the same distribution.
The TwoSampleKolmogorovSmirnovTest class
The two sample Kolmogorov-Smirnov test is implemented by the TwoSampleKolmogorovSmirnovTest class. It has two constructors. The first constructor takes no arguments. The second constructor takes two arguments. Both are vectors that specify the two samples that are being compared.
Example
We investigate whether we can distinguish a sample taken from a lognormal distribution from a sample taken from a similar looking Weibull distribution. We use the lognormal samples we created in the previous section.
var weibullSample = weibull.Sample(25);
var ksTest2 = new TwoSampleKolmogorovSmirnovTest(logNormalSample, weibullSample);
Console.WriteLine("Test statistic: {0:F4}", ksTest2.Statistic);
Console.WriteLine("P-value: {0:F4}", ksTest2.PValue);
Console.WriteLine("Reject null hypothesis? {0}",
ksTest2.Reject() ? "yes" : "no");Normality tests
Normality testing is covered separately in Testing Normality. That page discusses the ShapiroWilkTest, AndersonDarlingTest, JarqueBeraTest, and DAgostinoPearsonOmnibusTest classes in detail.
This page remains focused on general goodness-of-fit procedures, including chi-square goodness-of-fit tests and one-sample and two-sample Kolmogorov-Smirnov tests.