Mercurial > octave-nkf
annotate scripts/statistics/distributions/nbininv.m @ 14138:72c96de7a403 stable
maint: update copyright notices for 2012
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 02 Jan 2012 14:25:41 -0500 |
parents | 19b9f17d22af |
children | f3d52523cde1 |
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 |
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
13171
diff
changeset
|
2 ## Copyright (C) 1995-2012 Kurt Hornik |
6349 | 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. | |
6349 | 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/>. | |
6349 | 19 |
20 ## -*- texinfo -*- | |
21 ## @deftypefn {Function File} {} nbininv (@var{x}, @var{n}, @var{p}) | |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
22 ## For each element of @var{x}, compute the quantile (the inverse of |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
23 ## the CDF) at @var{x} of the negative binomial distribution |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
24 ## with parameters @var{n} and @var{p}. |
6349 | 25 ## |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
26 ## When @var{n} is integer this is the Pascal distribution. When |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
27 ## @var{n} is extended to real numbers this is the Polya distribution. |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
28 ## |
6349 | 29 ## The number of failures in a Bernoulli experiment with success |
30 ## probability @var{p} before the @var{n}-th success follows this | |
31 ## distribution. | |
32 ## @end deftypefn | |
33 | |
34 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> | |
35 ## Description: Quantile function of the Pascal distribution | |
36 | |
37 function inv = nbininv (x, n, p) | |
38 | |
39 if (nargin != 3) | |
40 print_usage (); | |
41 endif | |
42 | |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
43 if (!isscalar (n) || !isscalar (p)) |
6349 | 44 [retval, x, n, p] = common_size (x, n, p); |
45 if (retval > 0) | |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
46 error ("nbininv: X, N, and P must be of common size or scalars"); |
6349 | 47 endif |
48 endif | |
49 | |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
50 if (iscomplex (x) || iscomplex (n) || iscomplex (p)) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
51 error ("nbininv: X, N, and P must not be complex"); |
6349 | 52 endif |
53 | |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
54 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
|
55 inv = zeros (size (x), "single"); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
56 else |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
57 inv = zeros (size (x)); |
6349 | 58 endif |
59 | |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
60 k = (isnan (x) | (x < 0) | (x > 1) | isnan (n) | (n < 1) | (n == Inf) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
61 | isnan (p) | (p < 0) | (p > 1)); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
62 inv(k) = NaN; |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
63 |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
64 k = (x == 1) & (n > 0) & (n < Inf) & (p >= 0) & (p <= 1); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
65 inv(k) = Inf; |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
66 |
6349 | 67 k = find ((x >= 0) & (x < 1) & (n > 0) & (n < Inf) |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
68 & (p > 0) & (p <= 1)); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
69 m = zeros (size (k)); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
70 x = x(k); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
71 if (isscalar (n) && isscalar (p)) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
72 s = p ^ n * ones (size (k)); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
73 while (1) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
74 l = find (s < x); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
75 if (any (l)) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
76 m(l) = m(l) + 1; |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
77 s(l) = s(l) + nbinpdf (m(l), n, p); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
78 else |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
79 break; |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
80 endif |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
81 endwhile |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
82 else |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
83 n = n(k); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
84 p = p(k); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
85 s = p .^ n; |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
86 while (1) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
87 l = find (s < x); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
88 if (any (l)) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
89 m(l) = m(l) + 1; |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
90 s(l) = s(l) + nbinpdf (m(l), n(l), p(l)); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
91 else |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
92 break; |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
93 endif |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
94 endwhile |
6349 | 95 endif |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
96 inv(k) = m; |
6349 | 97 |
98 endfunction | |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
99 |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
100 |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
101 %!shared x |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
102 %! x = [-1 0 3/4 1 2]; |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
103 %!assert(nbininv (x, ones(1,5), 0.5*ones(1,5)), [NaN 0 1 Inf NaN]); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
104 %!assert(nbininv (x, 1, 0.5*ones(1,5)), [NaN 0 1 Inf NaN]); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
105 %!assert(nbininv (x, ones(1,5), 0.5), [NaN 0 1 Inf NaN]); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
106 %!assert(nbininv (x, [1 0 NaN Inf 1], 0.5), [NaN NaN NaN NaN NaN]); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
107 %!assert(nbininv (x, [1 0 1.5 Inf 1], 0.5), [NaN NaN 2 NaN NaN]); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
108 %!assert(nbininv (x, 1, 0.5*[1 -Inf NaN Inf 1]), [NaN NaN NaN NaN NaN]); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
109 %!assert(nbininv ([x(1:2) NaN x(4:5)], 1, 0.5), [NaN 0 NaN Inf NaN]); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
110 |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
111 %% Test class of input preserved |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
112 %!assert(nbininv ([x, NaN], 1, 0.5), [NaN 0 1 Inf NaN NaN]); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
113 %!assert(nbininv (single([x, NaN]), 1, 0.5), single([NaN 0 1 Inf NaN NaN])); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
114 %!assert(nbininv ([x, NaN], single(1), 0.5), single([NaN 0 1 Inf NaN NaN])); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
115 %!assert(nbininv ([x, NaN], 1, single(0.5)), single([NaN 0 1 Inf NaN NaN])); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
116 |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
117 %% Test input validation |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
118 %!error nbininv () |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
119 %!error nbininv (1) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
120 %!error nbininv (1,2) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
121 %!error nbininv (1,2,3,4) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
122 %!error nbininv (ones(3),ones(2),ones(2)) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
123 %!error nbininv (ones(2),ones(3),ones(2)) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
124 %!error nbininv (ones(2),ones(2),ones(3)) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
125 %!error nbininv (i, 2, 2) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
126 %!error nbininv (2, i, 2) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
127 %!error nbininv (2, 2, i) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
128 |