# Consistency Simulation
# Module 5 - What Consistency Means
# Dataset: SIMULATION

# Consistency Simulation
#
# Learning goal: Simulate OLS consistency under exogeneity.

# 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 = ["beta0","beta1","n"]
if not os.path.exists(DATASET):
    raise FileNotFoundError(
        "Dataset file not installed yet\n"
        "Dataset: SIMULATION\n"
        "Variables needed: beta0, beta1, n\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

np.random.seed(42)
beta0 = 1
beta1 = 2

def simulate_ols(n):
    x = np.random.normal(size=n)
    u = np.random.normal(size=n)
    y = beta0 + beta1 * x + u
    return sm.OLS(y, sm.add_constant(x)).fit().params[1]

rows = []
for n in [25, 50, 100, 500, 2000]:
    estimates = [simulate_ols(n) for _ in range(500)]
    rows.append({"n": n, "mean_beta_hat": np.mean(estimates), "std_beta_hat": np.std(estimates)})
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.
