Learning Unknown Markov Decision Processes: A Thompson Sampling Approach

We consider the problem of learning an unknown Markov Decision Process (MDP) that is weakly communicating in the infinite horizon setting. We propose a Thompson Sampling-based reinforcement learning algorithm with dynamic episodes (TSDE). At the beginning of each episode, the algorithm generates a sample from the posterior distribution over the unknown model parameters. It then follows the optimal stationary policy for the sampled model for the rest of the episode. The duration of each episode is dynamically determined by two stopping criteria. The first stopping criterion controls the growth rate of episode length. The second stopping criterion happens when the number of visits to any state-action pair is doubled. We establish $\tilde O(HS\sqrt{AT})$ bounds on expected regret under a Bayesian setting, where $S$ and $A$ are the sizes of the state and action spaces, $T$ is time, and $H$ is the bound of the span. This regret bound matches the best available bound for weakly communicating MDPs. Numerical results show it to perform better than existing algorithms for infinite horizon MDPs.


Introduction
We consider the problem of reinforcement learning by an agent interacting with an environment while trying to minimize the total cost accumulated over time. The environment is modeled by an infinite horizon Markov Decision Process (MDP) with finite state and action spaces. When the environment is perfectly known, the agent can determine optimal actions by solving a dynamic program for the MDP [1]. In reinforcement learning, however, the agent is uncertain about the true dynamics of the MDP. A naive approach to an unknown model is the certainty equivalence principle. The idea is to estimate the unknown MDP parameters from available information and then choose actions as if the estimates are the true parameters. But it is well-known in adaptive control theory that the certainty equivalence principle may lead to suboptimal performance due to the lack of exploration [2]. This issue actually comes from the fundamental exploitation-exploration trade-off: the agent wants to exploit available information to minimize cost, but it also needs to explore the environment to learn system dynamics.
One common way to handle the exploitation-exploration trade-off is to use the optimism in the face of uncertainty (OFU) principle [3]. Under this principle, the agent constructs confidence sets for the system parameters at each time, find the optimistic parameters that are associated with the minimum cost, and then selects an action based on the optimistic parameters. The optimism procedure encourages exploration for rarely visited states and actions. Several optimistic algorithms are proved to possess strong theoretical performance guarantees [4][5][6][7][8][9][10].
An alternative way to incentivize exploration is the Thompson Sampling (TS) or Posterior Sampling method. The idea of TS was first proposed by Thompson in [11] for stochastic bandit problems. It has been applied to MDP environments [12][13][14][15][16][17] where the agent computes the posterior distribution of unknown parameters using observed information and a prior distribution. A TS algorithm generally proceeds in episodes: at the beginning of each episode a set of MDP parameters is randomly sampled from the posterior distribution, then actions are selected based on the sampled model during the episode. TS algorithms have the following advantages over optimistic algorithms. First, TS algorithms can easily incorporate problem structures through the prior distribution. Second, they are more computationally efficient since a TS algorithm only needs to solve the sampled MDP, while an optimistic algorithm requires solving all MDPs that lie within the confident sets. Third, empirical studies suggest that TS algorithms outperform optimistic algorithms in bandit problems [18,19] as well as in MDP environments [13,16,17].
Due to the above advantages, we focus on TS algorithms for the MDP learning problem. The main challenge in the design of a TS algorithm is the lengths of the episodes. For finite horizon MDPs under the episodic setting, the length of each episode can be set as the time horizon [13]. When there exists a recurrent state under any stationary policy, the TS algorithm of [15] starts a new episode whenever the system enters the recurrent state. However, the above methods to end an episode can not be applied to MDPs without the special features. The work of [16] proposed a dynamic episode schedule based on the doubling trick used in [7], but a mistake in their proof of regret bound was pointed out by [20]. In view of the mistake in [16], there is no TS algorithm with strong performance guarantees for general MDPs to the best of our knowledge.
We consider the most general subclass of weakly communicating MDPs in which meaningful finite time regret guarantees can be analyzed. We propose the Thompson Sampling with Dynamic Episodes (TSDE) learning algorithm. In TSDE, there are two stopping criteria for an episode to end. The first stopping criterion controls the growth rate of episode length. The second stopping criterion is the doubling trick similar to the one in [7][8][9][10]16] that stops when the number of visits to any state-action pair is doubled. Under a Bayesian framework, we show that the expected regret of TSDE accumulated up to time T is bounded byÕ(HS √ AT ) whereÕ hides logarithmic factors. Here S and A are the sizes of the state and action spaces, T is time, and H is the bound of the span. This regret bound matches the best available bound for weakly communicating MDPs [7], and it matches the theoretical lower bound in order of T except for logarithmic factors. We present numerical results that show that TSDE actually outperforms current algorithms with known regret bounds that have the same order in T for a benchmark MDP problem as well as randomly generated MDPs.

