]> Creatis software - CreaPhase.git/blob - octave_packages/statistics-1.1.3/unidstat.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / statistics-1.1.3 / unidstat.m
1 ## Copyright (C) 2006, 2007 Arno Onken <asnelt@asnelt.org>
2 ##
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
6 ## version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 ## details.
12 ##
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn {Function File} {[@var{m}, @var{v}] =} unidstat (@var{n})
18 ## Compute mean and variance of the discrete uniform distribution.
19 ##
20 ## @subheading Arguments
21 ##
22 ## @itemize @bullet
23 ## @item
24 ## @var{n} is the parameter of the discrete uniform distribution. The elements
25 ## of @var{n} must be positive natural numbers
26 ## @end itemize
27 ##
28 ## @subheading Return values
29 ##
30 ## @itemize @bullet
31 ## @item
32 ## @var{m} is the mean of the discrete uniform distribution
33 ##
34 ## @item
35 ## @var{v} is the variance of the discrete uniform distribution
36 ## @end itemize
37 ##
38 ## @subheading Example
39 ##
40 ## @example
41 ## @group
42 ## n = 1:6;
43 ## [m, v] = unidstat (n)
44 ## @end group
45 ## @end example
46 ##
47 ## @subheading References
48 ##
49 ## @enumerate
50 ## @item
51 ## Wendy L. Martinez and Angel R. Martinez. @cite{Computational Statistics
52 ## Handbook with MATLAB}. Appendix E, pages 547-557, Chapman & Hall/CRC,
53 ## 2001.
54 ##
55 ## @item
56 ## Athanasios Papoulis. @cite{Probability, Random Variables, and Stochastic
57 ## Processes}. McGraw-Hill, New York, second edition, 1984.
58 ## @end enumerate
59 ## @end deftypefn
60
61 ## Author: Arno Onken <asnelt@asnelt.org>
62 ## Description: Moments of the discrete uniform distribution
63
64 function [m, v] = unidstat (n)
65
66   # Check arguments
67   if (nargin != 1)
68     print_usage ();
69   endif
70
71   if (! isempty (n) && ! ismatrix (n))
72     error ("unidstat: n must be a numeric matrix");
73   endif
74
75   # Calculate moments
76   m = (n + 1) ./ 2;
77   v = ((n .^ 2) - 1) ./ 12;
78
79   # Continue argument check
80   k = find (! (n > 0) | ! (n < Inf) | ! (n == round (n)));
81   if (any (k))
82     m(k) = NaN;
83     v(k) = NaN;
84   endif
85
86 endfunction
87
88 %!test
89 %! n = 1:6;
90 %! [m, v] = unidstat (n);
91 %! expected_m = [1.0000, 1.5000, 2.0000, 2.5000, 3.0000, 3.5000];
92 %! expected_v = [0.0000, 0.2500, 0.6667, 1.2500, 2.0000, 2.9167];
93 %! assert (m, expected_m, 0.001);
94 %! assert (v, expected_v, 0.001);