Mercurial > octave
annotate scripts/statistics/statistics.m @ 26376:00f796120a6d stable
maint: Update copyright dates in all source files.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 02 Jan 2019 16:32:43 -0500 |
parents | 6652d3823428 |
children | b442ec6dda5c |
rev | line source |
---|---|
26376
00f796120a6d
maint: Update copyright dates in all source files.
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
1 ## Copyright (C) 1995-2019 Kurt Hornik |
3426 | 2 ## |
3922 | 3 ## This file is part of Octave. |
4 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
5 ## Octave is free software: you can redistribute it and/or modify it |
3922 | 6 ## 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:
23220
diff
changeset
|
7 ## 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
|
8 ## (at 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 |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
13 ## GNU General Public License for more details. |
3426 | 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 |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
17 ## <https://www.gnu.org/licenses/>. |
3200 | 18 |
3453 | 19 ## -*- texinfo -*- |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20174
diff
changeset
|
20 ## @deftypefn {} {} statistics (@var{x}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20174
diff
changeset
|
21 ## @deftypefnx {} {} statistics (@var{x}, @var{dim}) |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
22 ## Return a vector with the minimum, first quartile, median, third quartile, |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
23 ## maximum, mean, standard deviation, skewness, and kurtosis of the elements of |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
24 ## the vector @var{x}. |
3200 | 25 ## |
20174
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
26 ## If @var{x} is a matrix, calculate statistics over the first non-singleton |
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
27 ## dimension. |
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
28 ## |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
29 ## If the optional argument @var{dim} is given, operate along this dimension. |
12575
d0b799dafede
Grammarcheck files for 3.4.1 release.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
30 ## @seealso{min, max, median, mean, std, skewness, kurtosis} |
3453 | 31 ## @end deftypefn |
3426 | 32 |
5428 | 33 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3456 | 34 ## Description: Compute basic statistics |
3200 | 35 |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
36 function stats = statistics (x, dim) |
3426 | 37 |
4885 | 38 if (nargin != 1 && nargin != 2) |
6046 | 39 print_usage (); |
3200 | 40 endif |
41 | |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
42 if (! (isnumeric (x) || islogical (x))) |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
43 error ("statistics: X must be a numeric vector or matrix"); |
10669
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
44 endif |
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
45 |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
46 nd = ndims (x); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
47 sz = size (x); |
4885 | 48 if (nargin != 2) |
4886 | 49 ## Find the first non-singleton dimension. |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
50 (dim = find (sz > 1, 1)) || (dim = 1); |
4885 | 51 else |
13279
984359717d71
Use common code idiom for checking whether a double value is an integer.
Rik <octave@nomad.inbox5.com>
parents:
12656
diff
changeset
|
52 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
|
53 || !(1 <= dim && dim <= nd)) |
10669
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
54 error ("statistics: DIM must be an integer and a valid dimension"); |
3200 | 55 endif |
4885 | 56 endif |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
57 |
10669
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
58 if (sz(dim) < 2) |
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
59 error ("statistics: dimension of X is too small (<2)"); |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
60 endif |
4885 | 61 |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
62 emp_inv = quantile (x, [0.25; 0.5; 0.75], dim, 7); |
4885 | 63 |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
64 stats = cat (dim, min (x, [], dim), emp_inv, max (x, [], dim), mean (x, dim), |
18956
ab5983dc0587
statistics.m: Fix regression caused by change in skewness, kurtosis functions.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
65 std (x, [], dim), skewness (x, [], dim), kurtosis (x, [], dim)); |
3426 | 66 |
3200 | 67 endfunction |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
68 |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
69 |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
70 %!test |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
71 %! x = rand (7,5); |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
72 %! s = statistics (x); |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
73 %! assert (min (x), s(1,:), eps); |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
74 %! assert (median (x), s(3,:), eps); |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
75 %! assert (max (x), s(5,:), eps); |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
76 %! assert (mean (x), s(6,:), eps); |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
77 %! assert (std (x), s(7,:), eps); |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
78 %! assert (skewness (x), s(8,:), eps); |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
79 %! assert (kurtosis (x), s(9,:), eps); |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
80 |
18956
ab5983dc0587
statistics.m: Fix regression caused by change in skewness, kurtosis functions.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
81 %! x = rand (7,5); |
ab5983dc0587
statistics.m: Fix regression caused by change in skewness, kurtosis functions.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
82 %! s = statistics (x, 2); |
ab5983dc0587
statistics.m: Fix regression caused by change in skewness, kurtosis functions.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
83 %! assert (min (x, [], 2), s(:,1), eps); |
ab5983dc0587
statistics.m: Fix regression caused by change in skewness, kurtosis functions.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
84 %! assert (median (x, 2), s(:,3), eps); |
ab5983dc0587
statistics.m: Fix regression caused by change in skewness, kurtosis functions.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
85 %! assert (max (x, [], 2), s(:,5), eps); |
ab5983dc0587
statistics.m: Fix regression caused by change in skewness, kurtosis functions.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
86 %! assert (mean (x, 2), s(:,6), eps); |
ab5983dc0587
statistics.m: Fix regression caused by change in skewness, kurtosis functions.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
87 %! assert (std (x, [], 2), s(:,7), eps); |
ab5983dc0587
statistics.m: Fix regression caused by change in skewness, kurtosis functions.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
88 %! assert (skewness (x, [], 2), s(:,8), eps); |
ab5983dc0587
statistics.m: Fix regression caused by change in skewness, kurtosis functions.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
89 %! assert (kurtosis (x, [], 2), s(:,9), eps); |
ab5983dc0587
statistics.m: Fix regression caused by change in skewness, kurtosis functions.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
90 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
91 ## Test input validation |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
92 %!error statistics () |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
93 %!error statistics (1, 2, 3) |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
94 %!error statistics (['A'; 'B']) |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
95 %!error statistics (1, ones (2,2)) |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
96 %!error statistics (1, 1.5) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
97 %!error statistics (1, 0) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
98 %!error statistics (1, 3) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
99 %!error statistics (1) |