Preliminaries
An infinite horizon Markov Decision Process (MDP) is described by (S, A, c, θ). Here S is the state space, A is the action space, c : S × A → [0, 1] 1 is the cost function, and θ : S 2 × A → [0, 1] represents the transition probabilities such that θ(s ′ |s, a) = P(s t+1 = s ′ |s t = s, a t = a) where s t ∈ S and a t ∈ A are the state and the action at t = 1, 2, 3 . . . . We assume that S and A are finite spaces with sizes S ≥ 2 and A ≥ 2, and the initial state s 1 is a known and fixed state. A stationary policy is a deterministic map π : S → A that maps a state to an action. The average cost per stage of a stationary policy is defined as Here we use J π (θ) to explicitly show the dependency of the average cost on θ. From MDP theory [1], we know that if the MDP is weakly communicating, the optimal average cost per stage J(θ) = min π J π (θ) satisfies the Bellman equation for all s ∈ S. The corresponding optimal stationary policy π * is the minimizer of the above optimization given by a = π * (s, θ).
Since the cost function c(s, a) ∈ [0, 1], J(θ) ∈ [0, 1] for all θ. If v satisfies the Bellman equation, v plus any constant also satisfies the Bellman equation. Without loss of generality, let min s∈S v(s, θ) = 0 and define the span of the MDP as sp(θ) = max s∈S v(s, θ). 2 We define Ω * to be the set of all θ such that the MDP with transition probabilities θ is weakly communicating, and there exists a number H such that sp(θ) ≤ H. We will focus on MDPs with transition probabilities in the set Ω * .

Reinforcement Learning for Weakly Communicating MDPs
We consider the reinforcement learning problem of an agent interacting with a random weakly communicating MDP (S, A, c, θ * ). We assume that S, A and the cost function c are completely known to the agent. The actual transition probabilities θ * is randomly generated at the beginning before the MDP interacts with the agent. The value of θ * is then fixed but unknown to the agent. The complete knowledge of the cost is typical as in [7,15]. Algorithms can generally be extended to the unknown costs/rewards case at the expense of some constant factor for the regret bound.
At each time t, the agent selects an action according to is the history of states and actions. The is called a learning algorithm. The functions φ t allow for the possibility of randomization over actions at each time.
We focus on a Bayesian framework for the unknown parameter θ * . Let µ 1 be the prior distribution for θ * , i.e., for any set Θ, P(θ * ∈ Θ) = µ 1 (Θ). We make the following assumptions on µ 1 . Assumption 1. The support of the prior distribution µ 1 is a subset of Ω * . That is, the MDP is weakly communicating and sp(θ * ) ≤ H.
In this Bayesian framework, we define the expected regret (also called Bayesian regret or Bayes risk) of a learning algorithm φ up to time T as where s t , a t , t = 1, . . . , T are generated by φ and J(θ * ) is the optimal per stage cost of the MDP. The above expectation is with respect to the prior distribution µ 1 for θ * , the randomness in state transitions, and the randomized algorithm. The expected regret is an important metric to quantify the performance of a learning algorithm.

