OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Fitting a binomial distribution to a curve with python

  • Thread starter Thread starter Juse Pallati
  • Start date Start date
J

Juse Pallati

Guest
I am trying to fit this list to binomial distribution: [0, 1, 1, 1, 3, 5 , 5, 9, 14, 20, 12, 8, 5, 3, 6, 9, 13, 15, 18, 23, 27, 35, 25, 18, 12, 10, 9, 5 , 0]

I need to retrieve the parameters of the distrbuition so I can apply it to some simulations I need to do. I am using scipy:

Code:
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
from scipy.stats import binom

data = [0, 1, 1, 1, 3, 5 , 5, 9, 14, 20, 12, 8, 5, 3, 6, 9, 13, 15, 18, 23, 27, 35, 25, 18, 12, 10, 9, 5 , 0]

def fit_function(x, n, p):
    return binom.pmf(x, n, p)

num_bins = 10

params, covmat = curve_fit(fit_function, 10,  data)

But I get the following error:



RuntimeError Traceback (most recent call last) in 4 5 # fit with curve_fit ----> 6 parameters, cov_matrix = curve_fit(fit_function, 10, data)

~\AppData\Local\Continuum\anaconda3\envs\py37\lib\site-packages\scipy\optimize\minpack.py in curve_fit(f, xdata, ydata, p0, sigma, absolute_sigma, check_finite, bounds, method, jac, **kwargs) 746 cost = np.sum(infodict['fvec'] ** 2) 747 if ier not in [1, 2, 3, 4]: --> 748 raise RuntimeError("Optimal parameters not found: " + errmsg) 749 else: 750 # Rename maxfev (leastsq) to max_nfev (least_squares), if specified.

RuntimeError: Optimal parameters not found: Number of calls to function has reached maxfev = 600.



Regardless of the error how can I fit this data to a binomial curve with python?
<p>I am trying to fit this list to binomial distribution:
[0, 1, 1, 1, 3, 5 , 5, 9, 14, 20, 12, 8, 5, 3, 6, 9, 13, 15, 18, 23, 27, 35, 25, 18, 12, 10, 9, 5 , 0]</p>
<p>I need to retrieve the parameters of the distrbuition so I can apply it to some simulations I need to do. I am using scipy:</p>
<pre><code>import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
from scipy.stats import binom

data = [0, 1, 1, 1, 3, 5 , 5, 9, 14, 20, 12, 8, 5, 3, 6, 9, 13, 15, 18, 23, 27, 35, 25, 18, 12, 10, 9, 5 , 0]

def fit_function(x, n, p):
return binom.pmf(x, n, p)

num_bins = 10

params, covmat = curve_fit(fit_function, 10, data)
</code></pre>
<p>But I get the following error:</p>
<hr />
<p>RuntimeError Traceback (most recent call last)
in
4
5 # fit with curve_fit
----> 6 parameters, cov_matrix = curve_fit(fit_function, 10, data)</p>
<p>~\AppData\Local\Continuum\anaconda3\envs\py37\lib\site-packages\scipy\optimize\minpack.py in curve_fit(f, xdata, ydata, p0, sigma, absolute_sigma, check_finite, bounds, method, jac, **kwargs)
746 cost = np.sum(infodict['fvec'] ** 2)
747 if ier not in [1, 2, 3, 4]:
--> 748 raise RuntimeError("Optimal parameters not found: " + errmsg)
749 else:
750 # Rename maxfev (leastsq) to max_nfev (least_squares), if specified.</p>
<p>RuntimeError: Optimal parameters not found: Number of calls to function has reached maxfev = 600.</p>
<hr />
<p>Regardless of the error how can I fit this data to a binomial curve with python?</p>
 

Latest posts

A
Replies
0
Views
1
Ashrik Ahamed
A
A
Replies
0
Views
1
Ashrik Ahamed
A

Online statistics

Members online
1
Guests online
4
Total visitors
5
Top