← All algorithms

Reinforcement Learning

RL: When MDP is unknown - Prediction

2023-12-28

If the reward function R(s,a) and the transition probability P(s'|s,a) are known, MDP is considered to be known. In other words, when taking a certain action a at the state s, we will know exactly what reward will be received and how the next state s' will transition with what distribution. What if MDP is not known or it is impossible to express it as a transition probability distribution, how can we do the value prediction and the action control? The answer is that in reality, we cannot know until we try actual trial and error actions and see the results. In cases without a model (i.e. model-free), eventually, after trying enough (i.e., accumulating enough experience with the environment) and averaging the profits, it can be expected that the result will be equal to the true profit. This is called the law of large numbers.

To get an idea of the role of learning rate α, looking at the formula for gradually obtaining the average, if we denote the average of n data as Xn, when a new data x is added, the new average Xn+1 can be expressed as the sum of the current average value plus the difference between the new data and the average value multiplied by the learning rate α which is the function of sample size. In other words, we don't need to keep track of all the values of the samples but the average and the sample size.

\(X_{n+1}=\dfrac{nX_n+x}{n+1}=\dfrac{(n+1)X_n+x-X_n}{n+1}\)

\(=X_n+\dfrac{1}{n+1}(x-X_n)\equiv X_n+\alpha (x-X_n)\)

Too big α can cause the convergence problem due to the oscillation, but too small α can cause a slow convergence.

(1) Monte Carlo learning, MC

The way to gain experience in MC is to start with an arbitrary value function and improve the state value after a full episode ends. For example, if an episode is structured from start to finish as follows,

\(s_0,\ a_0,\ r_0,\ s_1,\ a_1,\ r_1,\ s_2,\ a_2,\ r_2,\dots ,\ s_{T-1},\ a_{T-1},\ r_{T-1},\ s_T\)

The expected return is the sum of discounted rewards, so the one for each state is as follows.

\(G_0=r_0+\gamma r_1+\gamma ^2r_2+\gamma ^3r_3+\dots +\gamma ^{T-1}r_{T-1}=r_0+\gamma G_1\)

\(G_1=r_1+\gamma r_2+\gamma ^2r_3+\dots +\gamma ^{T-2}r_{T-1}=r_1+\gamma G_2\)

\(G_2=r_2+\gamma r_3+\gamma ^2r_4+\dots +\gamma ^{T-3}r_{T-1}=r_2+\gamma G_3\)

\(\dots \)

\(G_{T-1}=r_{T-1}=r_{T-1}+\gamma G_T\)

\(G_T=0\)

In reinforcement learning, if N(s) is the number of visits to state s and V(s) is the cumulative reward for visiting state s up to time t, through numerous episodes, the average reward becomes the state value function.

\(v_{\pi }(s)=\dfrac{V(s)}{N(s)}\)

When there is a new visit that results in a profit Gt in state s due to action a at this time, vπ(s) is updated as follows.

\(v_{\pi }(s)=\dfrac{V(s)+G_t}{N(s)+1}=\dfrac{N(s)v_{\pi }(s)}{N(s)+1}+\dfrac{G_t}{N(s)+1}\)

\(=\dfrac{(N(s)+1-1)v_{\pi }(s)}{N(s)+1}+\dfrac{G_t}{N(s)+1}=v_{\pi }(s)+\dfrac{1}{N(s)+1}(G_t-v_{\pi }(s))\)

\(=v_{\pi }(s)+\alpha (G_t-v_{\pi }(s))\)

Here, α = 1/(N(s)+1) means the learning rate, having a value between 0 and 1. The above expression is about improving the prediction of the value function towards Gt from the current value. Ultimately, after going through countless episodes (visits), it satisfies the following equation.

\(v_{\pi }(s)=E_{\pi }[G_t]\)

The steps to predict the state value vπ(s) through Monte Carlo (MC) are as follows:

(step 1) initialize vπ(s)

(step 2) Create episodes of actions that connect the starting point to the endpoint following the given policy.

(step 3) Starting from the endpoint of each episode, calculate Gt backwards until the starting point, and improve the state value function through the above expression.

(step 4) Using the improved value function from the previous episode as the initial value, repeat steps (2) and (3) for the next episode, and continue the process indefinitely until the state value function converges.


To implement MC method in Excel, we take the same 4x4 Grid World problem used in the blog "MDP is known".

Set the discount rate γ =1, the learning rate α =0.001, and the reward at the all environments R(s, a)=-1 except the end.

(step 1) Initialize the value function to 0 for states 0 to 14 in the first episode. The episode always starts at state 0.

(step 2) Take an action in random direction. RANDBETWEEN(1,4)

(step 3) Determine the next state, st+1, based on the action.

For the current state st=s and action at, update st+1 by the rule below.

(step 4) Calculate the return Gt .

​Since the expected return at the terminal state (s=15) is zero, the Gt becomes,

IF(st+1 = 15, Gt=0, Gt= Rt+1+γ Gt+1).

(step 5) Update the state value function vπ(s) at s using the Gt

\(v_{\pi }(s)=v_{\pi }(s)+\alpha (G_t-v_{\pi }(s))\)​

After roughly 1800 of Episodes execution, the converged state value function vπ(s) at each state are shown below.

​Table below shows the state value function v(s) at the grid position and the optimal route selected greedily is highlighted in yellow.

(2) Time Difference Learning, TD

In previous MC learning, it was necessary to wait for the end of an episode to improve the value function. In practical cases where the episode does not end, there are limitations to using MC. In such cases, TD can be used.

TD uses the Bellman 's expectation equation as it is for the expected return,

\(v_{\pi }(s)=E_{\pi }[r_{t+1}+\gamma v_{\pi }(s_{t+1})]\)

expecting that the average of rt+1+γvπ(st+1) is approach to the vπ(s) as the number of Episode is going up. Then, the state value function gets updated by the following equation.

\(v_{\pi }(s)=v_{\pi }(s)+\alpha \lbrace r_{t+1}+\gamma v_{\pi }(s_{t+1})-v_{\pi }(s)\rbrace \),

which means that the update relies on the action taken by the policy.


Following steps explain the way the value function got updated by TD in Excel.

(step 1) Initialize the value function to 0 for states 0 to 14 in the first episode. The episode always starts at state 0.

​(step 2) Take an action in random direction. RANDBETWEEN(1,4)

​(step 3) Determine the next state, st+1, based on the action.

For the current state st=s and action at, update st+1 by the rule below.

(step 4) Calculate TD Target by using the INDEX function.

\(\text{TD Target}=r_{t+1}+\gamma v_{\pi }(s_{t+1})\)

=IF(S6=15,0,-1+Gamma*INDEX($C5:$Q5,S6+1))

where the immediate reward at t+1, rt+1 is 0 at the terminal state, otherwise rt+1=-1 and vπ(st+1) is the current value function at the next state.

(step 5) Update the value function at s by using the TD Target

\(v_{\pi }(s)=v_{\pi }(s)+\alpha \lbrace \text{TD Target}-v_{\pi }(s)\rbrace \)

The state value function after running the 1700 Episodes with the learning rate α =0.005 is shown at the picture below.

Table below shows the state value function v(s) at the grid position and the optimal route selected greedily is highlighted in yellow.

So far, we just updated the TD Target at every step, but it is also possible to update the TD Target after N-step without any limitation. 

\(N=1:\ r_{t+1}+\gamma v_{\pi }(s_{t+1})\)

\(N=2:\ r_{t+1}+\gamma (r_{t+2}+\gamma v_{\pi }(s_{t+2}))=r_{t+1}+\gamma r_{t+2}+\gamma ^2v_{\pi }(s_{t+2})\)

\(\dots \)

\(N=n:\ r_{t+1}+\gamma r_{t+2}+\gamma ^2r_{t+3}+\dots +\gamma ^nv_{\pi }(s_{t+n})\)

\(N=\infty :\ r_{t+1}+\gamma r_{t+2}+\gamma ^2r_{t+3}+\dots +\gamma ^{T-1}r_{T-1}\)

The first case which uses N=1 is called TD(0). The extreme case of TD(N) with N=∞ becomes the MC.