function [number,pois,X,observed,mu,N,p] = poissonfit(series,M);
% function [number,pois,X,observed,mu,N,p] = poissonfit(series,M);
% determine Poisson distribution consistent with time series of
% event occurrences. Calculate probabilities for each value of
% n over the range n=0 to n=M.
%
% M should be chosen large enough so that probability of n>M is
% sufficiently close to zero
%
N=sum(series)
mu = mean(series)
p = mu/N
sigma = sqrt(mu)
pois(1)= exp(-mu);
numb(1)=0;
for i=1:M-1
   number(i+1)=i;
   pois(i+1) = (mu^i)*exp(-mu)/factorial(i);
end
for i=1:M
X(i)=i-1;
end
% calculate actual histogram at each value of n
[histo] = hist(series,X);
% normalize actual histogram to sum to 1
observed = histo/sum(histo);
