Mercurial > octave-nkf
annotate scripts/statistics/base/skewness.m @ 14138:72c96de7a403 stable
maint: update copyright notices for 2012
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 02 Jan 2012 14:25:41 -0500 |
parents | 984359717d71 |
children | 4d917a6a858b |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
13279
diff
changeset
|
1 ## Copyright (C) 1996-2012 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. |
12575
d0b799dafede
Grammarcheck files for 3.4.1 release.
Rik <octave@nomad.inbox5.com>
parents:
11587
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 | |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
54 if (! (isnumeric (x) || islogical (x))) |
11436
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. |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
62 (dim = find (sz > 1, 1)) || (dim = 1); |
3200 | 63 else |
13279
984359717d71
Use common code idiom for checking whether a double value is an integer.
Rik <octave@nomad.inbox5.com>
parents:
12656
diff
changeset
|
64 if (!(isscalar (dim) && dim == fix (dim)) |
11149
fe3c3dfc07eb
style fix: break lines before && and ||, not after
John W. Eaton <jwe@octave.org>
parents:
10821
diff
changeset
|
65 || !(1 <= dim && dim <= nd)) |
10669
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
66 error ("skewness: DIM must be an integer and a valid dimension"); |
4885 | 67 endif |
68 endif | |
69 | |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
70 n = sz(dim); |
4886 | 71 sz(dim) = 1; |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
72 x = center (x, dim); # center also promotes integer to double for next line |
11401
4c335d398c8d
skewness.m: make returned value have the same class as the input
John W. Eaton <jwe@octave.org>
parents:
11149
diff
changeset
|
73 retval = zeros (sz, class (x)); |
4885 | 74 s = std (x, [], dim); |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
75 idx = find (s > 0); |
4885 | 76 x = sum (x .^ 3, dim); |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
77 retval(idx) = x(idx) ./ (n * s(idx) .^ 3); |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
78 |
3200 | 79 endfunction |
7411 | 80 |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
81 |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11401
diff
changeset
|
82 %!assert(skewness ([-1,0,1]), 0); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11401
diff
changeset
|
83 %!assert(skewness ([-2,0,1]) < 0); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11401
diff
changeset
|
84 %!assert(skewness ([-1,0,2]) > 0); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11401
diff
changeset
|
85 %!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
|
86 %!test |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11401
diff
changeset
|
87 %! x = [0; 0; 0; 1]; |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11401
diff
changeset
|
88 %! y = [x, 2*x]; |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11401
diff
changeset
|
89 %! assert(all (abs (skewness (y) - [0.75, 0.75]) < sqrt (eps))); |
7411 | 90 |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
91 %!assert (skewness (single(1)), single(0)); |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
92 |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11401
diff
changeset
|
93 %% Test input validation |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11401
diff
changeset
|
94 %!error skewness () |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11401
diff
changeset
|
95 %!error skewness (1, 2, 3) |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
96 %!error skewness (['A'; 'B']) |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11401
diff
changeset
|
97 %!error skewness (1, ones(2,2)) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11401
diff
changeset
|
98 %!error skewness (1, 1.5) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11401
diff
changeset
|
99 %!error skewness (1, 0) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11401
diff
changeset
|
100 %!error skewness (1, 3) |