0% found this document useful (0 votes)
96 views1 page

Num Py Markov Chains I

This document provides an example of implementing a Markov chain model for stock price movements using NumPy. It obtains 1 year of closing price data for Apple (AAPL) stock, determines the daily price movements as being up, down, or flat based on the price differences, and represents this as states in a Markov chain model. The document shows how to use NumPy functions like diff and sign to efficiently analyze the stock price data and determine the states without using loops. It indicates that determining the state transitions will be covered in a future post.

Uploaded by

ivan2
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views1 page

Num Py Markov Chains I

This document provides an example of implementing a Markov chain model for stock price movements using NumPy. It obtains 1 year of closing price data for Apple (AAPL) stock, determines the daily price movements as being up, down, or flat based on the price differences, and represents this as states in a Markov chain model. The document shows how to use NumPy functions like diff and sign to efficiently analyze the stock price data and determine the states without using loops. It indicates that determining the state transitions will be covered in a future post.

Uploaded by

ivan2
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

ivanidris.

net

Secret Transitions of a Markov Chain


Numpy Markov Chains Example I 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). Lets 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 Beginners Guide. We will retrieve the data going back 1 year. Here is the code to do this:
1 today = date.today() 2 start = (today.year - 1, today.month, today.day) 3 4 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.
1 close = [q[4] for q in quotes] 2 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.
1 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. Lets 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 Beginners Guide by yours truly.

You might also like