Thompson Sampling with Dynamic Episodes
In this section, we propose the Thompson Sampling with Dynamic Episodes (TSDE) learning algorithm. The input of TSDE is the prior distribution µ 1 . At each time t, given the history h t , the agent can compute the posterior distribution µ t given by µ t (Θ) = P(θ * ∈ Θ|h t ) for any set Θ. Upon applying the action a t and observing the new state s t+1 , the posterior distribution at t + 1 can be updated according to Bayes' rule as Let N t (s, a) be the number of visits to any state-action pair (s, a) before time t. That is, N t (s, a) = |{τ < t : (s τ , a τ ) = (s, a)}|.
With these notations, TSDE is described as follows.
Algorithm 1 Thompson Sampling with Dynamic Episodes (TSDE) The TSDE algorithm operates in episodes. Let t k be start time of the kth episode and T k = t k+1 −t k be the length of the episode with the convention T 0 = 1. From the description of the algorithm, t 1 = 1 and t k+1 , k ≥ 1, is given by At the beginning of episode k, a parameter θ k is sampled from the posterior distribution µ t k . During each episode k, actions are generated from the optimal stationary policy π k for the sampled parameter θ k . One important feature of TSDE is that its episode lengths are not fixed. The length T k of each episode is dynamically determined according to two stopping criteria: (i) t > t k + T k−1 , and (ii) N t (s, a) > 2N t k (s, a) for some state-action pair (s, a). The first stopping criterion provides that the episode length grows at a linear rate without triggering the second criterion. The second stopping criterion ensures that the number of visits to any state-action pair (s, a) during an episode should not be more than the number visits to the pair before this episode. Remark 1. Note that TSDE only requires the knowledge of S, A, c, and the prior distribution µ 1 . TSDE can operate without the knowledge of time horizon T , the bound H on span used in [7], and any knowledge about the actual θ * such as the recurrent state needed in [15].

Main Result
Theorem 1. Under Assumption 1, The proof of Theorem 1 appears in Section 4. Remark 2. Note that our regret bound has the same order in H, S, A and T as the optimistic algorithm in [7] which is the best available bound for weakly communicating MDPs. Moreover, the bound does not depend on the prior distribution or other problem-dependent parameters such as the recurrent time of the optimal policy used in the regret bound of [15].

Approximation Error
At the beginning of each episode, TSDE computes the optimal stationary policy π k for the parameter θ k . This step requires the solution to a fixed finite MDP. Policy iteration or value iteration can be used to solve the sampled MDP, but the resulting stationary policy may be only approximately optimal in practice. We call π an ǫ−approximate policy if c(s, π(s)) + When the algorithm returns an ǫ k −approximate policyπ k instead of the optimal stationary policy π k at episode k, we have the following regret bound in the presence of such approximation error. Theorem 2. If TSDE computes an ǫ k −approximate policyπ k instead of the optimal stationary policy π k at each episode k, the expected regret of TSDE satisfies Theorem 2 shows that the approximation error in the computation of optimal stationary policy is only additive to the regret under TSDE. The regret bound would remainÕ(HS √ AT ) if the approximation error is such that ǫ k ≤ 1 k+1 . The proof of Theorem 2 is in the appendix due to the lack of space.

Number of Episodes
To analyze the performance of TSDE over T time steps, define K T = arg max {k : t k ≤ T } be the number of episodes of TSDE until time T . Note that K T is a random variable because the number of visits N t (x, u) depends on the dynamical state trajectory. In the analysis for time T we use the convention that t (KT +1) = T + 1. We provide an upper bound on K T as follows. Lemma 1.
Proof. Define macro episodes with start times t ni , i = 1, 2, . . . where t n1 = t 1 and The idea is that each macro episode starts when the second stopping criterion happens. Let M be the number of macro episodes until time T and define n (M+1) = K T + 1.
T k be the length of the ith macro episode. By the definition of macro episodes, any episode except the last one in a macro episode must be triggered by the first stopping criterion. Therefore, within the ith macro episode, T k = T k−1 + 1 for all k = n i , n i + 1, . . . , n i+1 − 2. Hence, Consequently, n i+1 − n i ≤ 2T i for all i = 1, . . . , M . From this property we obtain Using (7) and the fact that where the second inequality is Cauchy-Schwarz.
From Lemma 6 in the appendix, the number of macro episodes M ≤ SA log(T ). Substituting this bound into (8) we obtain the result of this lemma.
Remark 3. TSDE computes the optimal stationary policy of a finite MDP at each episode. Lemma 1 ensures that such computation only needs to be done at a sublinear rate of 2SAT log(T ).

