1 ## Copyright (C) 2012 Rik Wehbring
2 ## Copyright (C) 1995-2012 Kurt Hornik
4 ## This file is part of Octave.
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.
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.
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/>.
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
28 ## $$ {shape \over scale^{shape}} \cdot x^{shape-1} \cdot e^{-({x \over scale})^{shape}} $$
33 ## shape * scale^(-shape) * x^(shape-1) * exp (-(x/scale)^shape)
38 ## for @var{x} @geq{} 0.
40 ## Default values are @var{scale} = 1, @var{shape} = 1.
43 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
44 ## Description: PDF of the Weibull distribution
46 function pdf = wblpdf (x, scale = 1, shape = 1)
48 if (nargin < 1 || nargin > 3)
52 if (!isscalar (scale) || !isscalar (shape))
53 [retval, x, scale, shape] = common_size (x, scale, shape);
55 error ("wblpdf: X, SCALE, and SHAPE must be of common size or scalars");
59 if (iscomplex (x) || iscomplex (scale) || iscomplex (shape))
60 error ("wblpdf: X, SCALE, and SHAPE must not be complex");
63 if (isa (x, "single") || isa (scale, "single") || isa (shape, "single"))
64 pdf = NaN (size (x), "single");
69 ok = ((scale > 0) & (scale < Inf) & (shape > 0) & (shape < Inf));
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));
80 pdf(k) = (shape(k) .* (scale(k) .^ -shape(k))
81 .* (x(k) .^ (shape(k) - 1))
82 .* exp (- (x(k) ./ scale(k)) .^ shape(k)));
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]);
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]));
103 %% Test input validation
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)