Mercurial > octave-nkf
annotate scripts/statistics/base/kendall.m @ 8517:81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
author | sh@sh-laptop |
---|---|
date | Wed, 14 Jan 2009 20:44:25 -0500 |
parents | a1dbe9d80eee |
children | eb63fbe60fab |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1995, 1996, 1997, 1998, 2000, 2002, 2005, 2006, 2007 |
2 ## Kurt Hornik | |
3426 | 3 ## |
3922 | 4 ## This file is part of Octave. |
5 ## | |
6 ## Octave is free software; you can redistribute it and/or modify it | |
7 ## under the terms of the GNU General Public License as published by | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
3426 | 10 ## |
3922 | 11 ## Octave is distributed in the hope that it will be useful, but |
3200 | 12 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
3426 | 14 ## General Public License for more details. |
15 ## | |
3200 | 16 ## You should have received a copy of the GNU General Public License |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
3200 | 19 |
3453 | 20 ## -*- texinfo -*- |
21 ## @deftypefn {Function File} {} kendall (@var{x}, @var{y}) | |
22 ## Compute Kendall's @var{tau} for each of the variables specified by | |
23 ## the input arguments. | |
3200 | 24 ## |
25 ## For matrices, each row is an observation and each column a variable; | |
26 ## vectors are always observations and may be row or column vectors. | |
27 ## | |
3453 | 28 ## @code{kendall (@var{x})} is equivalent to @code{kendall (@var{x}, |
29 ## @var{x})}. | |
30 ## | |
31 ## For two data vectors @var{x}, @var{y} of common length @var{n}, | |
32 ## Kendall's @var{tau} is the correlation of the signs of all rank | |
33 ## differences of @var{x} and @var{y}; i.e., if both @var{x} and | |
34 ## @var{y} have distinct entries, then | |
3200 | 35 ## |
3453 | 36 ## @iftex |
37 ## @tex | |
38 ## $$ \tau = {1 \over n(n-1)} \sum_{i,j} {\rm sign}(q_i-q_j) {\rm sign}(r_i-r_j) $$ | |
39 ## @end tex | |
40 ## @end iftex | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7017
diff
changeset
|
41 ## @ifnottex |
3453 | 42 ## @example |
43 ## 1 | |
44 ## tau = ------- SUM sign (q(i) - q(j)) * sign (r(i) - r(j)) | |
45 ## n (n-1) i,j | |
46 ## @end example | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7017
diff
changeset
|
47 ## @end ifnottex |
3200 | 48 ## |
3453 | 49 ## @noindent |
50 ## in which the | |
51 ## @iftex | |
52 ## @tex | |
53 ## $q_i$ and $r_i$ | |
54 ## @end tex | |
55 ## @end iftex | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7017
diff
changeset
|
56 ## @ifnottex |
3453 | 57 ## @var{q}(@var{i}) and @var{r}(@var{i}) |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7017
diff
changeset
|
58 ## @end ifnottex |
3453 | 59 ## are the ranks of |
60 ## @var{x} and @var{y}, respectively. | |
61 ## | |
62 ## If @var{x} and @var{y} are drawn from independent distributions, | |
63 ## Kendall's @var{tau} is asymptotically normal with mean 0 and variance | |
6754 | 64 ## @iftex |
65 ## @tex | |
66 ## ${2 (2n+5) \over 9n(n-1)}$. | |
67 ## @end tex | |
68 ## @end iftex | |
69 ## @ifnottex | |
3453 | 70 ## @code{(2 * (2@var{n}+5)) / (9 * @var{n} * (@var{n}-1))}. |
6754 | 71 ## @end ifnottex |
3453 | 72 ## @end deftypefn |
3200 | 73 |
5428 | 74 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3456 | 75 ## Description: Kendall's rank correlation tau |
3200 | 76 |
77 function tau = kendall (x, y) | |
3426 | 78 |
3200 | 79 if ((nargin < 1) || (nargin > 2)) |
6046 | 80 print_usage (); |
3200 | 81 endif |
82 | |
83 if (rows (x) == 1) | |
84 x = x'; | |
85 endif | |
86 [n, c] = size (x); | |
87 | |
88 if (nargin == 2) | |
89 if (rows (y) == 1) | |
90 y = y'; | |
91 endif | |
92 if (rows (y) != n) | |
3456 | 93 error ("kendall: x and y must have the same number of observations"); |
3200 | 94 else |
95 x = [x, y]; | |
96 endif | |
97 endif | |
3426 | 98 |
3200 | 99 r = ranks (x); |
100 m = sign (kron (r, ones (n, 1)) - kron (ones (n, 1), r)); | |
101 tau = cor (m); | |
3426 | 102 |
3200 | 103 if (nargin == 2) |
104 tau = tau (1 : c, (c + 1) : columns (x)); | |
105 endif | |
3426 | 106 |
6754 | 107 endfunction |