Chi-Square Test for Goodness of Fit

Version 1.0.1 (2.89 KB) by Yu Chen
Chi-Square Test for Goodness of Fit
11 Downloads
Updated 14 Jan 2024

View License

and further compared with matlab build-in function chi2gof and based on the website https://ocw.mit.edu/courses/18-443-statistics-for-applications-fall-2006/896115b23d713bde212c45f62c086080_lecture11.pdf
[chi2, critical, H, pval] = CHI2TEST(DATA, N, ALPHA, DIST, X, Y, Z) returns the chi-square statistic for the samples contained in the row vector DATA.
N specifies the number of equal-probability class intervals for the test. ALPHA is the confidence level parameter used to find the critical chi-square value.
DIST is a string containing the probability distribution that we are testing against. See the staitsctics toolbox for supported distributions - 'exp', 'gam', 'unif' are some of them.
X, Y, and Z specify the estimated parameters for the selected DIST. Some distributions require only one of these parameters, and the order that these parameters are provided follows the values given to the cummulative distribution functions UNIFCDF, GAMCDF, EXPCDF, and others.
A is the computed chi-square statistic, and B is the critical tabulated value at the degrees of freedom. The number of degrees of freedom is the number of categories minus one. H returns a test decision for the chi-square goodness-of-fit test with additional options specified by one or more name-value pair arguments. pval returns the p-value p of the hypothesis test, using any of the input arguments from the previous syntaxes.
In general, if A is less than B, the H0 hypothesis that DATA follows the DIST distribution is accepted.
An attempt to fit some data with the uniform distribution on the interval from 1.5 to 2.9. The test fails, since A > B:
rng('default');
X = betarnd(5,2,100,1);
mu = 0.7421;
sigma = 0.1392;
[chi2, critical, H, pval] = chi2test (data, n, alpha, 'norm', mu, sigma)
[a, b, h, pval] = chi2test (data, 8, 0.05, 'unif', 1.5, 2.9)
See also MLE, CHI2INV, CHI2STAT, HIST, CDF, ICDF, PDF

Cite As

Yu Chen (2024). Chi-Square Test for Goodness of Fit (https://www.mathworks.com/matlabcentral/fileexchange/157646-chi-square-test-for-goodness-of-fit), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2023b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.0.1

some corrections.

1.0.0