{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Chapter 33: Decision Trees, Random Forests, and Boosting for Economic Prediction\n",
        "**Economic question:** Can flexible nonlinear models improve prediction without pretending to identify causal effects?\n",
        "\nTree ensembles can approximate complex nonlinear prediction functions, but their feature importance is not a causal effect.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor\n",
        "from sklearn.model_selection import train_test_split\n",
        "from sklearn.metrics import mean_squared_error\n",
        "rng=np.random.default_rng(33); X=rng.normal(size=(1000,8)); y=2*X[:,0]**2+np.sin(X[:,1])+X[:,2]+rng.normal(size=1000)\n",
        "Xt,Xv,yt,yv=train_test_split(X,y,test_size=.3,random_state=33)\n",
        "for m in [RandomForestRegressor(n_estimators=200,random_state=1),GradientBoostingRegressor(random_state=1)]:\n",
        " m.fit(Xt,yt); print(type(m).__name__,mean_squared_error(yv,m.predict(Xv))**.5)\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Interpretation checklist\n",
        "- State the unit of observation and units of every variable.\n",
        "- Separate association, prediction, and causation.\n",
        "- Report magnitude and uncertainty.\n",
        "- Identify the most important threat to validity.\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "name": "python",
      "version": "3.13"
    },
    "ceteris_lab": {
      "course_slug": "fundamentals-python-econometrics",
      "course_title": "Fundamentals of Python for Financial Econometrics",
      "chapter": 33,
      "source_origin": "supplied"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
