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

invtrinom_pdf(n, nn, p, q)
% invtrinom_pdf.m - evaluates an Inverse Trinomial Probability Density.
%   See "A Generalization of the Inverse Trinomial", K. Aoyama and K. Shimizu, Keio U., 
%   23 June, 2005.
%
%  Created by Jim Huntley,  8/31/06
%

function [pdf] = invtrinom_pdf(n, nn, p, q)

%persistent r logp logq logr

%if(isempty(r))
    r = 1 - p - q;
    logp = log(p);
    logq = log(q);
    logr = log(r);
%end

%nfac = factorial(n);
nfac = gammaln(n+1);
sum1 = 0;
a = fix((n-nn+eps)/2);

for jk = 1:a+1
    %sum1 = sum1 + nn * nfac * p^(nn+jk-1) * q^(n-nn-2*jk+2) * r^(jk-1) / ...
    %      (factorial(nn+jk-1) * factorial(n-nn-2*jk+2) * factorial(jk-1) * n);
    sum1 = sum1 + nn * exp(nfac + (nn+jk-1)*logp + (n-nn-2*jk+2)*logq + (jk-1)*logr - ...
          (gammaln(nn+jk) + gammaln(n-nn-2*jk+3) + gammaln(jk))) / n;  
end

pdf = sum1;

return

Contact us