2
$\begingroup$

I'm trying to fit a Poisson Distribution to data produced in a lab. I've successfully made a Histogram of the data, which looks like:

enter image description here

However, when I try to fit the Poisson Distribution using:

Show[
  Histogram[poisson, {0, 9, 1}, "PDF", 
    ChartLegends -> {"Experimental Result"}, ChartStyle -> "Pastel"],
  DiscretePlot[PDF[PoissonDistribution[2.2766917293233084`], x], {x, 0, 10}, 
    PlotStyle -> {Red, Medium}, 
    PlotLegends -> {"Theoretical Poisson"}],
    AxesLabel -> {"Time (s)", "Counts"}]

I get a fit that alters the y-axis:

enter image description here

Where the red dots represent the Poisson fit. I have several questions:

  1. Is my fit even representative of my data?
  2. How can I change the y-axis of the Poisson distribution?
  3. How can I get a $\chi ^2$ value for this distribution?

Edit:

Here is the data for reference:

{1., 1., 1., 2., 1., 1., 2., 2., 1., 2., 1., 4., 2., 3., 6., 0., 0., 
 3., 3., 3., 3., 0., 0., 0., 3., 1., 5., 3., 1., 3., 5., 2., 2., 1.,
 0., 4., 2., 3., 4., 3., 3., 2., 0., 4., 0., 0., 0., 2., 2., 7., 0.,
 2., 3., 2., 1., 4., 3., 7., 1., 1., 4., 5., 0., 2., 1., 1., 3., 2.,
 3., 3., 3., 4., 0., 2., 2., 2., 5., 4., 2., 0., 0., 3., 2., 2., 2.,
 0., 2., 1., 1., 1., 3., 4., 2., 2., 2., 1., 0., 3., 3., 1., 2., 5.,
 0., 1., 2., 2., 4., 2., 2., 0., 2., 4., 4., 1., 2., 4., 1., 2., 1.,
 1., 0., 5., 3., 4., 2., 2., 1., 1., 1., 7., 1., 1., 3., 3., 0., 4.,
 2., 5., 4., 0., 1., 4., 0., 2., 3., 1., 4., 1., 1., 1., 1., 2., 0.,
 4., 0., 2., 1., 4., 1., 4., 4., 2., 7., 3., 2., 3., 0., 0., 1., 2.,
 0., 2., 2., 2., 3., 1., 4., 1., 3., 3., 2., 3., 1., 3., 2., 2., 6.,
 0., 2., 1., 3., 1., 1., 3., 3., 4., 2., 0., 5., 3., 2., 2., 5., 5.,
 1., 2., 2., 2., 8., 3., 2., 5., 2., 5., 3., 1., 8., 3., 1., 3., 3.,
 2., 3., 3., 4., 2., 4., 2., 7., 2., 1., 1., 1., 4., 5., 2., 4., 2.,
 3., 1., 2., 0., 1., 2., 2., 3., 3., 2., 3., 0., 1., 4., 0., 2., 2.,
 1., 1., 4., 1., 1., 2., 2., 1., 1., 2., 0., 7., 1., 4., 2., 4., 2.,
 6., 5., 2., 3., 1., 3., 5., 0., 2., 1., 2., 3., 1., 4., 2., 1., 2.,
 2., 3., 2., 0., 2., 5., 2., 2., 1., 2., 1., 5., 2., 4., 2., 3., 0.,
 2., 0., 4., 3., 2., 2., 1., 1., 1., 2., 4., 1., 4., 2., 0., 0., 6.,
 4., 1., 2., 4., 0., 0., 0., 5., 3., 3., 1., 0., 1., 1., 4., 0., 0.,
 1., 2., 5., 2., 0., 0., 3., 3., 4., 1., 3., 1., 4., 1., 0., 4., 2.,
 1., 6., 0., 4., 1., 2., 3., 5., 3., 4., 3., 2., 2., 3., 3., 4., 3.,
 1., 3., 2., 5., 1., 5., 4., 1., 2., 5., 1., 2., 2., 0., 1., 3., 1.,
 3., 1., 3., 2., 3., 4., 0., 0., 2., 1., 5., 0., 2., 3., 3., 3., 1.,
 3., 2., 8., 2., 1., 2., 2., 1., 1., 1., 4., 0., 1., 3., 3., 2., 2.,
 2., 2., 2., 3., 2., 2., 4., 6., 3., 5., 4., 4., 4., 1., 2., 1., 3.,
 4., 1., 3., 4., 0., 1., 1., 4., 3., 3., 1., 0., 2., 2., 0., 3., 1.,
 2., 1., 1., 0., 1., 0., 1., 4., 0., 3., 1., 2., 4., 3., 1., 1., 4., 
 1., 5., 2., 4., 1., 3., 2., 2., 2., 3., 3., 3., 0., 3., 2., 6., 1.,
 8., 3., 3., 2., 2., 2., 3., 1., 2., 0., 6., 0., 2., 2., 0., 3., 4.,
 1., 1., 2., 3., 3., 2., 0., 3., 2., 2., 5., 3., 5., 1., 2., 3., 1.,
 3., 1., 2., 0., 3., 0., 3., 5., 4., 1., 2., 0., 0., 2., 5., 1., 1.,
 3., 2., 4., 2., 3., 2., 2., 4., 2., 0., 1., 3., 4., 1., 3., 1., 2.,
 3., 3., 1., 1., 3., 3., 2., 3., 4., 0., 2., 1., 3., 1., 1., 0., 1.,
 2., 1., 2., 1., 3., 3., 6., 3., 4., 2., 1., 3., 1., 3., 3., 1., 7.,
 1., 1., 4., 3., 3., 3., 3., 3., 3., 4., 4., 3., 3., 3., 1., 3., 3.,
 1., 3., 0., 5., 3., 3., 1., 1., 1., 4., 2., 1., 2., 4., 2., 2., 3.,
 4., 3., 2., 4., 2., 3., 1., 6., 1., 3., 2., 0., 3., 1., 2., 1., 2.,
 5., 2., 2., 1., 3., 4., 1., 2., 3., 1., 3., 4., 5., 1., 1., 3., 0.,
 1., 1.}
$\endgroup$
2
  • $\begingroup$ Hang on, I'll look into my crystal ball to see your data... in any case, multiply the PDF by the count. Use something like DsitributionFitTest to check fit... Please, in the future, provide minimal but complete examples (any data etc.) - more likely to get a response. $\endgroup$
    – ciao
    Jul 10, 2014 at 0:56
  • 1
    $\begingroup$ If your data is discrete, then there is a MASS at each point $x = 0, 1, 2, 3$, etc ... and nothing inbetween. Accordingly, you should not be using a Histogram that places mass over the say 1.5 to 2.5, as if your data is continuous ... because it is not. $\endgroup$
    – wolfies
    Jul 10, 2014 at 6:29

2 Answers 2

3
$\begingroup$
p1 = Histogram[data, {0, 9, 1}, "Count", 
   ChartLegends -> {"Experimental Result"}, ChartStyle -> "Pastel"];

p2 = DiscretePlot[
   Length@data*PDF[PoissonDistribution[2.2766917293233084`], x], {x, 
    0, 10}, PlotStyle -> {Red, Medium}, 
   PlotLegends -> {"Theoretical Poisson"}];

Show[{p1, p2}, AxesLabel -> {"Time (s)", "Counts"}]

enter image description here

DistributionFitTest[data, PoissonDistribution[2.2766917293233084`], 
                    "HypothesisTestData"]["TestDataTable", All]

enter image description here

$\endgroup$
2
$\begingroup$

Although I do realise that it is a bit late, maybe I can contribute a bit more to this. I really do like ciao's method and I think it gave good points. Here I'd like to show you a potentially more in depth way to do this.

{HistData, {binHistData}} = Reap[Histogram[Evaluate@data, Automatic,
Function[{bins, counts}, Sow[counts]], ImageSize -> Medium, 
ChartStyle -> "Pastel"]];

{HistData, ListLinePlot[binHistData, PlotRange -> Full, ImageSize -> Medium]}

enter image description here

What this does is to scan through the Histogram and 'pick' the end points. Now this gives you a more qualitative idea of to how the distribution looks like. What is really important however is that you use the correct binning size. For Poissonian statistics, Mathematica is pretty good, so I left the algorithm to Mathematica (set to Automatic).

The only 'peculiarity' of this code is that it leaves the important data (binHistData) within another list.

Now lets find a good fit:

  1. using your fit for mu (I suspect hand-picked?)
  2. using mathematica FindFit function
  3. using iterative chi-squared minimisation

So:

(* your fit*)
\[Mu]given = 2.2766917293233084;
kgiven = Log@Length@data // N;

(* FindFit *)
fit1 = {\[Mu] -> \[Mu]given, k -> kgiven}
fit2 = FindFit[Flatten@binHistData, Exp[-\[Mu] + k] \[Mu]^x/x!, {\[Mu], k}, x]

(* Iterative \[Chi]^2 minimisation fit *)
start = 0.1; stop = Length@Union@data; step = 0.01;
iterFit = Table[DistributionFitTest[Flatten@data, PoissonDistribution[i], 
  "HypothesisTestData"]["TestDataTable", All][[1, 1, 2, 2]], {i,start, Length@Union@data,step}];
pos = Flatten@Position[test, Min@test];
\[Mu]Iter = start + pos[[1]]*step;
fit3 = {\[Mu] -> \[Mu]Iter, k -> Log@Length@data // N}

(* {\[Mu] -> 2.27669, k -> 6.49979}*)
(* {\[Mu] -> 3.22453, k -> 6.58071}*)
(* {\[Mu] -> 2.28, k -> 6.49979}*)

A Poissonian distribution has the form that is shown in the FindFit function. The parameter k, is just a constant in an exponential so it results to the amplitude of the distribution. As you can see, your hand-picked value of mu is pretty close to what the iterative version found. However, the FindFit function seems to fail on the topic, even though it got the amplitude parameter right.

The last fit, checks the chi squared value for every value of mu, between 0.1 and the size of values in the data. It then extracts the position of the minimum and uses it to calculate the value of mu used for the minimum. This is then fed into the fit3 to provide a fitted model.

Here is the combined plots for each fit. Although you can't see it, Your fit, and the iterative fit are so close, that they overlap.

Show[Histogram[data, Automatic], Plot[Evaluate[model2 /. {fit1, fit2, fit3}], {x, 0, 25},  PlotStyle -> {Red, Blue, Green}]]


fits = {fit1[[1, 2]], fit2[[1, 2]], fit3[[1, 2]]}
Table[DistributionFitTest[Flatten@data, PoissonDistribution[fits[[i]]], "HypothesisTestData"][ "TestDataTable", All], {i, 1, 3}]

Here is now a comparison of the statistics involved (similarly to what ciao suggested you do) enter image description here

I have crammed a few plots together as I don't have enough reputation to add multiple images :P. The second plot, is how the iterative technique zeroes towards the value of the smallest chi squared.

I hope that helps,

A.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.