time | Calls | line |
---|
| | 1 | function y = binopdf(x,n,p)
|
| | 2 | % BINOPDF Binomial probability density function.
|
| | 3 | % Y = BINOPDF(X,N,P) returns the binomial probability density
|
| | 4 | % function with parameters N and P at the values in X.
|
| | 5 | % Note that the density function is zero unless X is an integer.
|
| | 6 | %
|
| | 7 | % The size of Y is the common size of the input arguments. A scalar input
|
| | 8 | % functions as a constant matrix of the same size as the other inputs.
|
| | 9 | %
|
| | 10 | % See also BINOCDF, BINOFIT, BINOINV, BINORND, BINOSTAT, PDF.
|
| | 11 |
|
| | 12 | % References:
|
| | 13 | % [1] M. Abramowitz and I. A. Stegun, "Handbook of Mathematical
|
| | 14 | % Functions", Government Printing Office, 1964, 26.1.20.
|
| | 15 | % [2] C. Loader, "Fast and Accurate Calculations
|
| | 16 | % of Binomial Probabilities", July 9, 2000.
|
| | 17 |
|
| | 18 | % Copyright 1993-2015 The MathWorks, Inc.
|
| | 19 |
|
< 0.001 | 4 | 20 | if nargin < 3,
|
| | 21 | error(message('stats:binopdf:TooFewInputs'));
|
| | 22 | end
|
| | 23 |
|
0.006 | 4 | 24 | [errorcode, x, n, p] = distchck(3,x,n,p);
|
| | 25 |
|
< 0.001 | 4 | 26 | if errorcode > 0
|
| | 27 | error(message('stats:binopdf:InputSizeMismatch'));
|
| | 28 | end
|
| | 29 |
|
| | 30 | % Binomial distribution is defined on positive integers less than N.
|
< 0.001 | 4 | 31 | if ~isfloat(x)
|
| | 32 | x = double(x);
|
| | 33 | end
|
< 0.001 | 4 | 34 | if ~isfloat(n)
|
| | 35 | n = double(n);
|
| | 36 | end
|
| | 37 |
|
| | 38 | % Initialize Y to zero.
|
0.003 | 4 | 39 | y = zeros(size(x),'like',internal.stats.dominantType(x,n,p)); % Single if x, n or p is.
|
< 0.001 | 4 | 40 | y(isnan(x) | isnan(n) | isnan(p)) = NaN;
|
| | 41 |
|
< 0.001 | 4 | 42 | k = find(x >= 0 & x==round(x) & x <= n & n==round(n) & p>=0 & p<=1);
|
< 0.001 | 4 | 43 | if any(k)
|
| | 44 | % First deal with borderline cases
|
< 0.001 | 4 | 45 | t = (p(k)==0);
|
< 0.001 | 4 | 46 | if any(t)
|
| | 47 | kt = k(t);
|
| | 48 | y(kt) = (x(kt)==0);
|
| | 49 | k(t) = [];
|
| | 50 | end
|
< 0.001 | 4 | 51 | t = (p(k)==1);
|
< 0.001 | 4 | 52 | if any(t)
|
| | 53 | kt = k(t);
|
| | 54 | y(kt) = (x(kt)==n(kt));
|
| | 55 | k(t) = [];
|
| | 56 | end
|
< 0.001 | 4 | 57 | end
|
| | 58 |
|
| | 59 | % More borderline cases: x=0 and x=n
|
< 0.001 | 4 | 60 | if any(k)
|
< 0.001 | 4 | 61 | t = (x(k)==0);
|
< 0.001 | 4 | 62 | if any(t)
|
< 0.001 | 4 | 63 | kt = k(t);
|
< 0.001 | 4 | 64 | y(kt) = exp(n(kt).*log(1-p(kt)));
|
< 0.001 | 4 | 65 | k(t) = [];
|
< 0.001 | 4 | 66 | end
|
< 0.001 | 4 | 67 | t = (x(k)==n(k));
|
< 0.001 | 4 | 68 | if any(t)
|
< 0.001 | 4 | 69 | kt = k(t);
|
< 0.001 | 4 | 70 | y(kt) = exp(n(kt).*log(p(kt)));
|
< 0.001 | 4 | 71 | k(t) = [];
|
< 0.001 | 4 | 72 | end
|
< 0.001 | 4 | 73 | end
|
< 0.001 | 4 | 74 | if any(k)
|
< 0.001 | 4 | 75 | t = (n(k)<10);
|
< 0.001 | 4 | 76 | if any(t)
|
| | 77 | % Faster method that is not accurate for large n
|
| | 78 | K = k(t);
|
| | 79 | nk = gammaln(n(K) + 1) - gammaln(x(K) + 1) - gammaln(n(K) - x(K) + 1);
|
| | 80 | lny = nk + x(K).*log( p(K)) + (n(K) - x(K)).*log1p(-p(K));
|
| | 81 | y(K) = exp(lny);
|
| | 82 | end
|
< 0.001 | 4 | 83 | if any(~t)
|
| | 84 | % Slower method
|
< 0.001 | 4 | 85 | K = k(~t);
|
| | 86 |
|
| | 87 | % Notes:
|
| | 88 | % 1- Equation 3 in reference [2] is used to calculate the pdf.
|
| | 89 | % 2- The second term on the RHS of Equation 3 is calculated using
|
| | 90 | % Equation 5.
|
| | 91 | % 3- The deviance, bd0(x,np), is equivalent to npD0(x/np) in the
|
| | 92 | % reference, which is calculated using Equation 5.2.
|
| | 93 | % 4- The function stirlerr(n) is the error term (delta(n)) in the
|
| | 94 | % Stirling-De Moivre series in Equation 4.2 of the reference.
|
| | 95 |
|
| | 96 | % Calculate the pdf
|
0.025 | 4 | 97 | lc = stirlerr(n(K))-stirlerr(x(K))-stirlerr(n(K)-x(K)) ...
|
| 4 | 98 | -binodeviance(x(K),n(K).*p(K)) ...
|
| 4 | 99 | -binodeviance(n(K)-x(K),n(K).*(1.0-p(K)));
|
< 0.001 | 4 | 100 | y(K) = exp(lc).*sqrt(n(K)./(2*pi*x(K).*(n(K)-x(K))));
|
< 0.001 | 4 | 101 | end
|
< 0.001 | 4 | 102 | end
|
< 0.001 | 4 | 103 | k1 = find(n < 0 | p < 0 | p > 1 | round(n) ~= n);
|
< 0.001 | 4 | 104 | if any(k1)
|
| | 105 | y(k1) = NaN;
|
| | 106 | end
|
< 0.001 | 4 | 107 | end
|
Other subfunctions in this file are not included in this listing.