def __init__(self, data, mleValue, fitParameters=True, mean=None, sigma=None):
super(normalModel, self).__init__(data)
self.MLE = mleValue
if(None in [mean, sigma]):
fitParameters=True
if(fitParameters):
mean = Mean(self.getDataSet())
sigma = standardDeviation(self.getDataSet())
try:
def normDist(x, x0, sigma):
output = 1.0/sqrt(2*np.pi*(sigma**2))
output *= exp(-0.5*((x - x0)/sigma)**2)
return output
self.n, self.bins, patches = plt.hist(self.getDataSet(), self.getDatasetSize()/10, normed=1, facecolor='blue', alpha = 0.55)
popt,pcov = curve_fit(normDist,self.bins[:-1], self.n, p0=[mean, sigma])
##plt.plot(bins[:-1], gaus(bins[:-1],*popt),'c-',label="Gaussian Curve with params\na=%f\nx0=%f\nsigma=%f" % (popt[0], popt[1], popt[2]), alpha=0.5)
print "Fitted gaussian curve to data with params x0 %f, sigma %f" % (popt[0], popt[1])
self.x0 = popt[0]
self.sigma = popt[1]
self.fitted = True
except RuntimeError:
print "Unable to fit data to normal curve, runtime error"
raise
except Warning:
raise RuntimeError
else:
self.x0 = mean
self.sigma = sigma
评论列表
文章目录