Class: MiniPGM::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/mini_pgm/node.rb

Overview

Represents an individual node, or random variable, in a model

Example string representation for a node with two incoming edges, and no outgoing edges:

{ Pollution, Smoker } -> Cancer -> { }

Or for nodes with no incoming edges and only outgoing edges:

{ } -> Pollution -> { Cancer }
{ } -> Smoker -> { Cancer }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label) ⇒ Node

Returns a new instance of Node.



22
23
24
25
26
# File 'lib/mini_pgm/node.rb', line 22

def initialize(label)
  @label = label
  @incoming_edges = Set.new
  @outgoing_edges = Set.new
end

Instance Attribute Details

#cpdObject

Returns the value of attribute cpd.



19
20
21
# File 'lib/mini_pgm/node.rb', line 19

def cpd
  @cpd
end

#incoming_edgesObject (readonly)

Returns the value of attribute incoming_edges.



20
21
22
# File 'lib/mini_pgm/node.rb', line 20

def incoming_edges
  @incoming_edges
end

#labelObject (readonly)

Returns the value of attribute label.



20
21
22
# File 'lib/mini_pgm/node.rb', line 20

def label
  @label
end

#outgoing_edgesObject (readonly)

Returns the value of attribute outgoing_edges.



20
21
22
# File 'lib/mini_pgm/node.rb', line 20

def outgoing_edges
  @outgoing_edges
end

Instance Method Details

#to_sObject



28
29
30
# File 'lib/mini_pgm/node.rb', line 28

def to_s
  [write_set(@incoming_edges), @label, write_set(@outgoing_edges)].join(' -> ')
end

#write_set(set) ⇒ Object



32
33
34
# File 'lib/mini_pgm/node.rb', line 32

def write_set(set)
  set.empty? ? '{ }' : "{ #{set.to_a.sort.join(', ')} }"
end