home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts.fat / stat / base / kendall.m < prev    next >
Text File  |  1999-04-29  |  2KB  |  71 lines

  1. ## Copyright (C) 1995, 1996, 1997  Kurt Hornik
  2. ## 
  3. ## This program is free software; you can redistribute it and/or modify
  4. ## it under the terms of the GNU General Public License as published by
  5. ## the Free Software Foundation; either version 2, or (at your option)
  6. ## any later version.
  7. ## 
  8. ## This program is distributed in the hope that it will be useful, but
  9. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  11. ## General Public License for more details. 
  12. ## 
  13. ## You should have received a copy of the GNU General Public License
  14. ## along with this file.  If not, write to the Free Software Foundation,
  15. ## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16.  
  17. ## usage:  kendall (x [, y])
  18. ##
  19. ## Computes Kendall's tau for each of the variables specified by the
  20. ## input arguments.
  21. ##
  22. ## For matrices, each row is an observation and each column a variable;
  23. ## vectors are always observations and may be row or column vectors.
  24. ##
  25. ## kendall (x) is equivalent to kendall (x, x).
  26. ##
  27. ## For two data vectors x, y of common length n, Kendall's tau is the
  28. ## correlation of the signs of all rank differences of x and y;  i.e.,
  29. ## if both x and y have distinct entries, then \tau = \frac{1}{n(n-1)}
  30. ## \sum_{i,j} SIGN(q_i-q_j) SIGN(r_i-r_j), where the q_i and r_i are the
  31. ## ranks of x and y, respectively.
  32. ##
  33. ## If x and y are drawn from independent dist, Kendall's tau is
  34. ## asymptotically normal with mean 0 and variance (2 * (2n+5)) / (9 * n
  35. ## * (n-1)).
  36.  
  37. ## Author:  KH <Kurt.Hornik@ci.tuwien.ac.at>
  38. ## Description:  Kendall's rank correlation tau
  39.  
  40. function tau = kendall (x, y)
  41.   
  42.   if ((nargin < 1) || (nargin > 2))
  43.     usage ("kendall (x [, y])");
  44.   endif
  45.  
  46.   if (rows (x) == 1)
  47.     x = x';
  48.   endif
  49.   [n, c] = size (x);
  50.  
  51.   if (nargin == 2)
  52.     if (rows (y) == 1)
  53.       y = y';
  54.     endif
  55.     if (rows (y) != n)
  56.       error (["kendall:  ", ...
  57.           "x and y must have the same number of observations"]);
  58.     else
  59.       x = [x, y];
  60.     endif
  61.   endif
  62.   
  63.   r   = ranks (x);
  64.   m   = sign (kron (r, ones (n, 1)) - kron (ones (n, 1), r));
  65.   tau = cor (m);
  66.   
  67.   if (nargin == 2)
  68.     tau = tau (1 : c, (c + 1) : columns (x));
  69.   endif
  70.   
  71. endfunction