Regret Bound
As discussed in [13,20,21], one key property of Thompson/Posterior Sampling algorithms is that for any function f , if θ t is sampled from the posterior distribution at time t. This property leads to regret bounds for algorithms with fixed sampling episodes since the start time t k of each episode is deterministic. However, our TSDE algorithm has dynamic episodes that requires us to have the stopping-time version of the above property.
Lemma 2. Under TSDE, t k is a stopping time for any episode k. Then for any measurable function f and any σ(h t k )−measurable random variable X, we have Proof. From the definition (6), the start time t k is a stopping-time, i.e. t k is σ(h t k )−measurable.
Note that θ k is randomly sampled from the posterior distribution µ t k . Since t k is a stopping time, t k and µ t k are both measurable with respect to σ(h t k ). From the assumption, X is also measurable with respect to σ(h t k ). Then conditioned on h t k , the only randomness in f (θ k , X) is the random sampling in the algorithm. This gives the following equation: since µ t k is the posterior distribution of θ * given h t k . Now the result follows by taking the expectation of both sides.
For t k ≤ t < t k+1 in episode k, the Bellman equation (1) holds by Assumption 1 for s = s t , θ = θ k and action a t = π k (s t ). Then we obtain Using (10), the expected regret of TSDE is equal to where R 0 , R 1 and R 2 are given by We proceed to derive bounds on R 0 , R 1 and R 2 .
Based on the key property of Lemma 2, we derive an upper bound on R 0 .
Lemma 3. The first term R 0 is bounded as Proof. From monotone convergence theorem we have Note that the first stopping criterion of TSDE ensures that T k ≤ T k−1 + 1 for all k. Because J(θ k ) ≥ 0, each term in the first summation satisfies Note that 1 {t k ≤T } (T k−1 + 1) is measurable with respect to σ(h t k ). Then, Lemma 2 gives Combining the above equations we get Note that the first stopping criterion of TSDE plays a crucial role in the proof of Lemma 3. It allows us to bound the length of an episode using the length of the previous episode which is measurable with respect to the information at the beginning of the episode.
The other two terms R 1 and R 2 of the regret are bounded in the following lemmas. Their proofs follow similar steps to those in [13,16]. The proofs are in the appendix due to the lack of space. Lemma 4. The second term R 1 is bounded as Lemma 5. The third term R 2 is bounded as We are now ready to prove Theorem 1.
Proof of Theorem 1. From (11), where the inequality comes from Lemma 3, Lemma 4. Then the claim of the theorem directly follows from Lemma 1 and Lemma 5.

Simulations
In this section, we compare through simulations the performance of TSDE with three learning algorithms with the same regret order: UCRL2 [8], TSMDP [15], and Lazy PSRL [16]. UCRL2 is an optimistic algorithm with similar regret bounds. TSMDP and Lazy PSRL are TS algorithms for infinite horizon MDPs. TSMDP has the same regret order in T given a recurrent state for resampling. The original regret analysis for Lazy PSRL is incorrect, but the regret bounds are conjectured to be correct [20]. We chose δ = 0.05 for the implementation of UCRL2 and assume an independent Dirichlet prior with parameters [0.1, . . . , 0.1] over the transition probabilities for all TS algorithms.
We consider two environments: randomly generated MDPs and the RiverSwim example [22]. For randomly generated MDPs, we use the independent Dirichlet prior over 6 states and 2 actions but with a fixed cost. We select the resampling state s 0 = 1 for TSMDP here since all states are recurrent under the Dirichlet prior. The RiverSwim example models an agent swimming in a river who can choose to swim either left or right. The MDP consists of six states arranged in a chain with the agent starting in the leftmost state (s = 1). If the agent decides to move left i.e with the river current then he is always successful but if he decides to move right he might fail with some probability. The cost function is given by: c(s, a) = 0.8 if s = 1, a = left; c(s, a) = 0 if s = 6, a = right; and c(s, a) = 1 otherwise. The optimal policy is to swim right to reach the rightmost state which minimizes the cost. For TSMDP in RiverSwim, we consider two versions with s 0 = 1 and with s 0 = 3 for the resampling state. We simulate 500 Monte Carlo runs for both the examples and run for T = 10 5 . From Figure 1(a) we can see that TSDE outperforms all the three algorithms in randomly generated MDPs. In particular, there is a significant gap between the regret of TSDE and that of UCRL2 and TSMDP. The poor performance of UCRL2 assures the motivation to consider TS algorithms. From the specification of TSMDP, its performance heavily hinges on the choice of an appropriate resampling state which is not possible for a general unknown MDP. This is reflected in the randomly generated MDPs experiment.
In the RiverSwim example, Figure 1(b) shows that TSDE significantly outperforms UCRL2, Lazy PSRL, and TSMDP with s 0 = 3. Although TSMDP with s 0 = 1 performs slightly better than TSDE, there is no way to pick this specific s 0 if the MDP is unknown in practice. Since Lazy PSRL is also equipped with the doubling trick criterion, the performance gap between TSDE and Lazy PSRL highlights the importance of the first stopping criterion on the growth rate of episode length. We also like to point out that in this example, the MDP is fixed and is not generated from the Dirichlet prior. Therefore, we conjecture that TSDE also has the same regret bounds under a non-Bayesian setting.

