5410
|
1 ## Copyright (C) 1995, 1996, 1997 Kurt Hornik |
|
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} {} logncdf (@var{x}, @var{mu}, @var{sigma}) |
5410
|
21 ## For each element of @var{x}, compute the cumulative distribution |
|
22 ## function (CDF) at @var{x} of the lognormal distribution with |
5692
|
23 ## parameters @var{mu} and @var{sigma}. If a random variable follows this |
5410
|
24 ## distribution, its logarithm is normally distributed with mean |
5692
|
25 ## @var{mu} and standard deviation @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: CDF of the log normal distribution |
|
32 |
5692
|
33 function cdf = logncdf (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 etc): |
5692
|
46 ## cdf = normal_cdf (log (x), log (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 ("logncdf: x, mu and sigma must be of common size or scalars"); |
5410
|
53 endif |
|
54 endif |
|
55 |
|
56 cdf = zeros (size (x)); |
|
57 |
5692
|
58 k = find (isnan (x) | !(sigma > 0) | !(sigma < Inf)); |
5410
|
59 if (any (k)) |
|
60 cdf(k) = NaN; |
|
61 endif |
|
62 |
5692
|
63 k = find ((x == Inf) & (sigma > 0) & (sigma < Inf)); |
5410
|
64 if (any (k)) |
|
65 cdf(k) = 1; |
|
66 endif |
|
67 |
5692
|
68 k = find ((x > 0) & (x < Inf) & (sigma > 0) & (sigma < Inf)); |
5410
|
69 if (any (k)) |
5692
|
70 if (isscalar (mu) && isscalar (sigma)) |
|
71 cdf(k) = stdnormal_cdf ((log (x(k)) - mu) / sigma); |
5410
|
72 else |
5692
|
73 cdf(k) = stdnormal_cdf ((log (x(k)) - mu(k)) ./ sigma(k)); |
5410
|
74 endif |
|
75 endif |
|
76 |
|
77 endfunction |