Entropy of a Policy
Problem Statement
Policy entropy encourages exploration and is often added as a bonus. For a policy distribution pi:
H(Ο)=ββaβΟ(a)logΟ(a)
Use natural log. Treat pi(a) = 0 as contributing 0 (since 0 log 0 = 0). Implement policy_entropy(pi) returning a float.
Example:
policy_entropy([0.5, 0.5])
0.6931
- Initialize the entropy accumulator to 0 to sum the contributions of each action in the policy distribution.
- Process the first probability p=0.5: since it is non-zero, calculate its contribution as β0.5Γln(0.5)β0.3466.
- Process the second probability p=0.5: similarly, calculate its contribution as β0.5Γln(0.5)β0.3466.
- Sum these individual contributions to find the total entropy: 0.3466+0.3466=0.6931.
- The final output is 0.6931
Constraints:
piis a valid distribution (sums to 1); entries>= 0.- Skip zero-probability actions.
- Return a float.
1. Background Knowledge
Entropy measures the uncertainty or "surprise" of a probability distribution. For a discrete policy Ο over a set of actions A, the entropy is defined as:
H(Ο)=βaβAββΟ(a)logΟ(a)A deterministic policy (one action has probability 1, others 0) has zero entropy, meaning no exploration is needed. A uniform policy over n actions has maximum entropy logn, representing maximal uncertainty. In policy gradient methods, adding an entropy bonus to the objective function encourages the agent to keep exploring rather than collapsing to a greedy policy too early.
The term Ο(a)logΟ(a) is well-behaved in the limit: as Ο(a)β0+, the product Ο(a)logΟ(a)β0. This is a standard result from calculus (L'HΓ΄pital's rule or the fact that xlogxβ0 as xβ0+). Therefore, actions with zero probability contribute nothing to the sum, and we should explicitly handle them to avoid computing log(0)=ββ.
In practice, policies are often represented as probability vectors (e.g., from a softmax output). The entropy is computed over this vector using the natural logarithm (ln), not log base 2 or base 10.
2. Algorithm Approach
This is a straightforward element-wise computation over a probability vector:
- Iterate over each action's probability Ο(a).
- If Ο(a)>0, compute Ο(a)β ln(Ο(a)) and accumulate.
- If Ο(a)=0, skip (contribution is 0).
- Return the negative of the accumulated sum.
The key insight is that the formula involves a sum of products, so you need a single pass over the vector with a running total. No sorting, no recursion, no data structures beyond a scalar accumulator.
3. Step-by-Step Strategy
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
Editor locked
The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.