annotate scripts/statistics/distributions/nbinpdf.m @ 20642:9d2023d1a63c

binoinv.m: Implement binary search algorithm for 28X performance increase (bug #34363). * binoinv.m: Call new functions scalar_binoinv or vector_binoinv to calculate binoinv. If there are still uncalculated values then call bin_search_binoinv to perform binary search for remaining values. Add more BIST tests. * binoinv.m (scalar_binoinv): New subfunction to calculate binoinv for scalar x. Stops when x > 1000. * binoinv.m (vector_binoinv): New subfunction to calculate binoinv for scalar x. Stops when x > 1000.
author Lachlan Andrew <lachlanbis@gmail.com>
date Sun, 11 Oct 2015 19:49:40 -0700
parents d9341b422488
children
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
19731
4197fc428c7d maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents: 19627
diff changeset
2 ## Copyright (C) 1995-2015 Kurt Hornik
6349
859f7aaea254 [project @ 2007-02-23 14:20:42 by dbateman]
dbateman
parents:
diff changeset
3 ##
859f7aaea254 [project @ 2007-02-23 14:20:42 by dbateman]
dbateman
parents:
diff changeset
4 ## This file is part of Octave.
859f7aaea254 [project @ 2007-02-23 14:20:42 by dbateman]
dbateman
parents:
diff changeset
5 ##
859f7aaea254 [project @ 2007-02-23 14:20:42 by dbateman]
dbateman
parents:
diff changeset
6 ## Octave is free software; you can redistribute it and/or modify it
859f7aaea254 [project @ 2007-02-23 14:20:42 by dbateman]
dbateman
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: 6349
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: 6349
diff changeset
9 ## your option) any later version.
6349
859f7aaea254 [project @ 2007-02-23 14:20:42 by dbateman]
dbateman
parents:
diff changeset
10 ##
859f7aaea254 [project @ 2007-02-23 14:20:42 by dbateman]
dbateman
parents:
diff changeset
11 ## Octave is distributed in the hope that it will be useful, but
859f7aaea254 [project @ 2007-02-23 14:20:42 by dbateman]
dbateman
parents:
diff changeset
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
859f7aaea254 [project @ 2007-02-23 14:20:42 by dbateman]
dbateman
parents:
diff changeset
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
859f7aaea254 [project @ 2007-02-23 14:20:42 by dbateman]
dbateman
parents:
diff changeset
14 ## General Public License for more details.
859f7aaea254 [project @ 2007-02-23 14:20:42 by dbateman]
dbateman
parents:
diff changeset
15 ##
859f7aaea254 [project @ 2007-02-23 14:20:42 by dbateman]
dbateman
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: 6349
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: 6349
diff changeset
18 ## <http://www.gnu.org/licenses/>.
6349
859f7aaea254 [project @ 2007-02-23 14:20:42 by dbateman]
dbateman
parents:
diff changeset
19
859f7aaea254 [project @ 2007-02-23 14:20:42 by dbateman]
dbateman
parents:
diff changeset
20 ## -*- texinfo -*-
859f7aaea254 [project @ 2007-02-23 14:20:42 by dbateman]
dbateman
parents:
diff changeset
21 ## @deftypefn {Function File} {} nbinpdf (@var{x}, @var{n}, @var{p})
20209
d9341b422488 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19867
diff changeset
22 ## For each element of @var{x}, compute the probability density function (PDF)
d9341b422488 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19867
diff changeset
23 ## at @var{x} of the negative binomial distribution with parameters
d9341b422488 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19867
diff changeset
24 ## @var{n} and @var{p}.
6349
859f7aaea254 [project @ 2007-02-23 14:20:42 by dbateman]
dbateman
parents:
diff changeset
25 ##
20209
d9341b422488 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19867
diff changeset
26 ## When @var{n} is integer this is the Pascal distribution.
d9341b422488 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19867
diff changeset
27 ## When @var{n} is extended to real numbers this is the Polya distribution.
19627
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17744
diff changeset
28 ##
20209
d9341b422488 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19867
diff changeset
29 ## The number of failures in a Bernoulli experiment with success probability
d9341b422488 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19867
diff changeset
30 ## @var{p} before the @var{n}-th success follows this distribution.
6349
859f7aaea254 [project @ 2007-02-23 14:20:42 by dbateman]
dbateman
parents:
diff changeset
31 ## @end deftypefn
859f7aaea254 [project @ 2007-02-23 14:20:42 by dbateman]
dbateman
parents:
diff changeset
32
859f7aaea254 [project @ 2007-02-23 14:20:42 by dbateman]
dbateman
parents:
diff changeset
33 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
859f7aaea254 [project @ 2007-02-23 14:20:42 by dbateman]
dbateman
parents:
diff changeset
34 ## Description: PDF of the Pascal (negative binomial) distribution
859f7aaea254 [project @ 2007-02-23 14:20:42 by dbateman]
dbateman
parents:
diff changeset
35
859f7aaea254 [project @ 2007-02-23 14:20:42 by dbateman]
dbateman
parents:
diff changeset
36 function pdf = nbinpdf (x, n, p)
859f7aaea254 [project @ 2007-02-23 14:20:42 by dbateman]
dbateman
parents:
diff changeset
37
859f7aaea254 [project @ 2007-02-23 14:20:42 by dbateman]
dbateman
parents:
diff changeset
38 if (nargin != 3)
859f7aaea254 [project @ 2007-02-23 14:20:42 by dbateman]
dbateman
parents:
diff changeset
39 print_usage ();
859f7aaea254 [project @ 2007-02-23 14:20:42 by dbateman]
dbateman
parents:
diff changeset
40 endif
859f7aaea254 [project @ 2007-02-23 14:20:42 by dbateman]
dbateman
parents:
diff changeset
41
19867
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19731
diff changeset
42 if (! isscalar (n) || ! isscalar (p))
6349
859f7aaea254 [project @ 2007-02-23 14:20:42 by dbateman]
dbateman
parents:
diff changeset
43 [retval, x, n, p] = common_size (x, n, p);
859f7aaea254 [project @ 2007-02-23 14:20:42 by dbateman]
dbateman
parents:
diff changeset
44 if (retval > 0)
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
45 error ("nbinpdf: X, N, and P must be of common size or scalars");
6349
859f7aaea254 [project @ 2007-02-23 14:20:42 by dbateman]
dbateman
parents:
diff changeset
46 endif
859f7aaea254 [project @ 2007-02-23 14:20:42 by dbateman]
dbateman
parents:
diff changeset
47 endif
859f7aaea254 [project @ 2007-02-23 14:20:42 by dbateman]
dbateman
parents:
diff changeset
48
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
49 if (iscomplex (x) || iscomplex (n) || iscomplex (p))
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
50 error ("nbinpdf: X, N, and P must not be complex");
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
51 endif
6349
859f7aaea254 [project @ 2007-02-23 14:20:42 by dbateman]
dbateman
parents:
diff changeset
52
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
53 if (isa (x, "single") || isa (n, "single") || isa (p, "single"))
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
54 pdf = NaN (size (x), "single");
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
55 else
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
56 pdf = NaN (size (x));
6349
859f7aaea254 [project @ 2007-02-23 14:20:42 by dbateman]
dbateman
parents:
diff changeset
57 endif
859f7aaea254 [project @ 2007-02-23 14:20:42 by dbateman]
dbateman
parents:
diff changeset
58
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
59 ok = (x < Inf) & (x == fix (x)) & (n > 0) & (n < Inf) & (p >= 0) & (p <= 1);
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
60
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
61 k = (x < 0) & ok;
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
62 pdf(k) = 0;
6349
859f7aaea254 [project @ 2007-02-23 14:20:42 by dbateman]
dbateman
parents:
diff changeset
63
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
64 k = (x >= 0) & ok;
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
65 if (isscalar (n) && isscalar (p))
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
66 pdf(k) = bincoeff (-n, x(k)) .* (p ^ n) .* ((p - 1) .^ x(k));
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
67 else
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
68 pdf(k) = bincoeff (-n(k), x(k)) .* (p(k) .^ n(k)) .* ((p(k) - 1) .^ x(k));
6349
859f7aaea254 [project @ 2007-02-23 14:20:42 by dbateman]
dbateman
parents:
diff changeset
69 endif
19627
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17744
diff changeset
70
6349
859f7aaea254 [project @ 2007-02-23 14:20:42 by dbateman]
dbateman
parents:
diff changeset
71
859f7aaea254 [project @ 2007-02-23 14:20:42 by dbateman]
dbateman
parents:
diff changeset
72 endfunction
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
73
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
74
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
75 %!shared x,y
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
76 %! x = [-1 0 1 2 Inf];
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
77 %! y = [0 1/2 1/4 1/8 NaN];
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
78 %!assert (nbinpdf (x, ones (1,5), 0.5*ones (1,5)), y)
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
79 %!assert (nbinpdf (x, 1, 0.5*ones (1,5)), y)
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
80 %!assert (nbinpdf (x, ones (1,5), 0.5), y)
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
81 %!assert (nbinpdf (x, [0 1 NaN 1.5 Inf], 0.5), [NaN 1/2 NaN 1.875*0.5^1.5/4 NaN], eps)
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
82 %!assert (nbinpdf (x, 1, 0.5*[-1 NaN 4 1 1]), [NaN NaN NaN y(4:5)])
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
83 %!assert (nbinpdf ([x, NaN], 1, 0.5), [y, NaN])
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
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
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
86 %!assert (nbinpdf (single ([x, NaN]), 1, 0.5), single ([y, NaN]))
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
87 %!assert (nbinpdf ([x, NaN], single (1), 0.5), single ([y, NaN]))
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
88 %!assert (nbinpdf ([x, NaN], 1, single (0.5)), single ([y, NaN]))
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
89
19867
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19731
diff changeset
90 ## Test input validation
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
91 %!error nbinpdf ()
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
92 %!error nbinpdf (1)
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
93 %!error nbinpdf (1,2)
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
94 %!error nbinpdf (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 nbinpdf (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 nbinpdf (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 nbinpdf (ones (2), ones (2), ones (3))
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
98 %!error nbinpdf (i, 2, 2)
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
99 %!error nbinpdf (2, i, 2)
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
100 %!error nbinpdf (2, 2, i)
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
101