# Robust Standard Errors after WLS
# Module 8 - What If the WLS Variance Model Is Wrong?
# Dataset: MODULE8_FGLS_DEMO

# Robust Standard Errors after WLS
#
# Module 8 notebook lab. This notebook uses original Ceteris Lab teaching data and does not report real empirical findings.

# Learning goal
# Compare WLS conventional and robust standard errors.
#
# Dataset: MODULE8_FGLS_DEMO. Variables: observation_id, outcome, income, age, treatment, variance_driver.

# %% Cell 3
import numpy as np
import pandas as pd
import statsmodels.api as sm

df = pd.read_csv("/data/module-8/module8_fgls_demo.csv")
X = sm.add_constant(df[["income", "age", "treatment"]])
ols_hc1 = sm.OLS(df["outcome"], X).fit(cov_type="HC1")
weights = 1 / df["true_variance"]
wls = sm.WLS(df["outcome"], X, weights=weights).fit()
wls_hc1 = sm.WLS(df["outcome"], X, weights=weights).fit(cov_type="HC1")
print("HC1 OLS SE:", ols_hc1.bse.round(4).to_dict())
print("WLS conventional SE:", wls.bse.round(4).to_dict())
print("WLS HC1 SE:", wls_hc1.bse.round(4).to_dict())

# Reflection
# Write two sentences: one sentence explaining what the diagnostic or robust result says, and one sentence explaining a limitation or next step.
