# Chapter 32: Regularization and High-Dimensional Economic Models
# Fundamentals of Python for Financial Econometrics - Regularization and High-Dimensional Economic Models
# Dataset: Ceteris Lab teaching sample

# Chapter 32: Regularization and High-Dimensional Economic Models
# **Economic question:** How do we build stable predictions when the number of candidate predictors becomes large?
#
# Regularization accepts some bias in exchange for lower variance and more stable out-of-sample prediction.

# %% Cell 2
import numpy as np
from sklearn.linear_model import RidgeCV, LassoCV
rng=np.random.default_rng(32); X=rng.normal(size=(500,30)); b=np.zeros(30); b[:5]=[2,-1.5,1,.5,.3]; y=X@b+rng.normal(size=500)
print('ridge alpha',RidgeCV(alphas=np.logspace(-3,3,40)).fit(X,y).alpha_)
print('lasso alpha',LassoCV(cv=5,random_state=1).fit(X,y).alpha_)

# 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.
