Bayes Net Table Probabilities Generator from Positive Negative Relations
Methods
Public Class methods
returns P(node=yes| parents={boolean_combination}) where parents have relations with the node showed in type_of_position_impact type_of_position_impact is a array of boolean values showing the relation ( positive | negative ) beetwen the node and its parents.
[ show source ]
# File lib/bn4r/bn_table_probabilities.rb, line 56 56: def self.get_node_probability_from_boolean_combination(boolean_combination, type_of_position_impact) 57: num_eq = 0.0 58: boolean_combination.size.times { |i| 59: num_eq += 1.0 if type_of_position_impact[i] && boolean_combination[i] 60: num_eq += 1.0 if !type_of_position_impact[i] && !boolean_combination[i] 61: } 62: num_eq / boolean_combination.size.to_f 63: end
Public Instance methods
type_of_position_impact is a array of boolean values showing the relation ( positive | negative ) beetwen the node and its parents.
[ show source ]
# File lib/bn4r/bn_table_probabilities.rb, line 25 25: def table_probabilities_for_node(node, type_of_position_impact) 26: raise "Node parents and type_of_position_impact with different size" if node.parents.size != type_of_position_impact.size 27: boolean_combinations = [] 28: (2**node.parents.size).times { |i| 29: boolean_combination = Array.new(node.parents.size, false) 30: actual_value = i 31: (node.parents.size).times { |j| 32: boolean_combination[j] = !(actual_value%2 == 0) 33: actual_value = actual_value / 2 34: } 35: boolean_combinations << boolean_combination 36: } 37: #p boolean_combinations 38: table_probabilities = [] # Array.new(2**(node.parents.size+1)) 39: boolean_combinations.each { |boolean_combination| 40: [true,false].each { |node_value| 41: prob = BNTPGFromPositiveNegativeRelations.get_node_probability_from_boolean_combination(boolean_combination, type_of_position_impact) 42: prob = 1 - prob if node_value == false 43: table_probabilities[node.get_table_index(node_value, boolean_combination)] = prob 44: #p "pos :" + node.get_table_index(node_value, boolean_combination).to_s 45: #p "Ok :" + node.get_table_index(node_value, boolean_combination).to_s if node.get_table_index(node_value, boolean_combination) > 2**node.parents.size 46: } 47: } 48: #p table_probabilities 49: table_probabilities 50: end