Skip to content
DeepForgeby svx
All problems
EasyInformation Theoryinfo-017

Total Variation Distance

Problem

Compute the total variation distance between two probability distributions:

TV = 0.5 * sum(|p_i - q_i|)

The result always lies between 0 and 1.

Hint

TV is half the L1 distance because probability mass shifted from one outcome must land somewhere else.

Test cases

case 1
in: [[0.5,0.5],[0.5,0.5]]
expected: 0
case 2
in: [[1,0],[0,1]]
expected: 1
case 3
in: [[0.5,0.5],[0.25,0.75]]
expected: 0.25
case 4
in: [[0.1,0.2,0.7],[0.2,0.2,0.6]]
expected: 0.09999999999999999
Starter codePython
def total_variation(p, q):
    # Your code here
    pass

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