<s>
The	O
forward	B-Algorithm
–	I-Algorithm
backward	I-Algorithm
algorithm	I-Algorithm
is	O
an	O
inference	O
algorithm	O
for	O
hidden	O
Markov	O
models	O
which	O
computes	O
the	O
posterior	O
marginals	O
of	O
all	O
hidden	O
state	O
variables	O
given	O
a	O
sequence	O
of	O
observations/emissions	O
,	O
i.e.	O
</s>
<s>
The	O
algorithm	O
makes	O
use	O
of	O
the	O
principle	O
of	O
dynamic	B-Algorithm
programming	I-Algorithm
to	O
efficiently	O
compute	O
the	O
values	O
that	O
are	O
required	O
to	O
obtain	O
the	O
posterior	O
marginal	O
distributions	O
in	O
two	O
passes	O
.	O
</s>
<s>
The	O
first	O
pass	O
goes	O
forward	O
in	O
time	O
while	O
the	O
second	O
goes	O
backward	O
in	O
time	O
;	O
hence	O
the	O
name	O
forward	B-Algorithm
–	I-Algorithm
backward	I-Algorithm
algorithm	I-Algorithm
.	O
</s>
<s>
The	O
term	O
forward	B-Algorithm
–	I-Algorithm
backward	I-Algorithm
algorithm	I-Algorithm
is	O
also	O
used	O
to	O
refer	O
to	O
any	O
algorithm	O
belonging	O
to	O
the	O
general	O
class	O
of	O
algorithms	O
that	O
operate	O
on	O
sequence	O
models	O
in	O
a	O
forward	O
–	O
backward	O
manner	O
.	O
</s>
<s>
In	O
the	O
first	O
pass	O
,	O
the	O
forward	B-Algorithm
–	I-Algorithm
backward	I-Algorithm
algorithm	I-Algorithm
computes	O
a	O
set	O
of	O
forward	O
probabilities	O
which	O
provide	O
,	O
for	O
all	O
,	O
the	O
probability	O
of	O
ending	O
up	O
in	O
any	O
particular	O
state	O
given	O
the	O
first	O
observations	O
in	O
the	O
sequence	O
,	O
i.e.	O
</s>
<s>
The	O
forward	B-Algorithm
–	I-Algorithm
backward	I-Algorithm
algorithm	I-Algorithm
can	O
be	O
used	O
to	O
find	O
the	O
most	O
likely	O
state	O
for	O
any	O
point	O
in	O
time	O
.	O
</s>
<s>
It	O
cannot	O
,	O
however	O
,	O
be	O
used	O
to	O
find	O
the	O
most	O
likely	O
sequence	O
of	O
states	O
(	O
see	O
Viterbi	B-Algorithm
algorithm	I-Algorithm
)	O
.	O
</s>
<s>
The	O
following	O
description	O
will	O
use	O
matrices	O
of	O
probability	O
values	O
rather	O
than	O
probability	O
distributions	O
,	O
although	O
in	O
general	O
the	O
forward-backward	B-Algorithm
algorithm	I-Algorithm
can	O
be	O
applied	O
to	O
continuous	O
as	O
well	O
as	O
discrete	O
probability	O
models	O
.	O
</s>
<s>
The	O
most	O
probable	O
sequence	O
of	O
states	O
that	O
produced	O
an	O
observation	O
sequence	O
can	O
be	O
found	O
using	O
the	O
Viterbi	B-Algorithm
algorithm	I-Algorithm
.	O
</s>
<s>
The	O
forward	B-Algorithm
–	I-Algorithm
backward	I-Algorithm
algorithm	I-Algorithm
runs	O
with	O
time	O
complexity	O
in	O
space	O
,	O
where	O
is	O
the	O
length	O
of	O
the	O
time	O
sequence	O
and	O
is	O
the	O
number	O
of	O
symbols	O
in	O
the	O
state	O
alphabet	O
.	O
</s>
<s>
An	O
enhancement	O
to	O
the	O
general	O
forward-backward	B-Algorithm
algorithm	I-Algorithm
,	O
called	O
the	O
Island	B-Algorithm
algorithm	I-Algorithm
,	O
trades	O
smaller	O
memory	O
usage	O
for	O
longer	O
running	O
time	O
,	O
taking	O
time	O
and	O
memory	O
.	O
</s>
<s>
Furthermore	O
,	O
it	O
is	O
possible	O
to	O
invert	O
the	O
process	O
model	O
to	O
obtain	O
an	O
space	O
,	O
time	O
algorithm	O
,	O
although	O
the	O
inverted	O
process	O
may	O
not	O
exist	O
or	O
be	O
ill-conditioned	B-Algorithm
.	O
</s>
<s>
Given	O
HMM	O
(	O
just	O
like	O
in	O
Viterbi	B-Algorithm
algorithm	I-Algorithm
)	O
represented	O
in	O
the	O
Python	B-Language
programming	I-Language
language	I-Language
:	O
</s>
<s>
We	O
can	O
write	O
the	O
implementation	O
of	O
the	O
forward-backward	B-Algorithm
algorithm	I-Algorithm
like	O
this	O
:	O
</s>
<s>
In	O
the	O
running	O
example	O
,	O
the	O
forward-backward	B-Algorithm
algorithm	I-Algorithm
is	O
used	O
as	O
follows	O
:	O
</s>
