]> Creatis software - CreaPhase.git/blob - octave_packages/m/statistics/distributions/wblpdf.m
update packages
[CreaPhase.git] / octave_packages / m / statistics / distributions / wblpdf.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} {} wblpdf (@var{x})
22 ## @deftypefnx {Function File} {} wblpdf (@var{x}, @var{scale})
23 ## @deftypefnx {Function File} {} wblpdf (@var{x}, @var{scale}, @var{shape})
24 ## Compute the probability density function (PDF) at @var{x} of the
25 ## Weibull distribution with scale parameter @var{scale} and shape
26 ## parameter @var{shape} which is given by
27 ## @tex
28 ## $$  {shape \over scale^{shape}} \cdot x^{shape-1} \cdot e^{-({x \over scale})^{shape}} $$
29 ## @end tex
30 ## @ifnottex
31 ##
32 ## @example
33 ## shape * scale^(-shape) * x^(shape-1) * exp (-(x/scale)^shape)
34 ## @end example
35 ##
36 ## @end ifnottex
37 ## @noindent
38 ## for @var{x} @geq{} 0.
39 ##
40 ## Default values are @var{scale} = 1, @var{shape} = 1.
41 ## @end deftypefn
42
43 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
44 ## Description: PDF of the Weibull distribution
45
46 function pdf = wblpdf (x, scale = 1, shape = 1)
47
48   if (nargin < 1 || nargin > 3)
49     print_usage ();
50   endif
51
52   if (!isscalar (scale) || !isscalar (shape))
53     [retval, x, scale, shape] = common_size (x, scale, shape);
54     if (retval > 0)
55       error ("wblpdf: X, SCALE, and SHAPE must be of common size or scalars");
56     endif
57   endif
58
59   if (iscomplex (x) || iscomplex (scale) || iscomplex (shape))
60     error ("wblpdf: X, SCALE, and SHAPE must not be complex");
61   endif
62
63   if (isa (x, "single") || isa (scale, "single") || isa (shape, "single"))
64     pdf = NaN (size (x), "single");
65   else
66     pdf = NaN (size (x));
67   endif
68
69   ok = ((scale > 0) & (scale < Inf) & (shape > 0) & (shape < Inf));
70
71   k = (x < 0) & ok;
72   pdf(k) = 0;
73
74   k = (x >= 0) & (x < Inf) & ok;
75   if (isscalar (scale) && isscalar (shape))
76     pdf(k) = (shape * (scale .^ -shape)
77               .* (x(k) .^ (shape - 1))
78               .* exp (- (x(k) / scale) .^ shape));
79   else
80     pdf(k) = (shape(k) .* (scale(k) .^ -shape(k))
81               .* (x(k) .^ (shape(k) - 1))
82               .* 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, exp(-x(2:4)), NaN];
91 %!assert(wblpdf (x, ones(1,5), ones(1,5)), y);
92 %!assert(wblpdf (x, 1, ones(1,5)), y);
93 %!assert(wblpdf (x, ones(1,5), 1), y);
94 %!assert(wblpdf (x, [0 NaN Inf 1 1], 1), [NaN NaN NaN y(4:5)]);
95 %!assert(wblpdf (x, 1, [0 NaN Inf 1 1]), [NaN NaN NaN y(4:5)]);
96 %!assert(wblpdf ([x, NaN], 1, 1), [y, NaN]);
97
98 %% Test class of input preserved
99 %!assert(wblpdf (single([x, NaN]), 1, 1), single([y, NaN]));
100 %!assert(wblpdf ([x, NaN], single(1), 1), single([y, NaN]));
101 %!assert(wblpdf ([x, NaN], 1, single(1)), single([y, NaN]));
102
103 %% Test input validation
104 %!error wblpdf ()
105 %!error wblpdf (1,2,3,4)
106 %!error wblpdf (ones(3),ones(2),ones(2))
107 %!error wblpdf (ones(2),ones(3),ones(2))
108 %!error wblpdf (ones(2),ones(2),ones(3))
109 %!error wblpdf (i, 2, 2)
110 %!error wblpdf (2, i, 2)
111 %!error wblpdf (2, 2, i)
112