Power Law Discovery

This entry is part 4 of 23 in the series Numpy Strategies

Numpy Strategies 0.1.4

Our Product Owner (disclaimer fictional character) has come back from a long vacation. During his holiday he found some time to google “get rich quick schemes”. This led to a new thrilling User Story.

User Story

• I want to Discover a Power Law in the closing Stock Prices Log Returns

The Business Value of this User Story comes from the fact that our Hedge Fund operates a long term trading strategy. We don’t want to trade too often, because of involved costs. Let’s say that we would prefer to buy and sell once a month based on a significant correction. The issue is to determine an appropriate signal given that we want to initiate a transaction every 1 out of about 20 days. Approximately 5% probability with other words.

1. Get close prices

First, let’s get historical end-of-day data for the past year from Yahoo Finance. After that extract the close prices for this period.

 1 2 3 4 5 today = date.today() start = (today.year - 1, today.month, today.day)   quotes = quotes_historical_yahoo(sys.argv, start, today) close = numpy.array([q for q in quotes])

2. Get positive log returns

Now calculate the log returns for the close prices. First, we will take the log of the close prices and then compute the first difference of this values with the NumPy diff function. From the log returns let’s select the positive values.

 1 2 logreturns = numpy.diff(numpy.log(close)) pos = logreturns[logreturns > 0]

3. Get frequencies of returns

We need to get the frequencies of the returns with the histogram function. Counts and an array of the bins is returned. At the end we need to take the log of the frequencies in order to get a nice linear relation.

 1 2 3 4 counts, rets = numpy.histogram(pos) rets = rets[:-1] + (rets - rets)/2 freqs = 1.0/counts freqs = numpy.log(freqs)

4. Fit the frequencies and returns to a line

Use the polyfit function to do a linear fit.

 1 p = numpy.polyfit(rets,freqs, 1)

5. Plot the results

Finally we will plot the data and linear fit with Matplotlib.

 1 2 3 matplotlib.pyplot.plot(rets, freqs, 'o') matplotlib.pyplot.plot(rets, p * rets + p) matplotlib.pyplot.show()

Here is the code in its entirety with imports and all.

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 from matplotlib.finance import quotes_historical_yahoo from datetime import date import numpy import sys import matplotlib.pyplot   #1. Get close prices. today = date.today() start = (today.year - 1, today.month, today.day)   quotes = quotes_historical_yahoo(sys.argv, start, today) close = numpy.array([q for q in quotes])   #2. Get positive log returns. logreturns = numpy.diff(numpy.log(close)) pos = logreturns[logreturns > 0]   #3. Get frequencies of returns. counts, rets = numpy.histogram(pos) rets = rets[:-1] + (rets - rets)/2 freqs = 1.0/counts freqs = numpy.log(freqs)   #4. Fit the frequencies and returns to a line. p = numpy.polyfit(rets,freqs, 1)   #5. Plot the results. matplotlib.pyplot.plot(rets, freqs, 'o') matplotlib.pyplot.plot(rets, p * rets + p) matplotlib.pyplot.show()

This is the resulting plot for AAPL (disclaimer I own AAPL shares). Another disclaimer – use this information at your own risk. I am not responsible for any losses. Please listen to your risk managers.

What Just Happened?

We discovered a power law in the positive log returns of stock prices. Now it should be trivial to determine our correction criteria.

Have a Go

There are some things worth investigating left:

• Have a look at the negative log returns.
• Try out different stocks.
• Look at different timeframes for instance intraday data.

If you liked this post and are interested in NumPy check out NumPy Beginner’s Guide by yours truly.

Series NavigationSteady State Vector of Markov ChainsNumPy Periodic Dips
By the author of NumPy Beginner's Guide, NumPy Cookbook and Instant Pygame. If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.
This entry was posted in programming and tagged , , , . Bookmark the permalink.