# Fit Poisson ditribution to histogram

**URL:** <https://discourse.julialang.org/t/fit-poisson-ditribution-to-histogram/48883>\
**Category:** Statistics\
**Tags:** question\
**Created:** [October 23, 2020, 1:32pm UTC](https://discourse.julialang.org/t/fit-poisson-ditribution-to-histogram/48883 "2020-10-23T13:32:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![phlavenk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phlavenk/32/1873_2.png) [@phlavenk](https://discourse.julialang.org/u/phlavenk)\
**Post date:** [October 23, 2020, 1:32pm UTC](https://discourse.julialang.org/t/fit-poisson-ditribution-to-histogram/48883/1 "2020-10-23T13:32:04Z")

</div>

Is there a way how to fit a Poisson distribution (determine the lambda parameter) from a histogram of an experimental process?

In the experimental setup, in each event we are recording the number of detected photons and the experiment records the histogram of a huge number of the observation. So, I don’t have access to the recordings of each observations - I can only acces the histogram of a huge number of observations.

However, the `Distributions.fit(D::Distribution, x)` expects that I can provide an array of observations. Is there a way how to use the histogram instead and fit lambda parameter of the Poisson distribution to the histogram?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 23, 2020, 1:46pm UTC](https://discourse.julialang.org/t/fit-poisson-ditribution-to-histogram/48883/2 "2020-10-23T13:46:08Z")

</div>

There might be more ready-to-go options, but otherwise you can use LsqFit to fit your data as you wish:

> **[GitHub - JuliaNLSolvers/LsqFit.jl: Simple curve fitting in Julia](https://github.com/JuliaNLSolvers/LsqFit.jl)**
>
> Simple curve fitting in Julia. Contribute to JuliaNLSolvers/LsqFit.jl development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![jonathanBieler](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jonathanBieler](https://discourse.julialang.org/u/jonathanBieler)\
**Post date:** [October 23, 2020, 4:16pm UTC](https://discourse.julialang.org/t/fit-poisson-ditribution-to-histogram/48883/3 "2020-10-23T16:16:15Z")

</div>

For a Poisson distribution lambda is just the mean of your data, which you can compute from your histogram.That said it would be nice to have interface that can take histogram-like data, since those are quite common. Otherwise better use maximum likelihood than least-square to fit a distribution.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [October 23, 2020, 6:11pm UTC](https://discourse.julialang.org/t/fit-poisson-ditribution-to-histogram/48883/4 "2020-10-23T18:11:50Z")

</div>

Adding to previous answers, if you only have access to samples in a stream of data, consider using OnlineStats.jl to compute the mean as the parameter of the distribution you are after.

---

<div class="post-metadata">

**Author:** ![NightRider](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nightrider/32/19003_2.png) [@NightRider](https://discourse.julialang.org/u/NightRider)\
**Post date:** [October 28, 2020, 7:43am UTC](https://discourse.julialang.org/t/fit-poisson-ditribution-to-histogram/48883/5 "2020-10-28T07:43:43Z")

</div>

As mentioned above the MLE would just be the mean, and then Poisson(lambda) would be the fitted distribution.

One perhaps more general possibility is to think about what a histogram is at the core and how you can potentially simulate data from the histogram itself. You are basically binning the data, and then each bin can be approximated by a Uniform(a,b) distribution. And if you have the proportion of the data in each bin (based on the Y-axis) you can approximate the empirical CDF. You now will also have the empirical CDF cutoffs for each bin.

Then you just simulate P from a Uniform(0,1) distribution and where P lands determines the bin to simulate from Uniform(a,b).

This leads to approximate data mimicking, and you can repeat the procedure many times to get a simulated mean (or whatever else).

But really if you have the mean value then that already gives you the maximum likelihood Poisson fit. The algorithm above is more of a nonparametric method that can be useful for other things too.
