All problems
Surprisal of a Sequence
Problem
Compute the total surprisal in bits of a sequence of event probabilities. The surprisal of a single event is -log2(p), and zero-probability events contribute nothing to the total. Return 0.0 for an empty list, and assume each probability lies in [0, 1].
Hint
Add up the surprisal of every event, skipping zeros.
Test cases
case 1
in: [[0.5,0.25,0.125]]
expected: 6
case 2
in: [[1]]
expected: 0
case 3
in: [[0.5,0.5]]
expected: 2
case 4
in: [[0,1]]
expected: 0
case 5
in: [[]]
expected: 0
Starter codePython
import math
def sequence_surprisal(probs):
# Your code here
pass5 test cases · run your code against them in the editor