Secret Transitions of a Markov Chain

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

Numpy Strategies 0.1.1

Imagine that one day your most important client/Product Manager/Product Owner/Other walks into your room with the following User Story:

  • I want to make huge profits on the stock market with Markov chains

You think a few seconds and say: “We can implement this easily with NumPy.” You split the User Story in Tasks, the first task being determining state transitions and defining the Markov chain model.

A Markov chain is a system that has at least two states. The system switches at random between these states. I would like to define a Markov chain for a stock – AAPL (disclaimer I own AAPL shares). Let’s say that we have the states flat F, up U and down D. We can determine the states based on end of day close prices.

1. Obtain 1 year of data

Now we need to obtain the data. One way we can do this is with Matplotlib. This is also mentioned in NumPy Beginner’s Guide. We will retrieve the data going back 1 year. Here is the code to do this:

today =
start = (today.year - 1, today.month,
quotes = quotes_historical_yahoo('AAPL', start, today)

2. Select the close price

We now have historical data from Yahoo Finance. The data is represented as a list of tuples, but we are only interested in the close price. We can select the close prices as follows.

close =  [q[4] for q in quotes]
print len(close)

The close price is the fifth number in each tuple. We should have a list of about 253 close prices now.

3. Determine the states

Finally, we can determine the states by subtracting price of sequential days with the NumPy diff function. The state is then given by the sign of the difference. The NumPy sign function returns -1 for a negative, 1 for a positive number or 0 otherwise.

states = numpy.sign(numpy.diff(close))

The nice thing about this NumPy code, is that there were very few for loops. I am willing to bet that it is much faster than equivalent “normal” Python code, so it would be nice to measure the difference. Let’s leave this as an exercise for the reader.

Next time we will compute the transition probabilities for the various states. We will need these numbers to do fancy things with the stochastic matrix and steady state vector.

If you liked this post and are interested in NumPy check out NumPy Beginner’s Guide by yours truly. Last time I checked it was on the bestsellers list of Packt Publishing. This week a review of the book by Marcel Caraciolo generated a lot of buzz on the Internet. Please show your support for this review. I will be back next week with the continuation of the Markov chains story.

Series NavigationHow skewed are the prices of stocks?Stock returns entropy, the CAPM and target practiceHow to optimize maximum drawdowns of stock returnsWhat is the optimal holding period for shares?Random walks get you nowhereStock selection with the mad CAPM and liquidity filteringThere is more than one way to get dataPortfolio analysis with Pandas for the winBig drops in stock price explainedJarque Bera, the CAPM and undervalued stocksStochastic matrix of the FUD states
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.