# OLS Inconsistency from Endogeneity
# Module 5 - Inconsistency and Asymptotic Bias
# Dataset: SIMULATION

# OLS Inconsistency from Endogeneity
#
# Learning goal: Show estimates converging to the wrong target when x and u are correlated.

# 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 = ["rho","beta1"]
if not os.path.exists(DATASET):
    raise FileNotFoundError(
        "Dataset file not installed yet\n"
        "Dataset: SIMULATION\n"
        "Variables needed: rho, beta1\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)
beta1 = 2
rho = 0.7
rows = []
for n in [100, 500, 2000, 10000]:
    estimates = []
    for _ in range(200):
        x = np.random.normal(size=n)
        e = np.random.normal(size=n)
        u = rho * x + e
        y = 1 + beta1 * x + u
        estimates.append(sm.OLS(y, sm.add_constant(x)).fit().params[1])
    rows.append({"n": n, "mean_beta_hat": np.mean(estimates), "wrong_large_sample_target": beta1 + rho})
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.
