Mercurial > hg > octave-lyh
annotate scripts/statistics/base/statistics.m @ 11540:b0ef6f28e09a
deprecate krylovb function
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sat, 15 Jan 2011 03:40:32 -0500 |
parents | fd0a3ac60b0e |
children | c792872f8942 |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 1995-2011 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 -*- |
10669
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
20 ## @deftypefn {Function File} {} statistics (@var{x}) |
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
21 ## @deftypefnx {Function File} {} 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 ## |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
26 ## If @var{x} is a matrix, calculate statistics over the first |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
27 ## non-singleton dimension. |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
28 ## 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
|
29 ## @seealso{min,max,median,mean,std,skewness,kurtosis} |
3453 | 30 ## @end deftypefn |
3426 | 31 |
5428 | 32 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3456 | 33 ## Description: Compute basic statistics |
3200 | 34 |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
35 function stats = statistics (x, dim) |
3426 | 36 |
4885 | 37 if (nargin != 1 && nargin != 2) |
6046 | 38 print_usage (); |
3200 | 39 endif |
40 | |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
41 if (!isnumeric(x)) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
42 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
|
43 endif |
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
44 |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
45 nd = ndims (x); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
46 sz = size (x); |
4885 | 47 if (nargin != 2) |
4886 | 48 ## Find the first non-singleton dimension. |
10669
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
49 dim = find (sz > 1, 1); |
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
50 if (isempty (dim)) |
4885 | 51 dim = 1; |
52 endif | |
53 else | |
11149
fe3c3dfc07eb
style fix: break lines before && and ||, not after
John W. Eaton <jwe@octave.org>
parents:
10669
diff
changeset
|
54 if (!(isscalar (dim) && dim == round (dim)) |
fe3c3dfc07eb
style fix: break lines before && and ||, not after
John W. Eaton <jwe@octave.org>
parents:
10669
diff
changeset
|
55 || !(1 <= dim && dim <= nd)) |
10669
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
56 error ("statistics: DIM must be an integer and a valid dimension"); |
3200 | 57 endif |
4885 | 58 endif |
59 | |
10669
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
60 if (sz(dim) < 2) |
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
61 error ("statistics: dimension of X is too small (<2)"); |
4885 | 62 endif |
63 | |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
64 emp_inv = quantile (x, [0.25; 0.5; 0.75], dim, 7); |
4885 | 65 |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
66 stats = cat (dim, min (x, [], dim), emp_inv, max (x, [], dim), mean (x, dim), |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
67 std (x, [], dim), skewness (x, dim), kurtosis (x, dim)); |
3426 | 68 |
3200 | 69 endfunction |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
70 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
71 %!test |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
72 %! x = rand(7,5); |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
73 %! s = statistics (x); |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
74 %! m = median (x); |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
75 %! assert (m, s(3,:), eps); |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
76 |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
77 %% Test input validation |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
78 %!error statistics () |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
79 %!error statistics (1, 2, 3) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
80 %!error statistics ([true true]) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
81 %!error statistics (1, ones(2,2)) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
82 %!error statistics (1, 1.5) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
83 %!error statistics (1, 0) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
84 %!error statistics (1, 3) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
85 %!error statistics (1) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
86 |