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