Policy Gradient Theorem
In this article, we will discuss the methods known as Policy Gradient Methods and their important theoretical result, the Policy Gradient Theorem. This article is related to Reinforcement Learning. For a quick introduction to the topic, we recommend reading the following
Several classical Reinforcement Learning methods are based on learning the action-value function and deriving the policy from it. That is, without this function, the policy cannot be found. However, policy gradient methods take a different approach: instead of computing and then estimating the policy, we directly parameterize the policy as . In this way, the problem transforms into learning the parameters . Nevertheless, the value function can still be used to improve the parameterized policy, as seen in actor-critic methods.
These methods aim to estimate the parameters to increase the value of , where:
Here, represents the trajectories generated by the policy. Thus, the goal is to update the parameters in such a way that we maximize . To achieve this, we perform gradient ascent:
All methods that follow this general scheme are called policy gradient methods, regardless of whether they require learning an approximation of the value function or not.
Advantages
Using these methods presents several advantages. First, their representation is highly versatile, as long as is differentiable. Additionally, they integrate easily into continuous spaces, unlike other methods that require discretization. They also have the ability to approximate a deterministic policy, unlike -greedy methods.
-greedy policies are those that choose a random action with probability instead of selecting the most optimal action according to their current function.
For a deeper understanding of these topics, we recommend reading the book Reinforcement Learning: An Introduction by Sutton & Barto, Part 13.1.
Now, besides their practical advantages, these methods also offer theoretical advantages compared to -greedy policies. The most important difference is that policy gradient methods directly parameterize the policy without needing to estimate another function and then apply an operator like:
The use of this operator can introduce discontinuities in the policy due to the very nature of the operator, as small changes in the function can cause abrupt variations in the policy.
On the other hand, if we parameterize , we can obtain a smoother estimation of the signals received from the environment and generate actions based on a state in a more stable manner. This is because differentiability and, in particular, continuity when parameterizing imply that small changes in the parameters translate into small changes in decision-making, resulting in a more robust and smooth process.
Policy Gradient Theorem
Our problem now is how to estimate the value of our gradient. Fortunately, there is a theoretical result that allows us to compute it. This was proposed by Sutton, and we will now see its derivation following the details in .
We can measure overall performance by assuming it is calculated as the expected value when starting in an initial state of an episode; that is:
where is the true value function of . The difficulty in computing the gradient of with respect to lies in the fact that this value depends on both the actions generated by the policy and the distribution of states in which the agent finds itself, . However, this distribution is determined by the environment and does not directly depend on the policy .
This is where our theorem becomes important. To derive it, we start by computing the gradient of the value function:
We distribute the gradient operator and apply the product rule:
Replacing in the initial equation:
Using the identity
we can rewrite the expression as:
Since does not depend on and using that , we can rewrite it as:
We observe that we have obtained a recursive equation: to compute , we also need the values of for all future states . To continue with the derivation, we introduce the following notation.
We define the probability that, starting from a state , we reach a state after steps following the policy . We can construct this probability recursively:
-
For :
-
For :
-
For :
This expression considers all the ways we can reach from in steps, using the information from the previous step.
Now we will see how this probability allows us to continue with the derivation of our initial equation.
Let’s reduce the expression into its recursive and non-recursive components:
Let’s denote . Then, our expression rewrites as:
From the above, we obtain:
Since we can express recursively as:
substituting it into the previous equation, we get:
Since , we can write:
Repeating the same process for any initial state , we obtain:
This allows us to compute the gradient of the value function without needing to compute the gradients of .
Application in the Objective Function
If we take the objective function as , where is the random initial state, we have:
Let’s define . Then:
Since is a constant, we can write:
Defining the stationary distribution as:
The stationary distribution represents the probability of being in a given state after an infinite number of steps in a Markov chain under policy . Formally, it is defined as:
Thus, we finally obtain:
Rewriting the expression:
Using the identity , we obtain:
where denotes the expectation over the distribution .
This formulation allows computing the gradient of the objective function without explicitly calculating the gradients of , which is useful since this function is usually unknown and depends more on the environment. In contrast, the policy is fully parameterizable.
Derivations of
As seen in various works , this is not the only way to compute . In fact, it can be generally expressed as:
Next, we will analyze some examples of functions used in relevant research and the theoretical conditions that the function should satisfy. Additionally, we will compare these functions by evaluating their bias and variance.
Bias and Variance in
In practice, access to the true gradient is not available. Instead, estimates based on samples, denoted as , are used. The idea is that these estimates should, on average, be similar to to ensure effective parameter updates.
The function plays a fundamental role in this process. As mentioned earlier, the objective function represents the expected value of the rewards from trajectories generated by the policy . Therefore, to maintain this relationship, must be unbiased, thus ensuring a correct estimation of the objective function.
Mathematically, this is expressed as:
That is, on average, both values must be equal, guaranteeing that the estimation of the objective function remains consistent.
However, the choice of also influences another important characteristic: variance. Variance measures the dispersion in gradient estimates due to the inherent randomness of the environment, the policy, or the samples used. High variance implies that gradient updates may fluctuate significantly from one iteration to another, making learning unstable or requiring more samples to converge.
Below, we present some examples of functions along with an analysis of their variance and bias.
Examples of Functions
In the context of Reinforcement Learning, given a trajectory , the accumulated reward is defined as:
In the work of R. J. Williams , the gradient of the objective function is estimated as:
This function is valid because it satisfies the condition that . Below is the proof:
The total reward includes all rewards from to . However, when conditioning on , the rewards prior to —that is, —are constants, since and are fixed. Therefore, the expectation of given depends only on future rewards:
However, in the policy gradient, the term does not depend on past rewards. Therefore, when computing the expectation of the gradient:
Since past rewards are independent of , they can be canceled in the expectation. Finally, we obtain:
Thus, we conclude that:
Variance Analysis
The variance when is high. This is because depends on a single trajectory, which can vary significantly due to the randomness of the environment and policy decisions. In stochastic environments or those with long horizons, two trajectories starting from the same initial state may have very different returns. This large fluctuation results in high variance, slowing down learning and making it less stable, as gradient updates can oscillate significantly between iterations.
This estimation appears in the classical work on Actor-Critic by Barto, Sutton, and Anderson (1983). Here, the original formulation of the gradient of is used:
The proof is trivial, so we will not include it. However, let's move on to the variance analysis.
Variance Analysis
The variance in the gradient estimator when using is moderate, as it smooths out fluctuations present in individual returns by taking an expectation over multiple trajectories. Compared to using directly, where variance is high due to total dependence on observed rewards in each trajectory, provides a more stable estimate by considering the expectation over all possible sequences of actions and states.
However, despite this reduction, variance is not completely low. The estimation of is still affected by the inherent randomness in action selection and environment dynamics, introducing some fluctuation in gradient updates. This may require more samples to stabilize training compared to methods that use the advantage function . Nevertheless, its use in Actor-Critic methods has proven to be a viable alternative, balancing variance reduction and bias in policy gradient estimation.
This estimation appears in the work of Schulman et al. (2016) . This function is defined as:
The advantage function measures how much better taking action in state is compared to the average value of the state . Since does not depend on action , subtracting it does not affect the gradient direction, and the estimate remains unbiased. Now, let's see the proof that validates this property.
Consider the policy gradient estimator when using the advantage function:
Substituting the definition of the advantage function, we obtain:
Splitting the expectation into two terms:
Since is independent of action , we can write term (II) as:
Here, we use the score function property, which states that:
Thus, term (II) cancels out:
Consequently, the estimator remains:
This shows that using the advantage function eliminates the term without introducing bias in the gradient.
Conclusions
In this article, we have discussed the general methodology of policy gradient-based methods, their advantages over other approaches, and the theoretical result that supports them: the Policy Gradient Theorem. Finally, we provided its proof and explored different functions within this theoretical framework.
In future articles, we will examine various algorithms based on these methods in more detail. For a more comprehensive reference, we recommend reading the following article by OpenAI researcher Lilian Weng
References
- Lilian Weng. "A (Long) Peek into Reinforcement Learning".
- R. S. Sutton and A. G. Barto. "Reinforcement Learning: An Introduction". MIT Press, 2018.
- J. Schulman, P. Moritz, S. Levine, M. I. Jordan, and P. Abbeel. "High-dimensional continuous control using generalized advantage estimation". ICLR, 2016.
- R. J. Williams. "Simple statistical gradient-following algorithms for connectionist reinforcement learning". Machine Learning, 1992.
- A. G. Barto, R. S. Sutton, and C. W. Anderson. "Neuronlike adaptive elements that can solve difficult learning control problems". IEEE Transactions on Systems, Man, and Cybernetics, 1983.
- Lilian Weng. "Policy Gradient Algorithms".