arXiv:1507.02314v2 [cs.DS] 9 May 2016

Distinguishing Hidden Markov Chains∗
Stefan Kiefer

A. Prasad Sistla

University of Oxford, UK

University of Illinois at Chicago, USA

ABSTRACT
Hidden Markov Chains (HMCs) are commonly used mathematical models of probabilistic systems. They are employed
in various fields such as speech recognition, signal processing, and biological sequence analysis. Motivated by applications in stochastic runtime verification, we consider the
problem of distinguishing two given HMCs based on a single
observation sequence that one of the HMCs generates. More
precisely, given two HMCs and an observation sequence, a
distinguishing algorithm is expected to identify the HMC
that generates the observation sequence. Two HMCs are
called distinguishable if for every ε > 0 there is a distinguishing algorithm whose error probability is less than ε. We show
that one can decide in polynomial time whether two HMCs
are distinguishable. Further, we present and analyze two distinguishing algorithms for distinguishable HMCs. The first
algorithm makes a decision after processing a fixed number
of observations, and it exhibits two-sided error. The second
algorithm processes an unbounded number of observations,
but the algorithm has only one-sided error. The error probability, for both algorithms, decays exponentially with the
number of processed observations. We also provide an algorithm for distinguishing multiple HMCs.

CCS Concepts
•Theory of computation → Random walks
and Markov chains;
Probabilistic computation;
•Mathematics of computing → Computing most
probable explanation;

Keywords
Hidden Markov chains; Labelled Markov chains; monitors

1.

INTRODUCTION

∗This is the full version of a LICS’16 paper.
Permission to make digital or hard copies of all or part of this work for personal or
classroom use is granted without fee provided that copies are not made or distributed
for profit or commercial advantage and that copies bear this notice and the full citation
on the first page. Copyrights for components of this work owned by others than the
author(s) must be honored. Abstracting with credit is permitted. To copy otherwise, or
republish, to post on servers or to redistribute to lists, requires prior specific permission
and/or a fee. Request permissions from permissions@acm.org.

LICS ’16, July 05 - 08, 2016, New York, NY, USA
c 2016 Copyright held by the owner/author(s). Publication rights licensed to ACM.
ISBN 978-1-4503-4391-6/16/07. . . $15.00
DOI: http://dx.doi.org/10.1145/2933575.2933608

Hidden Markov Chains (HMCs) are commonly used mathematical models of probabilistic systems. They are specified
by a Markov Chain, capturing the probabilistic behavior of a
system, and an observation function specifying the outputs
generated from each of its states. Figure 1 depicts two example HMCs H1 , H2 , with observations a and b. We consider
finite-state HMCs in this paper. An HMC randomly generates a (conceptually infinite) string of observations. The
states producing the observations are not observable (note
that s0 and s1 output the same observation a in the example). This motivates the term hidden.
HMCs are widely employed in fields such as speech recognition (see [22] for a tutorial), gesture recognition [6], musical score following [23], signal processing [9], and climate
modeling [1]. HMCs are heavily used in computational biology [12], more specifically in DNA modeling [8] and biological sequence analysis [11], including protein structure
prediction [18], detecting similarities in genomes [14] and
gene finding [2]. Following [19], applications of HMCs are
based on two basic problems, cf. [13, Chapter 2]: The first
one is, given an observation string and an HMC, what is
the most likely sequence of states that produced the string?
This is useful for areas like speech recognition, see [22] for
efficient algorithms based on dynamic programming. The
second problem is, given an observation string and multiple
HMCs, identify the HMC that is most likely to produce the
observation. This is used for classification.
The second problem raises a fundamental question, which
we address in this work: Given two HMCs, and assuming
that one of them produces a random single observation sequence, is it even possible to identify the producing HMC
with a high probability? And if yes, how many observations in that observation sequence are needed? At its heart,
this question is about comparing two HMCs in terms of their
(distributions on) observation sequences. To make this more
precise, let a monitor for two given HMCs H1 , H2 be an algorithm that reads (increasing prefixes of) a single observation sequence, and at some point outputs “H1 ” or “H2 ”. The
distinguishability problem asks for two given HMCs H1 , H2 ,
whether for all ε > 0 there is a monitor such that for both
i = 1, 2, if the monitor reads a random observation sequence
produced by Hi , then with probability at least 1 − ε the
monitor outputs “Hi ”.
A related problem is equivalence of HMCs. Two HMCs
are called equivalent if they produce the same (prefixes of)
observation sequences with the same probability. Equivalence of HMCs has been well-studied and can be decided in
polynomial time, using algorithms based on linear algebra,

1
4

H1 :

1
2

1

s2
1
2

b

s0
1
4

a
1

H2 :

1
2

t0
a

t1
1

b

s1
a

Figure 1: Two HMCs. Here H1 and H2 are distinguishable (see Example 2) and hence not equivalent.
see e.g. [15, 29, 10]. The exact relation between equivalence
and distinguishability depends on whether a monitor has access to a single random observation sequence or to multiple
such sequences.
(1) Consider first a notion of a “monitor” that has access
to several random observation sequences, each generated starting from the same initial state. Call this a
multi-monitor. If the two given HMCs are equivalent
then even a multi-monitor can only guess. Now assume the two HMCs are not equivalent. It is known
(see e.g. [29]) that then there exists a linear-length prefix of the observation sequence that is more likely in
one HMC than in the other HMC. A multi-monitor
could exploit the law of large numbers and only count
how often that particular observation prefix occurs.
Hence for multi-monitors, distinguishability and nonequivalence coincide.
(2) Consider now a monitor that has access to only a single
random observation sequence. Here, non-equivalence
does not imply distinguishability: loosely speaking, for
some HMCs it is the case that while the observation
prefix is increasing, the evidence added by each new
observation does not help the monitor enough to make
up its mind about which HMC produces the sequence.
Figure 2 shows an example of two HMCs that are
neither equivalent nor distinguishable. (On the other
hand, the HMCs in Figure 1 are not equivalent, but
are distinguishable as shown later in Section 3).
We assume in the rest of the paper that a monitor has access
to only a single random observation sequence. This is the
more natural version of the problem, both from the point
of view of the motivation mentioned above and from our
application in stochastic runtime monitoring.
We prove that the distinguishability problem is decidable
in polynomial time. We establish this result by showing that
two HMCs are distinguishable if and only if their total variation distance is equal to 1. This distance measure for HMCs
was studied in [7], and a polynomial-time algorithm for deciding whether the distance of two HMCs is 1 was given
there. That polynomial-time algorithm includes a mechanism for checking whether two given HMCs are equivalent
(but also needs other ingredients).
It is important to note that deciding distinguishability
does not readily provide a family of monitors as required by
the definition of distinguishability; it only guarantees their
existence. Developing a family of monitors (one for any desired error bound ε > 0) requires more insights. Inspired by

the area of sequential analysis [30], we design monitors that
track the likelihood ratio of the sequence of observations.
However, estimating the error probability of the monitors
is challenging, since one needs a bound on the change of
the likelihood ratio per observation. Unfortunately, such
a bound does not exist for HMCs in general, not even on
the difference of the log-likelihood ratio (see Example 6).
Hence, in this paper we take a different route: We consider
a different class of monitors that translate the given random
observation sequence into a certain kind of non-homogenous
“random walk” with bounded step size. This allows us to
employ martingale techniques, specifically Azuma’s inequality, to prove error bounds that decay exponentially with the
number of observations the monitor makes. Then we show
that the error bounds from a random-walk monitor carry
over to a likelihood-based monitor.
More specifically, we present two likelihood-based monitors for distinguishable HMCs. The first one makes a decision after reading a fixed number of observation symbols.
This number is chosen depending on the desired error bound:
we show that for an error probability ε it suffices to read the
prefix of length C log 1ε , where C > 0 is a polynomial-time
computable constant. This error is two-sided, i.e., the monitor may mistake H1 for H2 and vice versa.
The second monitor has only one-sided error: observation
sequences from H1 are almost always (i.e., with probability 1) recognized as stemming from H1 . However, on sequences generated by H2 , with high probability the monitor
never gives an answer. This is useful in applications such as
runtime verification (see Section 6). The expected number
of observations from H1 that the monitor processes before
giving its decision is O(log 1ε ), while ensuring an error probability of at most ε on observations from H2 . For this class
of monitors, we have a polynomial-time algorithm that computes an O(log 1ε ) upper bound on the expected number of
observations from H1 before a decision is given.
Main Contributions.
• We show that the distinguishability problem can be
decided in polynomial time (Section 3).
• We design two classes of likelihood-based monitors
that accomplish the following tasks (ε > 0 is an error bound):
(1) After O(log 1ε ) observations (the exact number
can be efficiently computed from the given HMCs)
the first monitor class provides a guess about the
source of the observations, such that the probability that the guess is wrong is at most ε (Sec-

1
2

H1 :

s0
a

2
3

1
1
2

s1
b

H2 :

t0
a

1
1
3

t1
b

Figure 2: Two HMCs. Here H1 and H2 are not distinguishable but not equivalent.
tion 4.2). This can be extended to more than two
HMCs (Section 4.4).
(2) For the second monitor class, if H1 produces the
observation sequence then the monitor raises an
alarm almost surely, and after an expected number
of O(log 1ε ) observations (such an upper bound
can be efficiently computed from the given HMCs
and ε); if H2 produces the observation sequence
then, with probability at least 1 − ε, the monitor
never raises an alarm (Section 4.3).
• We apply our results to stochastic runtime verification,
where a monitor should distinguish correct and faulty
behaviour of a single stochastic system. This yields
polynomial-time decidability of monitorability as defined in [26], as well as efficient runtime monitors for
stochastic systems, see Section 6.
Missing proofs can be found in the appendix.
Related Work. The area of sequential analysis in statistics, pioneered by Wald (see [30]), deals with the problem of
hypothesis testing using repeated and unbounded sampling.
A line of work going back to Phatarfod [21, 28, 24] investigated the application of sequential analysis, more specifically
the sequential probability ratio test, to Markov chains. Similar to our work, the goal in the above works is to identify
a Markov chain among several, in this case using likelihood
ratios. A monitor algorithm is derived by keeping track of
likelihood ratios: it gives notice once the likelihood ratio
drops below or exceeds some fixed threshold. One problem
with this approach is that error probabilities can only be
estimated—not bounded—by the heuristic assumption that
the excess over the threshold is not big. This assumption
is not always true. A more important difference from our
work is that the observation in each state equals the state,
in other words, the Markov chains are not hidden.
There is early related work that is more specific to HMCs.
The paper [16] aims at measuring a certain distance between
two HMCs by running one of them. This is in spirit close
to our work, as a positive distance in their sense could be
transformed to a monitor. However, the authors place strong
assumptions on the Markov chains, in particular ergodicity.
If this assumption is removed, their distance can be different
for different runs, and the existence of a lower bound on the
possible distances is unclear.
Work by Alur et al. [3] also aims at distinguishing
probabilistic models, but there are important differences.
First, they consider Markov Decision Processes rather than
Markov chains, i.e., they consider strategies to distinguish
two such processes, which is a more general, and computationally harder problem (they show PSPACE- and
EXPTIME-completeness results). Second, their problems

