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} {} kendall (@var{x})
21 ## @deftypefnx {Function File} {} kendall (@var{x}, @var{y})
22 ## @cindex Kendall's Tau
23 ## Compute Kendall's @var{tau}.
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
31 ## $$ \tau = {1 \over n(n-1)} \sum_{i,j} {\rm sign}(q_i-q_j) {\rm sign}(r_i-r_j) $$
38 ## tau = ------- SUM sign (q(i) - q(j)) * sign (r(i) - r(j))
50 ## @var{q}(@var{i}) and @var{r}(@var{i})
52 ## are the ranks of @var{x} and @var{y}, respectively.
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
57 ## ${2 (2n+5) \over 9n(n-1)}$.
60 ## @code{(2 * (2@var{n}+5)) / (9 * @var{n} * (@var{n}-1))}.
63 ## @code{kendall (@var{x})} is equivalent to @code{kendall (@var{x},
65 ## @seealso{ranks, spearman}
68 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
69 ## Description: Kendall's rank correlation tau
71 function tau = kendall (x, y = [])
73 if (nargin < 1 || nargin > 2)
77 if ( ! (isnumeric (x) || islogical (x))
78 || ! (isnumeric (y) || islogical (y)))
79 error ("kendall: X and Y must be numeric matrices or vectors");
82 if (ndims (x) != 2 || ndims (y) != 2)
83 error ("kendall: X and Y must be 2-D matrices or vectors");
96 error ("kendall: X and Y must have the same number of observations");
102 if (isa (x, 'single') || isa (y, 'single'))
108 m = sign (kron (r, ones (n, 1, cls)) - kron (ones (n, 1, cls), r));
112 tau = tau(1 : c, (c + 1) : columns (x));
121 %! assert (kendall (x,y), 1, 5*eps);
122 %! assert (kendall (x,fliplr (y)), -1, 5*eps);
124 %!assert (kendall (logical(1)), 1);
125 %!assert (kendall (single(1)), single(1));
127 %% Test input validation
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));