]> Creatis software - CreaPhase.git/blob - octave_packages/statistics-1.1.3/unifstat.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / statistics-1.1.3 / unifstat.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}] =} unifstat (@var{a}, @var{b})
18 ## Compute mean and variance of the continuous uniform distribution.
19 ##
20 ## @subheading Arguments
21 ##
22 ## @itemize @bullet
23 ## @item
24 ## @var{a} is the first parameter of the continuous uniform distribution
25 ##
26 ## @item
27 ## @var{b} is the second parameter of the continuous uniform distribution
28 ## @end itemize
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}
31 ##
32 ## @subheading Return values
33 ##
34 ## @itemize @bullet
35 ## @item
36 ## @var{m} is the mean of the continuous uniform distribution
37 ##
38 ## @item
39 ## @var{v} is the variance of the continuous uniform distribution
40 ## @end itemize
41 ##
42 ## @subheading Examples
43 ##
44 ## @example
45 ## @group
46 ## a = 1:6;
47 ## b = 2:2:12;
48 ## [m, v] = unifstat (a, b)
49 ## @end group
50 ##
51 ## @group
52 ## [m, v] = unifstat (a, 10)
53 ## @end group
54 ## @end example
55 ##
56 ## @subheading References
57 ##
58 ## @enumerate
59 ## @item
60 ## Wendy L. Martinez and Angel R. Martinez. @cite{Computational Statistics
61 ## Handbook with MATLAB}. Appendix E, pages 547-557, Chapman & Hall/CRC,
62 ## 2001.
63 ##
64 ## @item
65 ## Athanasios Papoulis. @cite{Probability, Random Variables, and Stochastic
66 ## Processes}. McGraw-Hill, New York, second edition, 1984.
67 ## @end enumerate
68 ## @end deftypefn
69
70 ## Author: Arno Onken <asnelt@asnelt.org>
71 ## Description: Moments of the continuous uniform distribution
72
73 function [m, v] = unifstat (a, b)
74
75   # Check arguments
76   if (nargin != 2)
77     print_usage ();
78   endif
79
80   if (! isempty (a) && ! ismatrix (a))
81     error ("unifstat: a must be a numeric matrix");
82   endif
83   if (! isempty (b) && ! ismatrix (b))
84     error ("unifstat: b must be a numeric matrix");
85   endif
86
87   if (! isscalar (a) || ! isscalar (b))
88     [retval, a, b] = common_size (a, b);
89     if (retval > 0)
90       error ("unifstat: a and b must be of common size or scalar");
91     endif
92   endif
93
94   # Calculate moments
95   m = (a + b) ./ 2;
96   v = ((b - a) .^ 2) ./ 12;
97
98   # Continue argument check
99   k = find (! (-Inf < a) | ! (a < b) | ! (b < Inf));
100   if (any (k))
101     m(k) = NaN;
102     v(k) = NaN;
103   endif
104
105 endfunction
106
107 %!test
108 %! a = 1:6;
109 %! b = 2:2:12;
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);
115
116 %!test
117 %! a = 1:6;
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);