{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Weighted Least Squares\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",
        "Choose inverse-variance weights and compare OLS with WLS.\n",
        "\n",
        "Dataset: MODULE8_INCOME_SAVINGS_SYNTHETIC. Variables: household_id, income, savings, age, education, family_size.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import pandas as pd\n",
        "import statsmodels.api as sm\n",
        "\n",
        "df = pd.read_csv(\"/data/module-8/module8_income_savings_heteroskedastic.csv\")\n",
        "X = sm.add_constant(df[[\"income\", \"age\", \"education\"]])\n",
        "weights = 1 / (df[\"error_scale\"] ** 2)\n",
        "ols = sm.OLS(df[\"savings\"], X).fit(cov_type=\"HC1\")\n",
        "wls = sm.WLS(df[\"savings\"], X, weights=weights).fit()\n",
        "print(\"Robust OLS coefficients\")\n",
        "print(ols.params.round(4))\n",
        "print(\"WLS coefficients\")\n",
        "print(wls.params.round(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": "Weighted Least Squares Intuition",
      "dataset": "MODULE8_INCOME_SAVINGS_SYNTHETIC",
      "copyright": "Original Ceteris Lab notebook. No external text, tables, or figures are reproduced."
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
