手动进行多重线性回归的AIC评分

问题描述 投票:0回答:1

我已经手动实现了一个多元线性回归类,现在我正在研究度量方法。我尝试手动计算AIC和BIC分数,但结果不正确。原因是我没有使用对数似然函数,而是使用了SSE方法。您能否建议我如何更改实施方式以计算完整的AIC和BIC分数?

这是我的方法现在的样子:

  def AIC_BIC(self, actual = None, pred = None):
    if actual is None:
      actual = self.response
    if pred is None:
      pred = self.response_pred

    n = len(actual)
    k = self.num_features

    residual = np.subtract(pred, actual)
    RSS = np.sum(np.power(residual, 2))

    AIC = n * np.log(RSS / n) + 2 * k
    BIC = n * np.log(RSS / n) + k * np.log(n)

    return (AIC, BIC)

[请尝试给我一种手动方法,而不要给图书馆打电话。谢谢!

python python-3.x machine-learning linear-regression data-science
1个回答
0
投票

我设法在@James的帮助下解决了这个问题。这是我的新实现的样子:

 def LogLikelihood(self):
     n = self.num_obs
     k = self.num_features
     residuals = self.residuals

     ll = -(n * 1/2) * (1 + np.log(2 * np.pi)) - (n / 2) * np.log(residuals.dot(residuals) / n)

     return ll

  def AIC_BIC(self):
    ll = self.LogLikelihood()
    n = self.num_obs
    k = self.num_features + 1

    AIC = (-2 * ll) + (2 * k)
    BIC = (-2 * ll) + (k * np.log(n))

    return AIC, BIC

我实现了对数似然计算,并将其用于可在Wikipedia上找到的公式中。

© www.soinside.com 2019 - 2024. All rights reserved.