Econometric Knowledge

MATH

Standard errors

OLS 的标准误差、稳健误差、HC0、HC1、HC2 推导及 Python 代码实现

同方差和异方差

Yi=α+β1xi+eiY_i=\alpha+\beta_1 x_i+e_i
  • xiRx_i\in \mathbb{R}
  • YiRY_i\in \mathbb{R}
β1=Cov(Yi,xi)V(xi)\beta_1=\frac{Cov(Y_i,x_i)}{V(x_i)} Yi=β0+β1x1i++βkxki++βKxKi+eiY_i=\beta_0+\beta_1x_{1i}+\dots+\beta_k x_{ki}+\dots+\beta_{K}x_{Ki}+e_i
  • xkiRx_{ki}\in \mathbb{R}

  • Xi=[x1i,x2i,,xKi]TRKX_i=[x_{1i},x_{2i},\dots,x_{Ki}]^T\in \mathbb{R}^K

βk=Cov(Yi,x~ki)V(x~ki)\beta_k=\frac{Cov(Y_i,\tilde x_{ki})}{V(\tilde x_{ki})}
  • x~kiRK\tilde x_{ki}\in \mathbb{R}^K

yi=xiTβ+εy=Xβ+ϵβ^=argminβS(β)S(β)=i=1nyixiTβ2=yXβ2y_i=x_i^T\beta+\varepsilon\\ y=X\beta+\epsilon\\ \hat\beta=\arg\min_\beta S(\beta)\\ S(\beta)=\sum_{i=1}^n|y_i-x_i^T\beta|^2=||y-X\beta||^2 S(β)β=XT2(yXβ)=0XTy=XTXββ^=(XTX)1XTyβ^=(XTX)1XT(Xβ+ϵ)=(XTX)1(XTX)β+(XTX)1XTϵβ^=β+(XTX)1XTϵ\frac{\partial S(\beta)}{\partial \beta}=-X^T2(y-X\beta)=0\\ X^Ty=X^TX\beta\\ \downarrow\\ \hat\beta=(X^TX)^{-1}X^Ty\\ \downarrow\\ \hat\beta = (X^TX)^{-1}X^T(X\beta+\epsilon)=(X^TX)^{-1}(X^TX)\beta+(X^TX)^{-1}X^T\epsilon\\ \downarrow\\ \hat\beta=\beta+(X^TX)^{-1}X^T\epsilon y^=Xβ^=X(XTX)1XTy=Pyϵ^=yy^=yXβ^=(IP)y=Myϵ^=M(Xβ+ϵ)=MXβ+Mϵ=Mϵ\hat y=X\hat \beta =X(X^TX)^{-1}X^T y=Py\\ \downarrow\\ \hat\epsilon=y-\hat y=y-X\hat\beta=(I-P)y=My\\ \downarrow\\ \hat\epsilon=M(X\beta+\epsilon)=MX\beta+M\epsilon=M\epsilon
  • PX=X(IP)X=MX=0PX=X\rightarrow (I-P)X=MX=0
y=Xβ+ϵy=X\beta+\epsilon β^OLS=(XTX)1XTy\hat\beta_{OLS}=(X^TX)^{-1}X^Ty V^[β^OLS]=s2(XTX)1\mathbb{\hat V}[\hat\beta_{OLS}]=s^2 (X^TX)^{-1} V^[β^OLS]=E[(β^OLSE[β^OLS])2]=E[(β^OLSβ)2]=E[((XTX)1XTϵ)T((XTX)1XTϵ)]=((XTX)1XT)TE[ϵTϵ]((XTX)1XT)=((XTX)1XT)TV[ϵ]((XTX)1XT)=(XTX)1XTV[ϵ]X(XTX)1\begin{aligned} \mathbb{\hat V}[\hat\beta_{OLS}]&=E[(\hat \beta_{OLS}-E[\hat\beta_{OLS}])^2]\\ &=E[(\hat\beta_{OLS}-\beta)^2]\\ &=E[((X^TX)^{-1}X^T\epsilon)^T((X^TX)^{-1}X^T\epsilon)]\\ &=((X^TX)^{-1}X^T)^T E[\epsilon^T\epsilon] ((X^TX)^{-1}X^T) \\ &=((X^TX)^{-1}X^T)^T \mathbb{V}[\epsilon] ((X^TX)^{-1}X^T) \\ &=(X^TX)^{-1} X^T \mathbb{V}[\epsilon] X (X^TX)^{-1} \end{aligned}
y=Xβ+ϵy=X\beta+\epsilon y^=Xβ^ϵ^=y^y\hat y=X\hat\beta\\ \hat\epsilon=\hat y-y argminβϵTϵ=yXβ2ϵTϵβ=2(X)T(yXβ)=0XTXβ^=XTyβ^=(XTX)1XTy=(XTX)1XT(Xβ+ϵ)β^=(XTX)1XTXβ+(XTX)1XTϵβ^=β+(XTX)1XTϵ\arg\min_{\beta} \epsilon^T\epsilon=||y-X\beta||^2\\ \downarrow\\ \frac{\partial \epsilon^T\epsilon}{\partial \beta}=2(-X)^T(y-X\beta)=0\\ \downarrow\\ X^TX\hat\beta=X^Ty\\ \downarrow\\ \hat\beta=(X^TX)^{-1}X^Ty=(X^TX)^{-1}X^T (X\beta+\epsilon)\\ \downarrow\\ \hat\beta=(X^TX)^{-1}X^TX\beta+(X^TX)^{-1}X^T\epsilon\\ \downarrow\\ \hat\beta=\beta+(X^TX)^{-1}X^T\epsilon V^[β^]=s2(XTX)1s2=iϵ^i2nk\mathbb{\hat V}[\hat\beta]=s^2 (X^TX)^{-1}\\ s^2=\frac{\sum_i\hat\epsilon_i^2}{n-k} V[β^]=E[(XTX)1XTϵϵTX(XTX)1]=(XTX)1XTE[ϵϵT]X(XTX)1=(XTX)1XTV[ϵ]X(XTX)1\begin{aligned} \mathbb{ V}[\hat\beta]&=E[(X^TX)^{-1}X^T\epsilon \epsilon^T X (X^TX)^{-1}]\\ &=(X^TX)^{-1}X^T E[\epsilon\epsilon^T] X(X^TX)^{-1}\\ &=(X^TX)^{-1}X^T \mathbb{V}[\epsilon]X(X^TX)^{-1} \end{aligned}

上面这个算不出来,因为 V[ϵ]\mathbb{V}[\epsilon] 不知道

V^HCE[β^]=(XTX)1XTdiag(σ^1,,σ^n)X(XTX)1\mathbb{\hat V}_{\mathrm{HCE}}[\hat\beta]=(X^TX)^{-1}X^T \mathrm{diag}(\hat\sigma_1,\dots,\hat\sigma_n) X(X^TX)^{-1}

ϵi\epsilon_i 如果是独立的,则 V[ϵ]=diag(σ12,,σn2)\mathbb{V}[\epsilon]=\mathrm{diag} (\sigma_1^2,\dots,\sigma_n^2)

V[ϵ^]=diag(ϵ^12,,ϵ^n2)\mathbb{V}[\hat \epsilon]=\mathrm{diag}(\hat\epsilon_1^2,\dots,\hat\epsilon_n^2)

如果 σ1==σn\sigma_1=\dots=\sigma_n

那么 V[ϵ]=Iσ\mathbb{V [\epsilon]}=\mathbb{I} \sigma