]> Creatis software - CreaPhase.git/blob - octave_packages/m/statistics/distributions/logninv.m
update packages
[CreaPhase.git] / octave_packages / m / statistics / distributions / logninv.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} {} 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}.
28 ##
29 ## Default values are @var{mu} = 1, @var{sigma} = 1.
30 ## @end deftypefn
31
32 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
33 ## Description: Quantile function of the log normal distribution
34
35 function inv = logninv (x, mu = 0, sigma = 1)
36
37   if (nargin != 1 && nargin != 3)
38     print_usage ();
39   endif
40
41   if (!isscalar (mu) || !isscalar (sigma))
42     [retval, x, mu, sigma] = common_size (x, mu, sigma);
43     if (retval > 0)
44       error ("logninv: X, MU, and SIGMA must be of common size or scalars");
45     endif
46   endif
47
48   if (iscomplex (x) || iscomplex (mu) || iscomplex (sigma))
49     error ("logninv: X, MU, and SIGMA must not be complex");
50   endif
51
52   if (isa (x, "single") || isa (mu, "single") || isa (sigma, "single"))
53     inv = NaN (size (x), "single");
54   else
55     inv = NaN (size (x));
56   endif
57
58   k = !(x >= 0) | !(x <= 1) | !(sigma > 0) | !(sigma < Inf);
59   inv(k) = NaN;
60
61   k = (x == 1) & (sigma > 0) & (sigma < Inf);
62   inv(k) = Inf;
63
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)));
67   else
68     inv(k) = exp (mu(k)) .* exp (sigma(k) .* stdnormal_inv (x(k)));
69   endif
70
71 endfunction
72
73
74 %!shared x
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]);
82
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]));
88
89 %% Test input validation
90 %!error logninv ()
91 %!error logninv (1,2)
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)
99