1 ## Copyright (C) 1996-2012 John W. Eaton
3 ## This file is part of Octave.
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.
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.
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/>.
20 ## @deftypefn {Function File} {} kurtosis (@var{x})
21 ## @deftypefnx {Function File} {} kurtosis (@var{x}, @var{dim})
22 ## Compute the kurtosis of the elements of the vector @var{x}.
25 ## {\rm kurtosis} (x) = {1\over N \sigma^4} \sum_{i=1}^N (x_i-\bar{x})^4 - 3
27 ## where $\bar{x}$ is the mean value of $x$.
32 ## kurtosis (x) = 1/N std(x)^(-4) sum ((x - mean(x)).^4) - 3
36 ## If @var{x} is a matrix, return the kurtosis over the
37 ## first non-singleton dimension of the matrix. If the optional
38 ## @var{dim} argument is given, operate along this dimension.
40 ## Note: The definition of kurtosis above yields a kurtosis of zero for the
41 ## stdnormal distribution and is sometimes referred to as "excess kurtosis".
42 ## To calculate kurtosis without the normalization factor of @math{-3} use
43 ## @code{moment (@var{x}, 4, 'c') / std (@var{x})^4}.
44 ## @seealso{var, skewness, moment}
47 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
48 ## Created: 29 July 1994
51 function retval = kurtosis (x, dim)
53 if (nargin != 1 && nargin != 2)
57 if (! (isnumeric (x) || islogical (x)))
58 error ("kurtosis: X must be a numeric vector or matrix");
64 ## Find the first non-singleton dimension.
65 (dim = find (sz > 1, 1)) || (dim = 1);
67 if (!(isscalar (dim) && dim == fix (dim))
68 || !(1 <= dim && dim <= nd))
69 error ("kurtosis: DIM must be an integer and a valid dimension");
75 x = center (x, dim); # center also promotes integer to double for next line
76 retval = zeros (sz, class (x));
80 retval(idx) = x(idx) ./ (n * s(idx) .^ 4) - 3;
86 %! x = [-1; 0; 0; 0; 1];
88 %! assert (kurtosis (y), [-1.4, -1.4], sqrt (eps));
90 %!assert (kurtosis (single(1)), single(0));
92 %% Test input validation
94 %!error kurtosis (1, 2, 3)
95 %!error kurtosis (['A'; 'B'])
96 %!error kurtosis (1, ones(2,2))
97 %!error kurtosis (1, 1.5)
98 %!error kurtosis (1, 0)
99 %!error kurtosis (1, 3)