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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
3 ##
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
4 ## This file is part of Octave.
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
5 ##
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
6 ## Octave is free software; you can redistribute it and/or modify it
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
7 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6617
diff changeset
8 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6617
diff changeset
9 ## your option) any later version.
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
10 ##
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
11 ## Octave is distributed in the hope that it will be useful, but
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
14 ## General Public License for more details.
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
15 ##
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
16 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6617
diff changeset
17 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6617
diff changeset
18 ## <http://www.gnu.org/licenses/>.
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
19
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
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
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
23 ## For each element of @var{x}, compute the probability density function
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
24 ## (PDF) at @var{x} of the lognormal distribution with parameters
5692
fe226f54d259 [project @ 2006-03-17 17:02:32 by jwe]
jwe
parents: 5428
diff changeset
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
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
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
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
30 ## @end deftypefn
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
31
5428
2a16423e4aa0 [project @ 2005-08-23 18:38:27 by jwe]
jwe
parents: 5411
diff changeset
32 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
33 ## Description: PDF of the log normal distribution
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
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
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
36
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
37 if (nargin != 1 && nargin != 3)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5692
diff changeset
38 print_usage ();
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
39 endif
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
40
5692
fe226f54d259 [project @ 2006-03-17 17:02:32 by jwe]
jwe
parents: 5428
diff changeset
41 if (!isscalar (mu) || !isscalar (sigma))
fe226f54d259 [project @ 2006-03-17 17:02:32 by jwe]
jwe
parents: 5428
diff changeset
42 [retval, x, mu, sigma] = common_size (x, mu, sigma);
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
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
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
45 endif
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
46 endif
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
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
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
50 endif
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
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
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
66 endif
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
67
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
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