Mercurial > octave-nkf
annotate scripts/statistics/base/kurtosis.m @ 14399:d931d9b458fc
kurtosis.m: Make docstring equation prettier in Info format
* kurtosis.m: Make docstring equation prettier in Info format
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Thu, 23 Feb 2012 20:07:25 -0800 |
parents | f3d52523cde1 |
children | f0e777cf348f |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
12656
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:
11149
diff
changeset
|
20 ## @deftypefn {Function File} {} kurtosis (@var{x}) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
21 ## @deftypefnx {Function File} {} kurtosis (@var{x}, @var{dim}) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
22 ## Compute the kurtosis 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:
11149
diff
changeset
|
25 ## {\rm kurtosis} (x) = {1\over N \sigma^4} \sum_{i=1}^N (x_i-\bar{x})^4 - 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 |
14399
d931d9b458fc
kurtosis.m: Make docstring equation prettier in Info format
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
32 ## @group |
d931d9b458fc
kurtosis.m: Make docstring equation prettier in Info format
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
33 ## 1 sum ((x - mean(x)).^4) |
d931d9b458fc
kurtosis.m: Make docstring equation prettier in Info format
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
34 ## kurtosis (x) = --- * ---------------------- - 3 |
d931d9b458fc
kurtosis.m: Make docstring equation prettier in Info format
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
35 ## N std(x)^4 |
d931d9b458fc
kurtosis.m: Make docstring equation prettier in Info format
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
36 ## @end group |
3367 | 37 ## @end example |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10669
diff
changeset
|
38 ## |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7411
diff
changeset
|
39 ## @end ifnottex |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
40 ## If @var{x} is a matrix, return the kurtosis over the |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
41 ## first non-singleton dimension of the matrix. If the optional |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
42 ## @var{dim} argument is given, operate along this dimension. |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
43 ## |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
44 ## Note: The definition of kurtosis above yields a kurtosis of zero for the |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
45 ## stdnormal distribution and is sometimes referred to as "excess kurtosis". |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
46 ## To calculate kurtosis without the normalization factor of @math{-3} use |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
47 ## @code{moment (@var{x}, 4, 'c') / std (@var{x})^4}. |
12575
d0b799dafede
Grammarcheck files for 3.4.1 release.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
48 ## @seealso{var, skewness, moment} |
3367 | 49 ## @end deftypefn |
3200 | 50 |
5428 | 51 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3200 | 52 ## Created: 29 July 1994 |
53 ## Adapted-By: jwe | |
54 | |
4885 | 55 function retval = kurtosis (x, dim) |
3200 | 56 |
4885 | 57 if (nargin != 1 && nargin != 2) |
6046 | 58 print_usage (); |
3200 | 59 endif |
60 | |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
61 if (! (isnumeric (x) || islogical (x))) |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
62 error ("kurtosis: 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
|
63 endif |
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
64 |
4885 | 65 nd = ndims (x); |
66 sz = size (x); | |
67 if (nargin != 2) | |
4886 | 68 ## Find the first non-singleton dimension. |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
69 (dim = find (sz > 1, 1)) || (dim = 1); |
3200 | 70 else |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
71 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
|
72 || !(1 <= dim && dim <= nd)) |
10669
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
73 error ("kurtosis: DIM must be an integer and a valid dimension"); |
4885 | 74 endif |
75 endif | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
76 |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
77 n = sz(dim); |
4886 | 78 sz(dim) = 1; |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
79 x = center (x, dim); # center also promotes integer to double for next line |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
80 retval = zeros (sz, class (x)); |
4885 | 81 s = std (x, [], dim); |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
82 idx = find (s > 0); |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
83 x = sum (x.^4, dim); |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
84 retval(idx) = x(idx) ./ (n * s(idx) .^ 4) - 3; |
4885 | 85 |
3200 | 86 endfunction |
7411 | 87 |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
88 |
7411 | 89 %!test |
90 %! x = [-1; 0; 0; 0; 1]; | |
91 %! y = [x, 2*x]; | |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
92 %! assert (kurtosis (y), [-1.4, -1.4], sqrt (eps)); |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
93 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
94 %!assert (kurtosis (single (1)), single (0)) |
7411 | 95 |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
96 %% Test input validation |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
97 %!error kurtosis () |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
98 %!error kurtosis (1, 2, 3) |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
99 %!error kurtosis (['A'; 'B']) |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
100 %!error kurtosis (1, ones (2,2)) |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
101 %!error kurtosis (1, 1.5) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
102 %!error kurtosis (1, 0) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
103 %!error kurtosis (1, 3) |
7411 | 104 |