Skip to content
DeepForgeby svx
All problems
MediumProbabilitypr-269

Detailed Balance Residual

Problem

Detailed balance requires pi[i] * matrix[i][j] = pi[j] * matrix[j][i] for all i and j. Return the largest absolute violation, max over i and j of abs(pi[i] * matrix[i][j] - pi[j] * matrix[j][i]). Return -1.0 for malformed input.

Hint

Compare every off-diagonal flow in both directions and keep the worst mismatch.

Test cases

case 1
in: [[0.5,0.5],[[0.5,0.5],[0.5,0.5]]]
expected: 0
case 2
in: [[0.5,0.5],[[0.8,0.2],[0.2,0.8]]]
expected: 0
case 3
in: [[0.3,0.7],[[0.5,0.5],[0.5,0.5]]]
expected: 0.19999999999999998
case 4
in: [[0.25,0.75],[[0.9,0.1],[0.3,0.7]]]
expected: 0.19999999999999998
case 5
in: [[0.5,0.5],[[0.5,0.5]]]
expected: -1
Starter codePython
def detailed_balance_residual(pi, matrix):
    # Your code here
    pass

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