given is a sample (r) in form of a numpy array. I created a histogram from that sample:
plt.hist(x=r, bins='auto', color='#0504aa',alpha=0.7, rwidth=0.85)
objective_prob, bin_edges = np.histogram(r, bins='auto', density=True)
bin_centers = 0.5*(bin_edges[1:] + bin_edges[:-1])
What I tried to do here was to get the values of a possible PDF, which is supposed to be defined as
def f(x, N):
return N/k*T*np.exp(-N*x**2/2*k*T)
with x being the sample (r in that case), and N the parameter to be fitted. k and T are constants. It's a Boltzmann distribution.
First question: Is my array "objective_prob" correct in the sense that it correctly gives the values of a Probability Density Function (PDF)? I am asking because I'm unsure if I understood the 'normed=True' argument correctly. Second question: Am I right to use the array bin_centers for my x-axis?
Next, (the important step). I want to do the fit (so that I get parameter N from my function f)
params, params_covariance = curve_fit(f, bin_centers , objective_prob, p0=None)
Here I use my array bin_centers as x-data, and objective_prob as y-data. Now, the value for N is completely off, and if I try to plot it with
plt.figure(figsize=(6, 4))
plt.plot(bin_centers, objective_prob, label="Histogram")
plt.plot(bin_centers, f(bin_centers, params[0]), 'r-')
plt.legend()
plt.show()
I get a straight line for my fitted curve. So third question: Is my curve_fit wrong? Where else could I be wrong? Is it correct to use the respective arrays in my curve_fit, or am I using the bin-centers wrongly?
Any help is greatly appreciated! Thanks.