2016-04-01 10 views
2

Ich versuche, eine kumulative Gauß-Verteilung zu meinen Daten anzupassen, aber die Anpassungen sind eindeutig falsch. Warum bekomme ich falsche Mittel und Standardabweichungen? Unten finden Sie meinen Code und die Ausgabe.Wie schätze ich die richtigen Parameter für eine kumulative Gauß-Anpassung?

import numpy as np 
import matplotlib.pyplot as plt 
from scipy.stats import norm 

testrefratios=np.array([ 0.2, 0.4, 0.6, 0.8, 0.9, 1. , 1.1, 1.2, 1.4, 1.6, 1.8]) 
Pn_final=np.array([ 0. , 0. , 0.03 , 0.35 , 0.47, 0.57 , 0.68, 0.73, 0.76 , 0.85 , 0.91]) 
Pd_final=np.array([ 0. , 0.03, 0.36 , 0.85 , 0.97, 0.98 , 0.98 , 0.99 , 1., 1., 1. ]) 

# cumulative gaussian fit 
fg = plt.figure(1); fg.clf() 
ax = fg.add_subplot(1, 1, 1) 
t = np.linspace(0,2, 1000) 

ax.grid(True) 
ax.set_ylabel("Cumulative Probability Density") 
ax.set_title("Fit to Normal Distribution") 

mu1,sigma1 = norm.fit(Pn_final) # classical fit 
ax.plot(t, norm.cdf(t, mu1, sigma1), alpha=.5) 

mu1,sigma1 = norm.fit(Pd_final) # classical fit 
ax.plot(t, norm.cdf(t, mu1, sigma1), alpha=.5) 

ax.plot(testrefratios, Pn_final, 'bo',label='numerosity comparison') 
ax.plot(testrefratios, Pd_final, 'ro', label='density comparison') 

plt.legend(loc='lower right') 


fg.canvas.draw() 

Ausgang:

Fit results with code shown

Antwort

3

Im Moment nichts, was du tust, das System ist zu sagen, dass Sie versuchen, ein kumulativen Gaussian zu passen. norm.fit(Pn_final) tut sein Bestes unter der Annahme, dass Pn_final eine Gaussian darstellt.

Eine Möglichkeit wäre, scipy.optimize.curve_fit zu verwenden, und das Hinzufügen von

from scipy.optimize import curve_fit 

mu1,sigma1 = curve_fit(norm.cdf, testrefratios, Pn_final, p0=[0,1])[0] 
ax.plot(t, norm.cdf(t, mu1, sigma1), alpha=.5) 

mu1,sigma1 = curve_fit(norm.cdf, testrefratios, Pd_final, p0=[0,1])[0] 
ax.plot(t, norm.cdf(t, mu1, sigma1), alpha=.5) 

gibt mir

example fit

, die zumindest glaubhafter aussieht.

+0

Dies behandelt die 'Pn_final' und' Pd_final' Werte als empirische Cdf Werte, richtig? – ayhan

+0

Danke, das ist perfekt. – galliwuzz