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{m}, @var{v}] =} unifstat (@var{a}, @var{b})
18 ## Compute mean and variance of the continuous uniform distribution.
20 ## @subheading Arguments
24 ## @var{a} is the first parameter of the continuous uniform distribution
27 ## @var{b} is the second parameter of the continuous uniform distribution
29 ## @var{a} and @var{b} must be of common size or one of them must be scalar
30 ## and @var{a} must be less than @var{b}
32 ## @subheading Return values
36 ## @var{m} is the mean of the continuous uniform distribution
39 ## @var{v} is the variance of the continuous uniform distribution
42 ## @subheading Examples
48 ## [m, v] = unifstat (a, b)
52 ## [m, v] = unifstat (a, 10)
56 ## @subheading References
60 ## Wendy L. Martinez and Angel R. Martinez. @cite{Computational Statistics
61 ## Handbook with MATLAB}. Appendix E, pages 547-557, Chapman & Hall/CRC,
65 ## Athanasios Papoulis. @cite{Probability, Random Variables, and Stochastic
66 ## Processes}. McGraw-Hill, New York, second edition, 1984.
70 ## Author: Arno Onken <asnelt@asnelt.org>
71 ## Description: Moments of the continuous uniform distribution
73 function [m, v] = unifstat (a, b)
80 if (! isempty (a) && ! ismatrix (a))
81 error ("unifstat: a must be a numeric matrix");
83 if (! isempty (b) && ! ismatrix (b))
84 error ("unifstat: b must be a numeric matrix");
87 if (! isscalar (a) || ! isscalar (b))
88 [retval, a, b] = common_size (a, b);
90 error ("unifstat: a and b must be of common size or scalar");
96 v = ((b - a) .^ 2) ./ 12;
98 # Continue argument check
99 k = find (! (-Inf < a) | ! (a < b) | ! (b < Inf));
110 %! [m, v] = unifstat (a, b);
111 %! expected_m = [1.5000, 3.0000, 4.5000, 6.0000, 7.5000, 9.0000];
112 %! expected_v = [0.0833, 0.3333, 0.7500, 1.3333, 2.0833, 3.0000];
113 %! assert (m, expected_m, 0.001);
114 %! assert (v, expected_v, 0.001);
118 %! [m, v] = unifstat (a, 10);
119 %! expected_m = [5.5000, 6.0000, 6.5000, 7.0000, 7.5000, 8.0000];
120 %! expected_v = [6.7500, 5.3333, 4.0833, 3.0000, 2.0833, 1.3333];
121 %! assert (m, expected_m, 0.001);
122 %! assert (v, expected_v, 0.001);