are defined such that the exact values of the transition probabilities is unimportant. In our case this is different.
The work in [19] deals with comparing two HMCs in terms
of various distance measures. Among other results, they
show NP-hardness of computing and approximating the ℓ1 distance. The HMCs considered there generate distributions
on finite strings of observations, as each HMC has a dedicated end state, reached with probability 1, where the HMC
“stops”. Such HMCs form a subclass of HMCs, whereas we
consider general HMCs.
Our work on distinguishability is inspired by the work
on monitorability that was defined in [26]. In [26, Section
4.1] a notion of strong monitorability is proposed and it is
shown that deciding it is PSPACE-complete. By our results
in Section 6, strong monitorability corresponds to a stronger
form of distinguishability, so the latter is PSPACE-complete
as well. In light of this it might be surprising that (general)
distinguishability turns out to be decidable in polynomial
time. In [26] it was wrongly claimed that monitorability is
undecidable for finite-state systems. Our result not only
shows that it is decidable, but also gives a polynomial-time
decision procedure.
Our work on exponentially decaying monitors is inspired
by the exponentially converging monitorable systems defined
in [27]. The algorithms presented there are for a very restricted class of HMCs, whereas our monitors work for all
pairs of distinguishable HMCs.
Closely related to some of our results is a very recent
work by Bertrand et. al. [5]. This paper also exploits the
results of [7] to obtain polynomial-time decidability of “AAdiagnosability” of stochastic systems, a problem related to
monitorability (Section 6). Although the technical report
of our work had been available [17], the results in [5] were
obtained independently and are largely orthogonal to ours:
whereas we focus on constructing specific monitors with
computable error bounds, they investigate the decidability
and complexity of several variants of diagnosability.

2. DEFINITIONS
Notation. For a countable set S, a probability distribution
ψ over S is a function ψ : S → [0, 1] such that
P
s∈S ψ(s) = 1. For an element s ∈ S, we let δs denote
the unique distribution with δs (s) = 1. We let Distr (S)
denote the set of all distributions over S. We let S ∗ , S ω respectively denote the set of finite sequences (strings) and the
set of infinite sequences of symbols from S. If S is a finite
set then we let |S| denote its cardinality. For any u ∈ S ∗ ,
we let |u| denote its length. For any real number x, we let
|x| denote its absolute value.
Hidden Markov Chains. A Markov chain is a triple
G = (S, R, φ) where S is a set of states, R ⊆ S × S, and

P
φ : R → (0, 1] is such that
t:(s,t)∈R φ(s, t) = 1 for all
s ∈ S. A Markov chain G and an initial state s ∈ S induce
a probability measure, denoted by Ps , on measurable subsets of {s}S ω in the usual way: more precisely, we consider
the σ-algebra generated by the cylinder sets {s0 s1 · · · sn }S ω
for n ≥ 0 and s0 = s and si ∈ S, with the probability
measure Ps such that
Ps ({s0 s1 · · · sn }S ω ) =

n
Y

φ(si−1 , si ) .

i=1

Let Σ be a finite set. A Hidden Markov Chain (HMC),
with observation alphabet Σ, is a triple (G, O, s0 ), where
G = (S, R, φ) is a Markov chain, and O : S → Σ is the
observation function, and s0 ∈ S is the initial state. We
may write P for Ps0 . For L ⊆ Σω we define the inverse
observation function

follows:
P1 ([L(M )]1 ) ≥ 1 − ε

P1 ([L(M )]1 ) ≥ 1 − ε

3.

and

P2 ([L(M )]2 ) ≤ ε .

POLYNOMIAL-TIME DECIDABILITY
OF THE DISTINGUISHABILITY PROBLEM

For two HMCs H1 , H2 define the (total variation) distance
between H1 and H2 , denoted by d(H1 , H2 ), as follows:
d(H1 , H2 ) := sup |P1 ([E]1 ) − P2 ([E]2 )| ,
E⊆Σω

where the supremum ranges over all measurable subsets
of Σω . It is shown in [7] that the supremum is in fact a
maximum. In particular, if d(H1 , H2 ) = 1 then there exists a
measurable set E ⊆ Σω with P1 ([E]1 ) = 1 and P2 ([E]2 ) = 0.
We show:
Proposition 1. HMCs H1 , H2 are distinguishable if and
only if d(H1 , H2 ) = 1.
Proof. Let H1 , H2 be two given HMCs. We show that
H1 , H2 are distinguishable if and only if d(H1 , H2 ) = 1.
• “if ”: Let d(H1 , H2 ) = 1. Choose ε > 0 arbitrarily. It
follows from [7, Theorem 7] and the discussion after
[7, Proposition 5] that there are k ∈ N and W ⊆ Σk
such that
P1 ([W Σω ]1 ) ≥ 1 − ε

and

P2 ([W Σω ]2 ) ≤ ε .

Construct a monitor M that outputs 1 after having
read a string in W . Then we have L(M ) = W Σω . It

P2 ([L(M )]2 ) ≤ ε .

Since ε was chosen arbitrarily, the HMCs H1 , H2 are
distinguishable.
• “only if ”: Let H1 , H2 be distinguishable, i.e., for every
ε > 0 there exists a monitor Mε such that
P1 ([L(Mε )]1 ) ≥ 1 − ε

and

P2 ([L(Mε )]2 ) ≤ ε .

Then we have:
d(H1 , H2 ) = sup |P([E]1 ) − P([E]2 )|
E⊆Σω

≥ sup P([L(Mε )]1 ) − P([L(Mε )]2 )
ε>0

≥ sup (1 − 2ε) = 1



ε>0

[L] := {s0 s1 · · · ∈ S ω | O(s0 )O(s1 ) · · · ∈ L} .
Monitors. A monitor M : Σ∗ → {⊥, 1} is a computable
function with the property that, for any u ∈ Σ∗ , if M (u) = 1
then M (uv) = 1 for every v ∈ Σ∗ . Let L(M ) ⊆ Σω denote
the set of infinite sequences that have a prefix u with M (u) =
1. (Intuitively, L(M ) is the set of observation sequences
which the monitor decides to have been generated by the
first HMC among a pair of such HMCs.) Given an HMC,
the event [L(M )] is measurable, as it is a countable union
of cylinder sets.
Distinguishability. Given two HMCs H1 , H2 with the
same observation alphabet Σ, we write P1 , P2 , [·]1 , [·]2 for
their associated probability measures and inverse observation functions. HMCs H1 , H2 are called distinguishable if
for every ε > 0 there exists a monitor M such that

and

This concludes the proof.
It follows that HMCs H1 , H2 are distinguishable iff there is
a distinguishing event, i.e., a set E ⊆ Σω with P1 ([E]1 ) = 1
and P2 ([E]2 ) = 0.
Example 2. Consider the HMCs H1 , H2 from Figure 1.
By computing the stationary distributions, one can show
that, the distinguishing event E is given by
E = {σ1 σ2 · · · ∈ Σω | lim

n→∞

f (n)
= 5/7} ,
n

where f (n) denotes the number of occurrences of a in
the prefix σ1 σ2 · · · σn , is a distinguishing event for H1 , H2 .
Hence H1 , H2 are distinguishable. Here, counting the frequencies of the observations symbols suffices for distinguishing two distinguishable HMCs. In general, this is not true:
the order of observations may matter.
Proposition 1 implies the following theorem:
Theorem 3. One can decide in polynomial time whether
given HMCs H1 , H2 are distinguishable.
Proof. In [7, Algorithm 1 and Theorem 21] it is shown
that, given two HMCs H1 , H2 , one can decide in polynomial
time whether d(H1 , H2 ) = 1. (The algorithm given there
solves n1 linear programs, each with n1 +n2 variables, where
n1 , n2 is the number of states in H1 , H2 , respectively.) Then
the result follows from Proposition 1.
Distinguishing events cannot in general be defined by monitors, as a monitor can reject an observation sequence only on
the basis of a finite prefix. Moreover, the decision algorithm
for Theorem 3 can assure the existence of a monitor for two
given HMCs, but the decision algorithm does not provide
useful monitors. That is the subject of the next section.

4. MONITORS
In this section, we present concrete monitors, with error
bounds. To this end we give some additional definitions in
Section 4.1, where we also explain how monitors can keep
track of certain conditional distributions. We also introduce
“profiles”, a key concept for our proofs of error bounds. In
Sections 4.2 and 4.3 we present monitors for distinguishable
HMCs with two-sided and one-sided error, respectively. In

Section 4.4 we provide a monitor for distinguishing among
multiple HMCs.
For i = 1, 2, let Hi = (Gi , Oi , si,0 ) be two HMCs with
the same observation alphabet Σ, where Gi = (Si , Ri , φi ).
Without loss of generality we assume S1 ∩ S2 = ∅. Let
m := |S1 | + |S2 |. We fix H1 , H2 and m throughout the
section.

4.1 Keeping Track of Probabilities and Profiles
Let i ∈ {1, 2} and ψ ∈ Distr (Si ). For u ∈ Σ∗ define
X
pr i (ψ, u) :=
ψ(s) · Pi,s ([uΣω ]i ) .
s∈Si

Intuitively, pr i (ψ, u) is the probability that the string u is
output by HMC Hi starting from the initial distribution ψ.
For W ⊆ Σm we also define
X
pr i (ψ, u) ,
pr i (ψ, W ) :=
u∈W

which is the probability that Hi outputs a string in W starting from distribution ψ. For u ∈ ΣΣ∗ and s, t ∈ Si define
|u|−1

sub i (s, u, t) := Pi,s ([uΣω ]i ∩ Si

{t}Siω ) .

Intuitively, sub i (s, u, t) is the probability that Hi outputs u
and is then in state t, starting from state s. We have:
X
X
pr i (ψ, u) =
ψ(s) ·
sub i (s, u, t)
(1)
s∈Si

t∈Si

∗

