# Feasible GLS
# Module 8 - Feasible GLS
# Dataset: MODULE8_FGLS_DEMO

# Feasible GLS
#
# Module 8 notebook lab. This notebook uses original Ceteris Lab teaching data and does not report real empirical findings.

# Learning goal
# Estimate a variance function and fit FGLS.
#
# 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 = sm.OLS(df["outcome"], X).fit()
variance_model = sm.OLS(np.log(ols.resid ** 2 + 1e-6), X).fit()
h_hat = np.exp(variance_model.fittedvalues)
fgls = sm.WLS(df["outcome"], X, weights=1 / h_hat).fit()
print("OLS")
print(ols.params.round(4))
print("FGLS")
print(fgls.params.round(4))

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