3191
|
1 ## Copyright (C) 1995, 1996, 1997 Kurt Hornik |
3426
|
2 ## |
3922
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
3191
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
3426
|
9 ## |
3922
|
10 ## Octave is distributed in the hope that it will be useful, but |
3191
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3426
|
13 ## General Public License for more details. |
|
14 ## |
3191
|
15 ## You should have received a copy of the GNU General Public License |
3922
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
|
18 ## 02111-1307, USA. |
3191
|
19 |
3456
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} binomial_pdf (@var{x}, @var{n}, @var{p}) |
|
22 ## For each element of @var{x}, compute the probability density function |
|
23 ## (PDF) at @var{x} of the binomial distribution with parameters @var{n} |
|
24 ## and @var{p}. |
|
25 ## @end deftypefn |
3191
|
26 |
3456
|
27 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
28 ## Description: PDF of the binomial distribution |
3191
|
29 |
|
30 function pdf = binomial_pdf (x, n, p) |
3426
|
31 |
3191
|
32 if (nargin != 3) |
|
33 usage ("binomial_pdf (x, n, p)"); |
|
34 endif |
|
35 |
5179
|
36 if (! isscalar (n) || ! isscalar (p)) |
4854
|
37 [retval, x, n, p] = common_size (x, n, p); |
|
38 if (retval > 0) |
|
39 error ("binomial_pdf: x, n and p must be of common size or scalar"); |
|
40 endif |
3191
|
41 endif |
3426
|
42 |
5179
|
43 k = ((x >= 0) & (x <= n) |
|
44 & (x == round (x)) & (n == round (n)) |
|
45 & (p >= 0) & (p <= 1)); |
3426
|
46 |
5179
|
47 pdf = zeros (size (x)); |
|
48 pdf(! k) = NaN; |
|
49 if (any (k(:))) |
|
50 x = x(k); |
|
51 if (! isscalar (n)) |
|
52 n = n(k); |
4854
|
53 endif |
5179
|
54 if (! isscalar (p)) |
|
55 p = p(k); |
|
56 endif |
|
57 z = gammaln(n+1) - gammaln(x+1) - gammaln(n-x+1) + x.*log(p) + (n-x).*log(1-p); |
|
58 pdf(k) = exp (z); |
3191
|
59 endif |
3426
|
60 |
3191
|
61 endfunction |