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 pandas_datareader.data 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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.