Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Good-looking HMM and Lattice diagrams using TikZ
% (C) Mathieu Blondel, July 2010
\documentclass[a4paper,10pt]{article}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage[ansinew]{inputenc}
\usepackage{lmodern}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amsfonts}
\usepackage{tikz}
\begin{document}
\tikzstyle{state}=[shape=circle,draw=blue!50,fill=blue!20]
\tikzstyle{observation}=[shape=rectangle,draw=orange!50,fill=orange!20]
\tikzstyle{lightedge}=[<-,dotted]
\tikzstyle{mainstate}=[state,thick]
\tikzstyle{mainedge}=[<-,thick]
\begin{figure}[htbp]
\begin{center}
\begin{tikzpicture}[]
% states
\node[state] (s1) at (0,2) {$s_1$}
edge [loop above] ();
\node[state] (s2) at (2,2) {$s_2$}
edge [<-,bend right=45] node[auto,swap] {$a_{12}$} (s1)
edge [->,bend left=45] (s1)
edge [loop above] ();
\node[state] (s3) at (4,2) {$s_3$}
edge [<-,bend right=45] (s2)
edge [->,bend left=45] (s2)
edge [loop above] ();
\node[state] (s4) at (6,2) {$s_4$}
edge [<-,bend right=45] (s3)
edge [->,bend left=45] (s3)
edge [loop above] ();
% observations
\node[observation] (y1) at (2,0) {$y_1$}
edge [lightedge] (s1)
edge [lightedge] (s2)
edge [lightedge] (s3)
edge [lightedge] (s4);
\node[observation] (y2) at (4,0) {$y_2$}
edge [lightedge] (s1)
edge [lightedge] (s2)
edge [lightedge] (s3)
edge [lightedge] node[auto,swap] {$b_4(y_2)$} (s4);
\end{tikzpicture}
\end{center}
\caption{An HMM with 4 states which can emit 2 discrete symbols $y_1$ or $y_2$.
$a_{ij}$ is the probability to transition from state $s_i$ to state $s_j$.
$b_j(y_k)$ is the probability to emit symbol $y_k$ in state $s_j$.
In this particular HMM, states can only reach themselves or the adjacent state.}
\end{figure}
\begin{figure}[htbp]
\begin{center}
\begin{tikzpicture}[]
% 1st column
\node at (0,6) {$t=1$};
\node[state] (s1_1) at (0,5) {$s_1$};
\node[mainstate] (s2_1) at (0,4) {$s_2$};
\node[state] (s3_1) at (0,3) {$s_3$};
\node[state] (s4_1) at (0,2) {$s_4$};
\node at (0,1) {$y_1$};
% 2nd column
\node at (2,6) {$t=2$};
\node[mainstate] (s1_2) at (2,5) {$s_1$}
edge[lightedge] (s1_1)
edge[mainedge] (s2_1)
edge[lightedge] (s3_1)
edge[lightedge] (s4_1);
\node[state] (s2_2) at (2,4) {$s_2$}
edge[lightedge] (s1_1)
edge[lightedge] (s2_1)
edge[lightedge] (s3_1)
edge[lightedge] (s4_1);
\node[state] (s3_2) at (2,3) {$s_3$}
edge[lightedge] (s1_1)
edge[lightedge] (s2_1)
edge[lightedge] (s3_1)
edge[lightedge] (s4_1);
\node[state] (s4_2) at (2,2) {$s_4$}
edge[lightedge] (s1_1)
edge[lightedge] (s2_1)
edge[lightedge] (s3_1)
edge[lightedge] (s4_1);
\node at (2,1) {$y_2$};
% 3rd column
\node at (4,6) {$t=3$};
\node[mainstate] (s1_3) at (4,5) {$s_1$}
edge[mainedge] (s1_2)
edge[lightedge] (s2_2)
edge[lightedge] (s3_2)
edge[lightedge] (s4_2);
\node[state] (s2_3) at (4,4) {$s_2$}
edge[lightedge] (s1_2)
edge[lightedge] (s2_2)
edge[lightedge] (s3_2)
edge[lightedge] (s4_2);
\node[state] (s3_3) at (4,3) {$s_3$}
edge[lightedge] (s1_2)
edge[lightedge] (s2_2)
edge[lightedge] (s3_2)
edge[lightedge] (s4_2);
\node[state] (s4_3) at (4,2) {$s_4$}
edge[lightedge] (s1_2)
edge[lightedge] (s2_2)
edge[lightedge] (s3_2)
edge[lightedge] (s4_2);
\node at (4,1) {$y_2$};
\end{tikzpicture}
\end{center}
\caption{Trellis of the observation sequence $y_1$,$y_2$,$y_2$ for the above HMM. The thick arrows
indicate the most probable transitions. As an example, the transition
between state $s_1$ at time t=2 and state $s_4$ at time t=3 has probability
$\alpha_2(1)a_{14}b_4(y_2)$, where $\alpha_t(i)$ is the probability to be in
state $s_i$ at time t.}
\end{figure}
\end{document}
@Jim-Holmstroem

This comment has been minimized.

Copy link

commented May 6, 2012

Nice tikz:ing :D thanks :D

@csmfindling

This comment has been minimized.

Copy link

commented Feb 24, 2015

Awesome. Thanks

@imsrgadich

This comment has been minimized.

Copy link

commented Jan 11, 2018

Thanks!

@whhsu2

This comment has been minimized.

Copy link

commented Nov 7, 2018

thank s man

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.