7017
|
1 ## Copyright (C) 1996, 1997, 1998, 1999, 2000, 2002, 2004, 2005, 2006, |
|
2 ## 2007 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 ## @iftex |
|
24 ## @tex |
|
25 ## $$ |
|
26 ## {\rm kurtosis} (x) = {1\over N \sigma(x)^4} \sum_{i=1}^N (x_i-\bar{x})^4 - 3 |
|
27 ## $$ |
6754
|
28 ## where $\bar{x}$ is the mean value of $x$. |
3367
|
29 ## @end tex |
|
30 ## @end iftex |
|
31 ## @ifinfo |
3426
|
32 ## |
3367
|
33 ## @example |
|
34 ## kurtosis (x) = N^(-1) std(x)^(-4) sum ((x - mean(x)).^4) - 3 |
|
35 ## @end example |
|
36 ## @end ifinfo |
3426
|
37 ## |
3367
|
38 ## @noindent |
4885
|
39 ## of @var{x}. If @var{x} is a matrix, return the kurtosis over the |
|
40 ## first non-singleton dimension. The optional argument @var{dim} |
|
41 ## can be given to force the kurtosis to be given over that |
|
42 ## dimension. |
3367
|
43 ## @end deftypefn |
3200
|
44 |
5428
|
45 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3200
|
46 ## Created: 29 July 1994 |
|
47 ## Adapted-By: jwe |
|
48 |
4885
|
49 function retval = kurtosis (x, dim) |
3200
|
50 |
4885
|
51 if (nargin != 1 && nargin != 2) |
6046
|
52 print_usage (); |
3200
|
53 endif |
|
54 |
4885
|
55 nd = ndims (x); |
|
56 sz = size (x); |
|
57 if (nargin != 2) |
4886
|
58 ## Find the first non-singleton dimension. |
4885
|
59 dim = 1; |
4886
|
60 while (dim < nd + 1 && sz(dim) == 1) |
4885
|
61 dim = dim + 1; |
|
62 endwhile |
|
63 if (dim > nd) |
|
64 dim = 1; |
3200
|
65 endif |
|
66 else |
4886
|
67 if (! (isscalar (dim) && dim == round (dim)) |
|
68 && dim > 0 |
|
69 && dim < (nd + 1)) |
4885
|
70 error ("kurtosis: dim must be an integer and valid dimension"); |
|
71 endif |
|
72 endif |
|
73 |
|
74 if (! ismatrix (x)) |
3458
|
75 error ("kurtosis: x has to be a matrix or a vector"); |
3200
|
76 endif |
|
77 |
4886
|
78 c = sz(dim); |
|
79 sz(dim) = 1; |
4885
|
80 idx = ones (1, nd); |
4886
|
81 idx(dim) = c; |
4885
|
82 x = x - repmat (mean (x, dim), idx); |
|
83 retval = zeros (sz); |
|
84 s = std (x, [], dim); |
|
85 x = sum(x.^4, dim); |
|
86 ind = find (s > 0); |
4886
|
87 retval(ind) = x(ind) ./ (c * s(ind) .^ 4) - 3; |
4885
|
88 |
3200
|
89 endfunction |