I am trying to plot a chi squared probability density function trained on some experimental data at different conditions in python. My code is shown below.
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as ss
data= [] #read from CSV file.
chi_linespace = np.linspace(4, 1500, len(data))
x,y,z = ss.chi2.fit(data)
pdf_chi2 = ss.chi2.pdf(linespace, x,y,z)
plt.hist(data, bins=100, density=True, alpha=0.3)
plt.plot(linespace, pdf_chi2, label='Chi2')
plt.legend()
plt.show()
I have roughly 1500 observations per example, and when I run the code below most of the time I get a nice fit distribution.
I am finding sometimes when I run the same code on a different dataset the probability density function explodes at ~0 and does not appear to be fit from the dataset at all.
Has someone experienced this before, and how did they go about resolving this?