Here's an implementation of a function in MATLAB that calculates the total variation distance of two probability distributions:
main.m396 chars14 lines
The function takes in two probability distributions P
and Q
, and checks that they are valid probability distributions (i.e., they contain only non-negative values and their sum is equal to 1 within a small tolerance eps
).
If the distributions are valid, the function calculates the total variation distance as half the sum of the absolute differences between their corresponding elements. The function returns the total variation distance as TVD
.
gistlibby LogSnag