# Chapter 28: Matching, Propensity Scores, Weighting, and Doubly Robust Estimation
# Fundamentals of Python for Financial Econometrics - Matching, Propensity Scores, Weighting, and Doubly Robust Estimation
# Dataset: Ceteris Lab teaching sample

# Chapter 28: Matching, Propensity Scores, Weighting, and Doubly Robust Estimation
# **Economic question:** How can observational studies improve comparability when treatment is not randomized?
#
# Propensity methods rebalance observed covariates; they do not eliminate bias from unobserved confounding.

# %% Cell 2
import numpy as np
from sklearn.linear_model import LogisticRegression
rng=np.random.default_rng(28); n=1500; x=rng.normal(size=(n,3)); ps=1/(1+np.exp(-(x[:,0]-.5*x[:,1]))); d=rng.binomial(1,ps); y=2*d+x[:,0]+rng.normal(size=n)
logit=LogisticRegression().fit(x,d); phat=logit.predict_proba(x)[:,1]
w=d/phat+(1-d)/(1-phat)
print('IPW ATE',np.average(d*y/phat)-np.average((1-d)*y/(1-phat)))

# 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.
