# How can I draw hidden Markov model by this way

How can I draw hidden Markov model by this way, I don't know how to add the circle.

Starting from this post, you can just add circle,draw to the nodes in the cells in the columns except for the first and fifth one.

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{amsmath}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix[matrix of math nodes,column sep=2em,row
sep=4em,cells={nodes={circle,draw,minimum width=3em,inner sep=0pt}},
column 1/.style={nodes={rectangle,draw=none}},
column 5/.style={nodes={rectangle,draw=none}}] (m) {
\text{Markov process}: &
X_0 & X_1 & X_2 & \cdots & X_{T-1}\\
\text{Observations}: &
\mathcal{O}_0 & \mathcal{O}_1 & \mathcal{O}_2 & \cdots & \mathcal{O}_{T-1}\\
};
\foreach \X in {2,3,4,5}
{\draw[-latex] (m-1-\X) -- (m-1-\the\numexpr\X+1) node[midway,above]{$A$};
\ifnum\X=5
\draw[-latex] (m-1-6) -- (m-2-6) node[pos=0.6,left]{$B$};
\else
\draw[-latex] (m-1-\X) -- (m-2-\X) node[pos=0.6,left]{$B$};
\fi}
\draw[dashed] ([yshift=1ex]m.east) -- ([yshift=1ex]m.east-|m-1-1.east);
\end{tikzpicture}
\end{document}


You can put it in a figure in the usual way. If you really need to use \resizebox to rescale it you need to use an ampersand replacemet.

\documentclass{article}
\usepackage{tikz}
\usepackage{amsmath}
\usetikzlibrary{matrix}
\begin{document}
\begin{figure}
\centering
\resizebox{3in}{!}{\begin{tikzpicture}
\matrix[matrix of math nodes,column sep=2em,row
sep=4em,cells={nodes={circle,draw,minimum width=3em,inner sep=0pt}},
column 1/.style={nodes={rectangle,draw=none}},
column 5/.style={nodes={rectangle,draw=none}},
ampersand replacement=\&] (m) {
\text{Markov process}: \&
X_0 \& X_1 \& X_2 \& \cdots \& X_{T-1}\\
\text{Observations}: \&
\mathcal{O}_0 \& \mathcal{O}_1 \& \mathcal{O}_2 \& \cdots \& \mathcal{O}_{T-1}\\
};
\foreach \X in {2,3,4,5}
{\draw[-latex] (m-1-\X) -- (m-1-\the\numexpr\X+1) node[midway,above]{$A$};
\ifnum\X=5
\draw[-latex] (m-1-6) -- (m-2-6) node[pos=0.6,left]{$B$};
\else
\draw[-latex] (m-1-\X) -- (m-2-\X) node[pos=0.6,left]{$B$};
\fi}
\draw[dashed] ([yshift=1ex]m.east) -- ([yshift=1ex]m.east-|m-1-1.east);
\end{tikzpicture}}
\caption{A hidden Markov model.}
\label{fig:HiddenMarkov}
\end{figure}
\end{document}

• thank you very much, could you tell me how can I use these code in \begin{figure} environment? because I want to adjust the size of this figure and name it. – QIAN HAN Jul 6 at 19:18
• @QIANHAN I added a code that puts it in a figure. – user121799 Jul 6 at 19:23
• and I use command \resizebox{3in}{!} to adjust size, but it didn't work, please tell me why and how to adjust, thank you. – QIAN HAN Jul 6 at 19:27
• @QIANHAN I added that as well. You need to use ampersand replacement. – user121799 Jul 6 at 19:32