Code covered by the BSD License

# Generation of Random Variates

### James Huntley (view profile)

generates random variates from over 870 univariate distributions

compoisok_pdf(n, k, alpha, c)
```% compoisok_pdf.m - evaluates a Compound Poisson Order K Probability denisity.
%   See "Univariate Discrete Distributions", Johnson, Kemp, and Kotz,
%
%   Created by  J. Huntley,  12/07/06.
%
%

function [pdf] = compoisok_pdf(n, k, alpha, c)

%persistent odkpa lodkpa coef facm1

%if(isempty(odkpa))
%Initializations.
odkpa = 1 / (k+alpha);
lodkpa = log(odkpa);
coef = (alpha *odkpa)^c;
facm1 = gammaln(c);
%end

if(n == 0)
pdf = coef;
elseif(n > 0)

% Fetch pre-stored partitions of 'n'. Frequencies returned in array, "d".
pname = ['partition' num2str(n)];
d = double(d)
spc = size(d,2);
spr = size(d,1);

% Select rows of 'd' with only min(k,spc) columns populated and store in array, 'xs'.
indx = 0;
for jr = 1:spr
if(size(find(d(jr,k+1:spc)),2)== 0)
indx = indx + 1;
dd(indx,:) = d(jr,:);
end
end
xlim = min(k,spc);
xs = dd(1:indx,1:xlim);

% Calculate PDF.
% Sum over solutions up to order 'k' for Diophantine Equation.
sum1 = 0;
sx1 = size(xs,1);
for jr = 1:sx1
for jc = 1:xlim
x(jc) = xs(jr,jc);
fx(jc) = gammaln(x(jc)+1);
end
sumx = sum(x);
pfx = sum(fx) + facm1;
sum1 = sum1 + exp(gammaln(sumx+c) + sumx*lodkpa - pfx);
end
pdf = coef * sum1;
end  % n > 0

return

```