# Chapter 23: Instrumental Variables and Two-Stage Least Squares
# Fundamentals of Python for Financial Econometrics - Instrumental Variables and Two-Stage Least Squares
# Dataset: Ceteris Lab teaching sample

# Chapter 23: Instrumental Variables and Two-Stage Least Squares
# **Economic question:** Can we isolate useful variation in an endogenous explanatory variable?
#
# An instrument is useful only if it moves the endogenous variable and affects the outcome through the permitted channel.

# %% Cell 2
import numpy as np, statsmodels.api as sm
rng=np.random.default_rng(23)
z=rng.normal(size=500); u=rng.normal(size=500); x=.9*z+u
e=.7*u+rng.normal(size=500); y=1+2*x+e
first=sm.OLS(x,sm.add_constant(z)).fit(); xhat=first.fittedvalues
second=sm.OLS(y,sm.add_constant(xhat)).fit()
print('first-stage slope',first.params[1]); print('educational 2SLS slope',second.params[1])

# Interpretation checklist
# - State the unit of observation and units of every variable.
# - Separate association, prediction, and causation.
# - Report magnitude and uncertainty.
# - Identify the most important threat to validity.
