← All algorithms

Reinforcement Learning

Basics of Reinforcement Learning - Bellman Equation

2023-12-28

Reinforcement learning, like supervised learning, uses experience. Supervised learning uses data to learn rules for predicting outcomes, whereas reinforcement learning uses trial and error to improve a policy. The objective is to maximize cumulative rewards.

If we use the following figure to provide a rudimentary introduction of Reinforcement learning, it is defined as the process of learning through sequential decision-making for an agent (mouse) in a given environment (maze) to maximize the reward (cheese piece) in the current state (room) by taking actions (moving to another room).

Image Source from The very basics of Reinforcement Learning | by Aneek Das | Becoming Human: Artificial Intelligence Magazine

​In a little more elaborated explanation, the agent is described as making decisions at the current time t based on the state and reward depicted as the prevailing situation St, Rt, to maximize accumulated rewards by taking action At, as a result of which it will be placed in a different situation St+1, Rt+1 at the next step (i.e., time t+1), and this process will be repeated until reaching the final (Terminal) destination ST or obtaining the desired outcome RT. As the outcome of the learning, it predicts the expected returns at each state (Prediction) and also informs about which actions should be taken to generate those expected returns (Control, policy learning). Ultimately, the goal of reinforcement learning is these predictions and action control.

When the above steps are represented sequentially, it is expressed as follows.

\(S_0,\ R_0,\ A_0,\ S_1,\ R_1,\ A_1,\ S_2,\ R_2,\ A_2,\dots ,\ S_T,\ R_T\)

(1) Expected Gain, Gt

The expected gain Gt at time t is the total sum of expected future rewards when viewed from the current state St.

\(G_t=R_{t+1}+\gamma R_{t+2}+\gamma ^2R_{t+3}+\dots =\sum _{k=0}^{\infty }\gamma ^kR_{t+k+1}\)

Here, γ is the discount factor, which takes a value between 0 and 1 as a damping factor to reduce the expected future value from the current state as the distance from the destination increases.

We can derive the following relationship between Gt and Gt+1 using the above equation.

\(G_t=R_{t+1}+\gamma \left(R_{t+2}+\gamma R_{t+3}+\dots \right)=R_{t+1}+\gamma G_{t+1}\)

​The fact that Gt can be expressed as Gt+1 is called the "Markov assumption," and the context is that the current state is determined only by the immediately preceding state in the past. This Markov assumption is applied to the general form of expected return and is applicable to both the state value function and the action value function which will be shown in shortly after.

Depending on how we define the "accumulated rewards" or "expected future gains ", there exist various reinforcement learning algorithm.

When studying reinforcement learning, it is important to focus on understanding the context rather than concentrating on the definitions of the environment, agent, state, reward, and action. Always remember that "expected gain" should be the focus of all behavior and decision-making. For example, when playing poker, we always make a decision on action each moment to win the game at the ends. Remembering this help to understand many of the reinforcement learning.  

This repeated situation of state-action-reward can be well understood by representing it in a backup diagram.

​(2) State Value Function

​

We define the state value function from the backup diagram. If a probability so-called policy function π(a|s) for an agent to take an action At=a at any state St=s is known as below:

\(\pi (a|s)=\text{Probability}[A_t=a|S_t=s]\)

The expected gain, vπ(s), at the state, s, can be expressed as the total sum of the probability, π, of which the action, a, is chosen multiplied by the expected reward qπ(s,a). Since it is assumed that there are only two actions a1, and a2 in the diagram.

\(v_{\pi }(s)=\pi (a_1|s)q_{\pi }(s,a_1)+\pi (a_2|s)q_{\pi }(s,a_2)=\sum _a^{\ }\pi (a|s)q_{\pi }(s,a)\)

The vπ(s) is called "the state value function", where v represents the value and is only function of the state, s. In other words, it becomes the expected gain at the state, s.

\(v_{\pi }(s)=E[G_t|S_t=s]\)

(3) Action value function

The next backup diagram explains the expected gain from the state transition due to the action taken. If the probability P(s'|s,a) of the transition at t by selecting the action At=a, from the state, St=s, to the next state, s', is known, i.e.

\(P(s'|s,a)=\text{Probability}[S_{t+1}=s'|S_t=s,A_t=a]\)

The expected gain qπ(s,a) at the state, s, due to the action, a, is the sum of the immediate reward function, r|s,a, plus the probability of the transition to St+1=s' multiplied by the expected gain at s', vπ(s') with the discounted rate γ.

When there are only two possible next states, s1' and s2', due to the action, a, the expected gain due the action becomes:

\(q_{\pi }(s,a)=r_{|s,a}+P(s_1'|s,a)\gamma v_{\pi }(s_1')+P(s_2'|s,a)\gamma v_{\pi }(s_2')=r|s,a+\gamma \sum _{s'}^{\ }P(s'|s,a)v_{\pi }(s')\)

The qπ(s,a) is called "the action value function" and it defines the expected gain at state, s, due to the action, a.

​\(q_{\pi }(s,a)=E[G_t|S_t=s,A_t=a]\)​

(4) Bellman's expectation equation

As shown in the backup diagrams, the state value function, vπ(s), and the action value function, qπ(s,a), get woven one after the other with the discount rate. In the end, the next equation called "Bellman expectation equation" can be derived from (1), (2), and (3).

\(v_{\pi }(s)=E[r'+\gamma v_{\pi }(s')]=\sum _a^{\ }\pi (a|s)q_{\pi }(s,a)=\sum _a^{\ }\pi (a|s)\lbrace r_{|s,a}+\gamma \sum _{s'}^{\ }P(s'|s,a)v_{\pi }(s')\rbrace \)

\(q_{\pi }(s,a)=E[r'+\gamma q_{\pi }(s',a')]=r_{|s,a}+\gamma \sum _{s'}^{\ }P(s'|s,a)\sum _{a'}^{\ }\pi (a'|s')q_{\pi }(s',a')\)

Here, we assumed that the immediate reward function, r|s,a, and the state transition probability, P(s'|s,a), are known, and we call this state of knowing, in other words, as "Markov Decision Process (MDP)" is known.

\(MDP=(S,A,R,P,\gamma )\)

This type of the reinforcement learning with the known MDP state is called "Model Based Planning".

​(5) Bellman's optimality equation

By using the Bellman expectation equation, we can calculate the expected gain for the given the policy function π(a|s) to choose an action. Meanwhile, the decision making is required to choose the optimal value and the optimal policy to maximize the expected gain. How to achieve it? As a matter of fact, it can be easily done by choosing the policy to take the action, a, at the state, s, to produce the highest return. In other words,

\(v_{\pi }(s)=\sum _a^{\ }\pi (a|s)q_{\pi }(s,a)\rightarrow v_*(s)=\max _a^{ }q_*(s,a)\).

\(q_{\pi }(s,a)=r_{|s,a}+\gamma _{s'}P(s'|s,a)v_{\pi }(s')\rightarrow q_*(s,a)=r_{|s,a}+\gamma _{s'}P(s'|s,a)v_*(s')\)

In the end, the Bellman optimality equation becomes: 

\(v_*(s)=\max _a^{ }\lbrace r_{|s,a}+\gamma \sum _{s'}^{\ }P(s'|s,a)v_*(s')\rbrace \)

\(q_*(s,a)=r_{|s,a}+\gamma \sum _{s'}^{\ }P(s'|s,a)\max _a^{ }q_*(s,a)\)

The decision making to choose the optimal action by using the Bellman equations is called "Dynamic Programming".