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 |
|
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
|
18 ## 02111-1307, USA. |
|
19 |
3367
|
20 ## -*- texinfo -*- |
4885
|
21 ## @deftypefn {Function File} {} skewness (@var{x}, @var{dim}) |
3499
|
22 ## If @var{x} is a vector of length @math{n}, return the skewness |
3367
|
23 ## @iftex |
|
24 ## @tex |
|
25 ## $$ |
|
26 ## {\rm skewness} (x) = {1\over N \sigma(x)^3} \sum_{i=1}^N (x_i-\bar{x})^3 |
|
27 ## $$ |
|
28 ## @end tex |
|
29 ## @end iftex |
|
30 ## @ifinfo |
3426
|
31 ## |
3367
|
32 ## @example |
|
33 ## skewness (x) = N^(-1) std(x)^(-3) sum ((x - mean(x)).^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 skewness along the |
|
39 ## first non-singleton dimension of the matrix. If the optional |
|
40 ## @var{dim} argument is given, operate along this dimension. |
3367
|
41 ## @end deftypefn |
3200
|
42 |
|
43 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
44 ## Created: 29 July 1994 |
|
45 ## Adapted-By: jwe |
|
46 |
4885
|
47 function retval = skewness (x, dim) |
3200
|
48 |
4885
|
49 if (nargin != 1 && nargin != 2) |
|
50 usage ("skewness (x, dim)"); |
3200
|
51 endif |
|
52 |
4885
|
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; |
3200
|
63 endif |
|
64 else |
4885
|
65 if (! (isscalar (dim) && dim == round (dim)) && 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)) |
3458
|
72 error ("skewness: x has to be a matrix or a vector"); |
3200
|
73 endif |
|
74 |
4885
|
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 |
3200
|
86 endfunction |