HMM Simulation

We describe how to generate sequences of observations from a hidden Markov model.

Worksheet Functions

The Real Statistics Resource Pack provides the following function where Rp is a k × k transition matrix, Rd is a k × 1 initial distribution array, Rq is a k × m emission matrix, and n is a positive integer.

HMM_Sim(Rp, Rd, Rq, n): returns an n × 2 array whose first column contains a sequence of n hidden states from a run of the HMM, and whose second column contains the corresponding observations.

HMM_SimX(Rq, Rs): returns an n × h array with the observations corresponding to the hidden states in the h runs of the HMM, each of length n, in Rs.

Examples

Example 1: Given a hidden Markov model with S = {1, 2, 3}, U = {1, 2, 3}, and P and Q as shown in ranges B2:D4 and B7;D9, respectively, of Figure 1. Generate a random sequence of 20 observations from the HMM.

For example, suppose the states in S represent the weather on a particular day: “rainy”, “cloudy”, “sunny”, and the observations in U are the daily activities of a particular squirrel in the park at 8 am: “remaining in his burrow”, “climbing a tree”, “walking on the grass”.

Hidden Markov Model simulation

Figure 1 – Hidden Markov Model simulation

Using the formula =HMM_Sim(B2:D4,F2:F4,B7:D9,20), we obtain the output shown in range H2:I21, with the 20 simulated observations shown in column I. We see that the squirrel remains in his burrow in 20% of the observations, climbs a tree in 45%, and walks on the grass in 35%. It rains on 20% of these simulated 20 days, cloudy on 15%, and sunny on 65%.

Example 2: Create 3 sequences of simulated observations, each of length 20, for the HMM from Example 1.

We create a simulation of three sequences of hidden states by inserting the formula MarkovSim($B2:$D4, $F2:$F4, 20) in range H2:J21 of Figure 2, highlighting range H2:J21, and pressing Ctrl-R. We then create the corresponding observations shown in range L2:N21 using the formula =HMM_SimX(B7:D9, H2:J21).

Multi-run simulation

Figure 2– Multi-run HMM simulation

Links

↑ Hidden Markov models

Examples Workbook

Click here to download the Excel workbook with the examples described on this webpage.

References

Wikipedia (2026) Hidden Markov model
https://en.wikipedia.org/wiki/Hidden_Markov_model

Leave a Comment