How to do it...

Follow these steps to implement the four- and five-factor models in Python.

  1. Import the libraries:
import pandas as pdimport yfinance as yfimport statsmodels.formula.api as smfimport as web
  1. Specify the risky asset and the time horizon:
RISKY_ASSET = 'AMZN'START_DATE = '2013-12-31'END_DATE = '2018-12-31'
  1. Download the risk factors from prof. French's website:
# three factors df_three_factor = web.DataReader('F-F_Research_Data_Factors',                                  'famafrench', start=START_DATE)[0]df_three_factor.index = df_three_factor.index.format()# momentum factordf_mom = web.DataReader('F-F_Momentum_Factor', 'famafrench',                         start=START_DATE)[0]df_mom.index = df_mom.index.format()# five factorsdf_five_factor = web.DataReader('F-F_Research_Data_5_Factors_2x3', ...

Get Python for Finance Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.