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} {} 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 |
5428
|
43 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3200
|
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) |
6046
|
50 print_usage (); |
3200
|
51 endif |
|
52 |
4885
|
53 nd = ndims (x); |
|
54 sz = size (x); |
|
55 if (nargin != 2) |
4886
|
56 ## Find the first non-singleton dimension. |
4885
|
57 dim = 1; |
4886
|
58 while (dim < nd + 1 && sz(dim) == 1) |
4885
|
59 dim = dim + 1; |
|
60 endwhile |
|
61 if (dim > nd) |
|
62 dim = 1; |
3200
|
63 endif |
|
64 else |
4886
|
65 if (! (isscalar (dim) && dim == round (dim)) |
|
66 && dim > 0 |
|
67 && dim < (nd + 1)) |
4885
|
68 error ("skewness: dim must be an integer and valid dimension"); |
|
69 endif |
|
70 endif |
|
71 |
|
72 if (! ismatrix (x)) |
3458
|
73 error ("skewness: x has to be a matrix or a vector"); |
3200
|
74 endif |
|
75 |
4886
|
76 c = sz(dim); |
4885
|
77 idx = ones (1, nd); |
|
78 idx (dim) = c; |
|
79 x = x - repmat (mean (x, dim), idx); |
4886
|
80 sz(dim) = 1; |
4885
|
81 retval = zeros (sz); |
|
82 s = std (x, [], dim); |
|
83 ind = find (s > 0); |
|
84 x = sum (x .^ 3, dim); |
4886
|
85 retval(ind) = x(ind) ./ (c * s(ind) .^ 3); |
4885
|
86 |
3200
|
87 endfunction |