Entropy of a Distribution

~12 mincode completion

Implement returning in bits as a float, treating zero probabilities as contributing zero.

Examples

A fair coin takes exactly one bit

Input
entropy([0.5, 0.5])
Output
1

A certain outcome carries no uncertainty, and the zero must not become nan

Input
entropy([1, 0])
Output
0

Four equally likely outcomes take two bits

Input
entropy([0.25, 0.25, 0.25, 0.25])
Output
2

Hints

Hint 1

is the natural log, which is what this formula wants.

Hint 2

Watch for this: log2(0) produces nan.

Requirements

  • probs: array of probabilities, shape (n,), summing to 1.

  • Return float: -sum(p log2(p)), with 0log2(0) treated as 0

Constraints

  • Allowed library: NumPy only

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~12 min

8 employers weight this skill

4 quant funds, 2 health and bio companies, 2 big tech firms. Top match scores 92.

Python
import numpy as np


def entropy(probs):
    """
    Shannon entropy in bits.

    Args:
        probs: array of probabilities, shape (n,), summing to 1.
               May contain exact zeros.

    Returns:
        float: -sum(p * log2(p)), with 0*log2(0) treated as 0
    """
    # YOUR CODE HERE
    pass
Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.