{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# White Test\n",
        "\n",
        "Module 8 notebook lab. This notebook uses original Ceteris Lab teaching data and does not report real empirical findings.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Learning goal\n",
        "Use full and fitted-value White diagnostics.\n",
        "\n",
        "Dataset: MODULE8_HOUSING_VARIANCE_SYNTHETIC. Variables: house_id, price, log_price, lotsize, sqrft, bedrooms.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import pandas as pd\n",
        "import statsmodels.api as sm\n",
        "from statsmodels.stats.diagnostic import het_white\n",
        "\n",
        "df = pd.read_csv(\"/data/module-8/module8_housing_variance_synthetic.csv\")\n",
        "X = sm.add_constant(df[[\"sqrft\", \"lotsize\", \"bedrooms\"]])\n",
        "model = sm.OLS(df[\"price\"], X).fit()\n",
        "lm, lm_pvalue, fvalue, f_pvalue = het_white(model.resid, X)\n",
        "print({\"White LM\": round(lm, 4), \"LM p-value\": round(lm_pvalue, 4), \"F\": round(fvalue, 4), \"F p-value\": round(f_pvalue, 4)})\n",
        "\n",
        "import pandas as pd\n",
        "import statsmodels.api as sm\n",
        "\n",
        "df = pd.read_csv(\"/data/module-8/module8_housing_variance_synthetic.csv\")\n",
        "X = sm.add_constant(df[[\"sqrft\", \"lotsize\", \"bedrooms\"]])\n",
        "model = sm.OLS(df[\"price\"], X).fit()\n",
        "aux_x = pd.DataFrame({\"fitted\": model.fittedvalues, \"fitted_sq\": model.fittedvalues ** 2})\n",
        "aux = sm.OLS(model.resid ** 2, sm.add_constant(aux_x)).fit()\n",
        "print(aux.summary().tables[1])\n",
        "print(\"Special White LM = nR^2:\", round(len(df) * aux.rsquared, 4))\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Reflection\n",
        "Write two sentences: one sentence explaining what the diagnostic or robust result says, and one sentence explaining a limitation or next step.\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "name": "python",
      "version": "3.11"
    },
    "ceteris_lab": {
      "module": "Module 8",
      "lesson": "The White Test",
      "dataset": "MODULE8_HOUSING_VARIANCE_SYNTHETIC",
      "copyright": "Original Ceteris Lab notebook. No external text, tables, or figures are reproduced."
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
