Explaining simulating weather forecast code using Markov chain
Explaining simulating weather forecast code using Markov chain
If today is a sunny day, an average of 55% that the next day is another sunny day, or 30% with
cloudy day and only 15% that the next day is a rainy day. Besides, if it’s a rainy day today, then
45% chance that the next day will be another rainy day, 20% for cloudy day and 35% chance that
it will be a sunny day. And if today is cloudy day, then 30% for the same cloudy next day, 40%
for rainy and 30% for sunny.
In our weather forecast example, we define S = [Sunny Rainy Cloudy]. Therefore, assumed that
today is a sunny day, we define S0 = [1 0 0] because there is hundred percent of a sunny day and
zero chance of a rainy day and cloudy day.
Therefore, the next state, S1, we can calculate the matrix product S1 = S0P.
[0.55¿0.15¿0.30 ]
[1 0 0 ] . [0.35¿0.45¿0.20 ] = [0.55 0.15 0.30 ]
[0.30¿0.40¿ 0.30]
Since the formula for computing successive state is Sn = Sn-1P, the general formula for probability
of a process ending up in a certain state is: Sn = S0Pn.
In 50 days: S50=S0P50=
[0.55¿0.15¿0.30 ]
[1 0 0 ] . [0.35¿0.45¿0.20 ]^50 = [0.4207 0.3103 0.2690 ]
[0.30¿0.40¿ 0.30]
In 100 days: S100=S0P100=
[0.55¿0.15¿0.30 ]
[1 0 0 ] . [0.35¿0.45¿0.20 ] ^100= [0.4207 0.3103 0.2690 ]
[0.30¿0.40¿ 0.30]
From this we can conclude that when n → ∞, the probabilities will converge to a steady state,
indicating that in the long-term, 42.07% of weather will be sunny day, 26.9% of weather will be
cloudy day and 31.03% of weather will be rainy day. What we can see that the steady-state
probabilities of this Markov chain do not depend upon the initial state.
3. Explaining simulating weather forecast code using Markov chain:
Step 1: Creating transition matrix, which represents the probabilities of moving from one state
(e.g., sunny, rainy, cloudy) to another, based on given datas.
Step 2: Creating a list of possible weather states, which is “Sunny”, “Rainny”, “Cloudy”. Then,
we set the “intial_state” value, which is the beginning state, as 1 (Sunny).
Step 3: Setting the number of days that the programme has to forecast (in this case it is 7 days)
to the “num_days” value. After that, we initialize the “forecast” array to store the weather
predictions in “num_days”. The first day’s weather has to set to the “initial_state”.
Step 4: This loop generates the weather forecast for the remaining days:
“forecast(i-1)” finds the current day's weather.
“probabilities = transition_matrix(forecast(i-1), :)” gets the row from the transition matrix
corresponding to the current state.
“find(rand <= cumsum(probabilities), 1)” uses a random number (rand) to determine the next
day's weather based on the cumulative sum of transition probabilities.
“rand” generates a random number between 0 and 1.
“cumsum(probabilities)” creates a cumulative probability distribution.
“find()” selects the next state based on this random number.
Step 5: Using the loop to print the weather predictions in “num_days” from “forecast” array.
4. References:
[1] https://iopscience.iop.org/article/10.1088/1742-6596/1848/1/012061