]> Creatis software - CreaPhase.git/blob - octave_packages/m/statistics/distributions/wblcdf.m
update packages
[CreaPhase.git] / octave_packages / m / statistics / distributions / wblcdf.m
1 ## Copyright (C) 2012 Rik Wehbring
2 ## Copyright (C) 1995-2012 Kurt Hornik
3 ##
4 ## This file is part of Octave.
5 ##
6 ## Octave is free software; you can redistribute it and/or modify it
7 ## under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 3 of the License, or (at
9 ## your option) any later version.
10 ##
11 ## Octave is distributed in the hope that it will be useful, but
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 ## General Public License for more details.
15 ##
16 ## You should have received a copy of the GNU General Public License
17 ## along with Octave; see the file COPYING.  If not, see
18 ## <http://www.gnu.org/licenses/>.
19
20 ## -*- texinfo -*-
21 ## @deftypefn  {Function File} {} wblcdf (@var{x})
22 ## @deftypefnx {Function File} {} wblcdf (@var{x}, @var{scale})
23 ## @deftypefnx {Function File} {} wblcdf (@var{x}, @var{scale}, @var{shape})
24 ## Compute the cumulative distribution function (CDF) at @var{x} of the
25 ## Weibull distribution with scale parameter @var{scale} and shape
26 ## parameter @var{shape}, which is
27 ## @tex
28 ## $$ 1 - e^{-({x \over scale})^{shape}} $$
29 ## for $x \geq 0$.
30 ## @end tex
31 ## @ifnottex
32 ##
33 ## @example
34 ## 1 - exp (-(x/scale)^shape)
35 ## @end example
36 ##
37 ## @noindent
38 ## for @var{x} @geq{} 0.
39 ##
40 ## Default values are @var{scale} = 1, @var{shape} = 1.
41 ## @end ifnottex
42 ## @end deftypefn
43
44 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
45 ## Description: CDF of the Weibull distribution
46
47 function cdf = wblcdf (x, scale = 1, shape = 1)
48
49   if (nargin < 1 || nargin > 3)
50     print_usage ();
51   endif
52
53   if (!isscalar (shape) || !isscalar (scale))
54     [retval, x, shape, scale] = common_size (x, shape, scale);
55     if (retval > 0)
56       error ("wblcdf: X, SCALE, and SHAPE must be of common size or scalars");
57     endif
58   endif
59
60   if (iscomplex (x) || iscomplex (scale) || iscomplex (shape))
61     error ("wblcdf: X, SCALE, and SHAPE must not be complex");
62   endif
63
64   if (isa (x, "single") || isa (scale, "single") || isa (shape, "single"))
65     cdf = NaN (size (x), "single");
66   else
67     cdf = NaN (size (x));
68   endif
69
70   ok = (shape > 0) & (shape < Inf) & (scale > 0) & (scale < Inf);
71
72   k = (x <= 0) & ok;
73   cdf(k) = 0;
74
75   k = (x == Inf) & ok;
76   cdf(k) = 1;
77
78   k = (x > 0) & (x < Inf) & ok;
79   if (isscalar (shape) && isscalar (scale))
80     cdf(k) = 1 - exp (- (x(k) / scale) .^ shape);
81   else
82     cdf(k) = 1 - exp (- (x(k) ./ scale(k)) .^ shape(k));
83   endif
84
85 endfunction
86
87
88 %!shared x,y
89 %! x = [-1 0 0.5 1 Inf];
90 %! y = [0, 1-exp(-x(2:4)), 1];
91 %!assert(wblcdf (x, ones(1,5), ones(1,5)), y);
92 %!assert(wblcdf (x, 1, ones(1,5)), y);
93 %!assert(wblcdf (x, ones(1,5), 1), y);
94 %!assert(wblcdf (x, [0 1 NaN Inf 1], 1), [NaN 0 NaN NaN 1]);
95 %!assert(wblcdf (x, 1, [0 1 NaN Inf 1]), [NaN 0 NaN NaN 1]);
96 %!assert(wblcdf ([x(1:2) NaN x(4:5)], 1, 1), [y(1:2) NaN y(4:5)]);
97
98 %% Test class of input preserved
99 %!assert(wblcdf ([x, NaN], 1, 1), [y, NaN]);
100 %!assert(wblcdf (single([x, NaN]), 1, 1), single([y, NaN]));
101 %!assert(wblcdf ([x, NaN], single(1), 1), single([y, NaN]));
102 %!assert(wblcdf ([x, NaN], 1, single(1)), single([y, NaN]));
103
104 %% Test input validation
105 %!error wblcdf ()
106 %!error wblcdf (1,2,3,4)
107 %!error wblcdf (ones(3),ones(2),ones(2))
108 %!error wblcdf (ones(2),ones(3),ones(2))
109 %!error wblcdf (ones(2),ones(2),ones(3))
110 %!error wblcdf (i, 2, 2)
111 %!error wblcdf (2, i, 2)
112 %!error wblcdf (2, 2, i)
113