]> Creatis software - CreaPhase.git/blob - octave_packages/m/statistics/distributions/lognpdf.m
update packages
[CreaPhase.git] / octave_packages / m / statistics / distributions / lognpdf.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} {} lognpdf (@var{x})
22 ## @deftypefnx {Function File} {} lognpdf (@var{x}, @var{mu}, @var{sigma})
23 ## For each element of @var{x}, compute the probability density function
24 ## (PDF) at @var{x} of the lognormal distribution with parameters
25 ## @var{mu} and @var{sigma}.  If a random variable follows this distribution,
26 ## its logarithm is normally distributed with mean @var{mu}
27 ## and standard deviation @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: PDF of the log normal distribution
34
35 function pdf = lognpdf (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 ("lognpdf: 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 ("lognpdf: X, MU, and SIGMA must not be complex");
50   endif
51
52   if (isa (x, "single") || isa (mu, "single") || isa (sigma, "single"))
53     pdf = zeros (size (x), "single");
54   else
55     pdf = zeros (size (x));
56   endif
57
58   k = isnan (x) | !(sigma > 0) | !(sigma < Inf);
59   pdf(k) = NaN;
60
61   k = (x > 0) & (x < Inf) & (sigma > 0) & (sigma < Inf);
62   if (isscalar (mu) && isscalar (sigma))
63     pdf(k) = normpdf (log (x(k)), mu, sigma) ./ x(k);
64   else
65     pdf(k) = normpdf (log (x(k)), mu(k), sigma(k)) ./ x(k);
66   endif
67
68 endfunction
69
70
71 %!shared x,y
72 %! x = [-1 0 e Inf];
73 %! y = [0, 0, 1/(e*sqrt(2*pi)) * exp(-1/2), 0];
74 %!assert(lognpdf (x, zeros(1,4), ones(1,4)), y, eps);
75 %!assert(lognpdf (x, 0, ones(1,4)), y, eps);
76 %!assert(lognpdf (x, zeros(1,4), 1), y, eps);
77 %!assert(lognpdf (x, [0 1 NaN 0], 1), [0 0 NaN y(4)], eps);
78 %!assert(lognpdf (x, 0, [0 NaN Inf 1]), [NaN NaN NaN y(4)], eps);
79 %!assert(lognpdf ([x, NaN], 0, 1), [y, NaN], eps);
80
81 %% Test class of input preserved
82 %!assert(lognpdf (single([x, NaN]), 0, 1), single([y, NaN]), eps("single"));
83 %!assert(lognpdf ([x, NaN], single(0), 1), single([y, NaN]), eps("single"));
84 %!assert(lognpdf ([x, NaN], 0, single(1)), single([y, NaN]), eps("single"));
85
86 %% Test input validation
87 %!error lognpdf ()
88 %!error lognpdf (1,2)
89 %!error lognpdf (1,2,3,4)
90 %!error lognpdf (ones(3),ones(2),ones(2))
91 %!error lognpdf (ones(2),ones(3),ones(2))
92 %!error lognpdf (ones(2),ones(2),ones(3))
93 %!error lognpdf (i, 2, 2)
94 %!error lognpdf (2, i, 2)
95 %!error lognpdf (2, 2, i)
96