Skip to content
DeepForgeby svx
All problems
MediumTime Seriests-198

Spectral Entropy

Problem

Compute the normalized spectral entropy of a series. Take the periodogram powers at bins k = 0..n//2, normalize them into a distribution, compute the Shannon entropy in nats, and divide by log(number of bins).

Return 0.0 when there are fewer than two points or total power is zero. A pure tone gives near 0; white noise gives near 1.

Hint

A flat spectrum maximizes entropy; a single spike minimizes it.

Test cases

case 1
in: [[1,2,3,4]]
expected: 0.37201270119579155
case 2
in: [[5,5,5,5]]
expected: 3.8340693177808697e-31
case 3
in: [[0,1,0,1]]
expected: 0.6309297535714574
case 4
in: [[1,-1,1,-1]]
expected: 1.2859084648443834e-31
Starter codePython
import math
def spectral_entropy(series):
    # Your code here
    pass

4 test cases · run your code against them in the editor