# Chapter 33: Decision Trees, Random Forests, and Boosting for Economic Prediction
# Fundamentals of Python for Financial Econometrics - Decision Trees, Random Forests, and Boosting for Economic Prediction
# Dataset: Ceteris Lab teaching sample

# Chapter 33: Decision Trees, Random Forests, and Boosting for Economic Prediction
# **Economic question:** Can flexible nonlinear models improve prediction without pretending to identify causal effects?
#
# Tree ensembles can approximate complex nonlinear prediction functions, but their feature importance is not a causal effect.

# %% Cell 2
import numpy as np
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
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)
Xt,Xv,yt,yv=train_test_split(X,y,test_size=.3,random_state=33)
for m in [RandomForestRegressor(n_estimators=200,random_state=1),GradientBoostingRegressor(random_state=1)]:
 m.fit(Xt,yt); print(type(m).__name__,mean_squared_error(yv,m.predict(Xv))**.5)

# Interpretation checklist
# - State the unit of observation and units of every variable.
# - Separate association, prediction, and causation.
# - Report magnitude and uncertainty.
# - Identify the most important threat to validity.
