File Exchange

image thumbnail


version 1.0 (2.44 KB) by

Two-sample chi-squared test for discrete data.

1 Download


View License

H = CHI2TEST2(X) performs a chi-squared two-sample test of the
similarity of two samples, under the null hypothesis that two samples
come from the same (non-specified) distribution.

X1 and X2 are two vectors containing discrete (categorical) data.
CHI2TEST2 treats all unique values as separate 'bins' for testing
purposes. X1 and X2 can be different lengths. CHI2TEST2 treats NaNs as
missing values, and ignores them.

H = TTEST(...,ALPHA) performs the test at the significance level
(100*ALPHA)%. ALPHA must be a scalar. Default value of ALPHA is 0.05.
i.e., H = 1 suggests different distributions.

[H,P] = TTEST(...) returns the p-value, i.e., the probability of
observing the given result, or one more extreme, by chance if the null
hypothesis is true. Small values of P cast doubt on the validity of
the null hypothesis (i.e. suggests different distributions).

[H,P,TEST] = TTEST(...) returns TEST, the value of the test statistic.

[H,P,TEST,DF] = TTEST(...) returns DF, the degrees of freedom of the test.

EXAMPLE 1: Generate two uniform random vectors, should be H = 0
x1 = round(rand(1000,1)*10);
x2 = round(rand(1000,1)*10);
[h,p,test,df] = chi2test2(x1,x2)
h =
p =
test =
df =

EXAMPLE 2: Generate two random vectors, one uniform and one normal,
should be H = 1
x1 = round(rand(1000,1)*10);
x2 = 5 + round(randn(1000,1));
[h,p,test,df] = chi2test2(x1,x2)
h =
p =
test =
df =

Accessed March 29, 2013, and cites: "Numerical Recipes in
Fortan: The Art of Scientific Computing", Second Edition,
Press, Teukolsky, Vetterlling, and Flannery, Cambridge
University Press, 1992, pp. 614-622.

Copyright 2013 James Meldrum. Provided for entertainment only; author
is not responsible for any use or misuse of code. Verify before use!
Revision: 1, 2013/03/29 $

Comments and Ratings (0)

MATLAB Release
MATLAB 8.0 (R2012b)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video