-
Notifications
You must be signed in to change notification settings - Fork 18
/
hmm.Rmd
203 lines (189 loc) · 5.47 KB
/
hmm.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# Hidden Markov Models
A hidden Markov model (HMM) defines a density function for a
sequence of observations $y_1, \ldots, y_N$, where
* the observations are conditionally independent draws from a mixture
distribution with $K$ components, and
* the unobserved mixture components $z_1, \ldots, z_N \in 1:K$ form a
Markov process.
The Markov process for the mixture components is governed by
* an initial probability simplex $\phi \in \mathbb{R}^K$,
* stochastic matrix $\Theta \in \mathbb{R}^{K \times K}$, and
with
$$
p(z \mid \phi, \Theta)
= \phi_{z[1]} \cdot \prod_{n = 2}^N \theta_{z[n - 1], z[n]}.
$$
The sequence of observations $y$ is conditionally independent given
$z$,
$$
p(y \mid z) = \prod_{n=1}^N p(y \mid z_n = k).
$$
The $N \times K$ emission matrix is defined by taking
$$
\Lambda_{n, k} = p(y \mid z_n = k).
$$
The complete data density for HMMs is
$$
p(y, z \mid \phi, \Theta, \Lambda)
= \phi_{z[1]}
\cdot \prod_{n = 2}^N \Theta_{z[n - 1], z[n]}
\cdot \prod_{n = 1}^N \Lambda_{n, z[n]}.
$$
The density is defined by marginalizing out the unobserved latent
states $z$,
$$
p(y \mid \phi, \Theta, \Lambda)
= \sum_{z \in (1:K)^N} p(y, z \mid \phi, \Theta, \Lambda).
$$
The goal is to compute the derivatives of this function for a fixed
observation sequence $y$ with respect to the parameters
$\phi,$ $\Theta,$ and $\Lambda.$
The direct summation is intractable because there are $K^N$ possible
values for the sequence $z.$ The forward algorithm uses dynamic
programming to compute the marginal likelihood in $\mathcal{O}(K^2
\cdot N)$. The forward algorithm is neatly derived from the matrix
expression for the density,
$$
p(y \mid \phi, \theta, \Lambda)
= \phi^{\top} \cdot \textrm{diag}(\Lambda_1)
\cdot \Theta \cdot \textrm{diag}(\Lambda_2)
\cdots \Theta \cdot \textrm{diag}(\Lambda_N)
\cdot \textrm{1}
$$
where $\textrm{1} = \begin{bmatrix}1 & \cdots & 1\end{bmatrix}^{\top}$ is a
vector of ones of size $K$, and
$$
\textrm{diag}(\Lambda_n)
=
\begin{bmatrix}
\Lambda_{n, 1} & 0 & \cdots & 0
\\
0 & \Lambda_{n, 2} & \cdots & 0
\\
\vdots & \vdots & \ddots & \vdots
\\
0 & 0 & \cdots & \Lambda_{n, K}
\end{bmatrix}.
$$
The forward algorithm is traditionally defined in terms of
the forward vectors,
$$
\alpha_n
=
\begin{bmatrix}
\phi^{\top} \cdot \textrm{diag}(\Lambda_1)
& \cdots &
\Theta \cdot \textrm{diag}(\Lambda_n)
\end{bmatrix}^{\top},
$$
which are column vectors formed from the prefixes of the likelihood
function. A final multiplication by $\textrm{1}$ yields a means to
compute the likelihood function. This forward algorithm may be
automatically differentiated and the resulting derivative calculation
also takes $\mathcal{O}(K^2 \cdot N).$ But the constant factor and
memory usage is high, so it is more efficient to work out derivatives
analytically.
The backward algorithm defines the backward row vectors,
$$
\beta_n
=
\begin{bmatrix}
\Theta \cdot \textrm{diag}(\Lambda_n)
& \cdots &
\Theta \cdot \textrm{diag}(\Lambda_N)
\cdot \textrm{1}
\end{bmatrix}.
$$
The recursive form of the backward algorithm begins with $\beta_N,$
then defines $\beta_{n - 1}$ in terms of $\beta_N.$
The derivative of the HMM density can be rendered as a sum of terms
involving forward and backward variables by repeatedly applying the
chain rule to peel pairs of terms off of the product, resulting in a
sum
$$
\begin{array}{rcl}
\displaystyle
\frac{\partial}{\partial x} p(y \mid \phi, \Theta, \Lambda)
& = &
\displaystyle
\frac{\partial}{\partial x}
\phi^{\top} \cdot \textrm{diag}(\Lambda_1)
\cdot \Theta \cdot \textrm{diag}(\Lambda_2)
\cdots \Theta \cdot \textrm{diag}(\Lambda_N)
\cdot \textrm{1}
\\[8pt]
& = &
\displaystyle
\begin{array}[t]{l}
\displaystyle
\left( \frac{\partial}{\partial x}
\phi^{\top} \cdot \textrm{diag}(\Lambda_1) \right)
\cdot \Theta \cdot \textrm{diag}(\Lambda_2)
\cdots \Theta \cdot \textrm{diag}(\Lambda_N)
\cdot \textrm{1}
\\
{ } + \displaystyle \
\phi^{\top} \cdot \textrm{diag}(\Lambda_1)
\cdot
\left( \frac{\partial}{\partial x}
\Theta \cdot \textrm{diag}(\Lambda_2)
\cdots \Theta \cdot \textrm{diag}(\Lambda_N) \right)
\end{array}
\\[8pt]
& = & \hfill \vdots \hfill
\\[8pt]
& = &
\displaystyle
\left( \frac{\partial}
{\partial x}
\phi^{\top} \cdot \textrm{diag}(\Lambda_1) \right)
\cdot \beta_1
+ \sum_{n = 2}^{N}
\alpha_{n - 1}^{\top}
\cdot \left( \frac{\partial}
{\partial x}
\Theta \cdot \textrm{diag}(\Lambda_{n})
\right)
\cdot \beta_{n}
\end{array}
$$
involving the forward terms $\alpha,$ backward terms $\beta,$ and the
derivatives of the parameters $\phi, \Theta, \Lambda.$
To simplify the notation, let
$$
\mathcal{L} = p(y \mid \phi, \Theta, \Lambda).
$$
The derivative with
respect to the initial distribution $\phi$ is
$$
\frac{\partial}{\partial \phi} \mathcal{L}
=
\textrm{diag}(\Lambda_1) \cdot \beta_1.
$$
The derivative with respect to the initial emission density
$\Lambda_1$ is
$$
\frac{\partial}{\partial \Lambda_1} \mathcal{L}
=
\textrm{diag}(\phi) \cdot \beta_1.
$$
The derivative with respect to the emission density $\Lambda_n$ for
$n > 1$ is
$$
\frac{\partial}{\partial \Lambda_n} \mathcal{L}
=
\alpha_{n - 1}^{\top} \cdot \Theta \cdot \beta_{n}.
$$
The deriative with respect to the stochastic transition matrix
$\Theta$ is
$$
\frac{\partial}{\partial \Theta} \mathcal{L}
=
\sum_{n = 2}^N
\alpha_{n - 1}^{\top}
\cdot \textrm{diag}(\Lambda_{n})
\cdot \beta_{n}.
$$
## References {-}
The matrix form of the likelihood and forward-backward algorithm, as
well as the matrix derivatives are based on the presentaiton in [@qin:2000].