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} {} logncdf (@var{x})
22 ## @deftypefnx {Function File} {} logncdf (@var{x}, @var{mu}, @var{sigma})
23 ## For each element of @var{x}, compute the cumulative distribution
24 ## function (CDF) at @var{x} of the lognormal distribution with
25 ## parameters @var{mu} and @var{sigma}. If a random variable follows this
26 ## distribution, its logarithm is normally distributed with mean
27 ## @var{mu} and standard deviation @var{sigma}.
29 ## Default values are @var{mu} = 1, @var{sigma} = 1.
32 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
33 ## Description: CDF of the log normal distribution
35 function cdf = logncdf (x, mu = 0, sigma = 1)
37 if (nargin != 1 && nargin != 3)
41 if (!isscalar (mu) || !isscalar (sigma))
42 [retval, x, mu, sigma] = common_size (x, mu, sigma);
44 error ("logncdf: X, MU, and SIGMA must be of common size or scalars");
48 if (iscomplex (x) || iscomplex (mu) || iscomplex (sigma))
49 error ("logncdf: X, MU, and SIGMA must not be complex");
52 if (isa (x, "single") || isa (mu, "single") || isa (sigma, "single"))
53 cdf = zeros (size (x), "single");
55 cdf = zeros (size (x));
58 k = isnan (x) | !(sigma > 0) | !(sigma < Inf);
61 k = (x == Inf) & (sigma > 0) & (sigma < Inf);
64 k = (x > 0) & (x < Inf) & (sigma > 0) & (sigma < Inf);
65 if (isscalar (mu) && isscalar (sigma))
66 cdf(k) = stdnormal_cdf ((log (x(k)) - mu) / sigma);
68 cdf(k) = stdnormal_cdf ((log (x(k)) - mu(k)) ./ sigma(k));
75 %! x = [-1 0 1 e Inf];
76 %! y = [0, 0, 0.5, 1/2+1/2*erf(1/2), 1];
77 %!assert(logncdf (x, zeros(1,5), sqrt(2)*ones(1,5)), y);
78 %!assert(logncdf (x, 0, sqrt(2)*ones(1,5)), y);
79 %!assert(logncdf (x, zeros(1,5), sqrt(2)), y);
80 %!assert(logncdf (x, [0 1 NaN 0 1], sqrt(2)), [0 0 NaN y(4:5)]);
81 %!assert(logncdf (x, 0, sqrt(2)*[0 NaN Inf 1 1]), [NaN NaN NaN y(4:5)]);
82 %!assert(logncdf ([x(1:3) NaN x(5)], 0, sqrt(2)), [y(1:3) NaN y(5)]);
84 %% Test class of input preserved
85 %!assert(logncdf ([x, NaN], 0, sqrt(2)), [y, NaN]);
86 %!assert(logncdf (single([x, NaN]), 0, sqrt(2)), single([y, NaN]), eps("single"));
87 %!assert(logncdf ([x, NaN], single(0), sqrt(2)), single([y, NaN]), eps("single"));
88 %!assert(logncdf ([x, NaN], 0, single(sqrt(2))), single([y, NaN]), eps("single"));
90 %% Test input validation
93 %!error logncdf (1,2,3,4)
94 %!error logncdf (ones(3),ones(2),ones(2))
95 %!error logncdf (ones(2),ones(3),ones(2))
96 %!error logncdf (ones(2),ones(2),ones(3))
97 %!error logncdf (i, 2, 2)
98 %!error logncdf (2, i, 2)
99 %!error logncdf (2, 2, i)