function mu_fit = fitPoisson(data)
% This function will do a least squares fitting of a Poisson distribution
% to the data k,N.  These data are assumed to come from a histogram, the
% variable k is the list of histogram bin values (the number of counts in a
% given time interval) and N is the a list of bin populations (the number
% of times a particular count occured).  The fit parameter lam_fit is the
% mean value of the poisson distribution, and is the return value of the
% fitting. 
k = data(:,1);
n = data(:,2);
pdf = n/sum(n);

mu_guess = sum(pdf.*k);
mu_fit = fminsearch(@compSSE,mu_guess);

figure(1)
plot(k,pdf,'o',k,poisspdf(k,mu_fit));
title(sprintf('Fit with $\\mu = %0.2f$',mu_fit),'interpreter','latex')

function sse = compSSE(lam)
    sse = sum((pdf-poisspdf(k,lam)).^2);
end


end
