I have this polynomial fit to my data.
fig3 = plt.figure(3)
for dataset in [Bxfft]:
dataset = np.asarray(dataset)
freqs, psd = signal.welch(dataset, fs=266336/300, window='hamming', nperseg=8192)
plt.semilogy(freqs, psd/dataset.size**0, color='r')
# Polynomial 5th grade
def line(freqs, a, b, c, d, e, f):
return a*freqs**5 + b*freqs**4 + c*freqs**3 + d*freqs**2 + e*freqs + f
popt, pcov = curve_fit(line, freqs, np.log10(psd))
plt.semilogy(freqs, 10**line(freqs, popt[0], popt[1], popt[2], popt[3], popt[4], popt[5]), 'black')
This is what I get:
I would like to count chi square, but to be honest I have no idea how to do it. I was able to do something like this, but I think this is wrong.
chisquare = chi(popt)
print chisquare
Power_divergenceResult(statistic=-0.4318298090941465, pvalue=1.0)
chi-square is generally defined as sum-of-squares of
data-fit
. For your example that would be:Allow me to also suggest using lmfit (https://lmfit.github.io/lmfit-py/) for curve-fitting as an alternative to
curve_fit
that handles many such chores. Usinglmfit
, your example might look like this: