In [1]:
import sys
sys.path.append('/Users/george/Desktop/pyfolio/')
sys.path
Out[1]:
In [2]:
import pyfolio as pf
import matplotlib.pyplot as plt
%matplotlib inline
# silence warnings
import warnings
warnings.filterwarnings('ignore')
In [4]:
# Get the single stock returns
stock_rets = pf.utils.get_symbol_rets('FB')
In [5]:
# With just the stock returns, we can plot the rolling betas to the Fama-French factors.
# No need to actually compute the rolling betas; pyfolio does that for us!
fig, ax = plt.subplots(figsize=[14, 6])
pf.plotting.plot_rolling_fama_french(stock_rets, ax=ax)
Out[5]:
In [6]:
# However, for the bayesian tear sheet, we will actually need the rolling betas,
# so use pyfolio to get them
rolling_beta = pf.timeseries.rolling_fama_french(stock_rets)
# pf.timeseries.rolling_beta defaults to a 6-month trailing window.
# Thus, the first 6 months' data will be NaNs, which we must drop
rolling_beta.dropna(inplace=True)
In [7]:
rolling_beta.head()
Out[7]:
In [ ]:
# Suppose the last 2 months were our out-of-sample period
out_of_sample = stock_rets.index[-60]
# Use pyfolio to run the bayesian tear sheet.
# The bayesian tear sheet's back end makes heavy use of pymc3, so there will be
# a lot of graphical output before the actual tear sheet
pf.tears.create_bayesian_tear_sheet(stock_rets, live_start_date=out_of_sample, benchmark_rets=rolling_beta)
In [ ]: