# Asymptotic Normality
# Module 5 - Large-Sample Inference without Normal Errors
# Dataset: SIMULATION

# Asymptotic Normality
#
# Learning goal: Show coefficient distributions becoming approximately normal.

# Dataset check
# This cell confirms the dataset file is available. Simulation notebooks mount a harmless CSV so public file delivery is still validated.

# %% Cell 3
import os
DATASET = "WAGE1.csv"
VARIABLES = ["sample size","error distribution"]
if not os.path.exists(DATASET):
    raise FileNotFoundError(
        "Dataset file not installed yet\n"
        "Dataset: SIMULATION\n"
        "Variables needed: sample size, error distribution\n"
        "Course data folder: https://drive.google.com/drive/folders/1_STdcydIcst-opcbwOKRFzUXsgxQgBoS?usp=sharing\n"
        "Admin upload instruction: upload the dataset in Admin -> Datasets, publish it, and make the file available to the notebook runner."
    )
print("Ready:", DATASET)

# Run the lab
# Run the code, inspect the table or graph, and connect the result to the formula in the lesson.

# %% Cell 5
import numpy as np
import pandas as pd
import statsmodels.api as sm
import matplotlib.pyplot as plt

np.random.seed(91)
rows = []
for error_type in ["normal", "skewed", "discrete"]:
    estimates = []
    for _ in range(400):
        n = 600
        x = np.random.normal(size=n)
        if error_type == "normal":
            u = np.random.normal(size=n)
        elif error_type == "skewed":
            u = np.random.exponential(size=n) - 1
        else:
            u = np.random.choice([-1, 0, 2], size=n, p=[0.45, 0.45, 0.10])
        y = 1 + 0.5 * x + u
        estimates.append(sm.OLS(y, sm.add_constant(x)).fit().params[1])
    rows.append({"error_type": error_type, "mean": np.mean(estimates), "std": np.std(estimates)})
    plt.hist(estimates, bins=24, alpha=0.45, label=error_type)
plt.axvline(0.5, color="black", linestyle="--")
plt.title("Coefficient estimates can look normal even when errors do not")
plt.xlabel("beta_hat")
plt.legend()
print(pd.DataFrame(rows))

# Formula explanation
# Explain the probability limit, asymptotic approximation, standard-error pattern, or LM statistic in words. Do not treat output as automatic causal evidence.

# Short exercise
# Change one sample size, regressor, or restriction. Write two sentences: what changed mechanically, and what assumption still matters?
#
# Check your understanding
# Does increasing n fix nonnormality, endogeneity, heteroskedasticity, or omitted variables? Explain.
