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

poiss_cdf(x,lambda)
function p = poiss_cdf(x,lambda)
%POISS_CDF Poisson cumulative distribution function.
%   P = POISS_CDF(X,LAMBDA) computes the Poisson cumulative
%   distribution function with parameter LAMBDA at the values in X.
%
%   The size of P is the common size of X and LAMBDA. A scalar input   
%   functions as a constant matrix of the same size as the other input.    

%   References:
%      [1]  M. Abramowitz and I. A. Stegun, "Handbook of Mathematical
%      Functions", Government Printing Office, 1964, 26.1.22.

%   Copyright 1993-2000 The MathWorks, Inc. 
%   $Revision: 2.12 $  $Date: 2000/05/26 18:53:27 $
 
if nargin < 2, 
    error('Requires two input arguments.'); 
end

scalarlambda = (prod(size(lambda)) == 1);

[errorcode x lambda] = distchck(2,x,lambda);

if errorcode > 0
    error('Requires non-scalar arguments to match in size.');
end

% Initialize P to zero.
p = zeros(size(x));

% Return NaN if Lambda is not positive.
p(lambda < 0) = NaN;

% Compute P when X is positive.
xx = floor(x);
k = find(xx >= 0 & lambda >= 0);
val = max(xx(:));

if scalarlambda
  tmp = cumsum(poiss_pdf(0:val,lambda(1)));            
  p(k) = tmp(xx(k) + 1);
else
  compare = repmat((0:val)',1,length(k));
  bigx = xx(k);
  bigx = bigx(:);
  bigx = bigx(:,ones(1,val+1))';
  biglambda = lambda(k);
  biglambda = biglambda(:);
  biglambda = biglambda(:,ones(1,val+1))';
  index = (bigx >= compare);
  indicator = zeros(size(compare));
  indicator(index) = poiss_pdf(compare(index),biglambda(index));
  p(k) = sum(indicator,1);
end 

% Make sure that round-off errors never make P greater than 1.
p(p>1) = 1;

return

Contact us