3200
|
1 ## Copyright (C) 1996, 1997 John W. Eaton |
|
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 2, or (at your option) |
|
8 ## 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, write to the Free |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
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 ## $$ |
|
28 ## @end tex |
|
29 ## @end iftex |
|
30 ## @ifinfo |
3426
|
31 ## |
3367
|
32 ## @example |
|
33 ## kurtosis (x) = N^(-1) std(x)^(-4) sum ((x - mean(x)).^4) - 3 |
|
34 ## @end example |
|
35 ## @end ifinfo |
3426
|
36 ## |
3367
|
37 ## @noindent |
4885
|
38 ## of @var{x}. If @var{x} is a matrix, return the kurtosis over the |
|
39 ## first non-singleton dimension. The optional argument @var{dim} |
|
40 ## can be given to force the kurtosis to be given over that |
|
41 ## dimension. |
3367
|
42 ## @end deftypefn |
3200
|
43 |
5428
|
44 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3200
|
45 ## Created: 29 July 1994 |
|
46 ## Adapted-By: jwe |
|
47 |
4885
|
48 function retval = kurtosis (x, dim) |
3200
|
49 |
4885
|
50 if (nargin != 1 && nargin != 2) |
|
51 usage ("kurtosis (x, dim)"); |
3200
|
52 endif |
|
53 |
4885
|
54 nd = ndims (x); |
|
55 sz = size (x); |
|
56 if (nargin != 2) |
4886
|
57 ## Find the first non-singleton dimension. |
4885
|
58 dim = 1; |
4886
|
59 while (dim < nd + 1 && sz(dim) == 1) |
4885
|
60 dim = dim + 1; |
|
61 endwhile |
|
62 if (dim > nd) |
|
63 dim = 1; |
3200
|
64 endif |
|
65 else |
4886
|
66 if (! (isscalar (dim) && dim == round (dim)) |
|
67 && dim > 0 |
|
68 && dim < (nd + 1)) |
4885
|
69 error ("kurtosis: dim must be an integer and valid dimension"); |
|
70 endif |
|
71 endif |
|
72 |
|
73 if (! ismatrix (x)) |
3458
|
74 error ("kurtosis: x has to be a matrix or a vector"); |
3200
|
75 endif |
|
76 |
4886
|
77 c = sz(dim); |
|
78 sz(dim) = 1; |
4885
|
79 idx = ones (1, nd); |
4886
|
80 idx(dim) = c; |
4885
|
81 x = x - repmat (mean (x, dim), idx); |
|
82 retval = zeros (sz); |
|
83 s = std (x, [], dim); |
|
84 x = sum(x.^4, dim); |
|
85 ind = find (s > 0); |
4886
|
86 retval(ind) = x(ind) ./ (c * s(ind) .^ 4) - 3; |
4885
|
87 |
3200
|
88 endfunction |