Mercurial > hg > octave-terminal
changeset 5179:53f80b6d98d3
[project @ 2005-03-03 05:25:23 by jwe]
author | jwe |
---|---|
date | Thu, 03 Mar 2005 05:26:57 +0000 |
parents | 6758c11b5b99 |
children | e7438487c857 |
files | scripts/statistics/distributions/binomial_pdf.m |
diffstat | 1 files changed, 15 insertions(+), 17 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/statistics/distributions/binomial_pdf.m +++ b/scripts/statistics/distributions/binomial_pdf.m @@ -33,31 +33,29 @@ usage ("binomial_pdf (x, n, p)"); endif - if (!isscalar (n) || !isscalar (p)) + if (! isscalar (n) || ! isscalar (p)) [retval, x, n, p] = common_size (x, n, p); if (retval > 0) error ("binomial_pdf: x, n and p must be of common size or scalar"); endif endif - sz = size (x); - pdf = zeros (sz); - - k = find (isnan (x) | !(n >= 0) | (n != round (n)) | !(p >= 0) | !(p <= 1)); - if (any (k)) - pdf(k) = NaN; - endif + k = ((x >= 0) & (x <= n) + & (x == round (x)) & (n == round (n)) + & (p >= 0) & (p <= 1)); - k = find ((x >= 0) & (x <= n) & (x == round (x)) - & (n == round (n)) & (p >= 0) & (p <= 1)); - if (any (k)) - if (isscalar (n) && isscalar (p)) - pdf(k) = (bincoeff (n, x(k)) .* (p .^ x(k)) - .* ((1 - p) .^ (n - x(k)))); - else - pdf(k) = (bincoeff (n(k), x(k)) .* (p(k) .^ x(k)) - .* ((1 - p(k)) .^ (n(k) - x(k)))); + pdf = zeros (size (x)); + pdf(! k) = NaN; + if (any (k(:))) + x = x(k); + if (! isscalar (n)) + n = n(k); endif + if (! isscalar (p)) + p = p(k); + endif + z = gammaln(n+1) - gammaln(x+1) - gammaln(n-x+1) + x.*log(p) + (n-x).*log(1-p); + pdf(k) = exp (z); endif endfunction