{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Omitted-Variable Inconsistency\n\nLearning goal: Compare true and omitted models as n grows.\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 = [\"beta2\",\"covariance\"]\n",
        "if not os.path.exists(DATASET):\n",
        "    raise FileNotFoundError(\n",
        "        \"Dataset file not installed yet\\n\"\n",
        "        \"Dataset: SIMULATION\\n\"\n",
        "        \"Variables needed: beta2, covariance\\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",
        "\n",
        "np.random.seed(44)\n",
        "beta1 = 1\n",
        "beta2 = 2\n",
        "rows = []\n",
        "for corr in [-0.6, 0.6]:\n",
        "    for n in [100, 500, 2000, 10000]:\n",
        "        estimates = []\n",
        "        for _ in range(200):\n",
        "            x1 = np.random.normal(size=n)\n",
        "            x2 = corr * x1 + np.random.normal(scale=np.sqrt(1 - corr**2), size=n)\n",
        "            y = 1 + beta1 * x1 + beta2 * x2 + np.random.normal(size=n)\n",
        "            estimates.append(sm.OLS(y, sm.add_constant(x1)).fit().params[1])\n",
        "        rows.append({\"corr_x1_x2\": corr, \"n\": n, \"mean_omitted_slope\": np.mean(estimates)})\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
}