Mercurial > octave-antonio
annotate scripts/statistics/base/skewness.m @ 11523:fd0a3ac60b0e
update copyright notices
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 14 Jan 2011 05:47:45 -0500 |
parents | e151e23f73bc |
children | c792872f8942 |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 1996-2011 John W. Eaton |
3200 | 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 | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
3200 | 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 | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
3200 | 18 |
3367 | 19 ## -*- texinfo -*- |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11401
diff
changeset
|
20 ## @deftypefn {Function File} {} skewness (@var{x}) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11401
diff
changeset
|
21 ## @deftypefnx {Function File} {} skewness (@var{x}, @var{dim}) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11401
diff
changeset
|
22 ## Compute the skewness of the elements of the vector @var{x}. |
3367 | 23 ## @tex |
24 ## $$ | |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11401
diff
changeset
|
25 ## {\rm skewness} (x) = {1\over N \sigma^3} \sum_{i=1}^N (x_i-\bar{x})^3 |
3367 | 26 ## $$ |
6754 | 27 ## where $\bar{x}$ is the mean value of $x$. |
3367 | 28 ## @end tex |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7411
diff
changeset
|
29 ## @ifnottex |
3426 | 30 ## |
3367 | 31 ## @example |
32 ## skewness (x) = N^(-1) std(x)^(-3) sum ((x - mean(x)).^3) | |
33 ## @end example | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10669
diff
changeset
|
34 ## |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7411
diff
changeset
|
35 ## @end ifnottex |
3426 | 36 ## |
3367 | 37 ## @noindent |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11401
diff
changeset
|
38 ## If @var{x} is a matrix, return the skewness along the |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
39 ## first non-singleton dimension of the matrix. If the optional |
4885 | 40 ## @var{dim} argument is given, operate along this dimension. |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11401
diff
changeset
|
41 ## @seealso{var,kurtosis,moment} |
3367 | 42 ## @end deftypefn |
3200 | 43 |
5428 | 44 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3200 | 45 ## Created: 29 July 1994 |
46 ## Adapted-By: jwe | |
47 | |
4885 | 48 function retval = skewness (x, dim) |
3200 | 49 |
4885 | 50 if (nargin != 1 && nargin != 2) |
6046 | 51 print_usage (); |
3200 | 52 endif |
53 | |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11401
diff
changeset
|
54 if (!isnumeric(x)) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11401
diff
changeset
|
55 error ("skewness: X must be a numeric vector or matrix"); |
10669
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
56 endif |
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
57 |
4885 | 58 nd = ndims (x); |
59 sz = size (x); | |
60 if (nargin != 2) | |
4886 | 61 ## Find the first non-singleton dimension. |
10669
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
62 dim = find (sz > 1, 1); |
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
63 if (isempty (dim)) |
4885 | 64 dim = 1; |
3200 | 65 endif |
66 else | |
11149
fe3c3dfc07eb
style fix: break lines before && and ||, not after
John W. Eaton <jwe@octave.org>
parents:
10821
diff
changeset
|
67 if (!(isscalar (dim) && dim == round (dim)) |
fe3c3dfc07eb
style fix: break lines before && and ||, not after
John W. Eaton <jwe@octave.org>
parents:
10821
diff
changeset
|
68 || !(1 <= dim && dim <= nd)) |
10669
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
69 error ("skewness: DIM must be an integer and a valid dimension"); |
4885 | 70 endif |
71 endif | |
72 | |
4886 | 73 c = sz(dim); |
4885 | 74 idx = ones (1, nd); |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11401
diff
changeset
|
75 idx(dim) = c; |
4885 | 76 x = x - repmat (mean (x, dim), idx); |
4886 | 77 sz(dim) = 1; |
11401
4c335d398c8d
skewness.m: make returned value have the same class as the input
John W. Eaton <jwe@octave.org>
parents:
11149
diff
changeset
|
78 retval = zeros (sz, class (x)); |
4885 | 79 s = std (x, [], dim); |
80 ind = find (s > 0); | |
81 x = sum (x .^ 3, dim); | |
4886 | 82 retval(ind) = x(ind) ./ (c * s(ind) .^ 3); |
4885 | 83 |
3200 | 84 endfunction |
7411 | 85 |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11401
diff
changeset
|
86 %!assert(skewness ([-1,0,1]), 0); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11401
diff
changeset
|
87 %!assert(skewness ([-2,0,1]) < 0); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11401
diff
changeset
|
88 %!assert(skewness ([-1,0,2]) > 0); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11401
diff
changeset
|
89 %!assert(skewness ([-3,0,1]) == -1*skewness([-1,0,3])); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11401
diff
changeset
|
90 %!test |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11401
diff
changeset
|
91 %! x = [0; 0; 0; 1]; |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11401
diff
changeset
|
92 %! y = [x, 2*x]; |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11401
diff
changeset
|
93 %! assert(all (abs (skewness (y) - [0.75, 0.75]) < sqrt (eps))); |
7411 | 94 |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11401
diff
changeset
|
95 %% Test input validation |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11401
diff
changeset
|
96 %!error skewness () |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11401
diff
changeset
|
97 %!error skewness (1, 2, 3) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11401
diff
changeset
|
98 %!error skewness ([true true]) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11401
diff
changeset
|
99 %!error skewness (1, ones(2,2)) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11401
diff
changeset
|
100 %!error skewness (1, 1.5) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11401
diff
changeset
|
101 %!error skewness (1, 0) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11401
diff
changeset
|
102 %!error skewness (1, 3) |