Simulating continuous Markov chains
In a blog post I wrote in 2013, I showed how to simulate a discrete Markov chain. In this post we’ll (written with a bit of help from Geraint Palmer) show how to do the same with a continuous chain which can be used to speedily obtain steady state distributions for models of queueing processes for example.
A continuous Markov chain is defined by a transition rate matrix which shows the rates at which transitions from 1 state to an other occur. Here is an example of a continuous Markov chain:
This has transition rate matrix \(Q\) given by:
The diagonals have negative entries, which can be interpreted as a rate of no change. To obtain the steady state probabilities \(\pi\) for this chain we can solve the following matrix equation:
if we include the fact that the sum of \(\pi\) must be 1 (so that it is indeed a probability vector) we can obtain the probabilities in Sagemath using the following:
You can run this here (just click on ‘Evaluate’):
This returns:
Thus, if we were to randomly observe this chain:
- 25% of the time it would be in state 1;
- 50% of the time it would be in state 2;
- 25% of the time it would be in state 3.
Now, the markov chain in question means that if we’re in the first state the rate at which a change happens to go to the second state is 2 and the rate at which a change happens that goes to the third state is 1.
This is analagous to waiting at a bus stop at the first city. Buses to the second city arrive randomly 2 per hour, and buses to the third city arrive randomly 1 per hour. Everyone waiting for a bus catches the first one that arrives. So at steady state the population will be spread amongst the three cities according to \(\pi\).
Consider yourself at this bus stop. As all this is Markovian we do not care what time you arrived at the bus stop (memoryless property). You expect the bus to the second city to arrive 1/2 hours from now, with randomness, and the bus to the third city to arrive 1 hour from now, with randomness.
To simulate this we can sample two random numbers from the exponential distribution and find out which bus arrives first and ‘catch that bus’:
The above returned (for this particular instance):
So here it’s going to take .5 hours for a bus to the second city to arrive, whereas it would take .61 hours for a bus to the third. So we would catch the bust to the second city after spending 0.5 hours at the first city.
We can use this to write a function that will take a transition rate matrix, simulate the transitions and keep track of the time spent in each state:
Here are the probabilities from the same Markov chain as above:
which gave (on one particular run):
This approach was used by Geraint Palmer who is doing a PhD with Paul Harper and I. He used this to verify that calculations were being carried out correctly when he was trying to fit a model. James Campbell and I are going to try to use this to get an approximation for bigger chains that cannot be solved analytically in a reasonable amount of time. In essence the simulation of the Markov chain makes sure we spend time calculating probabilities in states that are common.