annotate scripts/statistics/mahalanobis.m @ 283:f3ce570869fc

[project @ 1994-01-10 20:29:54 by jwe] Initial revision
author jwe
date Mon, 10 Jan 1994 20:30:01 +0000
parents
children 3c23b8ea9099
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
283
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
1 # Copyright (C) 1993 John W. Eaton
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
2 #
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
3 # This file is part of Octave.
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
4 #
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
5 # Octave is free software; you can redistribute it and/or modify it
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
6 # under the terms of the GNU General Public License as published by the
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
7 # Free Software Foundation; either version 2, or (at your option) any
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
8 # later version.
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
9 #
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
10 # Octave is distributed in the hope that it will be useful, but WITHOUT
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
12 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
13 # for more details.
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
14 #
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
15 # You should have received a copy of the GNU General Public License
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
16 # along with Octave; see the file COPYING. If not, write to the Free
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
17 # Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
18
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
19 function retval = mahalanobis (X, Y)
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
20
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
21 # usage: mahalanobis (X, Y)
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
22 #
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
23 # Returns Mahalanobis' D-square distance between the multivariate
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
24 # samples X and Y, which must have the same number of components
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
25 # (columns), but may have a different number of observations (rows).
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
26
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
27 # Written by Friedrich Leisch (leisch@neuro.tuwien.ac.at) July 1993.
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
28 # Dept of Probability Theory and Statistics TU Wien, Austria.
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
29
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
30 if (nargin != 2)
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
31 error ("usage: mahalanobis (X, Y)");
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
32 endif
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
33
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
34 [xr, xc] = size (X);
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
35 [yr, yc] = size (Y);
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
36
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
37 if (xc != yc)
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
38 error ("mahalanobis: X and Y must have the same number of columns.");
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
39 endif
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
40
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
41 Xm = sum (X) / xr;
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
42 Ym = sum (Y) / yr;
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
43
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
44 X = X - ones (xr, 1) * Xm;
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
45 Y = Y - ones (yr, 1) * Ym;
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
46
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
47 W = (X' * X + Y' * Y) / (xr + yr - 2);
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
48
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
49 Winv = inv (W);
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
50
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
51 retval = (Xm - Ym) * Winv * (Xm - Ym)';
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
52
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
53 endfunction