]> Creatis software - CreaPhase.git/blob - octave_packages/statistics-1.1.3/hygestat.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / statistics-1.1.3 / hygestat.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{mn}, @var{v}] =} hygestat (@var{t}, @var{m}, @var{n})
18 ## Compute mean and variance of the hypergeometric distribution.
19 ##
20 ## @subheading Arguments
21 ##
22 ## @itemize @bullet
23 ## @item
24 ## @var{t} is the total size of the population of the hypergeometric
25 ## distribution. The elements of @var{t} must be positive natural numbers
26 ##
27 ## @item
28 ## @var{m} is the number of marked items of the hypergeometric distribution.
29 ## The elements of @var{m} must be natural numbers
30 ##
31 ## @item
32 ## @var{n} is the size of the drawn sample of the hypergeometric
33 ## distribution. The elements of @var{n} must be positive natural numbers
34 ## @end itemize
35 ## @var{t}, @var{m}, and @var{n} must be of common size or scalar
36 ##
37 ## @subheading Return values
38 ##
39 ## @itemize @bullet
40 ## @item
41 ## @var{mn} is the mean of the hypergeometric distribution
42 ##
43 ## @item
44 ## @var{v} is the variance of the hypergeometric distribution
45 ## @end itemize
46 ##
47 ## @subheading Examples
48 ##
49 ## @example
50 ## @group
51 ## t = 4:9;
52 ## m = 0:5;
53 ## n = 1:6;
54 ## [mn, v] = hygestat (t, m, n)
55 ## @end group
56 ##
57 ## @group
58 ## [mn, v] = hygestat (t, m, 2)
59 ## @end group
60 ## @end example
61 ##
62 ## @subheading References
63 ##
64 ## @enumerate
65 ## @item
66 ## Wendy L. Martinez and Angel R. Martinez. @cite{Computational Statistics
67 ## Handbook with MATLAB}. Appendix E, pages 547-557, Chapman & Hall/CRC,
68 ## 2001.
69 ##
70 ## @item
71 ## Athanasios Papoulis. @cite{Probability, Random Variables, and Stochastic
72 ## Processes}. McGraw-Hill, New York, second edition, 1984.
73 ## @end enumerate
74 ## @end deftypefn
75
76 ## Author: Arno Onken <asnelt@asnelt.org>
77 ## Description: Moments of the hypergeometric distribution
78
79 function [mn, v] = hygestat (t, m, n)
80
81   # Check arguments
82   if (nargin != 3)
83     print_usage ();
84   endif
85
86   if (! isempty (t) && ! ismatrix (t))
87     error ("hygestat: t must be a numeric matrix");
88   endif
89   if (! isempty (m) && ! ismatrix (m))
90     error ("hygestat: m must be a numeric matrix");
91   endif
92   if (! isempty (n) && ! ismatrix (n))
93     error ("hygestat: n must be a numeric matrix");
94   endif
95
96   if (! isscalar (t) || ! isscalar (m) || ! isscalar (n))
97     [retval, t, m, n] = common_size (t, m, n);
98     if (retval > 0)
99       error ("hygestat: t, m and n must be of common size or scalar");
100     endif
101   endif
102
103   # Calculate moments
104   mn = (n .* m) ./ t;
105   v = (n .* (m ./ t) .* (1 - m ./ t) .* (t - n)) ./ (t - 1);
106
107   # Continue argument check
108   k = find (! (t >= 0) | ! (m >= 0) | ! (n > 0) | ! (t == round (t)) | ! (m == round (m)) | ! (n == round (n)) | ! (m <= t) | ! (n <= t));
109   if (any (k))
110     mn(k) = NaN;
111     v(k) = NaN;
112   endif
113
114 endfunction
115
116 %!test
117 %! t = 4:9;
118 %! m = 0:5;
119 %! n = 1:6;
120 %! [mn, v] = hygestat (t, m, n);
121 %! expected_mn = [0.0000, 0.4000, 1.0000, 1.7143, 2.5000, 3.3333];
122 %! expected_v = [0.0000, 0.2400, 0.4000, 0.4898, 0.5357, 0.5556];
123 %! assert (mn, expected_mn, 0.001);
124 %! assert (v, expected_v, 0.001);
125
126 %!test
127 %! t = 4:9;
128 %! m = 0:5;
129 %! [mn, v] = hygestat (t, m, 2);
130 %! expected_mn = [0.0000, 0.4000, 0.6667, 0.8571, 1.0000, 1.1111];
131 %! expected_v = [0.0000, 0.2400, 0.3556, 0.4082, 0.4286, 0.4321];
132 %! assert (mn, expected_mn, 0.001);
133 %! assert (v, expected_v, 0.001);