changelog shortlog tags changeset files revisions annotate raw

scripts/statistics/base/kurtosis.m

changeset 10289: 4b124317dc38
parent:1bf0ce0930be
author: John W. Eaton <jwe@octave.org>
date: Tue Feb 09 20:58:55 2010 -0500 (65 minutes ago)
permissions: -rw-r--r--
description: base_properties::set_children: account for hidden children
1## Copyright (C) 1996, 1997, 1998, 1999, 2000, 2002, 2004, 2005, 2006,
2## 2007, 2008, 2009 John W. Eaton
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
8## the Free Software Foundation; either version 3 of the License, or (at
9## your option) any later version.
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
17## along with Octave; see the file COPYING. If not, see
18## <http://www.gnu.org/licenses/>.
19
20## -*- texinfo -*-
21## @deftypefn {Function File} {} kurtosis (@var{x}, @var{dim})
22## If @var{x} is a vector of length @math{N}, return the kurtosis
23## @tex
24## $$
25## {\rm kurtosis} (x) = {1\over N \sigma(x)^4} \sum_{i=1}^N (x_i-\bar{x})^4 - 3
26## $$
27## where $\bar{x}$ is the mean value of $x$.
28## @end tex
29## @ifnottex
30##
31## @example
32## kurtosis (x) = N^(-1) std(x)^(-4) sum ((x - mean(x)).^4) - 3
33## @end example
34## @end ifnottex
35##
36## @noindent
37## of @var{x}. If @var{x} is a matrix, return the kurtosis over the
38## first non-singleton dimension. The optional argument @var{dim}
39## can be given to force the kurtosis to be given over that
40## dimension.
41## @end deftypefn
42
43## Author: KH <Kurt.Hornik@wu-wien.ac.at>
44## Created: 29 July 1994
45## Adapted-By: jwe
46
47function retval = kurtosis (x, dim)
48
49 if (nargin != 1 && nargin != 2)
50 print_usage ();
51 endif
52
53 nd = ndims (x);
54 sz = size (x);
55 if (nargin != 2)
56 ## Find the first non-singleton dimension.
57 dim = 1;
58 while (dim < nd + 1 && sz(dim) == 1)
59 dim = dim + 1;
60 endwhile
61 if (dim > nd)
62 dim = 1;
63 endif
64 else
65 if (! (isscalar (dim) && dim == round (dim))
66 && dim > 0
67 && dim < (nd + 1))
68 error ("kurtosis: dim must be an integer and valid dimension");
69 endif
70 endif
71
72 if (! ismatrix (x))
73 error ("kurtosis: x has to be a matrix or a vector");
74 endif
75
76 c = sz(dim);
77 sz(dim) = 1;
78 idx = ones (1, nd);
79 idx(dim) = c;
80 x = x - repmat (mean (x, dim), idx);
81 retval = zeros (sz);
82 s = std (x, [], dim);
83 x = sum(x.^4, dim);
84 ind = find (s > 0);
85 retval(ind) = x(ind) ./ (c * s(ind) .^ 4) - 3;
86
87endfunction
88
89%!test
90%! x = [-1; 0; 0; 0; 1];
91%! y = [x, 2*x];
92%! assert(all (abs (kurtosis (y) - [-1.4, -1.4]) < sqrt (eps)));
93
94%!error kurtosis ();
95
96%!error kurtosis (1, 2, 3);
97