Mercurial > octave-nkf
annotate scripts/deprecated/mahalanobis.m @ 20595:c1a6c31ac29a
eliminate more simple uses of error_state
* ov-classdef.cc: Eliminate simple uses of error_state.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 06 Oct 2015 00:20:02 -0400 |
parents | afdb856e44f1 |
children |
rev | line source |
---|---|
19731
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
1 ## Copyright (C) 1996-2015 John W. Eaton |
3200 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
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 | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
3200 | 9 ## |
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. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
3200 | 18 |
3367 | 19 ## -*- texinfo -*- |
20 ## @deftypefn {Function File} {} mahalanobis (@var{x}, @var{y}) | |
20537 | 21 ## |
22 ## @code{mahalanobis} is deprecated and will be removed in Octave version 4.6. | |
23 ## See the @code{mahal} function in the statistics package from Octave-Forge | |
24 ## for equivalent functionality. | |
25 ## | |
3367 | 26 ## Return the Mahalanobis' D-square distance between the multivariate |
20209
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19867
diff
changeset
|
27 ## samples @var{x} and @var{y}. |
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19867
diff
changeset
|
28 ## |
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19867
diff
changeset
|
29 ## The data @var{x} and @var{y} must have the same number of components |
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19867
diff
changeset
|
30 ## (columns), but may have a different number of observations (rows). |
3367 | 31 ## @end deftypefn |
3200 | 32 |
33 ## Author: Friedrich Leisch <leisch@ci.tuwien.ac.at> | |
34 ## Created: July 1993 | |
35 ## Adapted-By: jwe | |
36 | |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
8920
diff
changeset
|
37 function retval = mahalanobis (x, y) |
3200 | 38 |
20537 | 39 persistent warned = false; |
40 if (! warned) | |
41 warned = true; | |
42 warning ("Octave:deprecated-function", | |
43 "mahalanobis is obsolete and will be removed from a future version of Octave, please use mahal from the statistics package in Octave-Forge instead"); | |
44 endif | |
45 | |
3200 | 46 if (nargin != 2) |
6046 | 47 print_usage (); |
3200 | 48 endif |
49 | |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
50 if ( ! (isnumeric (x) || islogical (x)) |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
51 || ! (isnumeric (y) || islogical (y))) |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
8920
diff
changeset
|
52 error ("mahalanobis: X and Y must be numeric matrices or vectors"); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
8920
diff
changeset
|
53 endif |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
8920
diff
changeset
|
54 |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
8920
diff
changeset
|
55 if (ndims (x) != 2 || ndims (y) != 2) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
8920
diff
changeset
|
56 error ("mahalanobis: X and Y must be 2-D matrices or vectors"); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
8920
diff
changeset
|
57 endif |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
8920
diff
changeset
|
58 |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
8920
diff
changeset
|
59 [xr, xc] = size (x); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
8920
diff
changeset
|
60 [yr, yc] = size (y); |
3200 | 61 |
62 if (xc != yc) | |
3458 | 63 error ("mahalanobis: X and Y must have the same number of columns"); |
3200 | 64 endif |
65 | |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
66 if (isinteger (x)) |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
67 x = double (x); |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
68 endif |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
69 |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
8920
diff
changeset
|
70 xm = mean (x); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
8920
diff
changeset
|
71 ym = mean (y); |
3200 | 72 |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
73 ## Center data by subtracting means |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
74 x = bsxfun (@minus, x, xm); |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
75 y = bsxfun (@minus, y, ym); |
3200 | 76 |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
8920
diff
changeset
|
77 w = (x' * x + y' * y) / (xr + yr - 2); |
3200 | 78 |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
8920
diff
changeset
|
79 winv = inv (w); |
3200 | 80 |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
8920
diff
changeset
|
81 retval = (xm - ym) * winv * (xm - ym)'; |
3200 | 82 |
83 endfunction | |
7411 | 84 |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
85 |
19867
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19731
diff
changeset
|
86 ## Test input validation |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
87 %!error mahalanobis () |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
88 %!error mahalanobis (1, 2, 3) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
89 %!error mahalanobis ('A', 'B') |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
90 %!error mahalanobis ([1, 2], ['A', 'B']) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
91 %!error mahalanobis (ones (2,2,2)) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
92 %!error mahalanobis (ones (2,2), ones (2,2,2)) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
93 %!error mahalanobis (ones (2,2), ones (2,3)) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
94 |