IV and weak IV
import numpy as np
import matplotlib.pyplot as plt
We simulate $$ y_i = x_i \beta + \beta^o u_i + \epsilon_i $$ $$ x_i = z_i \kappa + u_i$$
N=500
kappa = 0.1
beta = 1.0
beta_ommited = 0.3
def sim_iv(N,kappa,beta,beta_omitted):
Z = np.random.normal(size=N)
U = np.random.normal(size=N)
X = kappa * Z + U
Y = beta * X + beta_omitted*U + np.random.normal(size=N)
beta_iv = np.cov(Y,Z)[0,1] / np.cov(X,Z)[0,1]
beta_ols = np.cov(Y,X)[0,1] / np.cov(X,X)[0,1]
return(beta_iv,beta_ols)
betas = [sim_iv(N=25, kappa=0.5, beta=1.0, beta_omitted= -0.3)
for _ in range(10000)]
betas_ols = [ b[1] for b in betas]
betas_iv = [ b[0] for b in betas]
np.mean(betas_ols),np.mean(betas_iv)
(0.7599633955536516, 1.005229337989456)
plt.hist(np.clip(betas_iv,-4,6),50,color="blue",alpha=0.3)
#plt.hist(np.clip(betas_ols,-4,6),50,color="orange",alpha=0.3)
plt.axvline(np.mean(betas_iv),color="blue",label="IV")
plt.axvline(np.mean(betas_ols),color="orange",label="OLS")
plt.axvline(1.0,color="red",label="Truth")
plt.legend()
plt.show()
np.quantile(betas_iv,[0.025,0.975])
array([-6.05349953, 8.20573923])