# AIPW & Double Robustness with Cross-Fitting
# MS&E 226
# Date: Sys.Date()

# Setup -------------------------------------------------------------------
library(glmnet)
library(tidyverse)

# Parameter settings
n_ = 300  # Sample size
p_ = 300  # Number of covariates
s_ = 20   # Number of nonzero coefficients
tau_ = 2.0  # Average treatment effect (ATE)
noise_ = 1.5  # Observation noise
n_sims = 200  # Number of "parallel universe" simjulations

# 1. Simulation Setup -----------------------------------------------------
# We are simulating a high-dimensional setting to demonstrate the
# finite-sample properties of three estimators:
#
# 1. IPW: Inverse Probability Weighting
# 2. T-Learner (Plug-in): Difference in Lasso predictions
# 3. AIPW: Augmented IPW
#
# Data Generative Process:
# - N = 200 observations
# - p = 300 covariates (High-dimensional relative to N)
# - s = 20 active features (Sparse truth)
# - Confounding: The covariates affecting assignment T also affect outcome Y

generate_data <- function(n=200, p=300, s=20, tau=2.0, noise=1.5) {

  # 1. Covariates
  X <- matrix(rnorm(n * p), nrow = n, ncol = p)

  # 2. Coefficients (Confounding)
  # First 's' variables affect both T and Y
  beta_w <- rep(0, p)
  beta_y <- rep(0, p)

  # Lasso will shrink these, leaving residual confounding bias.
  beta_y[1:s] <- runif(s, 1, 3) * sample(c(-1, 1), s, replace = TRUE)

  # Strong enough to create imbalance, but not so strong we lose overlap.
  beta_w[1:s] <- runif(s, 0.5, 1.5) * sample(c(-1, 1), s, replace = TRUE)

  # 3. Propensity Scores
  logits <- X %*% beta_w
  ps <- 1 / (1 + exp(-logits))

  # 4. Treatment Assignment
  W_obs <- rbinom(n, 1, ps)

  # 5. Outcome
  mu_0 <- X %*% beta_y
  mu_1 <- mu_0 + tau
  Y <- (W_obs * mu_1) + ((1 - W_obs) * mu_0) + rnorm(n, 0, noise)

  return(list(X=X, W=W_obs, Y=as.vector(Y), ps=ps))
}

# When you run the simulation loop, ensure you pass these defaults:
# data <- generate_data(n=200, p=300, s=20, tau=2.0, noise=1.5)

# 2. Cross-Fitting Implementation ----------------------------------------
# Here we implement the Cross-Fitting procedure. We divide the sample
# into K folds. For each fold, we train our nuisance models (Propensity
# and Outcome models) on the *other* folds, then generate predictions
# for the *current* fold. This ensures our nuisance parameters are
# independent of the data used to estimate the effect.

estimate_effects <- function(data, k_folds=5) {
  X <- data$X
  T_obs <- data$W
  Y <- data$Y
  n <- length(Y)

  # Create Folds
  fold_ids <- sample(rep(1:k_folds, length.out = n))

  # Placeholders for nuisance predictions
  ps_hat <- numeric(n)
  mu0_hat <- numeric(n)
  mu1_hat <- numeric(n)

  # --- CROSS-FITTING LOOP ---
  for(k in 1:k_folds) {
    # Indices
    test_idx <- which(fold_ids == k)
    train_idx <- which(fold_ids != k)

    # 1. Train Propensity Model (Sparse Logistic) on Train Set
    # We use Lasso (alpha=1) for selection
    cv_ps <- cv.glmnet(X[train_idx,], T_obs[train_idx],
                       family="binomial", alpha=1)

    # Predict on Test Set
    ps_hat[test_idx] <- predict(cv_ps, newx=X[test_idx,],
                                s="lambda.min", type="response")

    # 2. Train Outcome Models (Lasso) on Train Set
    # Control Model (T=0 in training set)
    mask0 <- (T_obs[train_idx] == 0)
    if(sum(mask0) > 10) { # Stability check
       cv_mu0 <- cv.glmnet(X[train_idx,][mask0,], Y[train_idx][mask0], alpha=1)
       mu0_hat[test_idx] <- predict(cv_mu0, newx=X[test_idx,], s="lambda.min")
    } else {
       mu0_hat[test_idx] <- mean(Y[train_idx][mask0])
    }

    # Treated Model (T=1 in training set)
    mask1 <- (T_obs[train_idx] == 1)
    if(sum(mask1) > 10) {
       cv_mu1 <- cv.glmnet(X[train_idx,][mask1,], Y[train_idx][mask1], alpha=1)
       mu1_hat[test_idx] <- predict(cv_mu1, newx=X[test_idx,], s="lambda.min")
    } else {
       mu1_hat[test_idx] <- mean(Y[train_idx][mask1])
    }
  }

  # --- CALCULATE ESTIMATORS ---

  # Clip propensities for numerical stability
  ps_hat <- pmax(0.02, pmin(0.98, ps_hat))

  # 1. IPW Estimator
  # (TY)/e - ((1-T)Y)/(1-e)
  ipw_terms <- (T_obs * Y) / ps_hat - ((1 - T_obs) * Y) / (1 - ps_hat)
  est_ipw <- mean(ipw_terms)

  # 2. T-Learner (Plug-in)
  est_tlearner <- mean(mu1_hat - mu0_hat)

  # 3. AIPW Estimator
  # (mu1 - mu0) + T/e(Y-mu1) - (1-T)/(1-e)(Y-mu0)
  resid1 <- (T_obs / ps_hat) * (Y - mu1_hat)
  resid0 <- ((1 - T_obs) / (1 - ps_hat)) * (Y - mu0_hat)

  est_aipw <- mean( (mu1_hat - mu0_hat) + resid1 - resid0 )

  return(c(IPW = est_ipw, T_Learner = est_tlearner, AIPW = est_aipw))
}