Conclusion
We propose the Thompson Sampling with Dynamic Episodes (TSDE) learning algorithm and es-tablishÕ(HS √ AT ) bounds on expected regret for the general subclass of weakly communicating MDPs. Our result fills a gap in the theoretical analysis of Thompson Sampling for MDPs. Numerical results validate that the TSDE algorithm outperforms other learning algorithms for infinite horizon MDPs.
The TSDE algorithm determines the end of an episode by two stopping criteria. The second criterion comes from the doubling trick used in many reinforcement learning algorithms. But the first criterion on the linear growth rate of episode length seems to be a new idea for episodic learning algorithms. The stopping criterion is crucial in the proof of regret bound (Lemma 3). The simulation results of TSDE versus Lazy PSRL further shows that this criterion is not only a technical constraint for proofs, it indeed helps balance exploitation and exploration. Proof. Since the second stopping criterion is triggered whenever the number of visits to a stateaction pair is doubled, the start times of macro episodes can be expressed as Since the number of visits to (s, a) is doubled at every t k such that k ∈ M (s,a) , the size of M (s,a) should not be larger than O(log(T )). This argument is made rigorous as follows. If where the first inequality is the union bound and the third inequality holds because log is concave.

B Proof of Lemma 4
Proof. We have where the last equality holds because 0 ≤ v(s, θ) ≤ sp(θ) ≤ H for all s, θ from Assumption 1.

C Proof of Lemma 5
Proof. For notational simplicity, we use z = (s, a) ∈ S × A and z t = (s t , a t ) to denote the corresponding state-action pair. Then Since 0 ≤ v(s ′ , θ t ) ≤ H from Assumption 1, each term in the inner summation is bounded by Nt k (zt) is the empirical mean for the transition probability at the beginning of episode k where N t k (s t , a t , s ′ ) = |{τ < t k : (s τ , a τ , s τ +1 ) = (s t , a t , s ′ )}|.

Define confidence set
where β k (z) = 14S log(2At k T ) max(1,Nt k (z)) . Note that β k (z) is the confidence set used in [8] with δ = 1/T . Then we have For the first term in (14) we have .
for all t in the kth episodes from the second criterion. So we get 28S log(2AT 2 ) max(1, N t (z t )) ≤ 56S log(AT ) T t=1 1 max(1, N t (z t )) .
Since N t (z t ) is the count of visits to z t , we have Since z N T +1 (z) = T , we have Combining (15)- (16) we get Let's now work on the second term in (14). Since T k ≤ T for all k, we have Since B k is measurable with respect to σ(h t k ), using Lemma 2 we get By the definition of B k in (13), [8, Lemma 17, setting δ = 1/T ] implies that Combining (18), (19) and (20) we obtain The statement of the lemma then follows by substituting (17) and (21) into (14).
Since R 0 + R 1 + R 2 =Õ(HS √ AT ) from the proof of Theorem 1, we obtain the first part of the result.
where the last inequality follows from Lemma 1.