{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Standard Errors Shrink with n\n\nLearning goal: Estimate growing subsamples and compare standard errors.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Dataset check\nThis cell confirms the dataset file is available. Simulation notebooks mount a harmless CSV so public file delivery is still validated.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import os\n",
        "DATASET = \"GPA2.csv\"\n",
        "VARIABLES = [\"colgpa\",\"hsperc\",\"sat\"]\n",
        "if not os.path.exists(DATASET):\n",
        "    raise FileNotFoundError(\n",
        "        \"Dataset file not installed yet\\n\"\n",
        "        \"Dataset: GPA2\\n\"\n",
        "        \"Variables needed: colgpa, hsperc, sat\\n\"\n",
        "        \"Course data folder: https://drive.google.com/drive/folders/1_STdcydIcst-opcbwOKRFzUXsgxQgBoS?usp=sharing\\n\"\n",
        "        \"Admin upload instruction: upload the dataset in Admin -> Datasets, publish it, and make the file available to the notebook runner.\"\n",
        "    )\n",
        "print(\"Ready:\", DATASET)\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Run the lab\nRun the code, inspect the table or graph, and connect the result to the formula in the lesson.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import pandas as pd\n",
        "import statsmodels.api as sm\n",
        "import matplotlib.pyplot as plt\n",
        "\n",
        "data = pd.read_csv(\"GPA2.csv\")\n",
        "data = data.dropna(subset=[\"colgpa\", \"hsperc\", \"sat\"])\n",
        "rows = []\n",
        "for n in [200, 500, 1000, 2000, len(data)]:\n",
        "    sample = data.sample(n=min(n, len(data)), random_state=10)\n",
        "    X = sm.add_constant(sample[[\"hsperc\", \"sat\"]])\n",
        "    model = sm.OLS(sample[\"colgpa\"], X).fit()\n",
        "    rows.append({\"n\": len(sample), \"se_hsperc\": model.bse[\"hsperc\"], \"se_sat\": model.bse[\"sat\"]})\n",
        "result = pd.DataFrame(rows)\n",
        "print(result)\n",
        "plt.plot(result[\"n\"], result[\"se_hsperc\"], marker=\"o\", label=\"SE hsperc\")\n",
        "plt.plot(result[\"n\"], result[\"se_sat\"], marker=\"o\", label=\"SE sat\")\n",
        "plt.xlabel(\"sample size\")\n",
        "plt.ylabel(\"standard error\")\n",
        "plt.title(\"Standard errors tend to shrink as n grows\")\n",
        "plt.legend()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Formula explanation\nExplain the probability limit, asymptotic approximation, standard-error pattern, or LM statistic in words. Do not treat output as automatic causal evidence.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Short exercise\nChange one sample size, regressor, or restriction. Write two sentences: what changed mechanically, and what assumption still matters?\n\n## Check your understanding\nDoes increasing n fix nonnormality, endogeneity, heteroskedasticity, or omitted variables? Explain.\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "name": "python",
      "version": "3.11"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}