Fitting distribution to data (scipy/fitter/etc.)

Viewed 46

I have the following data:

y = np.array([8.8,7.2,5.8,4.7,3.8,3.1,2.6,2.2,2.0,1.7,1.8,1.8,1.9,1.7,1.4,1.2,1.7,1.2,1.5])   
x = np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19])  

I wish to fit a distribution to this data.

I've tried using scipy and fitter, but the distributions were of poor fit.

I got results akin to this example.

  1. Why do the distributions in said example seem to be scaled below the true data?
  2. Using my data, how do I fit a reasonable distribution? Any worked examples would be greatly appreciated.
1 Answers

I solved the problem via these steps:

(1) Warren's answer outlined that I couldn't fit a PDF - the 'area under the curve' was far greater than 1, and it should equal 1.

(2) Instead, I fit a curve to my data via the following code:

# Create a function which can create your line of best fit. In my case it's a 5PL equation. 
def func_5PL(x, d, a, c, b, g):
    return d + ((a-d)/((1+((x/c)**b))**g))

# Determine the coefficients for your equation.
popt_mock, _ = curve_fit(func_5PL, x, y)

# Plot the real data, along with the line of best fit. 
plt.plot(x, func_5PL(x, *popt_mock), label='line of best fit')
plt.scatter(x, y, label='real data')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()

my data, when a curve it fit to it

(4) When I had the curve, I just rescaled it such that it's integral was equal to 1 (for the range of x values that I was interested in). I treated this as my pdf.

Related