7017
|
1 ## Copyright (C) 1995, 1996, 1997, 2005, 2006, 2007 Kurt Hornik |
5410
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
5410
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
5410
|
18 |
|
19 ## -*- texinfo -*- |
5692
|
20 ## @deftypefn {Function File} {} logninv (@var{x}, @var{mu}, @var{sigma}) |
5410
|
21 ## For each element of @var{x}, compute the quantile (the inverse of the |
5692
|
22 ## CDF) at @var{x} of the lognormal distribution with parameters @var{mu} |
|
23 ## and @var{sigma}. If a random variable follows this distribution, its |
|
24 ## logarithm is normally distributed with mean @code{log (@var{mu})} and |
|
25 ## variance @var{sigma}. |
5410
|
26 ## |
5692
|
27 ## Default values are @var{mu} = 1, @var{sigma} = 1. |
5410
|
28 ## @end deftypefn |
|
29 |
5428
|
30 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
5410
|
31 ## Description: Quantile function of the log normal distribution |
|
32 |
5692
|
33 function inv = logninv (x, mu, sigma) |
5410
|
34 |
|
35 if (! ((nargin == 1) || (nargin == 3))) |
6046
|
36 print_usage (); |
5410
|
37 endif |
|
38 |
|
39 if (nargin == 1) |
5692
|
40 mu = 0; |
|
41 sigma = 1; |
5410
|
42 endif |
|
43 |
|
44 ## The following "straightforward" implementation unfortunately does |
|
45 ## not work (because exp (Inf) -> NaN): |
6617
|
46 ## inv = exp (norminv (x, mu, sigma)); |
5410
|
47 ## Hence ... |
|
48 |
5692
|
49 if (!isscalar (mu) || !isscalar (sigma)) |
|
50 [retval, x, mu, sigma] = common_size (x, mu, sigma); |
5410
|
51 if (retval > 0) |
5692
|
52 error ("logninv: x, mu and sigma must be of common size or scalars"); |
5410
|
53 endif |
|
54 endif |
|
55 |
|
56 inv = zeros (size (x)); |
|
57 |
5692
|
58 k = find (!(x >= 0) | !(x <= 1) | !(sigma > 0) | !(sigma < Inf)); |
5410
|
59 if (any (k)) |
|
60 inv(k) = NaN; |
|
61 endif |
|
62 |
5692
|
63 k = find ((x == 1) & (sigma > 0) & (sigma < Inf)); |
5410
|
64 if (any (k)) |
|
65 inv(k) = Inf; |
|
66 endif |
|
67 |
5692
|
68 k = find ((x > 0) & (x < 1) & (sigma > 0) & (sigma < Inf)); |
5410
|
69 if (any (k)) |
5692
|
70 if (isscalar (mu) && isscalar (sigma)) |
|
71 inv(k) = exp (mu) .* exp (sigma .* stdnormal_inv (x(k))); |
5410
|
72 else |
5692
|
73 inv(k) = exp (mu(k)) .* exp (sigma(k) .* stdnormal_inv (x(k))); |
5410
|
74 endif |
|
75 endif |
|
76 |
|
77 endfunction |