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