Implementing Propaganda of Faith

I am trying to implement Bayesian networks.

My main graph is a factor graph that I want to use to spread faith. But when spreading the faith in calculating messages, not all arguments are passed to the function, and the final function will be a limitation of the joint distribution.

The best way that comes to my mind is to restrict functions so as not to perform the entire replacement every time I want to calculate the marginal number for a new value.

I asked how to implement such a function here .

I want to know if there is a better way to do such a thing or if there are simpler and faster approaches than the one I want to do.

+8
python bayesian-networks belief-propagation
source share
1 answer

Here it is proposed: to create a closure that accepts a map containing the original variables, and their corresponding values ​​as its key-value pairs for the first calculation. The same closure returns an internal function that takes another map with the rest of the variables and values ​​for the final calculation.

So, we define a closure, where the first partial calculation is performed in an external function. Based on your reference, a partial calculation is a sum, but I believe that you will calculate the products of the probabilities. An internal function has access to the partial sum as a free variable. The calculation completes when you call it using a map containing the remaining pairs of value variables.

You can also define a set in the external function to store all the variables used in the first calculation. Then let the inner function also access this set as a free variable. This ensures that the values ​​of all the key variables encountered in the first calculation are excluded in the final calculation.

All of this is illustrated below.

def f1(map1): # set to contain seen variables as keys seen_keys = set() computed_val1 = 0.0 for key in map1.keys(): val = map1[key] computed_val1 += val # remember keys already in 1st computed seen_keys.add(key) def f2(map2): computed_val2 = computed_val1 for key2 in map2.keys(): # omit keys in first computation if key2 in seen_keys: continue val2 = map2[key2] computed_val2 += val2 return computed_val2 return f2 if __name__ == '__main__': partial_map = {'factor1': 1, 'factor2': 2} func = f1(partial_map) remaining_map1 = {'factor3': 3} answer1A = func(remaining_map1) print "Answer after using partial and remaining maps = ", answer1A # complete the partial map with factor3 to show that # the return function will ignore variables already seen in 1st computaion partial_map['factor3'] = 3 answer1B = func(partial_map) print "Answer with completed map to same func = ", answer1B # Compute remaining map with different value for factor 3 remaining_map2 = {'factor3': 15} answer2 = func(remaining_map2) print "Answer with different second map = ", answer2 
+2
source share

All Articles