We will download the stock quotes for the last year of two trackers using Matplotlib. As mentioned in the previous chapter, we can retrieve quotes from Yahoo! Finance. We will compare the log returns of the close price of DIA and SPY. Also we will perform the Jarque-Bera test on the difference of the log returns. Perform the following steps to do so:
def get_close(symbol): today = date.today() start = (today.year - 1, today.month, today.day) quotes = quotes_historical_yahoo(symbol, start, today) quotes = np.array(quotes) return quotes.T[4]
spy = np.diff(np.log(get_close(“SPY”))) dia = np.diff(np.log(get_close(“DIA”)))
print “Means comparison”, stats.ttest_ind(spy, dia)
The result of the means comparison test would be shown as follows:
Means comparison (-0.017995865641886155, 0.98564930169871368)
So there is about a 98 percent chance that the two samples have the same mean log return.
print “Kolmogorov smirnov test”, stats.ks_2samp(spy, dia)
Again, two values are returned of which the second value is the p-value.
Kolmogorov smirnov test (0.063492063492063516, 0.67615647616238039)
print “Jarque Bera test”, jarque_bera(spy – dia)[1]
The p-value of the Jarque-Bera normality test would be shown as follows:
Jarque Bera test 0.596125711042
plt.hist(spy, histtype=”step”, lw=1, label=”SPY”) plt.hist(dia, histtype=”step”, lw=2, label=”DIA”) plt.hist(spy - dia, histtype=”step”, lw=3, label=”Delta”) plt.legend() plt.show()
The histograms of the log returns and difference are shown in the following screenshot:
We compared samples of log returns for DIA and SPY. We also performed the Jarque-Bera test on the difference of the log returns (see pair.py
).
from matplotlib.finance import quotes_historical_yahoo from datetime import date import numpy as np from scipy import stats from statsmodels.stats.stattools import jarque_bera import matplotlib.pyplot as plt def get_close(symbol): today = date.today() start = (today.year - 1, today.month, today.day) quotes = quotes_historical_yahoo(symbol, start, today) quotes = np.array(quotes) return quotes.T[4] spy = np.diff(np.log(get_close(“SPY”))) dia = np.diff(np.log(get_close(“DIA”))) print “Means comparison”, stats.ttest_ind(spy, dia) print “Kolmogorov smirnov test”, stats.ks_2samp(spy, dia) print “Jarque Bera test”, jarque_bera(spy - dia)[1] plt.hist(spy, histtype=”step”, lw=1, label=”SPY”) plt.hist(dia, histtype=”step”, lw=2, label=”DIA”) plt.hist(spy - dia, histtype=”step”, lw=3, label=”Delta”) plt.legend() plt.show()