The opener for Week 4 discusses an example in which tomorrow's weather is predicted from today's weather. This is done by assigning probabilities to how today's weather transitions to tomorrow's weather.
import numpy as np
P = np.matrix( '0.4, 0.3, 0.1; \
0.4, 0.3, 0.6; \
0.2, 0.4, 0.3' )
print( P )
Vector $ x $ consists of three components: $$ \left( \begin{array}{c} \chi_s \\ \chi_c \\ \chi_r \end{array} \right). $$ If we want to use $ x^{(0)} $ to express that on day $ 0 $ it is cloudy, we set $$ \left( \begin{array}{c} \chi_s^{(0)} \\ \chi_c^{(0)} \\ \chi_r^{(0)} \end{array} \right) = \left( \begin{array}{r} 0 \\ 1 \\ 0 \end{array} \right). $$
x0 = np.matrix( '0; \
1; \
0' )
print( x0 )
The weather for day $ 1 $ is predicted by $ x^{(1)} = P x^{(0)} $. Notice that if P
and x0
are numpy matrices, then P * x0
computes the product of matrix P
times vector x0
.
x1 = P * x0
print( x1 )
Predict the weather for day 2 from day 1.
x2 = P * x1
print( x2 )
Predict the weather for day 3 from day 2.
Predict the weather for day 4 from day 3.
Predict the weather for day 5 from day 4.
Predict the weather for day 6 from day 5.
Predict the weather for day 7 from day 6.
What are the probabilities it is sunny, cloudy, rainy on day 7 (given that it was cloudy on day 0)? (Just think about this...)
This is best done with a loop, where $ x $ simply takes on the next value everytime through the loop.
x = x0
for i in range( 21 ): # you saw range( n ) in Week 1
print( 'Day', i ) # print the predictions for day i
print( x )
x = P * x # compute the predictions for day i+1
Modify the above loop to compute the prediction for Day 365.
Compare Day 365 to Day 20. Notice that they are almost the same. Go back and start with x0 = "today is sunny". Go back and start with x0 = "today is rainy" Notice that no matter what the weather is today, eventually the prediction becomes "the typical forecast for this location".