# Asymptotic Efficiency Simulation
# Module 5 - Asymptotic Efficiency of OLS
# Dataset: SIMULATION

# Asymptotic Efficiency Simulation
#
# Learning goal: Compare OLS with an alternative consistent estimator.

# 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 = ["x","z","variance"]
if not os.path.exists(DATASET):
    raise FileNotFoundError(
        "Dataset file not installed yet\n"
        "Dataset: SIMULATION\n"
        "Variables needed: x, z, variance\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(70)
beta1 = 2
rows = []
for _ in range(600):
    n = 300
    x = np.random.uniform(0.2, 2.5, size=n)
    u = np.random.normal(size=n)
    y = 1 + beta1 * x + u
    ols = sm.OLS(y, sm.add_constant(x)).fit().params[1]
    z = np.log1p(x**2)
    alt = np.sum((z - z.mean()) * y) / np.sum((z - z.mean()) * x)
    rows.append({"OLS": ols, "alternative": alt})
result = pd.DataFrame(rows)
print(result.agg(["mean", "std"]))
result.hist(bins=30)
plt.suptitle("Consistent estimators can have different spread")

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