Class: Fathom::BeliefNode

Inherits:
DiscreteNode show all
Defined in:
lib/fathom/node/belief_node.rb

Instance Attribute Summary collapse

Attributes inherited from DiscreteNode

#labels

Attributes inherited from Node

#description, #distribution, #name, #values

Instance Method Summary collapse

Methods inherited from DiscreteNode

#rand, #size

Methods inherited from Node

#children, #name_sym, #parents, #register_child, #register_parent, #simple_inspect

Constructor Details

#initialize(opts = {}) ⇒ BeliefNode

Returns a new instance of BeliefNode.



6
7
8
9
10
11
# File 'lib/fathom/node/belief_node.rb', line 6

def initialize(opts={})
  super(opts)
  assert_probabilities(opts)
  assert_liklihoods(opts)
  @precision_threshold = opts.fetch(:precision_threshold, 0.00001)
end

Instance Attribute Details

#likelihoodsObject (readonly)

Returns the value of attribute likelihoods.



4
5
6
# File 'lib/fathom/node/belief_node.rb', line 4

def likelihoods
  @likelihoods
end

#precision_thresholdObject (readonly)

Returns the value of attribute precision_threshold.



4
5
6
# File 'lib/fathom/node/belief_node.rb', line 4

def precision_threshold
  @precision_threshold
end

#probabilitiesObject (readonly)

Returns the value of attribute probabilities.



4
5
6
# File 'lib/fathom/node/belief_node.rb', line 4

def probabilities
  @probabilities
end

Instance Method Details

#add_accessor_for_cpm(cpm, node) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/fathom/node/belief_node.rb', line 54

def add_accessor_for_cpm(cpm, node)
  return false unless cpm.is_a?(CPMNode) and cpm.name_sym
  method_name = ("cpm_for_" + node.name_sym.to_s).to_sym
  return false if self.respond_to?(method_name)
  (class << self; self; end).module_eval do
    define_method method_name do
      cpm
    end
  end
end

#add_child(child) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/fathom/node/belief_node.rb', line 13

def add_child(child)
  if child.is_a?(BeliefNode)
    cpm = CPMNode.new(:parent => self, :child => child)
    # self.children << cpm
    self.add_accessor_for_cpm(cpm, child)
    self.add_accessor_for_node(child)
    # cpm.register_parent(self)
    self.children << child
    child.register_parent(self)
    child.add_accessor_for_cpm(cpm, self)
  else
    super(child)
  end
end

#add_parent(parent) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/fathom/node/belief_node.rb', line 28

def add_parent(parent)
  if parent.is_a?(BeliefNode)
    cpm = CPMNode.new(:parent => parent, :child => self)
    # self.parents << cpm
    self.add_accessor_for_cpm(cpm, parent)
    self.add_accessor_for_node(parent)
    # cpm.register_child(self)
    self.parents << parent
    parent.register_child(self)
    parent.add_accessor_for_cpm(cpm, self)
  else
    super(parent)
  end
end

#inspectObject



43
44
45
46
47
48
49
50
51
52
# File 'lib/fathom/node/belief_node.rb', line 43

def inspect
  "#{self.class.to_s}: " +  [
    self.name,
    self.description,
    "children:",
    self.children.map {|e| e.is_a?(CPMNode) ? e.child.simple_inspect : e.simple_inspect }.inspect,
    "parents: ",
    self.parents.map {|e| e.is_a?(CPMNode) ? e.parent.simple_inspect : e.simple_inspect }.inspect,
  ].compact.join(", ")
end

#likelihood(label) ⇒ Object



65
66
67
# File 'lib/fathom/node/belief_node.rb', line 65

def likelihood(label)
  OpenStruct.new
end