Class: BNTPGFromPositiveNegativeRelations

Inherits:
BnTableProbabilitiesGenerator show all
Defined in:
lib/bn4r/bn_table_probabilities.rb

Overview

Bayes Net Table Probabilities Generator from Positive Negative Relations

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BnTableProbabilitiesGenerator

#get_node_probability_from_boolean_combination

Class Method Details

.get_node_probability_from_boolean_combination(boolean_combination, type_of_position_impact) ⇒ Object

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.



56
57
58
59
60
61
62
63
# File 'lib/bn4r/bn_table_probabilities.rb', line 56

def self.get_node_probability_from_boolean_combination(boolean_combination, type_of_position_impact)
  num_eq = 0.0
  boolean_combination.size.times { |i|
    num_eq += 1.0 if type_of_position_impact[i] && boolean_combination[i]
    num_eq += 1.0 if !type_of_position_impact[i] && !boolean_combination[i]
  }
  num_eq / boolean_combination.size.to_f
end

Instance Method Details

#table_probabilities_for_node(node, type_of_position_impact) ⇒ Object

type_of_position_impact is a array of boolean values showing the relation ( positive | negative ) beetwen the node and its parents.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/bn4r/bn_table_probabilities.rb', line 25

def table_probabilities_for_node(node, type_of_position_impact)
  raise "Node parents and type_of_position_impact with different size" if node.parents.size != type_of_position_impact.size
  boolean_combinations = []
  (2**node.parents.size).times { |i|
    boolean_combination = Array.new(node.parents.size, false)
    actual_value = i
    (node.parents.size).times { |j|
      boolean_combination[j] = !(actual_value%2 == 0)
      actual_value = actual_value / 2
    }
    boolean_combinations << boolean_combination
  }
  #p boolean_combinations
  table_probabilities = [] # Array.new(2**(node.parents.size+1))
  boolean_combinations.each { |boolean_combination|
    [true,false].each { |node_value|
      prob = BNTPGFromPositiveNegativeRelations.get_node_probability_from_boolean_combination(boolean_combination, type_of_position_impact)
      prob = 1 - prob if node_value == false
      table_probabilities[node.get_table_index(node_value, boolean_combination)] = prob
      #p "pos :" + node.get_table_index(node_value, boolean_combination).to_s
      #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
    }
  }
 #p table_probabilities
 table_probabilities
end