# Standard Errors Shrink with n
# Module 5 - Asymptotic Standard Errors
# Dataset: GPA2

# Standard Errors Shrink with n
#
# Learning goal: Estimate growing subsamples and compare standard errors.

# 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 = "GPA2.csv"
VARIABLES = ["colgpa","hsperc","sat"]
if not os.path.exists(DATASET):
    raise FileNotFoundError(
        "Dataset file not installed yet\n"
        "Dataset: GPA2\n"
        "Variables needed: colgpa, hsperc, sat\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 pandas as pd
import statsmodels.api as sm
import matplotlib.pyplot as plt

data = pd.read_csv("GPA2.csv")
data = data.dropna(subset=["colgpa", "hsperc", "sat"])
rows = []
for n in [200, 500, 1000, 2000, len(data)]:
    sample = data.sample(n=min(n, len(data)), random_state=10)
    X = sm.add_constant(sample[["hsperc", "sat"]])
    model = sm.OLS(sample["colgpa"], X).fit()
    rows.append({"n": len(sample), "se_hsperc": model.bse["hsperc"], "se_sat": model.bse["sat"]})
result = pd.DataFrame(rows)
print(result)
plt.plot(result["n"], result["se_hsperc"], marker="o", label="SE hsperc")
plt.plot(result["n"], result["se_sat"], marker="o", label="SE sat")
plt.xlabel("sample size")
plt.ylabel("standard error")
plt.title("Standard errors tend to shrink as n grows")
plt.legend()

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