1 ## Copyright (C) 2006, 2007 Arno Onken <asnelt@asnelt.org>
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
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
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/>.
17 ## @deftypefn {Function File} {[@var{mn}, @var{v}] =} hygestat (@var{t}, @var{m}, @var{n})
18 ## Compute mean and variance of the hypergeometric distribution.
20 ## @subheading Arguments
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
28 ## @var{m} is the number of marked items of the hypergeometric distribution.
29 ## The elements of @var{m} must be natural numbers
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
35 ## @var{t}, @var{m}, and @var{n} must be of common size or scalar
37 ## @subheading Return values
41 ## @var{mn} is the mean of the hypergeometric distribution
44 ## @var{v} is the variance of the hypergeometric distribution
47 ## @subheading Examples
54 ## [mn, v] = hygestat (t, m, n)
58 ## [mn, v] = hygestat (t, m, 2)
62 ## @subheading References
66 ## Wendy L. Martinez and Angel R. Martinez. @cite{Computational Statistics
67 ## Handbook with MATLAB}. Appendix E, pages 547-557, Chapman & Hall/CRC,
71 ## Athanasios Papoulis. @cite{Probability, Random Variables, and Stochastic
72 ## Processes}. McGraw-Hill, New York, second edition, 1984.
76 ## Author: Arno Onken <asnelt@asnelt.org>
77 ## Description: Moments of the hypergeometric distribution
79 function [mn, v] = hygestat (t, m, n)
86 if (! isempty (t) && ! ismatrix (t))
87 error ("hygestat: t must be a numeric matrix");
89 if (! isempty (m) && ! ismatrix (m))
90 error ("hygestat: m must be a numeric matrix");
92 if (! isempty (n) && ! ismatrix (n))
93 error ("hygestat: n must be a numeric matrix");
96 if (! isscalar (t) || ! isscalar (m) || ! isscalar (n))
97 [retval, t, m, n] = common_size (t, m, n);
99 error ("hygestat: t, m and n must be of common size or scalar");
105 v = (n .* (m ./ t) .* (1 - m ./ t) .* (t - n)) ./ (t - 1);
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));
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);
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);