For any s, r ∈ Si and u ∈ ΣΣ and a ∈ Σ we have:
(P
if Oi (r) = a
t∈Si sub i (s, u, t)φi (t, r)
sub i (s, ua, r) =
0
otherwise
So if a monitor has kept track of the values sub i (s, u, t)s,t∈Si
for a prefix u of an observation sequence, it can,
upon reading the next observation a, efficiently compute
sub i (s, ua, t)s,t∈Si and, by (1), also pr i (δsi,0 , ua).
For u ∈ Σ∗ define the likelihood ratio
pr (δs , u)
lr (u) := 2 2,0
.
pr 1 (δs1,0 , u)
Finally, for u ∈ ΣΣ∗ with pr i (ψ, u) > 0, define the distribution cd i (ψ, u) (which stands for “conditional distribution”)
as follows:
X
1
cd i (ψ, u)(t) :=
·
ψ(s) · sub i (s, u, t) for t ∈ Si
pr i (ψ, u) s∈S
i

Intuitively, cd i (ψ, u)(t) is the conditional probability that Hi
is in state t given that it has output u and started from ψ.
As explained above, a monitor can efficiently keep track of
lr (u) and cd i (ψ, u).
We say that a pair of distributions (ψ1 , ψ2 ) ∈ Distr (S1 ) ×
Distr (S2 ) is reachable in (H1 , H2 ) if there is u ∈ ΣΣ∗ with
ψi = cd i (δsi,0 , u) for i = 1, 2. A profile for H1 , H2 is a
m
pair (A, c) such that A : Distr (S1 ) × Distr (S2 ) → 2Σ and
c ∈ (0, 1] and
pr 1 (ψ1 , A(ψ1 , ψ2 )) − pr 2 (ψ2 , A(ψ1 , ψ2 )) ≥ c
holds for all reachable pairs (ψ1 , ψ2 ) of distributions. For the
monitors presented in this section the following proposition
is crucial.

Proposition 4. Let HMCs H1 , H2 be distinguishable.
Then there is a number c > 0, computable in time polynomial in the sizes of H1 , H2 , such that there is a profile
(A, c).

4.2 Monitors with Two-Sided Error
In this and the next subsection, we assume that H1 , H2
are distinguishable, and fix a profile (A, c). The monitors
of this subsection take an observation sequence as input,
and at some point output a value from {1, 2, 3} indicating
a decision regarding which of the two HMCs generated the
observations. An output of 3 indicates that neither of the
HMCs could have generated it. The monitors of this subsection have two-sided errors: the answers 1 or 2 may be wrong
(with a small probability).
We define a likelihood-based monitor M2 (the subscript
denotes two-sided error) as follows. Monitor M2 runs in
phases; in each phase, the monitor receives m observations.
The monitor runs at most N phases, where N ∈ N is a
parameter fixed in advance: choosing a larger N leads to
smaller error probabilities. After reading an observation sequence u of length N · m, it computes the likelihood ratio
lr (u). Monitor M2 outputs 1 if lr (u) < 1, and 2 if lr (u) > 1.
It may output either 1 or 2 if lr (u) = 1. Monitor M2 needs
no access to the function A.
The following theorem says that the observation sequences
for which monitor M2 outputs 1 are much more likely to be
generated by H1 . By symmetry, the observation sequences
for which M2 outputs 2 are much more likely to be generated
by H2 .
Theorem 5. Consider the monitor M2 that reads the
first N · m observations. Let L(M2 ) ⊆ Σω be the set of observation sequences for which M2 outputs 1. Then we have
 2

c
P1 ([L(M2 )]1 ) − P2 ([L(M2 )]2 ) ≥ 1 − 2 exp −
·N .
18
Hence,

 2
c
·N
P1 ([L(M2 )]1 ) ≥ 1 − 2 exp −
18
 2

c
P2 ([L(M2 )]2 ) ≤ 2 exp −
·N .
18

and

Proving the bounds of Theorem 5 is challenging due to
the following reasons. For k ≥ 0 define a random variable
Lk : {s1,0 }S1ω → Q by

Lk (s1,0 s1 s2 · · · ) := lr O(s1,0 )O(s1 )O(s2 ) · · · O(sk−1 ) .

Denote by E1 the expectation with respect to P1 . It was
proved in [7, proof of Proposition 6] that E1 (Lk+1 | Lk =
x) = x holds for all x ∈ Q, i.e., the sequence L0 , L1 , . . . is
a martingale. Unfortunately, the differences |Lk+1 − Lk | are
not bounded, neither are the differences | log Lk+1 − log Lk |,
as the following example shows.
Example 6. Consider the HMCs H1 , H2 in Figure 3.
For n > 1, the probability
H1 generates the string
Pn−2 1that
i
an is ( 31 )n−1 + 13 ·
(
)
which
is easily shown to
i=0 3
be 21 (1 + ( 31 )n−1 ), and the probability that H1 generates
an b is ( 13 )n . The corresponding probabilities for H2 are
( 12 )n−1 and ( 21 )n , respectively. Now consider any α ∈
ω
{sn
0 s2 }{s0 , s1 , s2 } , for some n > 1. The two likelihood ratios Ln (α) and Ln+1 (α) corresponding to the length n and

1
3

H1 :

1
2

1
2

s2
1
3

1
2

b

H2 :

s0
a

1
2

t0
a

1
3

s1

1
2

t1
1
2

b

1

a

Figure 3: Two HMCs where the difference in log-likelihood ratios is unbounded
length n + 1 prefixes of α, are given by Ln (α) =
( 32 )n .

1 )n−1
(2
1 (1+( 1 )n−1 )
2
3

and Ln+1 (α) =
Since n > 1, we see that Ln (α) <
L
(α)
> ( 23 )n . So we have that
2 · ( 21 )n−1 ≤ 1. Hence, Ln+1
n (α)
log(Ln+1 (α)) − log(Ln (α)) > n · log( 32 ), which is unbounded
with increasing n. In a more general case, if α has b appearing infinitely often with an increasing number of a-symbols
between two successive b-symbols, then the difference in the
log-likelihood ratio of two successive prefixes of α, with the
second prefix ending with b, is unbounded.
This problem of unbounded differences between subsequent log-likelihood ratios prohibits a standard error analysis of hypothesis-testing methods from sequential analysis [30]. Moreover, Azuma’s inequality then does not yield
an exponentially decaying error bound. As a consequence,
we cannot directly prove the bounds of Theorem 5. Therefore, in this subsection, we take a detour. First we develop
another monitor M2′ that is not based on likelihoods but
is based on a random walk. Then we prove error bounds
for M2′ . Then we show that the error bounds for M2′ carry
over to the likelihood-based monitor M2 .
The monitor M2′ also runs in N phases, receiving m observations in each phase. The monitor maintains two probability distributions ψ1 ∈ Distr (S1 ), ψ2 ∈ Distr (S2 ), and
a variable x that takes rational values. Initially, ψ1 , ψ2 are
set to δs1,0 , δs2,0 respectively, and x is initialized to 0. The
monitor keeps track of ψi = cd i (δsi,0 , u), for i = 1, 2, where
u is the observation string received thus far. The variable x
indicates a current estimate about which of the two HMCs is
being observed: a negative value of x indicates a preference
for H1 ; a positive value indicates a preference for H2 . In
each phase, M2′ waits until it gets the next m observations
and then updates x, ψ1 and ψ2 .
We describe a phase of M2′ . Let ψ1 , ψ2 , x be the values at
the end of the previous phase. Let p1 = pr 1 (ψ1 , A(ψ1 , ψ2 ))
and p2 = pr 2 (ψ2 , A(ψ1 , ψ2 )). By the definition of a profile
we have p1 − p2 ≥ c > 0. Denote by v ∈ Σm the string
of observations received in the current phase. Assume that
pr 1 (ψ1 , v) > 0 and pr 2 (ψ2 , v) > 0 (i.e., v can be generated with non-zero probability by both H1 , H2 from ψ1 , ψ2
respectively). If p1 + p2 ≤ 1 then x is updated as follows:
(
x−1
if v ∈ A(ψ1 , ψ2 )
x :=
p1 +p2
x + 2−p
if v 6∈ A(ψ1 , ψ2 )
−p
1
2
If p1 + p2 > 1 then x is updated as follows:
(
1 −p2
if v ∈ A(ψ1 , ψ2 )
x − 2−p
p1 +p2
x :=
x+1
if v 6∈ A(ψ1 , ψ2 )

Note that in all cases, the value of x is increased or decreased
by at most 1. After this, ψ1 , ψ2 are set to cd 1 (ψ1 , v) and
cd 2 (ψ2 , v) respectively, and the phase is finished. On the
other hand, if pr 1 (ψ1 , v) > 0 and pr 2 (ψ2 , v) = 0 then 1 is
output; if pr 1 (ψ1 , v) = 0 and pr 2 (ψ2 , v) > 0 then 2 is output;
if pr 1 (ψ1 , v) = 0 and pr 2 (ψ2 , v) = 0 then 3 is output. In
those cases the monitor terminates immediately.
After N phases, if x ≤ 0 then the monitor M2′ outputs 1,
otherwise it outputs 2. An output of i indicates that the sequence is believed to be generated by Hi . Note that M2′ —in
contrast to M2 —needs access to the function A. By constructing a supermartingale and applying Azuma’s inequality we obtain:
Theorem 7. Consider the monitor M2′ running N
phases. Let L(M2′ ) ⊆ Σω be the set of observation sequences
for which M2′ outputs 1. Then,
 2

c
P1 ([L(M2′ )]1 ) ≥ 1 − exp −
·N
and
18

 2
c
·N .
P2 ([L(M2′ )]2 ) ≤ exp −
18
Hence the error probability decays exponentially with N .
To prove Theorem 5 we show (in the appendix) that the
same error bound, up to a factor of 2, holds for the
likelihood-based monitor M2 . The authors are not aware
of a proof of Theorem 5 that avoids reasoning about a
monitor like M2′ . The proof shows that the difference
P1 ([L(M2 )]1 ) − P2 ([L(M2 )]2 ) cannot be increased by any
other monitor that is based solely on the first N · m observations: M2 is optimal in that respect.
To guarantee an error probability bound of at most εof
the likelihood-based monitor M2 , we set N = ⌈ 18
· log 2ε ⌉.
c2

Example 8. Figure 4 shows two HMCs H1 , H2 with a
parameter δ ∈ (0, 14 ]. In every step except the first one,
H1 outputs a with probability 21 + δ, and b with probability
1
− δ. For H2 the probabilities are reversed. The HMCs
2
are distinguishable. The intuitive reason is that H1 tends to
output more a-symbols than b-symbols, whereas H2 tends to
output more b-symbols than a-symbols, and this difference is
exhibited in the long run. Intuitively speaking, the smaller δ
is, the “less distinguishable” are H1 and H2 . We will show
later that there is a profile with c = δ. By Theorem 5, the
probability that M2 mistakes H2 for H1 decays exponentially.
More specifically, for an error bound of ε it suffices to make
Bδ −2 log 1ε observations, for a constant B > 0. It can be
shown that there is a constant d > 0 such that M2 needs, for
small ε, at least dδ −2 log 1ε observations to push the error

1
2

H1 :

1
2

+δ
1
2

s1
1
2

+δ

b

H2 :

1
2

−δ
1
2

−δ

s0
a

1
2

−δ

+δ

t0
a

+δ

t1
1
2

−δ

b

Figure 4: Two distinguishable HMCs with a parameter δ ∈ (0, 14 ]
probability below ε. Hence, for the HMCs from Figure 4, the
bound of Theorem 5 is asymptotically tight. As mentioned
after the proof of Theorem 5, the likelihood-based monitor
is essentially optimal among monitors that observe a fixedlength prefix. So the bound from Theorem 7 is also asymptotically tight.

4.3 Monitors with One-Sided Error
Now we present M1 , a likelihood-based monitor with
one-sided error. Monitor M1 uses a threshold parameter
low ∈ (0, 1]. For each N > 0, after reading a prefix v, of
length N ·m, of observations, it computes the likelihood ratio
lr (v). If lr (v) ≤ low , it terminates outputting 1, otherwise
it continues.
For any infinite sequence u and integer i > 0, let u[i]
denote the prefix of u of length i. We fix an integer N > 0.
Let UN be the set of all u ∈ Σω such that lr (u[N · m]) ≤

c2
exp − 36
· N . Recall from Theorem 5 the set L(M2 ) of
observation sequences for which M2 outputs 1. It should
be easy to see that UN ⊆ L(M2 ). We need the following
technical lemma.
Lemma 9. P1 ([UN ]1 ) ≥ 1 − 4 exp




c2
·N
−
36

This allows us to prove the following theorem:
Theorem 10. Consider the monitor M1 with threshold
parameter low ∈ (0, 1]. Let L(M1 ) ⊆ Σω be the set of observation sequences for which M1 terminates (and hence outputs 1). Then,
P1 ([L(M1 )]1 ) = 1
and
P2 ([L(M1 )]2 ) ≤ low .
Now we analyze the response time of M1 taken on observation sequences generated by H1 . Formally, we define
a random variable T : {s1,0 }S1ω → N such that T is the
number of observations made by monitor M1 before outputting 1. The following proposition bounds the expected
value of T in H1 .
1
Proposition 11. E1 (T ) ≤ 36m
+ 147m
· log low
· low + m,
c2
c2
where E1 (T ) is the expected value of T under the probability
measure P1 .

The proof of this proposition employs ideas similar to those
in [27] for proving an upper bound on the expected monitoring time for exponentially converging monitorable systems.
Observe that as low decreases, the first term of the bound
dominates.

4.4 Monitors for Distinguishing Among Multiple HMCs
Now we address the problem of distinguishing among multiple mutually distinguishable HMCs. We present a monitor
based on likelihoods. For i = 1, . . . , k, let Hi = (Gi , Oi , si,0 )
be HMCs with the same observation alphabet Σ where
Gi = (Si , Ri , φi ). Let Pi and [·]i be the associated probability measures and inverse observation functions corresponding to the HMC Hi . We assume that they are mutually
distinguishable, i.e., for 1 ≤ i < j ≤ k, HMCs Hi and Hj
are distinguishable. So by Proposition 4 there are profiles
(Ai,j , ci,j ). Define c := min{ci,j | 1 ≤ i < j ≤ k}.
Let m := 2·max{|Si | | 1 ≤ i ≤ k} and N > 0 be an integer
parameter. The following monitor M distinguishes among
the k HMCs: it takes an observation sequence u ∈ ΣN·m as
input and outputs the smallest integer i ∈ {1, . . . , k} such
that pr i (δsi,0 , u) ≥ pr j (δsj,0 , u) for all j ∈ {1, . . . , k}. Essentially, M outputs the index of the HMC whose likelihood
value is the highest after N · m observations. By applying
the union bound to Theorem 5 we get:
Theorem 12. Consider the monitor M .
Let i ∈
{1, . . . , k} and let Li ⊆ ΣN·m be the set of observation
sequences for which M outputs i. Then we have for all
j ∈ {1, . . . , k} − {i}:
 2

c
ω
Pi ([Li Σ ]i ) ≥ 1 − 2k · exp −
·N
and
18
 2

c
Pj ([Li Σω ]j ) ≤ 2 exp −
·N
18

5. COMPUTING PROFILES
In the monitors of Section 4 the constant c > 0 determines the number N of phases needed to ensure a bound on
the error probability. Recall that c is the constant in a profile (A, c). Any such constant c will do, but the larger it is
the better, since the number of phases used will be smaller.
Note that even the existence of a positive c (as claimed by
Proposition 4) is not obvious. In this section, we prove Theorem 13—which strengthens Proposition 4—by presenting
a polynomial-time algorithm to compute a positive c and
also the representation of a profile function A in polynomial
time.
Let a test set Test ⊆ Σ∗ be a set of at most m words,
with |v| < m for all v ∈ Test . This defines a function
m
ATest : Distr (S1 ) × Distr (S2 ) → 2Σ in the following way.
Fix ψ1 ∈ Distr (S1 ) and ψ2 ∈ Distr (S2 ). Let v ∈ Test be
such that
v := arg max |pr 1 (ψ1 , w) − pr 2 (ψ2 , w)|
w∈Test

(2)

and write
∗

JvK := {vw | w ∈ Σ , |vw| = m}
for the set of strings of length m with v as a prefix. Then
define:
(
JvK
if pr 1 (ψ1 , v) > pr 2 (ψ2 , v)
ATest (ψ1 , ψ2 ) :=
Σm − JvK otherwise
Depending on the case above, pr i (ψi , ATest (ψ1 , ψ2 )) is either
pr i (ψi , v) or 1 − pr i (ψi , v). Hence:
pr 1 (ψ1 , ATest (ψ1 , ψ2 )) − pr 2 (ψ2 , ATest (ψ1 , ψ2 ))
= |pr 1 (ψ1 , v) − pr 2 (ψ2 , v)|

(3)

Given a test set Test and distributions ψ1 , ψ2 , a monitor
can compute the word v from (2) using (1), and hence the
probabilities pr i (ψi , ATest (ψ1 , ψ2 )). Moreover, a monitor
can check whether a given word w ∈ Σm is in ATest (ψ1 , ψ2 )
by checking whether v is a prefix of w.
Theorem 13. Let HMCs H1 , H2 be distinguishable. One
can compute, in polynomial time, a test set Test ⊆ Σ∗ and
a number c > 0 such that (ATest , c) is a profile.
The proof builds on [7] but requires further insights. For the
proof we need the concept of equivalence: For i = 1, 2 let
ψi ∈ Distr (Si ). We say that ψ1 is equivalent to ψ2 , written
as ψ1 ≡ ψ2 , if pr 1 (ψ1 , u) = pr 2 (ψ2 , u) holds for all u ∈ Σ∗ .
We have the following proposition:
Proposition 14. One can compute, in polynomial time,
a test set Test ⊆ Σ∗ such that for all ψ1 ∈ Distr (S1 ) and all
ψ2 ∈ Distr (S2 ) we have:
ψ1 ≡ ψ2

⇐⇒

∀u ∈ Test : pr 1 (ψ1 , u) = pr 2 (ψ2 , u)

The algorithm for Proposition 14 uses linear-algebra based
techniques that have been developed for deciding equivalence of HMCs, see e.g. [25, 29, 15, 10].
We fix Test for the remainder of the section. We define a
distance measure dist (ψ1 , ψ2 ) between ψ1 , ψ2 given by
dist (ψ1 , ψ2 ) := max |pr 1 (ψ1 , w) − pr 2 (ψ2 , w)| .
w∈Test

⇐⇒

dist (π1 , π2 ) = pr 1 (π1 , aa) − pr 2 (π2 , aa)


1
1
= +δ−
− δ = 2δ
2
2
dist (π1′ , π2′ ) = pr 1 (π1′ , ba) − pr 2 (π2′ , ba)


1
1
= +δ−
− δ = 2δ
2
2

Hence we have dist(ψ1 , ψ2 ) = 2δ > 0 for all reachable pairs
(ψ1 , ψ2 ) of distributions.
In order to illustrate some aspects of Proposition 15,
we use linear programming to compute a lower bound on
dist (ψ1 , ψ2 ) for all (reachable or unreachable) pairs (ψ1 , ψ2 )
of distributions. Concretely, we solve the following linear
program, where δ is the constant parameter from the HMCs
H1 , H2 , and the variables are x and variables encoding distributions ψ1 , ψ2 :
minimize x ≥ 0
subject to: ψ1 ∈ Distr (S1 ), ψ2 ∈ Distr (S2 ),
− x ≤ pr 1 (ψ1 , u)−pr 2 (ψ2 , u) ≤ x for all u ∈ Test .
An optimal solution is x = δ and ψ1 (s0 ) = 43 − δ2 and
ψ1 (s1 ) = 14 + δ2 and ψ2 (t0 ) = 43 + 2δ and ψ2 (t1 ) = 14 − 2δ .
Hence x = δ > 0 is a lower bound on dist (ψ1 , ψ2 ) for all
pairs of distributions, and hence, a fortiori, also for all
reachable pairs. As mentioned after Proposition 15, the
reachability aspect is in general (unlike in this example) essential for obtaining a positive lower bound. Indeed, the
proof of Proposition 15 takes advantage of further results
from [7].
If we compute a lower bound according to the proof Proposition 15, i.e., taking reachability into account, we obtain
c = 4δ/(3 + 2δ), which lies strictly between the previously
computed lower bounds δ and 2δ.
With Proposition 15 at hand, we are ready to prove Theorem 13:

By Proposition 14 we have:
ψ1 ≡ ψ2

In this example, the last symbol of any observation sequence reveals the state. Hence there are only two reachable
pairs of distributions: one is (π1 , π2 ) with π1 (s0 ) = π2 (t0 ) =
1, and the other one is (π1′ , π2′ ) with π1′ (s1 ) = π2′ (t1 ) = 1.
Using the definition of dist we compute:

dist(ψ1 , ψ2 ) = 0

For the following proposition, linear programming is used to
compute a lower bound on dist(ψ1 , ψ2 ) for reachable pairs
(ψ1 , ψ2 ) in distinguishable HMCs:
Proposition 15. Let H1 , H2 be distinguishable HMCs.
One can compute, in polynomial time, a rational number c > 0 such that for all reachable pairs (ψ1 , ψ2 ) of distributions we have dist(ψ1 , ψ2 ) ≥ c.
In general there may exist unreachable pairs (ψ1 , ψ2 ) of distributions with dist (ψ1 , ψ2 ) = 0, even for distinguishable
HMCs. Proposition 15 establishes in particular the nontrivial fact that for distinguishable HMCs there exists a positive
lower bound on dist (ψ1 , ψ2 ) for all reachable pairs (ψ1 , ψ2 ).
Example 16. Consider again the HMCs from Figure 4.
We compute the set Test according to the algorithm from
Proposition 14. This yields Test = {ε, a, aa, ba}, where ε
denotes the empty word.

Proof of Theorem 13. Compute Test according to
Proposition 14 and c > 0 according to Proposition 15. We
show that (ATest , c) is a profile. Let (ψ1 , ψ2 ) be a reachable
pair of distributions. Let v ∈ Σ∗ be as in (2). We have:
pr 1 (ψ1 , ATest (ψ1 , ψ2 )) − pr 2 (ψ2 , ATest (ψ1 , ψ2 ))
= |pr 1 (ψ1 , v) − pr 2 (ψ2 , v)|
by (3)
= max |pr 1 (ψ1 , w) − pr 2 (ψ2 , w)|
by (2)
w∈Test

= dist(ψ1 , ψ2 )
≥c

def. of dist
Proposition 15

This completes the proof.
We have seen that for a given error bound, the number of observations our monitors need to make depends
quadratically on 1c . So it may be beneficial to compute
a larger value of c, even if such a computation is expensive. To this end, for a distribution π ∈ Distr (S), write
supp(π) := {s ∈ S | π(s) > 0}. For HMCs H1 , H2 , if a

pair (ψ1 , ψ2 ) of distributions is reachable, we say that the
pair (supp(ψ1 ), supp(ψ2 )) is reachable. We have the following proposition:
Proposition 17. Let H1 , H2 be two distinguishable
HMCs. One can compute, in exponential time:
c :=

min

min

min

′ ,S ′ )∈2S1 ×2S2 ψ ∈Distr (S ′ ) ψ ∈Distr (S ′ )
reachable (S1
1
2
1
2
2

max

U ⊆Σm

pr 1 (ψ1 , U ) − pr 2 (ψ2 , U )



(Note that U ranges over a set of double-exponential size.)
This value of c is lower-bounded by the value of c > 0 from
Theorem 13, and it is part of a profile with

A(ψ1 , ψ2 ) = arg maxm pr 1 (ψ1 , U ) − pr 2 (ψ2 , U ) .
U ⊆Σ

6.

APPLICATION: RUNTIME VERIFICATION

In this section we discuss an application of monitors for
runtime verification of stochastic systems. Traditional verification aims at proving correctness of systems at the time of
their design. This quickly becomes infeasible, in particular
for complex systems with several components and stochastic
behavior, see e.g. [26]. Runtime verification is an alternative where a monitor observes a system while it is running,
and raises an alarm once a faulty behavior is detected. The
alarm may trigger, e.g., a fail-safe way of shutting the system down. HMCs were suggested in [26, 27] as models of
partially observable stochastic systems. In this section, the
monitor does not try to distinguish two HMCs, rather it tries
to distinguish correct and faulty behavior of a single HMC.
Definitions. For a probability measure P and measurable sets C, D such that P(C) > 0, we let P(D | C) denote the value P(C∩D)
, which is the conditional probability
P(C)
of D given C. A classifying HMC (cHMC) is a quadruple
H = (G, O, s0 , Class), where (G, O, s0 ) is an HMC and Class
is a condition classifying each bottom strongly connected
component (BSCC) of H as bad or good. For a cHMC and
a state s ∈ S we define:
Bad s := {ss1 s2 · · · ∈ {s}S ω | ∃i : si is in a bad BSCC}
Good s := {ss1 s2 · · · ∈ {s}S ω | ∃i : si is in a good BSCC}
Define Bad := Bad s0 and Good := Good s0 . The events
Bad and Good are disjoint and measurable. By fundamental
properties of Markov chains we have
P(Bad ∪ Good ) = P(Bad ) + P(Good ) = 1 .
To avoid trivialities we assume that P(Bad), P(Good ) > 0
(this can be checked in polynomial time by graph reachability). We say that a cHMC H is monitorable if for every
ε > 0 there exists a monitor M such that
P([L(M )] | Bad ) ≥ 1 − ε
P([L(M )] | Good ) ≤ ε .

and

In [26] the authors define and study monitorability of pairs
(H0 , A) where H0 is an HMC and A is a deterministic Streett
automaton. One can compute, in polynomial time, the product of H0 and A. That product is a cHMC H as defined
above. Then (H0 , A) is monitorable (in the sense of [26]) if
and only if H is monitorable (in the sense defined above).

A construction similar to one that was given in [4, Section
3] allows us, for a given cHMC H, to construct two HMCs
H1 , H2 that exhibit the bad and the good behavior of H
according to their conditional probabilities:
Proposition 18. Let
H
be
a
cHMC
with
P(Bad ), P(Good ) > 0.
Then one can compute, in
polynomial time, HMCs H1 , H2 such that for all measurable
events E ⊆ S ω we have
P1 (E) = P(E | Bad )

and

P2 (E) = P(E | Good ) .

It follows from Proposition 18 that distinguishing and
monitoring are equivalent: Given HMCs H1 , H2 , we can
combine them into a single cHMC H by introducing a new
initial state s0 , which branches to the initial states of H1 , H2
with probability 1/2 each. We classify the BSCCs of H1 and
of H2 as bad and good, respectively. Then for any E ⊆ Σω
we have
P1 (E) = P({O(s0 )}E | Bad ) and
P2 (E) = P({O(s0 )}E | Good ) ,
so any monitor for H can be translated in a straightforward
way into a monitor that distinguishes H1 and H2 . Conversely, given a cHMC H, we can compute H1 , H2 according
to Proposition 18. Then any monitor that distinguishes H1
and H2 also monitors H.
By combining this observation with Theorem 3 we obtain:
Corollary 19. One can decide in polynomial time
whether a given cHMC H is monitorable.
Another kind of monitorability, called strong monitorability [26], was shown PSPACE-complete in [26]. Strong monitorability implies monitorability.
Using Proposition 18 again, the monitors from Section 4
apply to monitoring cHMCs. For instance, the monitor with
one-sided error can guarantee that (a) given that the behavior is faulty then an alarm is raised with probability 1 and
within short expected time, and (b) given that the behavior
is correct then probably no alarm is raised.

7. CONCLUSIONS
In this paper we have considered the distinguishability
problem for HMCs. We have shown that it is decidable in
polynomial time.
We have presented two likelihood based monitors M1 , M2
for distinguishing between HMCs H1 , H2 based on the sequences of observations generated by them. The monitor M2
makes a decision after running for a fixed number of observations and exhibits two-sided error. It processes O(log 1ε )
observations to ensure an error probability of at most ε.
The monitor M1 has only one-sided error. The expected
number of observations it processes to identify a sequence
generated by H1 is O(log 1ε ) to guarantee an error probability of at most ε on sequences generated by H2 . We have also
provided a monitor for distinguishing multiple HMCs. All
error analyses rely on martingale techniques, in particular,
Azuma’s inequality.
Polynomial time bounded algorithms are provided, which
for the monitor M2 , compute the number of observations
that guarantees a given upper bound on the error, and for
the M1 compute the expected number of observations of H1

before which an alarm is raised, for a given error bound
on the probability of raising an alarm on inputs generated
by H2 . These algorithms employ linear programming based
techniques for computing profiles.
We have discussed an application to runtime verification of stochastic systems. The monitorability problem for
cHMCs is polynomial-time equivalent to distinguishability,
and hence decidable in polynomial time. We have shown
that the monitors developed in this paper can be adapted
so that they monitor cHMCs.
One direction for future work is to improve the efficiency
of computing a good lower bound on c. We have seen that
this bound strongly influences the number of observations
the monitor needs to make, so the bound may determine
the applicability of a monitor in practice. Another direction
is to develop a notion of a monitor for HMCs that are not
equivalent but not distinguishable. Such monitors might
still attempt to distinguish between the HMCs for as many
runs as possible.
Acknowledgments. Stefan Kiefer is supported by a University Research Fellowship of the Royal Society. Prasad
Sistla is partly supported by the NSF grants CCF-1319754
and CNS-1314485.

8.

REFERENCES

[1] P. Ailliot, C. Thompson, and P. Thomson. Space-time
modelling of precipitation by using a hidden Markov
model and censored Gaussian distributions. Journal of
the Royal Statistical Society, 58(3):405–426, 2009.
[2] M. Alexandersson, S. Cawley, and L. Pachter. SLAM:
Cross-species gene finding and alignment with a
generalized pair hidden Markov model. Genome
Research, 13:469–502, 2003.
[3] R. Alur, C. Courcoubetis, and M. Yannakakis.
Distinguishing tests for nondeterministic and
probabilistic machines. In Proceedings of STOC, pages
363–372. ACM, 1995.
[4] C. Baier, J. Klein, S. Klüppelholz, and S. Märcker.
Computing conditional probabilities in Markovian
models efficiently. In Proceedings of TACAS, volume
8413 of LNCS, pages 515–530, 2014.
[5] N. Bertrand, S. Haddad, and E. Lefaucheux. Accurate
approximate diagnosability of stochastic systems. In
Proceedings of LATA, pages 549–561, 2016.
[6] F.-S. Chen, C.-M. Fu, and C.-L. Huang. Hand gesture
recognition using a real-time tracking method and
hidden Markov models. Image and Vision Computing,
21(8):745–758, 2003.
[7] T. Chen and S. Kiefer. On the total variation distance
of labelled Markov chains. In Proceedings of
CSL-LICS, pages 33:1–33:10, 2014.
[8] G. Churchill. Stochastic models for heterogeneous
DNA sequences. Bulletin of Mathematical Biology,
51(1):79–94, 1989.
[9] M. Crouse, R. Nowak, and R. Baraniuk.
Wavelet-based statistical signal processing using
hidden Markov models. IEEE Transactions on Signal
Processing, 46(4):886–902, April 1998.
[10] L. Doyen, T. Henzinger, and J.-F. Raskin. Equivalence
of labeled Markov chains. International Journal of
Foundations of Computer Science, 19(3):549–563,
2008.

[11] R. Durbin. Biological Sequence Analysis: Probabilistic
Models of Proteins and Nucleic Acids. Cambridge
University Press, 1998.
[12] S. Eddy. What is a hidden Markov model? Nature
Biotechnology, 22(10):1315–1316, October 2004.
[13] A. Fraser. Hidden Markov Models and Dynamical
Systems. Society for Industrial and Applied
Mathematics, 2008.
[14] J. Gough, K. Karplus, R. Hughey, and C. Chothia.
Assignment of homology to genome sequences using a
library of hidden Markov models that represent all
proteins of known structure. Journal of Molecular
Biology, 313(4):903–919, 2001.
[15] H. Ito, S.-I. Amari, and K. Kobayashi. Identifiability
of hidden Markov information sources and their
minimum degrees of freedom. IEEE Transactions on
Information Theory, 38(2):324–333, March 1992.
[16] B.-H. Juang and L. Rabiner. A probabilistic distance
measure for hidden Markov models. AT&T Technical
Journal, 64(2):391–408, February 1985.
[17] S. Kiefer and A. Sistla. Distinguishing hidden Markov
chains. Technical report, arxiv.org, 2015. Available at
http://arxiv.org/abs/1507.02314.
[18] A. Krogh, B. Larsson, G. von Heijne, and
E. Sonnhammer. Predicting transmembrane protein
topology with a hidden Markov model: Application to
complete genomes. Journal of Molecular Biology,
305(3):567–580, 2001.
[19] R. Lyngsø and C. Pedersen. The consensus string
problem and the complexity of comparing hidden
Markov models. Journal of Computer and System
Sciences, 65(3):545–569, 2002.
[20] A. Papoulis and S. U. Pillai. Probability, Random
Variables and Stochastic Processes. McGrawHill,
NewYork, 2002.
[21] R. Phatarfod. Sequential analysis of dependent
observations. I. Biometrika, 52(1-2):157–165, 1965.
[22] L. Rabiner. A tutorial on hidden Markov models and
selected applications in speech recognition.
Proceedings of the IEEE, 77(2):257–286, 1989.
[23] C. Raphael. Automatic segmentation of acoustic
musical signals using hidden Markov models. IEEE
Transactions on Pattern Analysis and Machine
Intelligence, 21(4):360–370, April 1999.
[24] N. Schmitz and B. Süselbeck. Sequential probability
ratio tests for homogeneous Markov chains. In
Mathematical Learning Models – Theory and
Algorithms, volume 20 of Lecture Notes in Statistics,
pages 191–202, 1983.
[25] M.-P. Schützenberger. On the definition of a family of
automata. Inf. and Control, 4:245–270, 1961.
[26] A. Sistla, M. Žefran, and Y. Feng. Monitorability of
stochastic dynamical systems. In Proceedings of CAV,
volume 6806 of LNCS, pages 720–736, 2011.
[27] A. Sistla, M. Žefran, Y. Feng, and Y. Ben. Timely
monitoring of partially observable stochastic systems.
In Proceedings of the 17th international conference on
Hybrid systems: computation and control (HSCC14),
pages 61–70, 2014.
[28] R. Swamy. Sequential comparison of two Markov
chains. Biometrika, 70(1):293–296, 1983.

[29] W. Tzeng. A polynomial-time algorithm for the
equivalence of probabilistic automata. SIAM Journal
on Computing, 21(2):216–227, 1992.
[30] B. Wetherill and K. Glazenbrook. Sequential Methods
in Statistics. Chapman and Hall, 1986.
[31] D. Williams. Probability with Martingales. Cambridge
University Press, 1991.

APPENDIX

we obtain:

A.

2 !
c
n
o
·N
c
P1 {XN > 0} = P1 YN > · N ≤ exp − 2
2
2
2N · 23
 2

c
= exp −
·N
18

PROOFS OF SECTION 4

We prove Theorem 7 from the main text.
Theorem 7.
Consider the monitor M2′ running N
′
phases. Let L(M2 ) ⊆ Σω be the set of observation sequences
for which M2′ outputs 1. Then,

 2
c
·N
and
P1 ([L(M2′ )]1 ) ≥ 1 − exp −
18
 2

c
P2 ([L(M2′ )]2 ) ≤ exp −
·N .
18
Proof. Let ψ1,k , ψ2,k and Xk denote the values of ψ1 , ψ2
and x directly after the k-th phase, for k ≥ 0. Initially,
ψ1,0 = δs1,0 , ψ2,0 = δs2,0 and X0 = 0. For k ≥ 1, let
uk ∈ Σk·m be the sequence of all observations received until
and including phase k. By induction on k, it is easy to see
that ψi,k = cd i (δsi,0 , uk ) for k ≥ 0, i = 1, 2.
Note that Xk depends only on uk . In the following we view
X0 , X1 , . . . as a sequence of random variables. (Formally,
for k ≥ 0 the random variable Xk is a function of type Xk :
{s1,0 }S1ω → Q.) We also define a sequence Y0 , Y1 , . . . of
random variables with Yk = Xk + k · 2c . Note that X0 =
Y0 = 0.
We show that the sequence of random variables Y0 , Y1 , . . .
forms a supermartingale in H1 . Let k ≥ 0. Fix uk ∈ Σk·m .
Recall that this determines Xk . For the conditional expected
value of Xk+1 given prefix uk we have:
ω

E (Xk+1 | [uk Σ ]1 ) = Xk + d ,


 2
c
·N .
P1 ([L(M )]1 ) = P1 {XN ≤ 0} ≥ 1 − exp −
18
2

From this, it follows that P1 ([L(M )]1 ) ≥ 1 − exp(− c18 · N ).
The proof of the second inequality in the statement is similar with the following modifications. The random variables
Xk′ are defined like Xk , but on sequences of states in H2
rather than H1 . Define Yk′ = Xk′ − k · 2c . The sequence
Y0′ , Y1′ , . . . is now a submartingale. Applying Azuma’s inequality to this submartingale now leads to the second inequality claimed in the statement.
The following lemma is used for the proof of Theorem 5.
Lemma 20. Let S be a countable set. Let ψ1 , ψ2 be probability distributions over
PS. For i ∈ {1, 2} and any event
V ⊆ S define ψi (V ) := v∈V ψi (v). Define
W := {s ∈ S | ψ1 (s) ≥ ψ2 (s)} .

Then
max ψ1 (V ) − ψ2 (V )
V ⊆S

(4)

where d denotes the expected change of x after phase k +
1. Recall that ψi,k = cd i (δsi,0 , uk ) for i = 1, 2. Let pi =
pr i (ψi,k , A(ψ1,k , ψ2,k )), for i = 1, 2. Assume p1 + p2 ≤ 1.
According to our rule for updating x we then have:
d = p1 · (−1) + (1 − p1 ) ·

Hence,

p1 + p2
p2 − p1
=
2 − p1 − p2
2 − p1 − p2

p2 − p1
c
≤ −
2
2

Proof. If s ∈ S with s 6∈ V and ψ1 (s) ≥ ψ2 (s), then
ψ1 (V ∪ {s}) − ψ2 (V ∪ {s})
= ψ1 (V ) − ψ2 (V ) + ψ1 (s) − ψ2 (s)
≥ ψ1 (V ) − ψ2 (V ) .
Similarly, if s ∈ S with s ∈ V and ψ1 (s) < ψ2 (s), then

E (Yk+1

c
| [uk Σ ]1 ) = Yk + + d ≤ Yk
2

ψ1 (V \ {s}) − ψ2 (V \ {s})
= ψ1 (V ) − ψ2 (V ) − ψ1 (s) + ψ2 (s)
> ψ1 (V ) − ψ2 (V ) .
The statement of the lemma follows.

Combining this with (4) and the definition of Yk we obtain:
ω

= ψ1 (W ) − ψ2 (W ) ,

i.e., W maximizes the probability difference over all events.

This is negative. Moreover, by the definition of a profile we
have p1 − p2 ≥ c > 0. Further more, 1 ≤ 2 − p1 − p2 < 2.
Hence:
d ≤



(5)

Now assume p1 + p2 > 1. Then we have:
p2 − p1
2 − p1 − p2
+ (1 − p1 ) · 1 =
p1 + p2
p1 + p2
p2 − p1
c
≤
≤ − ,
2
2

d = −p1 ·

so (5) again follows. Hence we have shown that Y0 , Y1 , . . . is
a supermartingale in H1 .
By definition of the update rule we have |Xk+1 − Xk | ≤ 1
and hence |Yk+1 − Yk | ≤ 1 + 2c ≤ 23 . Applying Azuma’s
inequality (see, e.g., [31]) to the supermartingale Y0 , Y1 , . . .

Now we prove Theorem 5 from the main text.
Theorem 5.
Consider the monitor M2 that reads
the first N · m observations. Let L(M2 ) ⊆ Σω be the set
of observation sequences for which M2 outputs 1. Then we
have
 2

c
P1 ([L(M2 )]1 ) − P2 ([L(M2 )]2 ) ≥ 1 − 2 exp −
·N .
18
Hence,

 2
c
·N
P1 ([L(M2 )]1 ) ≥ 1 − 2 exp −
18
 2

c
P2 ([L(M2 )]2 ) ≤ 2 exp −
·N .
18

and

Proof. Let N ≥ 0. We can write L(M2 ) = W Σω
where W ⊆ ΣN·m denotes the set of observation prefixes
of length N · m on which M2 outputs 1. Then we have:
W = {u ∈ ΣN·m | pr 1 (δs1,0 , u) ≥ pr 2 (δs2,0 , u)}
= {u ∈ ΣN·m | P1 ([{u}Σω ]1 ) ≥ P2 ([{u}Σω ]2 )}
(We left the output of the monitor unspecified when the
likelihood ratio is equal to 1. As a consequence, the inequalities above might be strict. This does not affect the rest of
the argument.) Using Lemma 20 we obtain the following
inequality.
P1 ([W Σω ]1 ) − P2 ([W Σω ]2 ) ≥ P1 ([V Σω ]1 ) − P2 ([V Σω ]2 )
(6)
for all V ⊆ ΣN·m . In particular, this holds for the prefixes
of length N · m of L(M2′ ) from Theorem 7. Hence we have:
P1 ([L(M2 )]1 ) − P2 ([L(M2 )]2 )
= P1 ([W Σω ]1 ) − P2 ([W Σω ]2 )
≥
≥

P1 ([L(M2′ )]1 )


1 − 2 exp −

−
2

P2 ([L(M2′ )]2 )

c
·N
18



Proof. By contradiction. Contrary to the lemma, assume:

 c2
·N
(7)
P1 ([UN ]1 ) < 1 − 4 exp −
36

Let VN := L(M2 ) − UN , and let WN denote the set of all
prefixes, of length N · m, of sequences in VN . Clearly, for all
v ∈ WN :
 c2

exp −
· N < lr (v) < 1
36
It follows for all v ∈ WN :
 c2

pr (δs2,0 , v)
< exp
· N · pr 2 (δs2,0 , v)
pr 1 (δs1,0 , v) = 2
lr (v)
36
Hence, P1 ([VN ]1 ) =

as L(M2 ) = W Σ

< exp

by (6)
by Theorem 7

Computation for Example 8. We analyse the likelihoodbased monitor M2 for the HMCs of Figure 4. The monitor M2 makes N · m = 4N observations. It is easy to see
that it outputs 1 if and only if it reads at least as many
a-symbols as b-symbols, i.e., the number of read a-symbols
is at least 2N . Hence we have:
P2 ([L(M2 )]2 )
!
i 
4N−i
4N
X
1
4N
1
=
−δ
+δ
i
2
2
i=2N
!
2N 
2N
4N
1
1
−δ
+δ
≥
2
2
2N
2N

1
(4N )!
2
−δ
=
(2N )! · (2N )! 4
2N

22N · (4N − 1) · (4N − 3) · · · 5 · 3 1
=
− δ2
(2N ) · (2N − 1) · (2N − 2) · · · 2 · 1 4
2N

22N
1
≥
· 22N−1 ·
− δ2
(2N )
4
1
2 2N
=
· 1 − 4δ
4N

pr 1 (δs1,0 , v)

v∈WN

ω

This concludes the proof of the Theorem 5.

X

 c2

36

 X
·N ·
pr 2 (δs2,0 , v) .

From Theorem 5 we know that

(8)

v∈WN

|

{z

=P2 ([VN ]2 )

P2 ([VN ]2 ) ≤ P2 ([L(M2 )]2 ) ≤ 2 exp



−

}


c2
·N .
18

By combining this with (8), we get:
 c2

 c2

P1 ([VN ]1 ) < 2 exp
· N · exp −
·N
36
18
 c2

= 2 exp −
·N
36

(9)

We have P1 ([L(M2 )]1 ) = P1 ([VN ]1 ) + P1 ([UN ]1 ). Using (7)
and (9), we get:

 c2

 c2
· N < 1 − 2 exp −
·N
P1 ([L(M2 )]1 ) < 1 − 2 exp −
36
18
But this contradicts Theorem 5.

We prove Theorem 10 from the main text:
Theorem 10.
Consider the monitor M1 with threshold parameter low ∈ (0, 1]. Let L(M1 ) ⊆ Σω be the set of
observation sequences for which M1 terminates (and hence
outputs 1). Then,
P1 ([L(M1 )]1 ) = 1
and
P2 ([L(M1 )]2 ) ≤ low .

For x ∈ [0, 21 ] we have ln(1 − x) ≥ −2x. So we can continue
as follows:
P2 ([L(M2 )]2 )

1
≥
· exp −16δ 2 N
4N

≥ exp −17δ 2 N
for large N

It follows that for small ε, an inequality ε ≥ P2 ([L(M2 )]2 )
1 −2
implies that N ≥ 17
δ ln 1ε . This completes the calculation
for the example.
We prove Lemma 9 from the main text.

 c2
·N
Lemma 9. P1 ([UN ]1 ) ≥ 1 − 4 exp −
36

Proof.
 2 Let N0 be the smallest integer such that
c
exp − 36
· N0 ≤ low. Clearly, for all N ≥ N0 we have

L(M1 ) ⊇ UN where UN is the set defined at the beginning
of Section 4.3. From this observation and Lemma 9, we see
that for all N ≥ N0 :

 2
c
·N
P1 ([L(M1 )]1 ) ≥ 1 − 4 exp −
36
From this, we get

 2
c
· N = 1.
P1 ([L(M1 )]1 ) ≥ lim 1 − 4 exp −
N→∞
36

Let X = {v ∈ (Σm )∗ | pr 1 (δs1,0 , v) > 0, lr (v) ≤ low , ∀ i <
|v| : lr (v[i]) > low }. Intuitively, X is the set of shortest observation sequences whose length is a multiple of m
and whose likelihood ratio is ≤ low . It is easy to see that
L(M1 ) = XΣω . Observe that there do not exist two distinct
sequences v1 , v2 ∈ X such that v1 is a prefix of v2 .
X
pr 2 (δs2,0 , v)
P2 ([L(M1 )]2 ) =

terms decrease monotonically. Hence we can upper bound
2
its value by the sum of the first three terms,
which
is (1− c36 +


c4
2·362

2

c
). From this, we see that 1 − exp − 36

72c2 −c4
.
2·362

X

4m · low ·

1
8 · 362 · m · low
 ≤ 2

2
c
c · (72 − c2 )
1 − exp − 36
8 · 362 · m · low
71 · c2
147 · m · low
≤
c2

pr 1 (δs1,0 , v)

≤

v∈X

= low · P1 ([L(M1 )]1 ) ≤ low

We prove Proposition 11 from the main text.
1
+ 147m
·log low
·low +m,
Proposition 11. E1 (T ) ≤ 36m
c2
c2
where E1 (T ) is the expected value of T under the probability
measure P1 .
Proof. Since T is a nonnegative integer
P valued random
variable, from [20], we see that E1 (T ) = n≥0 P1 {T > n}.
Since M1 only decides after each phase, i.e., after reading each successive
P sequence of m observations, we see
that E1 (T ) =
N≥0 m · P1 {T > N · m}. Let N0 be
2
the smallest integer such that exp − c36 · N0 ≤ low , i.e.,
N0 = ⌈ c362 · log
N0 −1

E1 (T ) =

X

N=0

1
low

⌉. We have:

X
m · P1 {T > N · m}
m · P1 {T > N · m} +
{z
} N≥N
|

≤ m · N0 + m ·

≤1

X

0

P1 {T > N · m}

(10)

N≥N0

For N ≥ 0, let XN = {u ∈ Σω | lr (u[N · m]) > low }.
Observe that, for N ≥ N0 , XN ⊆ Σω − UN . Further,
P1 {T > N · m} ≤ P1 ([XN ]1 ) ≤ P1 ([Σω − UN ]1 )
 2

c
≤ 4 exp −
·N
from Lemma 9.
36
(11)
From (10) and (11), we get
 2

X
c
exp −
E1 (T ) = m · N0 + 4m ·
·N
36
N≥N0


X
c2
exp −
= m · N0 + 4m ·
· (N + N0 )
36
N≥0
 2
 2
 X

c
c
= m · N0 + 4m · exp −
exp −
· N0 ·
·N
36
36
N≥0

1


≤ m · N0 + 4m · low ·
2
1 − exp − c36
≤

(12)

substituting for N0 .



2

c
By using a Taylor series expansion of exp − 36
, we get an
infinite sum in which the signs of the terms alternate starting
with a positive sign, and in which the absolute values of the

4

c
− 2·36
2 =

since c < 1
(13)

Using the bound of (13) in (12), we obtain the statement.
We prove Theorem 12 from the main text.
Theorem 12.
Consider the monitor M . Let i ∈
{1, . . . , k} and let Li ⊆ ΣN·m be the set of observation
sequences for which M outputs i. Then we have for all
j ∈ {1, . . . , k} − {i}:
 2

c
Pi ([Li Σω ]i ) ≥ 1 − 2k · exp −
·N
and
18

 2
c
·N
Pj ([Li Σω ]j ) ≤ 2 exp −
18
Proof. For j ∈ {1, . . . , k} − {i} define:
(
{u ∈ ΣN·m | pr i (δsi,0 , u) ≥ pr j (δsj,0 , u)}
Li,j :=
{u ∈ ΣN·m | pr i (δsi,0 , u) > pr j (δsj,0 , u)}

if i < j
if i > j

By Theorem 5 we have:
 2

c
Pi ([Li,j Σω ]i ) ≥ 1 − 2 · exp −
·N
18
 2

c
Pj ([Li,j Σω ]j ) ≤ 2 exp −
·N
18

and

We have:
1 − Pi ([Li Σω ]i )
= 1 − Pi ([∩j6=i Li,j Σω ]i )

Li = ∩j6=i Li,j

N·m

ω

= Pi ([∪j6=i (Σ
− Li,j )Σ ]i )
X
N·m
Pi ([(Σ
− Li,j )Σω ]i )
≤

union bound

j6=i

=

X

1 − Pi ([Li,j Σω ]i )

j6=i




 2
c
·N
2 · exp −
18
j6=i
 2

c
≤ 2k · exp −
·N
18
≤

1
1
36m


· log
+ m + 4m · low ·
c2
c2
low
1 − exp − 36

c2
36

Using this, after simplification, we see that

v∈X

≤ low ·

≥

X

by (14)

The first inequality follows. Further we have:
Pj ([Li Σω ]j ) ≤ Pj ([Li,j Σω ]j )

 2
c
·N
≤ 2 exp −
18
This proves the second inequality.

Li ⊆ Li,j
by (14)

(14)

B.

PROOFS OF SECTION 5

We prove Proposition 14 from the main text.
Proposition 14.
One can compute, in polynomial
time, a test set Test ⊆ Σ∗ such that for all ψ1 ∈ Distr (S1 )
and all ψ2 ∈ Distr (S2 ) we have:
ψ1 ≡ ψ2

⇐⇒

∀u ∈ Test : pr 1 (ψ1 , u) = pr 2 (ψ2 , u)

Proof. For both i = 1, 2 and all a ∈ Σ define a matrix
Mi (a) ∈ [0, 1]Si ×Si with
(

φi (s, t) if Oi (s) = a
Mi (a) s,t =
for all s, t ∈ Si .
0
otherwise

For i = 1, 2 write ηi ∈ {1}Si for the column vector all
whose entries are 1. For i = 1, 2 and for any string
u = a1 . . . ak ∈ Σ∗ define the column vector ηi (u) ∈
[0, 1]Si with ηi (u) = Mi (a1 ) · . . . · Mi (ak ) · ηi . For all
s ∈ Si we have, according to the definitions, the equality
ηi (u) s = Pi,s ([uΣω ]i ), which is the probability that the
string u is output by Hi starting from s. For a distribution
ψi ∈ Distr (Si ) write hψi i ∈ [0, 1]Si for the stochastic row
vector with hψi is = ψi (s). According to the definitions, we
have pr i (ψ1 , u) = hψi i · ηi (u) for all u ∈ Σ∗ . Define


η1 (u)
for all u ∈ Σ∗ .
∈ [0, 1]S1 ∪S2
η(u) :=
−η2 (u)
Hence we have ψ1 ≡ ψ2 if and only if

hψ1 i hψ2 i · η(u) = 0
for all u ∈ Σ∗ .


It follows that we have ψ1 ≡ ψ2 if and only if hψ1 i hψ2 i
is orthogonal to the vector space, say V, spanned by {η(u) |
u ∈ Σ∗ }. Define


M1 (u)
0
M (u) :=
∈ [0, 1](S1 ∪S2 )×(S1 ∪S2 )
0
M2 (u)
for all u ∈ Σ∗ . Note that η(au) = M (a)η(u) holds for
all a ∈ Σ and all u ∈ Σ∗ . Hence the vector space V can
be equivalently described as the smallest vector space that
contains η(ε) (where ε denotes the empty string, i.e., all
entries of η(ε) are ±1) and satisfies M (a)v ∈ V for all a ∈ Σ
and all v ∈ V.
We now give a polynomial-time algorithm for computing
a set Test ⊆ Σ∗ . The algorithm is as follows: Initialize
Test := {ε} where ε denotes the empty string. Then, as
long as there are a ∈ Σ and w ∈ Test such that M (a)η(w)
is linearly independent of {η(u) | u ∈ Test }, set Test :=
Test ∪ {aw}.
Now we show that the computed set Test has the properties claimed in the proposition. Since V is the smallest vector space that contains η(ε) and satisfies M (a)v ∈ V for all
a ∈ Σ and all v ∈ V, the set U := {η(u) | u ∈ Test } for the
computed set Test is a basis for V. Since V is a subspace of
RS1 ∪S2 , the dimension of V is at most m = |S1 | + |S2 |. Since
U is a basis, we have |Test | ≤ m. Since every string that the
algorithm adds to Test is only one letter longer than some
other string already in Test , it follows that |u| < |Test | ≤ m
holds for all u ∈ Test . Finally we show for all ψ1 ∈ Distr (S1 )
and all ψ2 ∈ Distr (S2 ):
ψ1 ≡ ψ2

⇐⇒

∀u ∈ Test : pr 1 (ψ1 , u) = pr 2 (ψ2 , u)

The direction “=⇒” is immediate. For the converse “⇐=”,
assume pr 1 (ψ1 , u) = pr 2 (ψ2 , u) for all u ∈ Test . Then we
have for all u ∈ Test :
0 = pr 1 (ψ1 , u) − pr 2 (ψ2 , u)
= hψ1 i · η1 (u) − hψ2 i · η2 (u)

= hψ1 i hψ2 i · η(u)

Since {η(u) | u ∈ Test } = U is a basis for V, it follows that
hψ1 i hψ2 i is orthogonal to V. We have already argued
that this implies ψ1 ≡ ψ2 . This completes the proof.
We prove Proposition 15 from the main text.
Proposition 15. Let H1 , H2 be distinguishable HMCs.
One can compute, in polynomial time, a rational number c >
0 such that for all reachable pairs (ψ1 , ψ2 ) of distributions
we have dist (ψ1 , ψ2 ) ≥ c.
Proof. We say a state s1 ∈ S1 dominates a distribution
ψ1 ∈ Distr (S1 ) if ψ1 (s1 ) ≥ ψ1 (t1 ) holds for all t1 ∈ S1 .
We say a pair of states (s1 , s2 ) is reachable if there exists
a reachable pair of distributions (ψ1 , ψ2 ) with ψi (si ) > 0
for both i = 1, 2. Note that one can compute, in polynomial time, from H1 , H2 the set of all reachable pairs of
states. For s1 ∈ S1 define Unreach(s1 ) := {s2 ∈ S2 |
(s1 , s2 ) is not reachable.}. For every s1 ∈ S1 , consider the
following linear program LP(s1 ) over a real variable x and
over real variables encoding distributions ψ1 ∈ Distr (S1 )
and ψ2 ∈ Distr (S2 ):
minimize x ≥ 0
subject to: ψ1 ∈ Distr (S1 )
ψ2 ∈ Distr (S2 )
s1 dominates ψ1
ψ2 (s2 ) = 0 for all s2 ∈ Unreach(s1 )
− x ≤ pr 1 (ψ1 , u) − pr 2 (ψ2 , u) ≤ x
for all u ∈ Test .
Note that all constraints arePlinear (in)equalities. In particular, we have pr i (ψi , u) = s∈Si ψi (s) · Pi,s ([uΣω ]i ). The
probabilities Pi,s ([uΣω ]i ) can be computed in polynomial
time. (Those probabilities are computed already when computing the set Test according to the proof of Proposition 14:
they are the probabilities in the vectors ηi (u) defined there.)
For every s1 ∈ S1 , let c(s1 ) denote the optimum solution
(minimizing x) of LP(s1 ). Define c := min{c(s1 ) | s1 ∈ S1 }.
Note that c can be computed in polynomial time. We show
that c has the properties claimed by the proposition.
First we show that dist (ψ1 , ψ2 ) ≥ c holds for all reachable
pairs (ψ1 , ψ2 ). Towards a contradiction suppose that there is
a reachable pair (ψ1 , ψ2 ) with dist(ψ1 , ψ2 ) < c. Let s1 ∈ S1
be a state that dominates ψ1 . Since (ψ1 , ψ2 ) is reachable, we
have ψ2 (s2 ) = 0 for all s2 ∈ Unreach(s1 ). By the definition
of dist (ψ1 , ψ2 ), we have
−dist (ψ1 , ψ2 ) ≤ pr 1 (ψ1 , u) − pr 2 (ψ2 , u) ≤ dist(ψ1 , ψ2 )
for all u ∈ Test . It follows that x := dist (ψ1 , ψ2 ) along with
ψ1 , ψ2 is a feasible solution of the linear program LP(s1 ).
Since c(s1 ) is optimal, we have c(s1 ) ≤ dist(ψ1 , ψ2 ). By our
assumption we have dist(ψ1 , ψ2 ) < c, hence c(s1 ) < c. But
by the definition of c we have c ≤ c(s1 ), a contradiction. We
conclude that dist (ψ1 , ψ2 ) ≥ c holds for all reachable pairs
(ψ1 , ψ2 ).

Finally, we show c > 0. Towards a contradiction suppose
c = 0. So by definition of c there is s1 ∈ S1 with c(s1 ) = 0.
Thus, LP(s1 ) has a solution with x = 0. That is, there
exist ψ1 ∈ Distr (S1 ) and ψ2 ∈ Distr (S2 ) such that s1 dominates ψ1 , and
ψ2 (s2 ) = 0 holds for all s2 ∈ Unreach(s1 ),

ψ1 ≡ ψ2 .

(16)

Since s1 dominates ψ1 , we have
ψ1 (s1 ) > 0 .

(17)

It follows directly from [7, Theorem 21] that (15)–(17) together imply that we have d(H1 , H2 ) < 1 for the the total
variation distance d defined in the beginning of Section 3.
But then Proposition 1 implies that H1 , H2 are not distinguishable, which is a contradiction. Hence c > 0 must hold.
This concludes the proof.

min

′ ,S ′ )∈2S1 ×2S2 ψ ∈Distr (S ′ ) ψ ∈Distr (S ′ )
reachable (S1
1
2
1
2
2

max

U ⊆Σm

pr 1 (ψ1 , U ) − pr 2 (ψ2 , U )



Proof. The reachable pairs (S1′ , S2′ ) ∈ 2S1 × 2S2 can be
computed in exponential time. So it suffices to show that one
can compute, for a fixed reachable pair (S1′ , S2′ ) ∈ 2S1 × 2S2 ,
the value
cS1′ ,S2′ :=

min

min

′ ) ψ ∈Distr (S ′ )
ψ1 ∈Distr (S1
2
2

max

U ⊆Σm

pr 1 (ψ1 , U ) − pr 2 (ψ2 , U )



in exponential time. Consider the following linear program,
similar to the one from the proof of Proposition 15, with
variables xu for u ∈ Σm and variables encoding distributions
ψ1 , ψ2 :
X
minimize
xu
u∈Σm

subject to: ψ1 ∈ Distr (S1 )
ψ2 ∈ Distr (S2 )

0 ≤ xu
pr 1 (ψ1 , u) − pr 2 (ψ2 , u) ≤ xu

X

(pr 1 (ψ1 , u) − pr 2 (ψ2 , u))

u∈U

= pr 1 (ψ1 , U ) − pr 2 (ψ2 , U )
= cS1′ ,S2′
For the converse, we show that cS1′ ,S2′ is a lower bound to
the value of any feasible solution. Let (xu )u∈Σm along with
ψ1 , ψ2 denote a feasible solution. Let U be a set that attains

the maximum in maxU ⊆Σm pr 1 (ψ1 , U ) − pr 2 (ψ2 , U ) . We
m
can take U = {u ∈ Σ | pr 1 (ψ1 , u) ≥ pr 2 (ψ2 , u)}. Hence
we have:
X
xu
u∈Σm

≥

X

xu

u∈U

X

(xu ≥ 0 from the linear program)
(pr 1 (ψ1 , u) − pr 2 (ψ2 , u))

u∈U

Proposition 17.
Let H1 , H2 be two distinguishable
HMCs. One can compute, in exponential time:
min

u∈U

=

≥

We prove Proposition 17 from the main text.

min

u∈Σm

(15)

and pr 1 (ψ1 , u) = pr 2 (ψ2 , u) holds for all u ∈ Test . By
Proposition 14, the last fact implies

c :=

ψ1 , ψ2 is feasible. Moreover, its value is:
X
X
xu
xu =

(x ≥ pr 1 (ψ1 , u) − pr 2 (ψ2 , u) from the lin. program)
= pr 1 (ψ1 , U ) − pr 2 (ψ2 , U )

= maxm pr 1 (ψ1 , U ) − pr 2 (ψ2 , U )
U ⊆Σ

≥ cS1′ ,S2′


definition of cS1′ ,S2′



We conclude that cS1′ ,S2′ is an optimal solution of the linear
program.

C.

PROOFS OF SECTION 6

We prove Proposition 18 from the main text.
Proposition 18.
Let H be a cHMC with
P(Bad ), P(Good ) > 0. Then one can compute, in polynomial time, HMCs H1 , H2 such that for all measurable events
E ⊆ S ω we have
P1 (E) = P(E | Bad )

and

P2 (E) = P(E | Good ) .

Proof. By symmetry, it suffices to provide the construction for H1 . Let H = (G, O, s0 , Class) be the given cHMC
with G = (S, R, φ) a Markov chain. Define
S1 := {s ∈ S | Ps (Bad s ) > 0} .

for all u ∈ Σm
for all u ∈ Σm

This linear program has exponential size. We show that its
optimal solution is cS1′ ,S2′ .
First we show that it has a feasible solution whose value
is cS1′ ,S2′ . Let ψ1 , ψ2 be the distributions that attain the
minimum from the definition of cS1′ ,S2′ . Let U be a set that
attains the maximum from the definition of cS1′ ,S2′ . We can
take U = {u ∈ Σm | pr 1 (ψ1 , u) ≥ pr 2 (ψ2 , u)}. Let xu =
pr 1 (ψ1 , u) − pr 2 (ψ2 , u) for all u ∈ U , and let xu = 0 for
all u ∈ Σm − U . Then the solution with those xu and with

Note that s0 ∈ S1 . Define G1 := (S1 , R1 , φ1 ) with R1 :=
R ∩ (S1 × S1 ) and
φ1 (s, t) :=

φ(s, t) · Pt (Bad t )
Ps (Bad s )

for all (s, t) ∈ R1 .

Finally, take H1 := (G1 , O1 , s0 ) where O1 equals O restricted to S1 .
We show that the measures P1 (·) and P(· | Bad ) are equal.
By definition, it suffices to show that they are equal on the
cylinder sets {s0 r}S1ω for all r ∈ S1∗ . We show by induction
on the length of r that
P1,s ({sr}S1ω ) · Ps (Bad s ) = Ps ({sr}S1ω ∩ Bad s )

∀ s ∈ S1 .

For the induction base, let r be empty. Then the claim
follows from P1,s ({s}S1ω ) = 1 and Bad s ⊆ {s}S1ω . For the
induction step, let t ∈ S1 and r ∈ S1∗ . We want to show:
P1,s ({str}S1ω ) · Ps (Bad s ) = Ps ({str}S1ω ∩ Bad s )

(18)

If (s, t) 6∈ R1 then both sides of (18) are zero. So let (s, t) ∈
R1 . Then we have:
P1,s ({str}S1ω ) · Ps (Bad s )
= φ1 (s, t) · P1,t ({tr}S1ω ) · Ps (Bad s )
=

φ(s, t) · Pt (Bad t )
· P1,t ({tr}S1ω ) · Ps (Bad s )
Ps (Bad s )

= φ(s, t) · Pt ({tr}S1ω ∩ Bad t )
= Ps ({str}S1ω ∩ Bad s )

by the ind. hyp.

This shows (18) and hence the proposition.

