]> Creatis software - CreaPhase.git/blob - octave_packages/m/statistics/base/kendall.m
update packages
[CreaPhase.git] / octave_packages / m / statistics / base / kendall.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} {} kendall (@var{x})
21 ## @deftypefnx {Function File} {} kendall (@var{x}, @var{y})
22 ## @cindex Kendall's Tau
23 ## Compute Kendall's @var{tau}.
24 ##
25 ## For two data vectors @var{x}, @var{y} of common length @var{n},
26 ## Kendall's @var{tau} is the correlation of the signs of all rank
27 ## differences of @var{x} and @var{y}; i.e., if both @var{x} and
28 ## @var{y} have distinct entries, then
29 ##
30 ## @tex
31 ## $$ \tau = {1 \over n(n-1)} \sum_{i,j} {\rm sign}(q_i-q_j) {\rm sign}(r_i-r_j) $$
32 ## @end tex
33 ## @ifnottex
34 ##
35 ## @example
36 ## @group
37 ##          1
38 ## tau = -------   SUM sign (q(i) - q(j)) * sign (r(i) - r(j))
39 ##       n (n-1)   i,j
40 ## @end group
41 ## @end example
42 ##
43 ## @end ifnottex
44 ## @noindent
45 ## in which the
46 ## @tex
47 ## $q_i$ and $r_i$
48 ## @end tex
49 ## @ifnottex
50 ## @var{q}(@var{i}) and @var{r}(@var{i})
51 ## @end ifnottex
52 ## are the ranks of @var{x} and @var{y}, respectively.
53 ##
54 ## If @var{x} and @var{y} are drawn from independent distributions,
55 ## Kendall's @var{tau} is asymptotically normal with mean 0 and variance
56 ## @tex
57 ## ${2 (2n+5) \over 9n(n-1)}$.
58 ## @end tex
59 ## @ifnottex
60 ## @code{(2 * (2@var{n}+5)) / (9 * @var{n} * (@var{n}-1))}.
61 ## @end ifnottex
62 ##
63 ## @code{kendall (@var{x})} is equivalent to @code{kendall (@var{x},
64 ## @var{x})}.
65 ## @seealso{ranks, spearman}
66 ## @end deftypefn
67
68 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
69 ## Description: Kendall's rank correlation tau
70
71 function tau = kendall (x, y = [])
72
73   if (nargin < 1 || nargin > 2)
74     print_usage ();
75   endif
76
77   if (   ! (isnumeric (x) || islogical (x))
78       || ! (isnumeric (y) || islogical (y)))
79     error ("kendall: X and Y must be numeric matrices or vectors");
80   endif
81
82   if (ndims (x) != 2 || ndims (y) != 2)
83     error ("kendall: X and Y must be 2-D matrices or vectors");
84   endif
85
86   if (isrow (x))
87     x = x.';
88   endif
89   [n, c] = size (x);
90
91   if (nargin == 2)
92     if (isrow (y))
93       y = y.';
94     endif
95     if (rows (y) != n)
96       error ("kendall: X and Y must have the same number of observations");
97     else
98       x = [x, y];
99     endif
100   endif
101
102   if (isa (x, 'single') || isa (y, 'single'))
103     cls = 'single';
104   else
105     cls = 'double';
106   endif
107   r   = ranks (x);
108   m   = sign (kron (r, ones (n, 1, cls)) - kron (ones (n, 1, cls), r));
109   tau = corr (m);
110
111   if (nargin == 2)
112     tau = tau(1 : c, (c + 1) : columns (x));
113   endif
114
115 endfunction
116
117
118 %!test
119 %! x = [1:2:10];
120 %! y = [100:10:149];
121 %! assert (kendall (x,y), 1, 5*eps);
122 %! assert (kendall (x,fliplr (y)), -1, 5*eps);
123
124 %!assert (kendall (logical(1)), 1);
125 %!assert (kendall (single(1)), single(1));
126
127 %% Test input validation
128 %!error kendall ();
129 %!error kendall (1, 2, 3);
130 %!error kendall (['A'; 'B']);
131 %!error kendall (ones(2,1), ['A'; 'B']);
132 %!error kendall (ones (2,2,2));
133 %!error kendall (ones (2,2), ones (2,2,2));
134 %!error kendall (ones (2,2), ones (3,2));