In this Notebook we will build synthetic data suitable to Alphalens analysis. This is useful to understand how Alphalens expects the input to be formatted and also it is a good testing environment to experiment with Alphalens.
In [1]:
%matplotlib inline
from numpy import nan
from pandas import (DataFrame, date_range)
import matplotlib.pyplot as plt
from alphalens.tears import (create_returns_tear_sheet,
create_information_tear_sheet,
create_turnover_tear_sheet,
create_summary_tear_sheet,
create_full_tear_sheet,
create_event_returns_tear_sheet,
create_event_study_tear_sheet)
from alphalens.utils import get_clean_factor_and_forward_returns
In [2]:
#
# build price
#
price_index = date_range(start='2015-1-10', end='2015-2-28')
price_index.name = 'date'
tickers = ['A', 'B', 'C', 'D', 'E', 'F']
data = [[1.0025**i, 1.005**i, 1.00**i, 0.995**i, 1.005**i, 1.00**i]
for i in range(1, 51)]
prices = DataFrame(index=price_index, columns=tickers, data=data)
#
# build factor
#
factor_index = date_range(start='2015-1-15', end='2015-2-13')
factor_index.name = 'date'
factor = DataFrame(index=factor_index, columns=tickers,
data=[[3, 4, 2, 1, nan, nan], [3, nan, nan, 1, 4, 2],
[3, 4, 2, 1, nan, nan], [3, 4, 2, 1, nan, nan],
[3, 4, 2, 1, nan, nan], [3, 4, 2, 1, nan, nan],
[3, nan, nan, 1, 4, 2], [3, nan, nan, 1, 4, 2],
[3, 4, 2, 1, nan, nan], [3, 4, 2, 1, nan, nan],
[3, nan, nan, 1, 4, 2], [3, nan, nan, 1, 4, 2],
[3, nan, nan, 1, 4, 2], [3, nan, nan, 1, 4, 2],
[3, nan, nan, 1, 4, 2], [3, nan, nan, 1, 4, 2],
[3, nan, nan, 1, 4, 2], [3, nan, nan, 1, 4, 2],
[3, nan, nan, 1, 4, 2], [3, nan, nan, 1, 4, 2],
[3, 4, 2, 1, nan, nan], [3, 4, 2, 1, nan, nan],
[3, 4, 2, 1, nan, nan], [3, 4, 2, 1, nan, nan],
[3, 4, 2, 1, nan, nan], [3, 4, 2, 1, nan, nan],
[3, 4, 2, 1, nan, nan], [3, 4, 2, 1, nan, nan],
[3, nan, nan, 1, 4, 2], [3, nan, nan, 1, 4, 2]]) \
.stack()
factor_groups = {'A': 'Group1', 'B': 'Group2', 'C': 'Group1', 'D': 'Group2', 'E': 'Group1', 'F': 'Group2'}
In [3]:
prices.plot()
Out[3]:
In [4]:
prices.head()
Out[4]:
In [5]:
factor.head(10)
Out[5]:
In [6]:
factor_data = get_clean_factor_and_forward_returns(
factor,
prices,
groupby=factor_groups,
quantiles=4,
periods=(1, 3),
filter_zscore=None)
In [7]:
factor_data.head(10)
Out[7]:
In [8]:
create_full_tear_sheet(factor_data, long_short=False, group_neutral=False, by_group=False)
create_event_returns_tear_sheet(factor_data, prices, avgretplot=(3, 11),
long_short=False, group_neutral=False, by_group=False)
In [9]:
create_full_tear_sheet(factor_data, long_short=True, group_neutral=False, by_group=True)
create_event_returns_tear_sheet(factor_data, prices, avgretplot=(3, 11),
long_short=True, group_neutral=False, by_group=True)
In [10]:
create_full_tear_sheet(factor_data, long_short=True, group_neutral=True, by_group=True)
create_event_returns_tear_sheet(factor_data, prices, avgretplot=(3, 11),
long_short=True, group_neutral=True, by_group=True)