% Poisson approximation to Binomial.        Barnett 1/23/06

% If you don't have the Statistics Toolbox (you probably do), then build
% your own pdfs via `inline functions'. Note: you want input k to be a vector
poisspdf = @(k,l) exp(-l)*l.^k ./ factorial(k);  % k=vec: elementwise .^ ./
binopdf = @(k,n,p) p.^k.*(1-p).^(n-k) .* factorial(n)./(factorial(k).*factorial(n-k));

k = 0:10;                                        % set of k values to use
l = 1;                                           % lambda (expected value)
n = 10;                                          % choose # trials for binomial

y = poisspdf(k, l);
b = binopdf(k, n, l/n);                          % note p = l/n

% make bar graphs... note use of subtraction to examine size of error!
figure; bar(k, [y; b]'); xlabel k; legend('poisson', 'binomial'); % note '
figure; bar(k, y-b); xlabel k; title 'error of poisson approx to binomial'
