## Markov Chains in LaTeX

October 25, 2016

The TikZ package is a great tool for generating publication quality illustrations of Markov chains in LaTeX. This article presents 3 examples along with the complete LaTeX source. At each step, links are provided to the LaTeX source on Overleaf where you can view and edit the examples.

## Example 1: A Simple Weather Model

To busy to read the explanation? The final LaTeX source for this example can be found on Overleaf.

The 2-state weather model is often used as a simple introductory model to Markov chains. This model is a discrete-time Markov chain consisting of two states: sunny and rainy. On a given day, the probability of it raining or being sunny the following day is determined by the current days weather. In this example we will create an illustration of this Markov chain.

To get started, we must load the TikZ package:

For our Markov chain diagrams we will use the automata and positioning libraries within TikZ, these are loaded using the \usetikzlibrary command:

The tikzpicture environment is where our diagram will be built:

Drawing the Markov chain is broken into two steps:

• draw the states (nodes), and
• draw arrows connecting the states.

### Drawing the States

Within the tikzpicture environment, states can be added using the \node command:

For each node, you must specify a unique id. This id can be any combination of letters and numbers and will be used later to identify the node when connecting the states with arrows. Adding text to a node is optional, although if you chose not to you must use empty curly brackets {} . To add the node for the Sunny state to our diagram we use:

Here s is the label for our node. The state option controls the appearance of the node. Next, we will draw the Rainy state:

The option right=of s will place this node to the right of the Sunny node. If we look at our Markov chain now we will see two states next to one another with no arrows connecting them.

### Connecting the States

The \draw command is used to add arrows to our Markov chain representing transitions between states. The \draw command takes the form

where the start and end ids reference nodes previously defined. For our chain we will use the option every loop, bend right with the \draw command. This will draw arrows that bend to the right (from the perspective of the node at the starting end of the arrow) between the nodes. To specify that we want arrows connecting the sunny node to the rainy node and the rainy node to the sunny node we use

after the draw command. Note that no options have been used here for either arrow.

Putting everything together, the following LaTeX markup will create an illustration of the Markov chain consisting of 2-states, sunny and rainy, with curved arrows showing the transitions between the states. rainy node to the sunny node we use

Now our diagram is starting to look a little better

Suppose we wanted to add arrows to indicate that at each time step there is some probability of remaining in the same state. This can be done by adding arrows that connect nodes to themselves. To do this for each state we add

after the \draw command. The loop above option draws the arrow above the state.

The next thing we may want to add to our diagram is labels for the transition probabilities between states. This can be done by adding text between the curly brackets for each arrow. You can use inline math for the labels as well if you want to use symbols or expressions. Our states will now take the form:

which produces the image

Clearly we need to correct the positioning of the labels. To do this we will add an option to the arrows from sunny to rainy and rainy to sunny that controls where the labels are positioned relative to the arrows.

Why is one auto=left and the other auto=right? Every option that we specify is from the point of view of the start of the arrow.

In its current state, this would probably be appropriate for a research article or report. If you need a nice visual for a slideshow, video or website, you may consider some additional styling.

### Making it Look Amazing

Let’s add some color and adjust the styling of a little bit to make this perfect for presentations. To start, we will change the colors of the nodes. Some additional options are used with the \node command. There are ways to define options that can be used for all nodes but I will wait until the next example to get into that.

• The text option controls the color of the text.
• The draw option controls the style of the border. draw=none will prevent the border from being drawn.
• The fill option is used to set the background color for the node. Here I have used gray!50!black which means a mix of 50% gray and 50% black.

The figure is starting to look more colorful

The nodes can be moved further apart to avoid appearing crowded by changing right=of s to right=2cm of s. This will place the nodes 2cm way from each other.

Next, the color, width and arrow head of the arrows will be adjusted. This is done using options for the \draw command.

The font used for the diagram can be changed to a sans-serif font by adding the font=\sffamily option to the \begin{tikzpicture} command.

This produces the figure

## Example 2: Bull-Bear-Stagnant Markov Chain

In this example we will be creating a diagram of a three-state Markov chain where all states are connected. We will arrange the nodes in an equilateral triangle. In the previous example, the rainy node was positioned using right=of s. In this example we will use absolute position where we specify where we want the node to be. This uses the standard cartesian coordinate system for the positioning of the nodes.

## Example 3: A Phase-Type Mortality Model

This example comes from a mortality model that is based on a multi-state Markov chain. If you are interested in the details you can read the paper this model originates from here: Markov Aging Process and Phase-Type Law of Mortality.

## Final Thoughts

I have presented one way of producing Markov chain diagrams here. Are there other ways to do this? Yes. Are there easier ways of accomplishing what I have presented here? Let me know in the comments.