Mercurial > octave
annotate scripts/statistics/std.m @ 29358:0a5b15007766 stable
update Octave Project Developers copyright for the new year
In files that have the "Octave Project Developers" copyright notice,
update for 2021.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 10 Feb 2021 09:52:15 -0500 |
parents | bd51beb6205e |
children | 7854d5752dd2 |
rev | line source |
---|---|
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
1 ######################################################################## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
2 ## |
29358
0a5b15007766
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
27923
diff
changeset
|
3 ## Copyright (C) 1996-2021 The Octave Project Developers |
27918
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
27898
diff
changeset
|
4 ## |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
5 ## See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
6 ## distribution or <https://octave.org/copyright/>. |
3200 | 7 ## |
8 ## This file is part of Octave. | |
9 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
24512
diff
changeset
|
10 ## Octave is free software: you can redistribute it and/or modify it |
3200 | 11 ## under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
24512
diff
changeset
|
12 ## the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
13 ## (at your option) any later version. |
3200 | 14 ## |
15 ## Octave is distributed in the hope that it will be useful, but | |
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
18 ## GNU General Public License for more details. |
3200 | 19 ## |
20 ## You should have received a copy of the GNU General Public License | |
7016 | 21 ## along with Octave; see the file COPYING. If not, see |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
24512
diff
changeset
|
22 ## <https://www.gnu.org/licenses/>. |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
23 ## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
24 ######################################################################## |
3200 | 25 |
3367 | 26 ## -*- texinfo -*- |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20174
diff
changeset
|
27 ## @deftypefn {} {} std (@var{x}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20174
diff
changeset
|
28 ## @deftypefnx {} {} std (@var{x}, @var{opt}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20174
diff
changeset
|
29 ## @deftypefnx {} {} std (@var{x}, @var{opt}, @var{dim}) |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
30 ## Compute the standard deviation of the elements of the vector @var{x}. |
20174
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
31 ## |
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
32 ## The standard deviation is defined as |
3367 | 33 ## @tex |
34 ## $$ | |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
35 ## {\rm std} (x) = \sigma = \sqrt{{\sum_{i=1}^N (x_i - \bar{x})^2 \over N - 1}} |
3367 | 36 ## $$ |
24511
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
37 ## where $\bar{x}$ is the mean value of @var{x} and $N$ is the number of elements of @var{x}. |
3367 | 38 ## @end tex |
6754 | 39 ## @ifnottex |
3426 | 40 ## |
3367 | 41 ## @example |
42 ## @group | |
24511
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
43 ## std (@var{x}) = sqrt ( 1/(N-1) SUM_i (@var{x}(i) - mean(@var{x}))^2 ) |
3367 | 44 ## @end group |
45 ## @end example | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
46 ## |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
47 ## @noindent |
24511
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
48 ## where @math{N} is the number of elements of the @var{x} vector. |
6754 | 49 ## @end ifnottex |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
50 ## |
20174
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
51 ## If @var{x} is a matrix, compute the standard deviation for each column and |
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
52 ## return them in a row vector. |
4844 | 53 ## |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
54 ## The argument @var{opt} determines the type of normalization to use. |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
55 ## Valid values are |
4844 | 56 ## |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
57 ## @table @asis |
4844 | 58 ## @item 0: |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
59 ## normalize with @math{N-1}, provides the square root of the best unbiased |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
60 ## estimator of the variance [default] |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
61 ## |
4844 | 62 ## @item 1: |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
63 ## normalize with @math{N}, this provides the square root of the second |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
64 ## moment around the mean |
4844 | 65 ## @end table |
66 ## | |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
67 ## If the optional argument @var{dim} is given, operate along this dimension. |
24584
7a18e02a516e
mad.m: New function to calculate mean or median absolute deviation.
Rik <rik@octave.org>
parents:
24574
diff
changeset
|
68 ## @seealso{var, bounds, mad, range, iqr, mean, median} |
3367 | 69 ## @end deftypefn |
3200 | 70 |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
71 function retval = std (x, opt = 0, dim) |
3200 | 72 |
4844 | 73 if (nargin < 1 || nargin > 3) |
6046 | 74 print_usage (); |
4844 | 75 endif |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
76 |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
77 if (! (isnumeric (x) || islogical (x))) |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
78 error ("std: X must be a numeric vector or matrix"); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
79 endif |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
80 |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
81 if (isempty (opt)) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
82 opt = 0; |
21309
7fbecef105ca
Allow statistics functions to work over non-existent dimension (bug #33523).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
83 elseif (! isscalar (opt) || (opt != 0 && opt != 1)) |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
84 error ("std: normalization OPT must be 0 or 1"); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
85 endif |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
86 |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
87 nd = ndims (x); |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
88 sz = size (x); |
9500
bb37822e9b82
std.m: correctly work along singleton dimension
John W. Eaton <jwe@octave.org>
parents:
9211
diff
changeset
|
89 if (nargin < 3) |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
90 ## Find the first non-singleton dimension. |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
91 (dim = find (sz > 1, 1)) || (dim = 1); |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
92 else |
21309
7fbecef105ca
Allow statistics functions to work over non-existent dimension (bug #33523).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
93 if (! (isscalar (dim) && dim == fix (dim) && dim > 0)) |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
94 error ("std: DIM must be an integer and a valid dimension"); |
8507 | 95 endif |
4844 | 96 endif |
3200 | 97 |
21309
7fbecef105ca
Allow statistics functions to work over non-existent dimension (bug #33523).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
98 n = size (x, dim); |
12781
645a87434fcb
std.m: Allow null inputs []. Bug #33532.
Rik <octave@nomad.inbox5.com>
parents:
12656
diff
changeset
|
99 if (n == 1 || isempty (x)) |
14336
34af9f9ff98b
Use Octave coding conventions (double-quote " in preference to single quote ')
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
100 if (isa (x, "single")) |
34af9f9ff98b
Use Octave coding conventions (double-quote " in preference to single quote ')
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
101 retval = zeros (sz, "single"); |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
102 else |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
103 retval = zeros (sz); |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
104 endif |
12781
645a87434fcb
std.m: Allow null inputs []. Bug #33532.
Rik <octave@nomad.inbox5.com>
parents:
12656
diff
changeset
|
105 else |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
106 retval = sqrt (sumsq (center (x, dim), dim) / (n - 1 + opt)); |
3200 | 107 endif |
108 | |
109 endfunction | |
7411 | 110 |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
111 |
7411 | 112 %!test |
113 %! x = ones (10, 2); | |
114 %! y = [1, 3]; | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14336
diff
changeset
|
115 %! assert (std (x), [0, 0]); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14336
diff
changeset
|
116 %! assert (std (y), sqrt (2), sqrt (eps)); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14336
diff
changeset
|
117 %! assert (std (x, 0, 2), zeros (10, 1)); |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
118 |
21309
7fbecef105ca
Allow statistics functions to work over non-existent dimension (bug #33523).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
119 %!assert (std (ones (3, 1, 2), 0, 2), zeros (3, 1, 2)) |
7fbecef105ca
Allow statistics functions to work over non-existent dimension (bug #33523).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
120 %!assert (std ([1 2], 0), sqrt (2)/2, 5*eps) |
7fbecef105ca
Allow statistics functions to work over non-existent dimension (bug #33523).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
121 %!assert (std ([1 2], 1), 0.5, 5*eps) |
7fbecef105ca
Allow statistics functions to work over non-existent dimension (bug #33523).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
122 %!assert (std (1), 0) |
7fbecef105ca
Allow statistics functions to work over non-existent dimension (bug #33523).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
123 %!assert (std (single (1)), single (0)) |
7fbecef105ca
Allow statistics functions to work over non-existent dimension (bug #33523).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
124 %!assert (std ([]), []) |
7fbecef105ca
Allow statistics functions to work over non-existent dimension (bug #33523).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
125 %!assert (std (ones (1,3,0,2)), ones (1,3,0,2)) |
7fbecef105ca
Allow statistics functions to work over non-existent dimension (bug #33523).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
126 %!assert (std ([1 2 3], [], 3), [0 0 0]) |
7411 | 127 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
128 ## Test input validation |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14336
diff
changeset
|
129 %!error std () |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14336
diff
changeset
|
130 %!error std (1, 2, 3, 4) |
21309
7fbecef105ca
Allow statistics functions to work over non-existent dimension (bug #33523).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
131 %!error <X must be a numeric> std (['A'; 'B']) |
7fbecef105ca
Allow statistics functions to work over non-existent dimension (bug #33523).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
132 %!error <OPT must be 0 or 1> std (1, 2) |
7fbecef105ca
Allow statistics functions to work over non-existent dimension (bug #33523).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
133 %!error <DIM must be an integer> std (1, [], ones (2,2)) |
7fbecef105ca
Allow statistics functions to work over non-existent dimension (bug #33523).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
134 %!error <DIM must be an integer> std (1, [], 1.5) |
7fbecef105ca
Allow statistics functions to work over non-existent dimension (bug #33523).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
135 %!error <DIM must be .* a valid dimension> std (1, [], 0) |