1

I have created a histogram of Muon decays and want to find the r^2 value and display the function for the curve of best fit that I have graphed.

I am using scipy.stats expon.fit function to generate the curve, how would I go about displaying the generated curve as an equation and the corresponding r^2 value?

I have tried google the problem. Everything I have found has been for scatter plots or for np.polyfit. I have tried print(P), but of course it just printed ordered pairs.

#The Code
P = ss.expon.fit(data.deltaT)
rX = np.linspace(0, 10000, 321)
rP = ss.expon.pdf(rX,* * *P)
plt.hist(data.deltaT,bins=30, normed=True, color='slategrey')
plt.plot(rX, rP, color='darkturquoise')
plt.show()

Thank you for the help!

James Phillips
  • 4,526
  • 3
  • 13
  • 11

2 Answers2

1

After applying James's revisions. I ended up exporting the ordered pairs of rX,rP into a Excel file and finding the trendline that way. Then using df.corr to find the correlation between data.deltaT and rP. Thank you all for the help.

0

Here is an of example using expon in this way with test data:

import numpy as np
import scipy.stats as ss
import matplotlib.pyplot as plt

data = ss. expon.rvs(size=1000)

P = ss.expon.fit(data)
rX = np.linspace(min(data), max(data), 50)
rP = ss.expon.pdf(rX, *P)

plt.hist(data,bins=25, normed=True, color='slategrey')

plt.plot(rX, rP, color='darkturquoise')
plt.show()

plot

James Phillips
  • 4,526
  • 3
  • 13
  • 11
  • 1
    Thank you, that makes my curve fit a lot better. Do you know how I could get the equation of the curve? – Christopher Gamble Apr 11 '19 at 15:34
  • The scipy docs for expon discuss the pdf equation, also stating that the two values for "P" in the above code from the fit are location and scale parameters with discussion of how those two values are used. The fit() method itself is discussed at https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.rv_continuous.fit.html#scipy.stats.rv_continuous.fit and the fitted values are Maximum Likelihood Estimates (MLE) and the fit() docs state "This fit is computed by maximizing a log-likelihood function" and does not give an R-squared value. – James Phillips Apr 11 '19 at 16:29