0
votes

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:

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?

1

1 Answers

0
votes

It seems you need to increase the number of iterations maxfev, try

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