changelog shortlog tags changeset files revisions annotate raw

scripts/statistics/base/spearman.m

changeset 10289: 4b124317dc38
parent:93c65f2a5668
author: John W. Eaton <jwe@octave.org>
date: Tue Feb 09 20:58:55 2010 -0500 (66 minutes ago)
permissions: -rw-r--r--
description: base_properties::set_children: account for hidden children
1## Copyright (C) 1995, 1996, 1997, 1998, 2000, 2002, 2005, 2006, 2007
2## Kurt Hornik
3##
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
8## the Free Software Foundation; either version 3 of the License, or (at
9## your option) any later version.
10##
11## Octave is distributed in the hope that it will be useful, but
12## WITHOUT ANY WARRANTY; without even the implied warranty of
13## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14## General Public License for more details.
15##
16## You should have received a copy of the GNU General Public License
17## along with Octave; see the file COPYING. If not, see
18## <http://www.gnu.org/licenses/>.
19
20## -*- texinfo -*-
21## @deftypefn {Function File} {} spearman (@var{x}, @var{y})
22## Compute Spearman's rank correlation coefficient @var{rho} for each of
23## the variables specified by the input arguments.
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##
28## @code{spearman (@var{x})} is equivalent to @code{spearman (@var{x},
29## @var{x})}.
30##
31## For two data vectors @var{x} and @var{y}, Spearman's @var{rho} is the
32## correlation of the ranks of @var{x} and @var{y}.
33##
34## If @var{x} and @var{y} are drawn from independent distributions,
35## @var{rho} has zero mean and variance @code{1 / (n - 1)}, and is
36## asymptotically normally distributed.
37## @end deftypefn
38
39## Author: KH <Kurt.Hornik@wu-wien.ac.at>
40## Description: Spearman's rank correlation rho
41
42function rho = spearman (x, y)
43
44 if ((nargin < 1) || (nargin > 2))
45 print_usage ();
46 endif
47
48 if (rows (x) == 1)
49 x = x';
50 endif
51 n = rows (x);
52
53 if (nargin == 1)
54 rho = cor (ranks (x));
55 else
56 if (rows (y) == 1)
57 y = y';
58 endif
59 if (rows (y) != n)
60 error ("spearman: x and y must have the same number of observations");
61 endif
62 rho = cor (ranks (x), ranks (y));
63 endif
64
65endfunction