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 -*- |
|
21 ## @deftypefn {Function File} {} skewness (@var{x}) |
|
22 ## If @var{x} is a vector of length @var{N}, return the skewness |
|
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 |
|
31 ## |
|
32 ## @example |
|
33 ## skewness (x) = N^(-1) std(x)^(-3) sum ((x - mean(x)).^3) |
|
34 ## @end example |
|
35 ## @end ifinfo |
|
36 ## |
|
37 ## @noindent |
|
38 ## of @var{x}. If @var{x} is a matrix, return the row vector containing |
|
39 ## the skewness of each column. |
|
40 ## @end deftypefn |
3200
|
41 |
|
42 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
43 ## Created: 29 July 1994 |
|
44 ## Adapted-By: jwe |
|
45 |
|
46 function retval = skewness (x) |
|
47 |
|
48 if (nargin != 1) |
|
49 usage ("skewness (x)"); |
|
50 endif |
|
51 |
|
52 if (is_vector (x)) |
|
53 x = x - mean (x); |
|
54 if (! any (x)) |
|
55 retval = 0; |
|
56 else |
|
57 retval = sum (x .^ 3) / (length (x) * std (x) ^ 3); |
|
58 endif |
|
59 elseif (is_matrix (x)) |
|
60 [nr, nc] = size (x); |
|
61 x = x - ones (nr, 1) * mean (x); |
|
62 retval = zeros (1, nc); |
|
63 s = std (x); |
|
64 ind = find (s > 0); |
|
65 retval (ind) = sum (x (:, ind) .^ 3) ./ (nr * s (ind) .^ 3); |
|
66 else |
|
67 error ("skewness: x has to be a matrix or a vector."); |
|
68 endif |
|
69 |
|
70 endfunction |