Mercurial > octave-nkf
annotate scripts/statistics/base/kurtosis.m @ 10669:cab3b148d4e4
Improve validation of input arguments for base statistics functions.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Thu, 27 May 2010 20:12:51 -0700 |
parents | 95c3e38098bf |
children | 693e22af08ae |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1996, 1997, 1998, 1999, 2000, 2002, 2004, 2005, 2006, |
8920 | 2 ## 2007, 2008, 2009 John W. Eaton |
3200 | 3 ## |
4 ## This file is part of Octave. | |
5 ## | |
6 ## Octave is free software; you can redistribute it and/or modify it | |
7 ## under the terms of the GNU General Public License as published by | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
3200 | 10 ## |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
15 ## | |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
3200 | 19 |
3367 | 20 ## -*- texinfo -*- |
4885 | 21 ## @deftypefn {Function File} {} kurtosis (@var{x}, @var{dim}) |
3499 | 22 ## If @var{x} is a vector of length @math{N}, return the kurtosis |
3367 | 23 ## @tex |
24 ## $$ | |
25 ## {\rm kurtosis} (x) = {1\over N \sigma(x)^4} \sum_{i=1}^N (x_i-\bar{x})^4 - 3 | |
26 ## $$ | |
6754 | 27 ## where $\bar{x}$ is the mean value of $x$. |
3367 | 28 ## @end tex |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7411
diff
changeset
|
29 ## @ifnottex |
3426 | 30 ## |
3367 | 31 ## @example |
32 ## kurtosis (x) = N^(-1) std(x)^(-4) sum ((x - mean(x)).^4) - 3 | |
33 ## @end example | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7411
diff
changeset
|
34 ## @end ifnottex |
3426 | 35 ## |
3367 | 36 ## @noindent |
4885 | 37 ## of @var{x}. If @var{x} is a matrix, return the kurtosis over the |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
38 ## first non-singleton dimension. The optional argument @var{dim} |
4885 | 39 ## can be given to force the kurtosis to be given over that |
40 ## dimension. | |
3367 | 41 ## @end deftypefn |
3200 | 42 |
5428 | 43 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3200 | 44 ## Created: 29 July 1994 |
45 ## Adapted-By: jwe | |
46 | |
4885 | 47 function retval = kurtosis (x, dim) |
3200 | 48 |
4885 | 49 if (nargin != 1 && nargin != 2) |
6046 | 50 print_usage (); |
3200 | 51 endif |
52 | |
10669
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
53 if (!ismatrix(x) || ischar(x)) |
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
54 error ("kurtosis: X must be a numeric matrix or vector"); |
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
55 endif |
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
56 |
4885 | 57 nd = ndims (x); |
58 sz = size (x); | |
59 if (nargin != 2) | |
4886 | 60 ## 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
|
61 dim = find (sz > 1, 1); |
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
62 if (isempty (dim)) |
4885 | 63 dim = 1; |
3200 | 64 endif |
65 else | |
10669
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
66 if (!(isscalar (dim) && dim == round (dim)) || |
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
67 !(1 <= dim && dim <= nd)) |
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
68 error ("kurtosis: DIM must be an integer and a valid dimension"); |
4885 | 69 endif |
70 endif | |
71 | |
4886 | 72 c = sz(dim); |
73 sz(dim) = 1; | |
4885 | 74 idx = ones (1, nd); |
4886 | 75 idx(dim) = c; |
4885 | 76 x = x - repmat (mean (x, dim), idx); |
77 retval = zeros (sz); | |
78 s = std (x, [], dim); | |
79 x = sum(x.^4, dim); | |
80 ind = find (s > 0); | |
4886 | 81 retval(ind) = x(ind) ./ (c * s(ind) .^ 4) - 3; |
4885 | 82 |
3200 | 83 endfunction |
7411 | 84 |
85 %!test | |
86 %! x = [-1; 0; 0; 0; 1]; | |
87 %! y = [x, 2*x]; | |
88 %! assert(all (abs (kurtosis (y) - [-1.4, -1.4]) < sqrt (eps))); | |
89 | |
90 %!error kurtosis (); | |
91 | |
92 %!error kurtosis (1, 2, 3); | |
93 |