# 3. Run Simulation --------------------------------------------------------
# We run the simulation loop M times to approximate the sampling distributions.

set.seed(42)
M <- n_sims # Number of simulations

# File to save/load results
results_file <- "lecture15_AIPW_results.rds"

# Set this to TRUE to force re-running the simulation
FORCE_RERUN <- FALSE

# Check if results already exist
if (file.exists(results_file) && !FORCE_RERUN) {
  cat("Loading existing simulation results from", results_file, "\n")
  results <- readRDS(results_file)
  cat("Loaded", nrow(results), "simulation results\n\n")
} else {
  cat("Starting simulation...\n")
  cat("Progress: Running", M, "simulations\n")

  # Initialize results matrix
  results <- matrix(NA, nrow = M, ncol = 3)
  colnames(results) <- c("IPW", "T_Learner", "AIPW")

  # Run simulations with progress indicator
  for(i in 1:M) {
    # Show progress every 10 simulations
    if (i %% 10 == 0) {
      cat("  Completed", i, "/", M, "simulations\n")
    }

    # Generate data and estimate effects
    data <- generate_data(n=n_, p=p_, s=s_, tau=tau_)
    results[i, ] <- estimate_effects(data, k_folds=5)
  }

  cat("Simulation complete!\n")

  # Save results for future use
  saveRDS(results, results_file)
  cat("Results saved to", results_file, "\n\n")
}

# Convert to DataFrame for plotting
df_res <- as.data.frame(results) %>%
  pivot_longer(cols = everything(), names_to = "Estimator", values_to = "Estimate")

# 4. Calculate Performance Statistics -------------------------------------
true_tau <- tau_

# Calculate bias and variance for each estimator
stats <- as.data.frame(results) %>%
  pivot_longer(cols = everything(), names_to = "Estimator", values_to = "Estimate") %>%
  group_by(Estimator) %>%
  summarize(
    Mean = mean(Estimate),
    Bias = mean(Estimate) - true_tau,
    Variance = var(Estimate),
    RMSE = sqrt(mean((Estimate - true_tau)^2)),
    .groups = "drop"
  )

cat("========================================\n")
cat("Performance Statistics -- True ATE: ", true_tau, "\n")
cat("========================================\n\n")
print(stats, digits = 4)
cat("\n")

# Save statistics to CSV
write_csv(stats, "lecture15_AIPW_statistics.csv")
cat("Statistics saved to lecture15_AIPW_statistics.csv\n\n")

# 5. Results and Visualization --------------------------------------------
# The plot below illustrates the trade-offs.
#
# - IPW (Red): Notice the spread. The variance is high because small
#   propensity scores in the denominator amplify the noise.
# - T-Learner (Green): Notice the center. It is shifted away from the
#   dashed line (True tau=2). This is regularization bias; Lasso shrinks
#   coefficients toward zero, leading to residual confounding.
# - AIPW (Blue): It is centered closer to the truth than the T-Learner
#   (bias correction) and is narrower than IPW (variance reduction).

# Calculate means for vertical lines
means <- df_res %>%
  group_by(Estimator) %>%
  summarize(avg = mean(Estimate))

# Create the plot
p <- ggplot(df_res, aes(x = Estimate, fill = Estimator)) +
  # Density Plots
  geom_density(alpha = 0.4) +
  # True Treatment Effect Line
  geom_vline(xintercept = true_tau, linetype = "dashed", size = 1, color = "black") +
  # Label Truth
  annotate("text", x = true_tau + 0.05, y = 0.5, label = "Truth", angle = 90) +
  # Aesthetics
  scale_fill_manual(values = c("IPW" = "#E74C3C",
                               "T_Learner" = "#27AE60",
                               "AIPW" = "#2980B9")) +
  labs(title = "Sampling Distributions of Treatment Effect Estimators",
       subtitle = "Comparing IPW, T-Learner (Lasso), and AIPW under High-Dimensional Confounding",
       x = "Estimated ATE",
       y = "Density") +
  theme_minimal() +
  theme(legend.position = "bottom")

# Display the plot
print(p)

# Save the plot
ggsave("lecture15_AIPW_plot.pdf", plot = p, width = 10, height = 6)
ggsave("lecture15_AIPW_plot.png", plot = p, width = 10, height = 6, dpi = 300)
cat("\nPlots saved to lecture15_AIPW_plot.pdf and lecture15_AIPW_plot.png\n")
