changelog shortlog tags changeset files revisions annotate raw

scripts/statistics/base/skewness.m

changeset 10289: 4b124317dc38
parent:1bf0ce0930be
author: John W. Eaton <jwe@octave.org>
date: Tue Feb 09 20:58:55 2010 -0500 (66 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} {} skewness (@var{x}, @var{dim})
22## If @var{x} is a vector of length @math{n}, return the skewness
23## @tex
24## $$
25## {\rm skewness} (x) = {1\over N \sigma(x)^3} \sum_{i=1}^N (x_i-\bar{x})^3
26## $$
27## where $\bar{x}$ is the mean value of $x$.
28## @end tex
29## @ifnottex
30##
31## @example
32## skewness (x) = N^(-1) std(x)^(-3) sum ((x - mean(x)).^3)
33## @end example
34## @end ifnottex
35##
36## @noindent
37## of @var{x}. If @var{x} is a matrix, return the skewness along the
38## first non-singleton dimension of the matrix. If the optional
39## @var{dim} argument is given, operate along this dimension.
40## @end deftypefn
41
42## Author: KH <Kurt.Hornik@wu-wien.ac.at>
43## Created: 29 July 1994
44## Adapted-By: jwe
45
46function retval = skewness (x, dim)
47
48 if (nargin != 1 && nargin != 2)
49 print_usage ();
50 endif
51
52 nd = ndims (x);
53 sz = size (x);
54 if (nargin != 2)
55 ## Find the first non-singleton dimension.
56 dim = 1;
57 while (dim < nd + 1 && sz(dim) == 1)
58 dim = dim + 1;
59 endwhile
60 if (dim > nd)
61 dim = 1;
62 endif
63 else
64 if (! (isscalar (dim) && dim == round (dim))
65 && dim > 0
66 && dim < (nd + 1))
67 error ("skewness: dim must be an integer and valid dimension");
68 endif
69 endif
70
71 if (! ismatrix (x))
72 error ("skewness: x has to be a matrix or a vector");
73 endif
74
75 c = sz(dim);
76 idx = ones (1, nd);
77 idx (dim) = c;
78 x = x - repmat (mean (x, dim), idx);
79 sz(dim) = 1;
80 retval = zeros (sz);
81 s = std (x, [], dim);
82 ind = find (s > 0);
83 x = sum (x .^ 3, dim);
84 retval(ind) = x(ind) ./ (c * s(ind) .^ 3);
85
86endfunction
87
88%!error skewness ();
89
90%!error skewness (1, 2, 3);
91