Stochastic matrix of the FUD states

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

Numpy Strategies 0.1.2

So we are now in Sprint 12 of Project “NumPy Strategies”. Last week you finished the task of determining states and defining a Markov chain model. This took you ten minutes even though the estimate was 2 days. Hey, that’s the power of NumPy! The rest of the time you spent playing computer games/pool/pinball or reading your favorite book NumPy Beginner’s Guide.

Pop Quiz

Let’s see whether you remember what we decided the states should be:

• flat F, up U, bottom B, average A, rally R
• flat F, up U, down D
• None of the above.
• This is a trick question!

The next task is:

• Create the stochastic matrix with transition probabilities

We will estimate this task to 2 days again for obvious reasons.This task can be split into several subtasks, but these are of course not estimated.

1. Initialize the stochastic matrix to 0 values

We have 3 possible start states and 3 possible end states for each transition. For instance, if we start from a U state, we could switch to:

• U again
• F
• D

Initialize the stochastic matrix with the NumPy zeros function.

 1 SM = numpy.zeros((3, 3))

2. Create a list containing the signs

This is just a normal Python list.

 1 signs = [-1, 0, 1]

3. For each sign select the corresponding start state indices

Now the code becomes a bit messy. We will have to use actual loops! Maybe a better solution will occur to me one day. We will loop over the signs and select the start state indices corresponding to each sign. Select the indices with the NumPy where function.

 1 2 3 for i in xrange(len(signs)): #we start the transition from the state with the specified sign start_indices = numpy.where(states[:-1] == signs[i])

4. Continue to the next iteration if no start states are found

This is just straightforward Python code.

 1 2 3 4 5 N = len(start_indices)   # skip since there are no transitions possible if N == 0: continue

5. Compute the end state values

The end state indices in our simple model can be found by just getting the next available position.

 1 2 #find the values of states at the end positions end_values = states[start_indices + 1]

6. Calculate the transition probabilities

We can now count the number of occurrences of each transition. Dividing by the total number of transitions for a given start state gives us the transition probabilities for our stochastic matrix. This is not the best method by the way, since it could be overfitting.

 1 2 3 4 5 6 for j in xrange(len(signs)): # number of occurrences of this transition occurrences = len(end_values[end_values == signs[j]]) SM[i][j] = occurrences/float(N)   print SM

The result for the stochastic matrix is:

 1 2 3 [[ 0.47787611 0. 0.52212389] [ 0. 0. 0. ] [ 0.42753623 0. 0.57246377]]

As you can see the transitions involving the flat no change state have 0 probability, while the other transitions have a probability close to 50%. This is I guess what we would have expected.

If you liked this post and are interested in NumPy check out NumPy Beginner’s Guide by yours truly. Thanks goes to David W. Lambert for his recent review on Amazon.com and contribution to the unofficial errata. As far as I know there are still 3 reviews in the pipeline for December. One of them was announced on The Glowing Python blog. This announcement also contains a review of the free sample chapter. I will be back next week with the steady state vector task.

Series NavigationSecret Transitions of a Markov ChainSteady State Vector of Markov Chains
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.