# Omitted-Variable Inconsistency
# Module 5 - Omitted Variable Inconsistency
# Dataset: SIMULATION

# Omitted-Variable Inconsistency
#
# Learning goal: Compare true and omitted models as n grows.

# 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 = ["beta2","covariance"]
if not os.path.exists(DATASET):
    raise FileNotFoundError(
        "Dataset file not installed yet\n"
        "Dataset: SIMULATION\n"
        "Variables needed: beta2, covariance\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(44)
beta1 = 1
beta2 = 2
rows = []
for corr in [-0.6, 0.6]:
    for n in [100, 500, 2000, 10000]:
        estimates = []
        for _ in range(200):
            x1 = np.random.normal(size=n)
            x2 = corr * x1 + np.random.normal(scale=np.sqrt(1 - corr**2), size=n)
            y = 1 + beta1 * x1 + beta2 * x2 + np.random.normal(size=n)
            estimates.append(sm.OLS(y, sm.add_constant(x1)).fit().params[1])
        rows.append({"corr_x1_x2": corr, "n": n, "mean_omitted_slope": np.mean(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.
