]> Creatis software - CreaPhase.git/blob - octave_packages/m/statistics/base/statistics.m
update packages
[CreaPhase.git] / octave_packages / m / statistics / base / statistics.m
1 ## Copyright (C) 1995-2012 Kurt Hornik
2 ##
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
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn  {Function File} {} statistics (@var{x})
21 ## @deftypefnx {Function File} {} statistics (@var{x}, @var{dim})
22 ## Return a vector with the minimum, first quartile, median, third quartile,
23 ## maximum, mean, standard deviation, skewness, and kurtosis of the elements of
24 ## the vector @var{x}.
25 ##
26 ## If @var{x} is a matrix, calculate statistics over the first
27 ## non-singleton dimension.
28 ## If the optional argument @var{dim} is given, operate along this dimension.
29 ## @seealso{min, max, median, mean, std, skewness, kurtosis}
30 ## @end deftypefn
31
32 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
33 ## Description: Compute basic statistics
34
35 function stats = statistics (x, dim)
36
37   if (nargin != 1 && nargin != 2)
38     print_usage ();
39   endif
40
41   if (! (isnumeric (x) || islogical (x)))
42     error ("statistics: X must be a numeric vector or matrix");
43   endif
44
45   nd = ndims (x);
46   sz = size (x);
47   if (nargin != 2)
48     ## Find the first non-singleton dimension.
49     (dim = find (sz > 1, 1)) || (dim = 1);
50   else
51     if (!(isscalar (dim) && dim == fix (dim))
52         || !(1 <= dim && dim <= nd))
53       error ("statistics: DIM must be an integer and a valid dimension");
54     endif
55   endif
56
57   if (sz(dim) < 2)
58     error ("statistics: dimension of X is too small (<2)");
59   endif
60
61   emp_inv = quantile (x, [0.25; 0.5; 0.75], dim, 7);
62
63   stats = cat (dim, min (x, [], dim), emp_inv, max (x, [], dim), mean (x, dim),
64                std (x, [], dim), skewness (x, dim), kurtosis (x, dim));
65
66 endfunction
67
68
69 %!test
70 %! x = rand (7,5);
71 %! s = statistics (x);
72 %! assert (min (x), s(1,:), eps);
73 %! assert (median (x), s(3,:), eps);
74 %! assert (max (x), s(5,:), eps);
75 %! assert (mean (x), s(6,:), eps);
76 %! assert (std (x), s(7,:), eps);
77 %! assert (skewness (x), s(8,:), eps);
78 %! assert (kurtosis (x), s(9,:), eps);
79
80 %% Test input validation
81 %!error statistics ()
82 %!error statistics (1, 2, 3)
83 %!error statistics (['A'; 'B'])
84 %!error statistics (1, ones(2,2))
85 %!error statistics (1, 1.5)
86 %!error statistics (1, 0)
87 %!error statistics (1, 3)
88 %!error statistics (1)
89