手动AIC计算与套索拉尔斯IC



我正在尝试手动计算AIC,但是与LassoLarsIC分数相比,我的函数给出了不同的分数。有人可以告诉我我的计算出了什么问题。

这是我的函数:

def aic(y_pred, y, k):
ll = (-1/(2*np.var(y)))*np.sum((y_pred-y)**2) - (len(y)/2)*np.log(np.var(y)) - (len(y)/2)*np.log(2*np.pi)
return -2*ll + 2*k

多谢

编辑:

我的例子很简单,这是完整的代码:

X = np.array([0, 0.1111, 0.2222, 0.3333, 0.4444, 0.5556, 0.6667, 0.7778, 0.8889, 1]).reshape(-1, 1)
y = np.array([0.0528,  0.798 ,  0.8486,  0.8719,  0.1732, -0.3629, -0.7528, -0.9985, -0.6727, -0.1197]).reshape(-1, 1)
poly = plf(9)
F = poly.fit_transform(X)[:, 1:]
scl = StandardScaler()
F = scl.fit_transform(F)
aic_lasso = LassoLarsIC(normalize=False)
aic_lasso.fit(F, y)
aic_lasso.criterion_

输出:

array([10.        ,  7.29642036,  8.9544056 ,  7.06390981,  6.14233987,
7.96489293,  7.76894903,  7.61736515,  7.39575925,  7.25866825,
7.01418447,  6.90314784,  6.6465343 ,  6.60361937,  8.12547536,
8.09620652,  8.09610375, 10.09599191, 12.0959849 , 12.09597075,
12.09596367, 12.09579736, 10.09579645, 10.09579616, 12.09579393,
12.09579199, 12.09579079, 14.09541338, 16.01988119])
y_pred = aic_lasso.predict(F)
aic(y_pred, y, 2)

输出:

146.42615433502792

K 是 2,因为套索将另一个系数设置为 0。

我想这个答案来得太晚了,但错误在于你使用var(y)而不是std(residuals)

这将起作用

def aic(resid, nparams):
n = len (resid)
sig = np.std(resid)
ll = -n*np.log(sig*np.sqrt(np.pi*2))- np.sum((resid / sig)**2)/2
return float(2*nparams - 2*ll)

最新更新