{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Asymptotic Normality\n\nLearning goal: Show coefficient distributions becoming approximately normal.\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 = \"WAGE1.csv\"\n",
        "VARIABLES = [\"sample size\",\"error distribution\"]\n",
        "if not os.path.exists(DATASET):\n",
        "    raise FileNotFoundError(\n",
        "        \"Dataset file not installed yet\\n\"\n",
        "        \"Dataset: SIMULATION\\n\"\n",
        "        \"Variables needed: sample size, error distribution\\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 numpy as np\n",
        "import pandas as pd\n",
        "import statsmodels.api as sm\n",
        "import matplotlib.pyplot as plt\n",
        "\n",
        "np.random.seed(91)\n",
        "rows = []\n",
        "for error_type in [\"normal\", \"skewed\", \"discrete\"]:\n",
        "    estimates = []\n",
        "    for _ in range(400):\n",
        "        n = 600\n",
        "        x = np.random.normal(size=n)\n",
        "        if error_type == \"normal\":\n",
        "            u = np.random.normal(size=n)\n",
        "        elif error_type == \"skewed\":\n",
        "            u = np.random.exponential(size=n) - 1\n",
        "        else:\n",
        "            u = np.random.choice([-1, 0, 2], size=n, p=[0.45, 0.45, 0.10])\n",
        "        y = 1 + 0.5 * x + u\n",
        "        estimates.append(sm.OLS(y, sm.add_constant(x)).fit().params[1])\n",
        "    rows.append({\"error_type\": error_type, \"mean\": np.mean(estimates), \"std\": np.std(estimates)})\n",
        "    plt.hist(estimates, bins=24, alpha=0.45, label=error_type)\n",
        "plt.axvline(0.5, color=\"black\", linestyle=\"--\")\n",
        "plt.title(\"Coefficient estimates can look normal even when errors do not\")\n",
        "plt.xlabel(\"beta_hat\")\n",
        "plt.legend()\n",
        "print(pd.DataFrame(rows))"
      ]
    },
    {
      "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
}