你的似然函数
它只是高斯分布的概率密度函数的对数之和。
是的可能性为您的残差拟合 mu 和 sigma,而不是可能性给定你的数据的模型。简而言之,你的方法是wrong.
正弦你正在做非线性最小二乘,按照@usethedeathstar已经提到的,你应该直接去F-test
。 。考虑以下示例,修改自http://www.walkingrandomly.com/?p=5254 http://www.walkingrandomly.com/?p=5254,我们进行F-test
using R
。我们将讨论如何将其转化为python
到底。
# construct the data vectors using c()
> xdata = c(-2,-1.64,-1.33,-0.7,0,0.45,1.2,1.64,2.32,2.9)
> ydata = c(0.699369,0.700462,0.695354,1.03905,1.97389,2.41143,1.91091,0.919576,-0.730975,-1.42001)
# some starting values
> p1 = 1
> p2 = 0.2
> p3 = 0.01
# do the fit
> fit1 = nls(ydata ~ p1*cos(p2*xdata) + p2*sin(p1*xdata), start=list(p1=p1,p2=p2))
> fit2 = nls(ydata ~ p1*cos(p2*xdata) + p2*sin(p1*xdata)+p3*xdata, start=list(p1=p1,p2=p2,p3=p3))
# summarise
> summary(fit1)
Formula: ydata ~ p1 * cos(p2 * xdata) + p2 * sin(p1 * xdata)
Parameters:
Estimate Std. Error t value Pr(>|t|)
p1 1.881851 0.027430 68.61 2.27e-12 ***
p2 0.700230 0.009153 76.51 9.50e-13 ***
---
Signif. codes: 0 ?**?0.001 ?*?0.01 ??0.05 ??0.1 ??1
Residual standard error: 0.08202 on 8 degrees of freedom
Number of iterations to convergence: 7
Achieved convergence tolerance: 2.189e-06
> summary(fit2)
Formula: ydata ~ p1 * cos(p2 * xdata) + p2 * sin(p1 * xdata) + p3 * xdata
Parameters:
Estimate Std. Error t value Pr(>|t|)
p1 1.90108 0.03520 54.002 1.96e-10 ***
p2 0.70657 0.01167 60.528 8.82e-11 ***
p3 0.02029 0.02166 0.937 0.38
---
Signif. codes: 0 ?**?0.001 ?*?0.01 ??0.05 ??0.1 ??1
Residual standard error: 0.08243 on 7 degrees of freedom
Number of iterations to convergence: 9
Achieved convergence tolerance: 2.476e-06
> anova(fit2, fit1)
Analysis of Variance Table
Model 1: ydata ~ p1 * cos(p2 * xdata) + p2 * sin(p1 * xdata) + p3 * xdata
Model 2: ydata ~ p1 * cos(p2 * xdata) + p2 * sin(p1 * xdata)
Res.Df Res.Sum Sq Df Sum Sq F value Pr(>F)
1 7 0.047565
2 8 0.053813 -1 -0.0062473 0.9194 0.3696
这里我们有两个模型,fit1
有2个参数,因此残差有8个自由度;fit2
有一个附加参数,并且残差有 7 个自由度。模型 2 明显更好吗?否,F 值为 0.9194,上(1,7)
自由度并不重要。
要获得方差分析表:残差 DF 很容易。残差平方和:0.08202*0.08202*8=0.05381
and 0.08243*0.08243*7=0.04756293
(注意:“剩余标准误差:7 个自由度上的 0.08243”, ETC)。在python
,你可以通过(y_observed-y_fitted)**2
, since scipy.optimize.curve_fit()
不返回残留物。
The F-ratio
is 0.0062473/0.047565*7
并获得 P 值:1-scipy.stats.f.cdf(0.9194, 1, 7)
.
把它们放在一起我们就有了python
相等的:
In [1]:
import scipy.optimize as so
import scipy.stats as ss
xdata = np.array([-2,-1.64,-1.33,-0.7,0,0.45,1.2,1.64,2.32,2.9])
ydata = np.array([0.699369,0.700462,0.695354,1.03905,1.97389,2.41143,1.91091,0.919576,-0.730975,-1.42001])
def model0(x,p1,p2):
return p1*np.cos(p2*x) + p2*np.sin(p1*x)
def model1(x,p1,p2,p3):
return p1*np.cos(p2*x) + p2*np.sin(p1*x)+p3*x
p1, p2, p3 = 1, 0.2, 0.01
fit0=so.curve_fit(model0, xdata, ydata, p0=(p1,p2))[0]
fit1=so.curve_fit(model1, xdata, ydata, p0=(p1,p2,p3))[0]
yfit0=model0(xdata, fit0[0], fit0[1])
yfit1=model1(xdata, fit1[0], fit1[1], fit1[2])
ssq0=((yfit0-ydata)**2).sum()
ssq1=((yfit1-ydata)**2).sum()
df=len(xdata)-3
f_ratio=(ssq0-ssq1)/(ssq1/df)
p=1-ss.f.cdf(f_ratio, 1, df)
In [2]:
print f_ratio, p
0.919387419515 0.369574503394
正如@usethedeathstar 指出的:当你的残差呈正态分布时,非线性最小二乘IS最大可能性。因此F检验和似然比检验是等价的。因为,F-ratio 是似然比 λ 的单调变换.
或者以描述性的方式,请参阅:http://www.stata.com/support/faqs/statistics/chi-squared-and-f-distributions/ http://www.stata.com/support/faqs/statistics/chi-squared-and-f-distributions/