1 ## Copyright (C) 1995-2012 Kurt Hornik
3 ## This file is part of Octave.
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {[@var{pval}, @var{chisq}, @var{df}] =} chisquare_test_homogeneity (@var{x}, @var{y}, @var{c})
21 ## Given two samples @var{x} and @var{y}, perform a chisquare test for
22 ## homogeneity of the null hypothesis that @var{x} and @var{y} come from
23 ## the same distribution, based on the partition induced by the
24 ## (strictly increasing) entries of @var{c}.
26 ## For large samples, the test statistic @var{chisq} approximately follows a
27 ## chisquare distribution with @var{df} = @code{length (@var{c})}
28 ## degrees of freedom.
30 ## The p-value (1 minus the CDF of this distribution at @var{chisq}) is
31 ## returned in @var{pval}.
33 ## If no output argument is given, the p-value is displayed.
36 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
37 ## Description: Chi-square test for homogeneity
39 function [pval, chisq, df] = chisquare_test_homogeneity (x, y, c)
45 if (! (isvector(x) && isvector(y) && isvector(c)))
46 error ("chisquare_test_homogeneity: X, Y and C must be vectors");
48 ## Now test c for strictly increasing entries
50 if (any ((c(2 : df) - c(1 : (df - 1))) <= 0))
51 error ("chisquare_test_homogeneity: C must be increasing");
54 c = [(reshape (c, 1, df)), Inf];
56 x = reshape (x, l_x, 1);
57 n_x = sum (x * ones (1, df+1) < ones (l_x, 1) * c);
59 y = reshape (y, l_y, 1);
60 n_y = sum(y * ones (1, df+1) < ones (l_y, 1) * c);
61 chisq = l_x * l_y * sum ((n_x/l_x - n_y/l_y).^2 ./ (n_x + n_y));
62 pval = 1 - chi2cdf (chisq, df);
65 printf(" pval: %g\n", pval);