PIXELBANKv9.1.0
Menu

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.
πŸ”’

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

0/0
Run code to see test results.
Entropy of a Policy - Easy | PixelBank