All problems
Belief Propagation Factor Message
Problem
In the sum-product algorithm, the message from a factor to one of its variables sums the factor table over the other variables. Treat factor as a matrix whose rows index the summed-out variable and whose columns index the receiving variable, and return the column sums as the message vector. Return an empty list when the factor is empty or its rows have inconsistent widths.
Hint
Marginalizing a factor over one variable means summing over that axis.
Test cases
case 1
in: [[[0.1,0.2],[0.3,0.4]]]
expected: [0.4,0.6000000000000001]
case 2
in: [[[1,2,3]]]
expected: [1,2,3]
case 3
in: [[[1],[2]]]
expected: [3]
case 4
in: [[[0.5,0.5],[0.5,0.5],[0.5,0.5]]]
expected: [1.5,1.5]
case 5
in: [[[1,2],[3]]]
expected: []
Starter codePython
def belief_propagation_factor_message(factor):
# Your code here
pass5 test cases · run your code against them in the editor