Chi Square Goodness Of Fit Test Class
Definition
Assembly: Numerics.NET (in Numerics.NET.dll) Version: 9.0.7
public sealed class ChiSquareGoodnessOfFitTest : OneSampleTest<double>
- Inheritance
- Object → HypothesisTest → OneSampleTest<Double> → ChiSquareGoodnessOfFitTest
Remarks
Use the ChiSquareGoodnessOfFitTest class to test whether a sample comes from a specific distribution. The null hypothesis is that the sample comes from the specified distribution. The test statistic follows a chi-square distribution.
The sample can be supplied as either a vector or a categorical vector.
The distribution can be specified as a probability distribution (a class that inherits from Distribution) or a vector. The distribution can be discrete or continuous.
If the sample and distribution are both supplied as a vector, the boundaries must be identical.
A chi-square test uses binned data. A drawback is that the results of the test depend on how the data is binned. The test also needs a sufficient sample size for the chi-square approximation to be valid.
The AndersonDarlingTest and OneSampleKolmogorovSmirnovTest tests are alternatives to the chi-square goodness-of-fit test, but they are valid only for continuous distributions.
Example
The following example demonstrates how to use the ChiSquareGoodnessOfFitTest class to perform a chi-square goodness-of-fit test.
using Numerics.NET;
using Numerics.NET.Statistics;
using Numerics.NET.Statistics.Distributions;
using Numerics.NET.Statistics.Tests;
// Example 1: Using a sample vector and a distribution
Vector<double> sample = [1.0, 2.0, 3.0, 4.0, 5.0];
Distribution distribution = new NormalDistribution(3.0, 1.0);
ChiSquareGoodnessOfFitTest test1 = new ChiSquareGoodnessOfFitTest(sample, distribution);
double statistic1 = test1.CalculateStatistic();
Console.WriteLine($"Test Statistic: {statistic1}");
Console.WriteLine($"Degrees of Freedom: {test1.DegreesOfFreedom}");
Console.WriteLine($"P-Value: {test1.GetPValue(HypothesisType.OneTailedUpper)}");
// Example 2: Using a categorical vector and an expected vector
Vector<int> sample = [1, 2, 3, 1, 2, 1, 1, 1, 1, 3, 3, 1, 2, 3, 3, 2];
var categoricalSample = sample.AsCategorical();
Vector<double> expected = [4.0, 4.0, 8.0];
ChiSquareGoodnessOfFitTest test2 = new ChiSquareGoodnessOfFitTest(categoricalSample, expected);
double statistic2 = test2.CalculateStatistic();
Console.WriteLine($"Test Statistic: {statistic2}");
Console.WriteLine($"Degrees of Freedom: {test2.DegreesOfFreedom}");
Console.WriteLine($"P-Value: {test2.GetPValue(HypothesisType.OneTailedUpper)}");
Constructors
Chi | Constructs a new chi-squared goodness-of-fit test for the specified distribution using histogram data. |
Chi | Constructs a new chi-squared goodness-of-fit test for the specified distribution using histogram data. |
Chi | Constructs a new chi-squared goodness-of-fit test for the specified distribution. |
Chi | Constructs a new chi-squared goodness-of-fit test for the specified distribution. |
Chi | Constructs a new chi-squared goodness-of-fit test for the specified distribution using histogram data. |
Chi | Constructs a new chi-squared goodness-of-fit test for the specified distribution using histogram data. |
Chi | Constructs a new chi-squared goodness-of-fit test for the specified distribution using histogram data. |
Chi | Constructs a new chi-squared goodness-of-fit test for the specified distribution. |
Properties
Degrees | Gets the degrees of freedom of the test. |
Distribution |
Gets the probability distribution used in the hypothesis test.
(Inherited from HypothesisTest) |
Hypothesis |
Gets or sets whether the test is one or two-tailed.
(Inherited from HypothesisTest) |
Inclusion | Gets or sets the minimum expected value for a bin to contribute to the test. |
Name |
Gets the name of the hypothesis test.
(Overrides HypothesisTest.Name) |
PValue |
Gets the probability that the test statistic would take on the calculated value under the alternate hypothesis.
(Inherited from HypothesisTest) |
Sample |
Gets or sets the variable the test is to be applied to.
(Inherited from OneSampleTest<T>) |
Significance |
Gets the significance level used to test the null hypothesis.
(Inherited from HypothesisTest) |
Statistic |
Gets the value of the test statistic.
(Inherited from HypothesisTest) |
Methods
Equals | Determines whether the specified object is equal to the current object. (Inherited from Object) |
Get |
Returns the confidence interval for the test parameter for the default confidence level.
(Inherited from HypothesisTest) |
Get |
Returns the confidence interval for the test parameter for the specified confidence level.
(Inherited from HypothesisTest) |
Get | Serves as the default hash function. (Inherited from Object) |
Get |
Gets the lower critical value for the hypothesis test's current significance level.
(Inherited from HypothesisTest) |
Get |
Gets the lower critical value for the hypothesis test at the specified significance level.
(Inherited from HypothesisTest) |
Get |
Gets the probability that the test statistic would take on the calculated value under the specified alternate hypothesis.
(Inherited from HypothesisTest) |
Get | Gets the Type of the current instance. (Inherited from Object) |
Get |
Gets the upper critical value for the test statistic at the hypothesis test's current significance level.
(Inherited from HypothesisTest) |
Get |
Gets the upper critical value for the test statistic at the specified significance level.
(Inherited from HypothesisTest) |
Reject() |
Returns whether the null hypothesis is rejected
using the default significance level.
(Inherited from HypothesisTest) |
Reject( |
Returns whether the null hypothesis is rejected using the specified significance level.
(Inherited from HypothesisTest) |
Summarize() |
Returns a string containing a human-readable summary of the object.
(Inherited from HypothesisTest) |
Summarize( |
Returns a string containing a human-readable summary of the object
using the specified options.
(Inherited from OneSampleTest<T>) |
ToString | Returns a string that represents the current object. (Inherited from HypothesisTest) |