Mercurial > octave-nkf
annotate scripts/statistics/distributions/lognpdf.m @ 18444:8858d0ccfc93 stable
doc: Document that default mu is 0 for logn* statistical functions (bug #41547).
* logncdf.m, logninv.m, lognpdf.m: Document that default mu is 0 (bug #41547).
author | Rik <rik@octave.org> |
---|---|
date | Tue, 11 Feb 2014 14:31:25 -0800 |
parents | d63878346099 |
children | 4197fc428c7d |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
13171
diff
changeset
|
1 ## Copyright (C) 2012 Rik Wehbring |
17744
d63878346099
maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents:
14363
diff
changeset
|
2 ## Copyright (C) 1995-2013 Kurt Hornik |
5410 | 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 | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
5410 | 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 | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
5410 | 19 |
20 ## -*- texinfo -*- | |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
21 ## @deftypefn {Function File} {} lognpdf (@var{x}) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
22 ## @deftypefnx {Function File} {} lognpdf (@var{x}, @var{mu}, @var{sigma}) |
5410 | 23 ## For each element of @var{x}, compute the probability density function |
24 ## (PDF) at @var{x} of the lognormal distribution with parameters | |
5692 | 25 ## @var{mu} and @var{sigma}. If a random variable follows this distribution, |
18444
8858d0ccfc93
doc: Document that default mu is 0 for logn* statistical functions (bug #41547).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
26 ## its logarithm is normally distributed with mean @var{mu} and standard |
8858d0ccfc93
doc: Document that default mu is 0 for logn* statistical functions (bug #41547).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
27 ## deviation @var{sigma}. |
5410 | 28 ## |
18444
8858d0ccfc93
doc: Document that default mu is 0 for logn* statistical functions (bug #41547).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
29 ## Default values are @var{mu} = 0, @var{sigma} = 1. |
5410 | 30 ## @end deftypefn |
31 | |
5428 | 32 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
5410 | 33 ## Description: PDF of the log normal distribution |
34 | |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
35 function pdf = lognpdf (x, mu = 0, sigma = 1) |
5410 | 36 |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
37 if (nargin != 1 && nargin != 3) |
6046 | 38 print_usage (); |
5410 | 39 endif |
40 | |
5692 | 41 if (!isscalar (mu) || !isscalar (sigma)) |
42 [retval, x, mu, sigma] = common_size (x, mu, sigma); | |
5410 | 43 if (retval > 0) |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
44 error ("lognpdf: X, MU, and SIGMA must be of common size or scalars"); |
5410 | 45 endif |
46 endif | |
47 | |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
48 if (iscomplex (x) || iscomplex (mu) || iscomplex (sigma)) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
49 error ("lognpdf: X, MU, and SIGMA must not be complex"); |
5410 | 50 endif |
51 | |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
52 if (isa (x, "single") || isa (mu, "single") || isa (sigma, "single")) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
53 pdf = zeros (size (x), "single"); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
54 else |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
55 pdf = zeros (size (x)); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
56 endif |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
57 |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
58 k = isnan (x) | !(sigma > 0) | !(sigma < Inf); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
59 pdf(k) = NaN; |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
60 |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
61 k = (x > 0) & (x < Inf) & (sigma > 0) & (sigma < Inf); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
62 if (isscalar (mu) && isscalar (sigma)) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
63 pdf(k) = normpdf (log (x(k)), mu, sigma) ./ x(k); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
64 else |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
65 pdf(k) = normpdf (log (x(k)), mu(k), sigma(k)) ./ x(k); |
5410 | 66 endif |
67 | |
68 endfunction | |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
69 |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
70 |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
71 %!shared x,y |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
72 %! x = [-1 0 e Inf]; |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
73 %! y = [0, 0, 1/(e*sqrt(2*pi)) * exp(-1/2), 0]; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
74 %!assert (lognpdf (x, zeros (1,4), ones (1,4)), y, eps) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
75 %!assert (lognpdf (x, 0, ones (1,4)), y, eps) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
76 %!assert (lognpdf (x, zeros (1,4), 1), y, eps) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
77 %!assert (lognpdf (x, [0 1 NaN 0], 1), [0 0 NaN y(4)], eps) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
78 %!assert (lognpdf (x, 0, [0 NaN Inf 1]), [NaN NaN NaN y(4)], eps) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
79 %!assert (lognpdf ([x, NaN], 0, 1), [y, NaN], eps) |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
80 |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
81 %% Test class of input preserved |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
82 %!assert (lognpdf (single ([x, NaN]), 0, 1), single ([y, NaN]), eps ("single")) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
83 %!assert (lognpdf ([x, NaN], single (0), 1), single ([y, NaN]), eps ("single")) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
84 %!assert (lognpdf ([x, NaN], 0, single (1)), single ([y, NaN]), eps ("single")) |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
85 |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
86 %% Test input validation |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
87 %!error lognpdf () |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
88 %!error lognpdf (1,2) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
89 %!error lognpdf (1,2,3,4) |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
90 %!error lognpdf (ones (3), ones (2), ones (2)) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
91 %!error lognpdf (ones (2), ones (3), ones (2)) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
92 %!error lognpdf (ones (2), ones (2), ones (3)) |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
93 %!error lognpdf (i, 2, 2) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
94 %!error lognpdf (2, i, 2) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
95 %!error lognpdf (2, 2, i) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
96 |