Sunteți pe pagina 1din 2

ivanidris.

net

Stochastic matrix of the FUD states


Numpy Markov Chains example Part II The previous time 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, thats the power of NumPy! The rest of the time you spent playing computer games/pool/pinball or reading your favorite book NumPy Beginners Guide.

Pop Quiz
Lets 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: 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.

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 for i in xrange(len(signs)): 2 #we start the transition from the state with the specified sign 3 start_indices = numpy.where(states[:-1] == signs[i])[0]

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 for j in xrange(len(signs)): 2 # number of occurrences of this transition 3 occurrences = len(end_values[end_values == signs[j]]) 4 SM[i][j] = occurrences/float(N) 5 6 print SM

The result for the stochastic matrix is:


1 [[ 0.47787611 2 [ 0. 3 [ 0.42753623 0. 0. 0. 0.52212389] 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 Beginners Guide by yours truly.

S-ar putea să vă placă și