by Delaney Granizo-Mackenzie and Maxwell Margenot
https://www.quantopian.com/lectures/integration-cointegration-and-stationarity
This lecture corresponds to the Integration, Cointegration, and Stationarity lecture, which is part of the Quantopian lecture series. This homework expects you to rely heavily on the code presented in the corresponding lecture. Please copy and paste regularly from that lecture when starting to work on the problems, as trying to do them from scratch will likely be too difficult.
Part of the Quantopian Lecture Series:
In [1]:
# Useful Functions
def check_for_stationarity(X, cutoff=0.01):
# H_0 in adfuller is unit root exists (non-stationary)
# We must observe significant p-value to convince ourselves that the series is stationary
pvalue = adfuller(X)[1]
if pvalue < cutoff:
print 'p-value = ' + str(pvalue) + ' The series is likely stationary.'
return True
else:
print 'p-value = ' + str(pvalue) + ' The series is likely non-stationary.'
return False
def generate_datapoint(params):
mu = params[0]
sigma = params[1]
return np.random.normal(mu, sigma)
In [2]:
# Useful Libraries
import numpy as np
import pandas as pd
import statsmodels
import statsmodels.api as sm
from statsmodels.tsa.stattools import coint, adfuller
import matplotlib.pyplot as plt
In [3]:
QQQ = get_pricing("QQQ", start_date='2014-1-1', end_date='2015-1-1', fields='price')
QQQ.name = QQQ.name.symbol
check_for_stationarity(QQQ)
Out[3]:
In [4]:
from statsmodels.stats.stattools import jarque_bera
jarque_bera(QQQ)
Out[4]:
In [5]:
X = np.random.normal(0, 1, 100)
check_for_stationarity(X)
Out[5]:
In [6]:
# Set the number of datapoints
T = 100
B = pd.Series(index=range(T))
B.name = 'B'
for t in range(T):
# Now the parameters are dependent on time
# Specifically, the mean of the series changes over time
params = (np.power(t, 2), 1)
B[t] = generate_datapoint(params)
In [7]:
plt.plot(B);
In [8]:
check_for_stationarity(B)
Out[8]:
In [9]:
QQQ = get_pricing("QQQ", start_date='2014-1-1', end_date='2015-1-1', fields='price')
QQQ.name = QQQ.name.symbol
# Write code to estimate the order of integration of QQQ.
# Feel free to sample from the code provided in the lecture.
In [10]:
QQQ = QQQ.diff()[1:]
QQQ.name = QQQ.name + ' Additive Returns'
check_for_stationarity(QQQ)
plt.plot(QQQ.index, QQQ.values)
plt.ylabel('Additive Returns')
plt.legend([QQQ.name]);
In [11]:
T = 500
X1 = pd.Series(index=range(T))
X1.name = 'X1'
for t in range(T):
# Now the parameters are dependent on time
# Specifically, the mean of the series changes over time
params = (t * 0.1, 1)
X1[t] = generate_datapoint(params)
X2 = np.power(X1, 2) + X1
X3 = np.power(X1, 3) + X1
X4 = np.sin(X1) + X1
# We now have 4 time series, X1, X2, X3, X4
# Determine a linear combination of the 4 that is stationary over the
# time period shown using the techniques in the lecture.
In [12]:
X1 = sm.add_constant(X1)
results = sm.OLS(X4, X1).fit()
# Get rid of the constant column
X1 = X1['X1']
results.params
Out[12]:
In [13]:
plt.plot(X4-0.99 * X1);
In [14]:
check_for_stationarity(X4 - 0.99*X1)
Out[14]:
Congratulations on completing the Integration, Cointegration, and Stationarity answer key!
As you learn more about writing trading models and the Quantopian platform, enter the daily Quantopian Contest. Your strategy will be evaluated for a cash prize every day.
Start by going through the Writing a Contest Algorithm tutorial.
This presentation is for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation for any security; nor does it constitute an offer to provide investment advisory or other services by Quantopian, Inc. ("Quantopian"). Nothing contained herein constitutes investment advice or offers any opinion with respect to the suitability of any security, and any views expressed herein should not be taken as advice to buy, sell, or hold any security or as an endorsement of any security or company. In preparing the information contained herein, Quantopian, Inc. has not taken into account the investment needs, objectives, and financial circumstances of any particular investor. Any views expressed and data illustrated herein were prepared based upon information, believed to be reliable, available to Quantopian, Inc. at the time of publication. Quantopian makes no guarantees as to their accuracy or completeness. All information is subject to change and may quickly become unreliable for various reasons, including changes in market conditions or economic circumstances.