v9.1.1
Menu

Entropy of a Policy

easyPROPolicy Gradients and Advantage EstimationReinforcement LearningRL 1: Foundations

Problem Statement

Policy entropy encourages exploration and is often added as a bonus. For a policy distribution pi:

H(π)=−∑aπ(a) log⁡π(a)H(\pi) = -\sum_a \pi(a)\, \log \pi(a)

Use natural log. Treat pi(a) = 0 as contributing 0 (since 0 log 0 = 0). Implement policy_entropy(pi) returning a float.

Example

Input
policy_entropy([0.5, 0.5])
Output
0.6931
Reasoning
  • Initialize the entropy accumulator to 00 to sum the contributions of each action in the policy distribution.
  • Process the first probability p=0.5p = 0.5: since it is non-zero, calculate its contribution as −0.5×ln⁡(0.5)≈0.3466-0.5 \times \ln(0.5) \approx 0.3466.
  • Process the second probability p=0.5p = 0.5: similarly, calculate its contribution as −0.5×ln⁡(0.5)≈0.3466-0.5 \times \ln(0.5) \approx 0.3466.
  • Sum these individual contributions to find the total entropy: 0.3466+0.3466=0.69310.3466 + 0.3466 = 0.6931.
  • The final output is 0.6931

Constraints

  • pi is a valid distribution (sums to 1); entries >= 0.
  • Skip zero-probability actions.
  • Return a float.

Learn this in

More from RL 1: Foundations

PRO

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.

solution.py

Test results

Sign in to run your code against the test cases.