Mercurial > hg > octave-lyh
annotate scripts/statistics/base/zscore.m @ 14138:72c96de7a403 stable
maint: update copyright notices for 2012
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 02 Jan 2012 14:25:41 -0500 |
parents | 6b2f14af2360 |
children | 938a8d792c37 |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
12656
diff
changeset
|
1 ## Copyright (C) 1995-2012 Kurt Hornik |
3426 | 2 ## |
3922 | 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. | |
3426 | 9 ## |
3922 | 10 ## Octave is distributed in the hope that it will be useful, but |
3200 | 11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
3426 | 13 ## General Public License for more details. |
14 ## | |
3200 | 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 |
3453 | 19 ## -*- texinfo -*- |
12586
f9b7aa3b88f8
Deprecate studentize(), replace with zscore().
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
20 ## @deftypefn {Function File} {} zscore (@var{x}) |
f9b7aa3b88f8
Deprecate studentize(), replace with zscore().
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
21 ## @deftypefnx {Function File} {} zscore (@var{x}, @var{dim}) |
3453 | 22 ## If @var{x} is a vector, subtract its mean and divide by its standard |
3200 | 23 ## deviation. |
24 ## | |
4885 | 25 ## If @var{x} is a matrix, do the above along the first non-singleton |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
26 ## dimension. |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
27 ## If the optional argument @var{dim} is given, operate along this dimension. |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
28 ## @seealso{center} |
3453 | 29 ## @end deftypefn |
3200 | 30 |
5428 | 31 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3456 | 32 ## Description: Subtract mean and divide by standard deviation |
3200 | 33 |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12586
diff
changeset
|
34 function z = zscore (x, dim) |
3426 | 35 |
4885 | 36 if (nargin != 1 && nargin != 2) |
6046 | 37 print_usage (); |
3200 | 38 endif |
39 | |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12586
diff
changeset
|
40 if (! (isnumeric (x) || islogical (x))) |
12586
f9b7aa3b88f8
Deprecate studentize(), replace with zscore().
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
41 error ("zscore: X must be a numeric vector or matrix"); |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
42 endif |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
43 |
4885 | 44 nd = ndims (x); |
45 sz = size (x); | |
46 if (nargin != 2) | |
4886 | 47 ## Find the first non-singleton dimension. |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12586
diff
changeset
|
48 (dim = find (sz > 1, 1)) || (dim = 1); |
3200 | 49 else |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
50 if (!(isscalar (dim) && dim == fix (dim)) |
11149
fe3c3dfc07eb
style fix: break lines before && and ||, not after
John W. Eaton <jwe@octave.org>
parents:
10669
diff
changeset
|
51 || !(1 <= dim && dim <= nd)) |
12586
f9b7aa3b88f8
Deprecate studentize(), replace with zscore().
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
52 error ("zscore: DIM must be an integer and a valid dimension"); |
4885 | 53 endif |
54 endif | |
55 | |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12586
diff
changeset
|
56 n = sz(dim); |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12586
diff
changeset
|
57 if (n == 0) |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12586
diff
changeset
|
58 z = x; |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
59 else |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12586
diff
changeset
|
60 x = center (x, dim); # center also promotes integer to double for next line |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12586
diff
changeset
|
61 z = zeros (sz, class (x)); |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12586
diff
changeset
|
62 s = std (x, [], dim); |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12586
diff
changeset
|
63 s(s==0) = 1; |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12586
diff
changeset
|
64 z = bsxfun (@rdivide, x, s); |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
65 endif |
4885 | 66 |
67 endfunction | |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
68 |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12586
diff
changeset
|
69 |
12586
f9b7aa3b88f8
Deprecate studentize(), replace with zscore().
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
70 %!assert(zscore ([1,2,3]), [-1,0,1]) |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12586
diff
changeset
|
71 %!assert(zscore (single([1,2,3])), single([-1,0,1])) |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12586
diff
changeset
|
72 %!assert(zscore (int8([1,2,3])), [-1,0,1]) |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12586
diff
changeset
|
73 %!assert(zscore (ones (3,2,2,2)), zeros (3,2,2,2)) |
12586
f9b7aa3b88f8
Deprecate studentize(), replace with zscore().
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
74 %!assert(zscore ([2,0,-2;0,2,0;-2,-2,2]), [1,0,-1;0,1,0;-1,-1,1]) |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
75 |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
76 %% Test input validation |
12586
f9b7aa3b88f8
Deprecate studentize(), replace with zscore().
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
77 %!error zscore () |
f9b7aa3b88f8
Deprecate studentize(), replace with zscore().
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
78 %!error zscore (1, 2, 3) |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12586
diff
changeset
|
79 %!error zscore (['A'; 'B']) |
12586
f9b7aa3b88f8
Deprecate studentize(), replace with zscore().
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
80 %!error zscore (1, ones(2,2)) |
f9b7aa3b88f8
Deprecate studentize(), replace with zscore().
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
81 %!error zscore (1, 1.5) |
f9b7aa3b88f8
Deprecate studentize(), replace with zscore().
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
82 %!error zscore (1, 0) |
f9b7aa3b88f8
Deprecate studentize(), replace with zscore().
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
83 %!error zscore (1, 3) |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
84 |