Introduction to Portfolio Risk Management in Python
Dakota Wixom
Quantitative Analyst | QuantCourse.com
$$ R_{P} = $$
$$ RF + \beta_{M}(R_{M}-RF)+b_{SMB} \cdot SMB + b_{HML} \cdot HML + \alpha $$
Assuming you already have excess portfolio and market returns in the object Data
:
import statsmodels.formula.api as smf
model = smf.ols(formula='Port_Excess ~ Mkt_Excess + SMB + HML',
data=Data)
fit = model.fit()
adjusted_r_squared = fit.rsquared_adj
adjusted_r_squared
0.90
To extract the HML
p-value, assuming you have a fitted regression model object in your workspace as fit
:
fit.pvalues["HML"]
0.0063
To test if it is statistically significant, simply examine whether or not it is less than a given threshold, normally 0.05:
fit.pvalues["HML"] < 0.05
True
To extract the HML
coefficient, assuming you have a fitted regression model object in your workspace as fit
:
fit.params["HML"]
0.502
fit.params["SMB"]
-0.243
Assuming you already have a fitted regression analysis in the object fit
:
portfolio_alpha = fit.params["Intercept"]
portfolio_alpha_annualized = ((1 + portfolio_alpha) ** 252) - 1
portfolio_alpha_annualized
0.045
Introduction to Portfolio Risk Management in Python