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} {} logninv (@var{x})
22 ## @deftypefnx {Function File} {} logninv (@var{x}, @var{mu}, @var{sigma})
23 ## For each element of @var{x}, compute the quantile (the inverse of the
24 ## CDF) at @var{x} of the lognormal distribution with parameters @var{mu}
25 ## and @var{sigma}. If a random variable follows this distribution, its
26 ## logarithm is normally distributed with mean @code{log (@var{mu})} and
27 ## variance @var{sigma}.
29 ## Default values are @var{mu} = 1, @var{sigma} = 1.
32 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
33 ## Description: Quantile function of the log normal distribution
35 function inv = logninv (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 ("logninv: X, MU, and SIGMA must be of common size or scalars");
48 if (iscomplex (x) || iscomplex (mu) || iscomplex (sigma))
49 error ("logninv: X, MU, and SIGMA must not be complex");
52 if (isa (x, "single") || isa (mu, "single") || isa (sigma, "single"))
53 inv = NaN (size (x), "single");
58 k = !(x >= 0) | !(x <= 1) | !(sigma > 0) | !(sigma < Inf);
61 k = (x == 1) & (sigma > 0) & (sigma < Inf);
64 k = (x >= 0) & (x < 1) & (sigma > 0) & (sigma < Inf);
65 if (isscalar (mu) && isscalar (sigma))
66 inv(k) = exp (mu) .* exp (sigma .* stdnormal_inv (x(k)));
68 inv(k) = exp (mu(k)) .* exp (sigma(k) .* stdnormal_inv (x(k)));
75 %! x = [-1 0 0.5 1 2];
76 %!assert(logninv (x, ones(1,5), ones(1,5)), [NaN 0 e Inf NaN]);
77 %!assert(logninv (x, 1, ones(1,5)), [NaN 0 e Inf NaN]);
78 %!assert(logninv (x, ones(1,5), 1), [NaN 0 e Inf NaN]);
79 %!assert(logninv (x, [1 1 NaN 0 1], 1), [NaN 0 NaN Inf NaN]);
80 %!assert(logninv (x, 1, [1 0 NaN Inf 1]), [NaN NaN NaN NaN NaN]);
81 %!assert(logninv ([x(1:2) NaN x(4:5)], 1, 2), [NaN 0 NaN Inf NaN]);
83 %% Test class of input preserved
84 %!assert(logninv ([x, NaN], 1, 1), [NaN 0 e Inf NaN NaN]);
85 %!assert(logninv (single([x, NaN]), 1, 1), single([NaN 0 e Inf NaN NaN]));
86 %!assert(logninv ([x, NaN], single(1), 1), single([NaN 0 e Inf NaN NaN]));
87 %!assert(logninv ([x, NaN], 1, single(1)), single([NaN 0 e Inf NaN NaN]));
89 %% Test input validation
92 %!error logninv (1,2,3,4)
93 %!error logninv (ones(3),ones(2),ones(2))
94 %!error logninv (ones(2),ones(3),ones(2))
95 %!error logninv (ones(2),ones(2),ones(3))
96 %!error logninv (i, 2, 2)
97 %!error logninv (2, i, 2)
98 %!error logninv (2, 2, i)