]> Creatis software - CreaPhase.git/blob - octave_packages/m/statistics/tests/chisquare_test_homogeneity.m
update packages
[CreaPhase.git] / octave_packages / m / statistics / tests / chisquare_test_homogeneity.m
1 ## Copyright (C) 1995-2012 Kurt Hornik
2 ##
3 ## This file is part of Octave.
4 ##
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.
9 ##
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.
14 ##
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/>.
18
19 ## -*- texinfo -*-
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}.
25 ##
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.
29 ##
30 ## The p-value (1 minus the CDF of this distribution at @var{chisq}) is
31 ## returned in @var{pval}.
32 ##
33 ## If no output argument is given, the p-value is displayed.
34 ## @end deftypefn
35
36 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
37 ## Description: Chi-square test for homogeneity
38
39 function [pval, chisq, df] = chisquare_test_homogeneity (x, y, c)
40
41   if (nargin != 3)
42     print_usage ();
43   endif
44
45   if (! (isvector(x) && isvector(y) && isvector(c)))
46     error ("chisquare_test_homogeneity: X, Y and C must be vectors");
47   endif
48   ## Now test c for strictly increasing entries
49   df = length (c);
50   if (any ((c(2 : df) - c(1 : (df - 1))) <= 0))
51     error ("chisquare_test_homogeneity: C must be increasing");
52   endif
53
54   c     = [(reshape (c, 1, df)), Inf];
55   l_x   = length (x);
56   x     = reshape (x, l_x, 1);
57   n_x   = sum (x * ones (1, df+1) < ones (l_x, 1) * c);
58   l_y   = length (y);
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);
63
64   if (nargout == 0)
65     printf("  pval: %g\n", pval);
66   endif
67
68 endfunction