Time for action – comparing stock log returns

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:

  1. Write a function that can return the close price for a specified stock.
    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]
  2. Calculate the log returns for DIA and SPY. The log returns are calculated by taking the natural logarithm of the close price and then taking the difference of consecutive values.
    spy =  np.diff(np.log(get_close(“SPY”)))
    dia =  np.diff(np.log(get_close(“DIA”)))
  3. The means comparison test checks whether two different samples could have the same mean value. Two values are returned, of which the second is a p-value from 0 to 1.
    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.

  4. The Kolmogorov-Smirnov two samples test tells us how likely it is that two samples are drawn from the same distribution.
    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)
    
  5. Unleash the Jarque-Bera normality test on the difference of the log returns.
    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
    
  6. Plot the histograms of the log returns and the difference thereof with Matplotlib.
    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:

    Time for action – comparing stock log returns

What just happened?

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()
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset