Code covered by the BSD License  

Highlights from
Generation of Random Variates

image thumbnail

Generation of Random Variates

by

James Huntley (view profile)

 

generates random variates from over 870 univariate distributions

negbinbin_pdf(n, q, Q, k, nn)
% negbinbin_pdf.m - evaluates a Negative Binomial Binomial Probability Density.
%   See "Univariate Discrete Distributions", Johnson, Kemp & Kotz,
%   J. Wiley, 2005, 334. 
%
%  Created by Jim Huntley,  7/17/07
%

function [pdf] = negbinbin_pdf(n, q, Q, k, nn)

%persistent lcoef arg qnn lratio logQ

%if(isempty(arg))
    p = 1 - q;
    P = Q - 1;
    %coef = nn * p * q^(nn-1);
    lcoef = log(nn * p * q^(nn-1));
    arg = -p * Q / (P * q);
    qnn = q^nn;
    %ratio = P / Q;
    lratio = log(P / Q);
    logQ = log(Q);
%end

pdf = qnn;
if(n > 0)
    %pdf = coef * gamma(k*n+n-1) * ratio^(n-1) * Q^(-k*n) * ...
    %      hypergeom([1-n 1-nn],2-n-k*n,arg) / (gamma(n+1)*gamma(k*n));
    pdf = exp(lcoef + gammaln(k*n+n-1) + (n-1)*lratio + (-k*n)*logQ + ...
          log(genHyper([1-n 1-nn],2-n-k*n,arg)) - (gammaln(n+1)+gammaln(k*n)));
end

return

Contact us