Mercurial > octave-nkf
annotate scripts/statistics/distributions/logncdf.m @ 20641:c3c052b9192a
Improve performance and error reporting of betainv, gaminv (bug #34363).
* betainv.m: Replace for loop with do-until loop. Shorten max loop cycles to
40, rather than 10,000. Issue warning if algorithm fails.
* gaminv.m: Replace for loop with do-until loop. Shorten max loop cycles to
40, rather than 100. Issue warning if algorithm fails.
author | Rik <rik@octave.org> |
---|---|
date | Sun, 11 Oct 2015 16:55:17 -0700 |
parents | d9341b422488 |
children |
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 |
19731
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
18444
diff
changeset
|
2 ## Copyright (C) 1995-2015 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} {} logncdf (@var{x}) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
22 ## @deftypefnx {Function File} {} logncdf (@var{x}, @var{mu}, @var{sigma}) |
18444
8858d0ccfc93
doc: Document that default mu is 0 for logn* statistical functions (bug #41547).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
23 ## For each element of @var{x}, compute the cumulative distribution function |
8858d0ccfc93
doc: Document that default mu is 0 for logn* statistical functions (bug #41547).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
24 ## (CDF) at @var{x} of the lognormal distribution with parameters |
20209
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19867
diff
changeset
|
25 ## @var{mu} and @var{sigma}. |
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19867
diff
changeset
|
26 ## |
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19867
diff
changeset
|
27 ## If a random variable follows this distribution, its logarithm is normally |
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19867
diff
changeset
|
28 ## distributed with mean @var{mu} and standard deviation @var{sigma}. |
5410 | 29 ## |
18444
8858d0ccfc93
doc: Document that default mu is 0 for logn* statistical functions (bug #41547).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
30 ## Default values are @var{mu} = 0, @var{sigma} = 1. |
5410 | 31 ## @end deftypefn |
32 | |
5428 | 33 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
5410 | 34 ## Description: CDF of the log normal distribution |
35 | |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
36 function cdf = logncdf (x, mu = 0, sigma = 1) |
5410 | 37 |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
38 if (nargin != 1 && nargin != 3) |
6046 | 39 print_usage (); |
5410 | 40 endif |
41 | |
19867
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19731
diff
changeset
|
42 if (! isscalar (mu) || ! isscalar (sigma)) |
5692 | 43 [retval, x, mu, sigma] = common_size (x, mu, sigma); |
5410 | 44 if (retval > 0) |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
45 error ("logncdf: X, MU, and SIGMA must be of common size or scalars"); |
5410 | 46 endif |
47 endif | |
48 | |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
49 if (iscomplex (x) || iscomplex (mu) || iscomplex (sigma)) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
50 error ("logncdf: X, MU, and SIGMA must not be complex"); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
51 endif |
5410 | 52 |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
53 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
|
54 cdf = zeros (size (x), "single"); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
55 else |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
56 cdf = zeros (size (x)); |
5410 | 57 endif |
58 | |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
59 k = isnan (x) | !(sigma > 0) | !(sigma < Inf); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
60 cdf(k) = NaN; |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
61 |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
62 k = (x == Inf) & (sigma > 0) & (sigma < Inf); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
63 cdf(k) = 1; |
5410 | 64 |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
65 k = (x > 0) & (x < Inf) & (sigma > 0) & (sigma < Inf); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
66 if (isscalar (mu) && isscalar (sigma)) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
67 cdf(k) = stdnormal_cdf ((log (x(k)) - mu) / sigma); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
68 else |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
69 cdf(k) = stdnormal_cdf ((log (x(k)) - mu(k)) ./ sigma(k)); |
5410 | 70 endif |
71 | |
72 endfunction | |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
73 |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
74 |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
75 %!shared x,y |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
76 %! x = [-1 0 1 e Inf]; |
14772
39a2e91a246e
Fix failing logncdf tests by adding eps tolerance.
Rik <octave@nomad.inbox5.com>
parents:
14760
diff
changeset
|
77 %! y = [0, 0, 0.5, 1/2+1/2*erf(1/2), 1]; |
39a2e91a246e
Fix failing logncdf tests by adding eps tolerance.
Rik <octave@nomad.inbox5.com>
parents:
14760
diff
changeset
|
78 %!assert (logncdf (x, zeros (1,5), sqrt(2)*ones (1,5)), y, eps) |
39a2e91a246e
Fix failing logncdf tests by adding eps tolerance.
Rik <octave@nomad.inbox5.com>
parents:
14760
diff
changeset
|
79 %!assert (logncdf (x, 0, sqrt(2)*ones (1,5)), y, eps) |
39a2e91a246e
Fix failing logncdf tests by adding eps tolerance.
Rik <octave@nomad.inbox5.com>
parents:
14760
diff
changeset
|
80 %!assert (logncdf (x, zeros (1,5), sqrt(2)), y, eps) |
39a2e91a246e
Fix failing logncdf tests by adding eps tolerance.
Rik <octave@nomad.inbox5.com>
parents:
14760
diff
changeset
|
81 %!assert (logncdf (x, [0 1 NaN 0 1], sqrt(2)), [0 0 NaN y(4:5)], eps) |
39a2e91a246e
Fix failing logncdf tests by adding eps tolerance.
Rik <octave@nomad.inbox5.com>
parents:
14760
diff
changeset
|
82 %!assert (logncdf (x, 0, sqrt(2)*[0 NaN Inf 1 1]), [NaN NaN NaN y(4:5)], eps) |
39a2e91a246e
Fix failing logncdf tests by adding eps tolerance.
Rik <octave@nomad.inbox5.com>
parents:
14760
diff
changeset
|
83 %!assert (logncdf ([x(1:3) NaN x(5)], 0, sqrt(2)), [y(1:3) NaN y(5)], eps) |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
84 |
19867
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19731
diff
changeset
|
85 ## Test class of input preserved |
14772
39a2e91a246e
Fix failing logncdf tests by adding eps tolerance.
Rik <octave@nomad.inbox5.com>
parents:
14760
diff
changeset
|
86 %!assert (logncdf ([x, NaN], 0, sqrt(2)), [y, NaN], eps) |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
87 %!assert (logncdf (single ([x, NaN]), 0, sqrt(2)), 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
|
88 %!assert (logncdf ([x, NaN], single (0), sqrt(2)), 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
|
89 %!assert (logncdf ([x, NaN], 0, single (sqrt(2))), single ([y, NaN]), eps ("single")) |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
90 |
19867
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19731
diff
changeset
|
91 ## Test input validation |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
92 %!error logncdf () |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
93 %!error logncdf (1,2) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
94 %!error logncdf (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
|
95 %!error logncdf (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
|
96 %!error logncdf (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
|
97 %!error logncdf (ones (2), ones (2), ones (3)) |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
98 %!error logncdf (i, 2, 2) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
99 %!error logncdf (2, i, 2) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
100 %!error logncdf (2, 2, i) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
101 |