annotate scripts/statistics/base/zscore.m @ 19867:9fc020886ae9

maint: Clean up m-files to follow Octave coding conventions. Try to trim long lines to < 80 chars. Use '##' for single line comments. Use '(...)' around tests for if/elseif/switch/while. Abut cell indexing operator '{' next to variable. Abut array indexing operator '(' next to variable. Use space between negation operator '!' and following expression. Use two newlines between endfunction and start of %!test or %!demo code. Remove unnecessary parens grouping between short-circuit operators. Remove stray extra spaces (typos) between variables and assignment operators. Remove stray extra spaces from ends of lines.
author Rik <rik@octave.org>
date Mon, 23 Feb 2015 14:54:39 -0800
parents 4197fc428c7d
children d9341b422488
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19731
4197fc428c7d maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents: 17744
diff changeset
1 ## Copyright (C) 1995-2015 Kurt Hornik
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3388
diff changeset
2 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3458
diff changeset
3 ## This file is part of Octave.
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3458
diff changeset
4 ##
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3458
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3458
diff changeset
6 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
8 ## your option) any later version.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3388
diff changeset
9 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3458
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3388
diff changeset
13 ## General Public License for more details.
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3388
diff changeset
14 ##
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
17 ## <http://www.gnu.org/licenses/>.
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
18
3453
71d2e09c15a2 [project @ 2000-01-18 08:32:09 by jwe]
jwe
parents: 3426
diff changeset
19 ## -*- texinfo -*-
14234
938a8d792c37 zscore.m: Return mean and std dev for Matlab compatibility. Update docstring.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
20 ## @deftypefn {Function File} {[@var{z}, @var{mu}, @var{sigma}] =} zscore (@var{x})
938a8d792c37 zscore.m: Return mean and std dev for Matlab compatibility. Update docstring.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
21 ## @deftypefnx {Function File} {[@var{z}, @var{mu}, @var{sigma}] =} zscore (@var{x}, @var{opt})
938a8d792c37 zscore.m: Return mean and std dev for Matlab compatibility. Update docstring.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
22 ## @deftypefnx {Function File} {[@var{z}, @var{mu}, @var{sigma}] =} zscore (@var{x}, @var{opt}, @var{dim})
3453
71d2e09c15a2 [project @ 2000-01-18 08:32:09 by jwe]
jwe
parents: 3426
diff changeset
23 ## If @var{x} is a vector, subtract its mean and divide by its standard
14366
b76f0740940e doc: Periodic grammar check of documentation.
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
24 ## deviation. If the standard deviation is zero, divide by 1 instead.
14234
938a8d792c37 zscore.m: Return mean and std dev for Matlab compatibility. Update docstring.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
25 ## The optional parameter @var{opt} determines the normalization to use
938a8d792c37 zscore.m: Return mean and std dev for Matlab compatibility. Update docstring.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
26 ## when computing the standard deviation and is the same as the
938a8d792c37 zscore.m: Return mean and std dev for Matlab compatibility. Update docstring.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
27 ## corresponding parameter for @code{std}.
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
28 ##
4885
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 4030
diff changeset
29 ## If @var{x} is a matrix, do the above along the first non-singleton
14366
b76f0740940e doc: Periodic grammar check of documentation.
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
30 ## dimension. If the third optional argument @var{dim} is given, operate
14234
938a8d792c37 zscore.m: Return mean and std dev for Matlab compatibility. Update docstring.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
31 ## along this dimension.
938a8d792c37 zscore.m: Return mean and std dev for Matlab compatibility. Update docstring.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
32 ##
938a8d792c37 zscore.m: Return mean and std dev for Matlab compatibility. Update docstring.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
33 ## The mean and standard deviation along @var{dim} are given in @var{mu}
938a8d792c37 zscore.m: Return mean and std dev for Matlab compatibility. Update docstring.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
34 ## and @var{sigma} respectively.
938a8d792c37 zscore.m: Return mean and std dev for Matlab compatibility. Update docstring.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
35 ##
938a8d792c37 zscore.m: Return mean and std dev for Matlab compatibility. Update docstring.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
36 ## @seealso{mean, std, center}
3453
71d2e09c15a2 [project @ 2000-01-18 08:32:09 by jwe]
jwe
parents: 3426
diff changeset
37 ## @end deftypefn
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
38
5428
2a16423e4aa0 [project @ 2005-08-23 18:38:27 by jwe]
jwe
parents: 5307
diff changeset
39 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
3456
434790acb067 [project @ 2000-01-19 06:58:51 by jwe]
jwe
parents: 3453
diff changeset
40 ## Description: Subtract mean and divide by standard deviation
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
41
14234
938a8d792c37 zscore.m: Return mean and std dev for Matlab compatibility. Update docstring.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
42 function [z, mu, sigma] = zscore (x, opt, dim)
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3388
diff changeset
43
14234
938a8d792c37 zscore.m: Return mean and std dev for Matlab compatibility. Update docstring.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
44 if (nargin < 1 || nargin > 3 )
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5428
diff changeset
45 print_usage ();
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
46 endif
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
47
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12586
diff changeset
48 if (! (isnumeric (x) || islogical (x)))
12586
f9b7aa3b88f8 Deprecate studentize(), replace with zscore().
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
49 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
50 endif
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
51
14234
938a8d792c37 zscore.m: Return mean and std dev for Matlab compatibility. Update docstring.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
52 if (nargin < 2)
938a8d792c37 zscore.m: Return mean and std dev for Matlab compatibility. Update docstring.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
53 opt = 0;
938a8d792c37 zscore.m: Return mean and std dev for Matlab compatibility. Update docstring.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
54 else
14868
5d3a684236b0 maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents: 14366
diff changeset
55 if (opt != 0 && opt != 1 || ! isscalar (opt))
5d3a684236b0 maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents: 14366
diff changeset
56 error ("zscore: OPT must be empty, 0, or 1");
14234
938a8d792c37 zscore.m: Return mean and std dev for Matlab compatibility. Update docstring.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
57 endif
938a8d792c37 zscore.m: Return mean and std dev for Matlab compatibility. Update docstring.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
58 endif
938a8d792c37 zscore.m: Return mean and std dev for Matlab compatibility. Update docstring.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
59
4885
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 4030
diff changeset
60 nd = ndims (x);
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 4030
diff changeset
61 sz = size (x);
14234
938a8d792c37 zscore.m: Return mean and std dev for Matlab compatibility. Update docstring.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
62 if (nargin < 3)
4886
54b076a24718 [project @ 2004-04-30 04:31:51 by jwe]
jwe
parents: 4885
diff changeset
63 ## Find the first non-singleton dimension.
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12586
diff changeset
64 (dim = find (sz > 1, 1)) || (dim = 1);
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
65 else
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
66 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
67 || !(1 <= dim && dim <= nd))
12586
f9b7aa3b88f8 Deprecate studentize(), replace with zscore().
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
68 error ("zscore: DIM must be an integer and a valid dimension");
4885
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 4030
diff changeset
69 endif
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 4030
diff changeset
70 endif
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 4030
diff changeset
71
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12586
diff changeset
72 n = sz(dim);
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12586
diff changeset
73 if (n == 0)
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12586
diff changeset
74 z = x;
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
75 else
14234
938a8d792c37 zscore.m: Return mean and std dev for Matlab compatibility. Update docstring.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
76
938a8d792c37 zscore.m: Return mean and std dev for Matlab compatibility. Update docstring.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
77 if (isinteger (x))
938a8d792c37 zscore.m: Return mean and std dev for Matlab compatibility. Update docstring.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
78 x = double (x);
938a8d792c37 zscore.m: Return mean and std dev for Matlab compatibility. Update docstring.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
79 endif
938a8d792c37 zscore.m: Return mean and std dev for Matlab compatibility. Update docstring.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
80
938a8d792c37 zscore.m: Return mean and std dev for Matlab compatibility. Update docstring.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
81 mu = mean (x, dim);
938a8d792c37 zscore.m: Return mean and std dev for Matlab compatibility. Update docstring.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
82 sigma = std (x, opt, dim);
938a8d792c37 zscore.m: Return mean and std dev for Matlab compatibility. Update docstring.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
83 s = sigma;
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12586
diff changeset
84 s(s==0) = 1;
14234
938a8d792c37 zscore.m: Return mean and std dev for Matlab compatibility. Update docstring.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
85 ## FIXME: Use normal broadcasting once we can disable that warning
938a8d792c37 zscore.m: Return mean and std dev for Matlab compatibility. Update docstring.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
86 z = bsxfun (@rdivide, bsxfun (@minus, x, mu), s);
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
87 endif
4885
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 4030
diff changeset
88
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 4030
diff changeset
89 endfunction
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
90
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12586
diff changeset
91
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14234
diff changeset
92 %!assert (zscore ([1,2,3]), [-1,0,1])
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14234
diff changeset
93 %!assert (zscore (single ([1,2,3])), single ([-1,0,1]))
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14234
diff changeset
94 %!assert (zscore (int8 ([1,2,3])), [-1,0,1])
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14234
diff changeset
95 %!assert (zscore (ones (3,2,2,2)), zeros (3,2,2,2))
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14234
diff changeset
96 %!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
97
19867
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19731
diff changeset
98 ## Test input validation
12586
f9b7aa3b88f8 Deprecate studentize(), replace with zscore().
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
99 %!error zscore ()
f9b7aa3b88f8 Deprecate studentize(), replace with zscore().
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
100 %!error zscore (1, 2, 3)
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12586
diff changeset
101 %!error zscore (['A'; 'B'])
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14234
diff changeset
102 %!error zscore (1, ones (2,2))
12586
f9b7aa3b88f8 Deprecate studentize(), replace with zscore().
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
103 %!error zscore (1, 1.5)
14234
938a8d792c37 zscore.m: Return mean and std dev for Matlab compatibility. Update docstring.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
104 %!error zscore (1, 1, 0)
12586
f9b7aa3b88f8 Deprecate studentize(), replace with zscore().
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
105 %!error zscore (1, 3)
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
106