class PGM::BayesianNet

Bayesian Network Class

Example:

create model

bayenet = PGM::BayesianNet.new()
bayenet.create_var(:cloudy, [:t, :f])
bayenet.create_var(:sprinkler, [:t, :f])
bayenet.create_var(:rain, [:t, :f])
bayenet.create_var(:grass_wet, [:t, :f])
bayenet.create_link(:cloudy, :sprinkler)
bayenet.create_link(:cloudy, :rain)
bayenet.create_link(:sprinkler, :grass_wet)
bayenet.create_link(:rain, :grass_wet)

learn with data

bayenet.learn([
  {:cloudy => :t, :sprinkler => :f, :rain => :t, :grass_wet => :t},
  {:cloudy => :t, :sprinkler => :t, :rain => :f, :grass_wet => :t},
  {:cloudy => :f, :sprinkler => :f, :rain => :t, :grass_wet => :t},
  {:cloudy => :t, :sprinkler => :f, :rain => :t, :grass_wet => :t},
  {:cloudy => :f, :sprinkler => :t, :rain => :f, :grass_wet => :f},
  {:cloudy => :f, :sprinkler => :f, :rain => :f, :grass_wet => :f},
  {:cloudy => :f, :sprinkler => :f, :rain => :f, :grass_wet => :f},
  {:cloudy => :t, :sprinkler => :f, :rain => :t, :grass_wet => :t},
  {:cloudy => :t, :sprinkler => :f, :rain => :f, :grass_wet => :f},
  {:cloudy => :f, :sprinkler => :f, :rain => :f, :grass_wet => :f},
])

print out the conditional_probability_table

bayenet.puts_conditional_probability_table

create graph from model

bayenet.write_to_graphic_file('jpg')

Public Instance Methods

add_data(condition) click to toggle source

Learn the model with discrete data. Condition is in a Hash format { var_name1: state_name1, var_name2: state_name2 }

@param condition [Hash{Symbol => Symbol,String}] Data for learning in a Hash objects

    # File lib/pgm/model.rb
165 def add_data(condition)
166   @variables.each_value do |var|
167     var.add_data(condition)
168   end
169 end
ancestral_ordering() click to toggle source

Order random variable by ancestral order

    # File lib/pgm/model.rb
204 def ancestral_ordering
205   return self.topsort_iterator.to_a
206 end
calculate_conditional_probability_table() click to toggle source

Calculate conditional probability table from learned data

    # File lib/pgm/model.rb
183 def calculate_conditional_probability_table
184   @variables.each_value do |var|
185     var.calculate_conditional_probability_table
186   end
187 end
Also aliased as: cal_cpt
cascade?(*vs) click to toggle source

Check if vertices are in cascading topology

@param vs [Array<PGM::Variable>] List of vertices @return [Boolean] Are vertices in cascading topology

    # File lib/pgm/model.rb
108 def cascade?(*vs)
109   prev_children = nil
110   r = true
111   if vs.length > 2
112     vs.each do |v|
113       prev_children ||= [v]
114       if !prev_children.include?(v)
115         r = false
116         break
117       else
118         prev_children = adjacent_vertices(v)
119       end
120     end
121   else
122     r = false
123   end
124   return r
125 end
common_children(*vs) click to toggle source

Find common children of input vertices

@param vs [Array<PGM::Variable>] List of vertices @return [Array<PGM::Variable>] List child vertices

   # File lib/pgm/model.rb
84 def common_children(*vs)
85   r = nil
86   vs.each do |v|
87     if r
88       r &= adjacent_vertices(v)
89     else
90       r = adjacent_vertices(v)
91     end
92   end
93   r ||= []
94 end
common_children?(*vs) click to toggle source

Check if there are common children of input vertices

@param vs [Array<PGM::Variable>] List of vertices @return [Boolean] Is there common children

    # File lib/pgm/model.rb
100 def common_children?(*vs)
101   return !common_children(*vs).empty?
102 end
common_parents(*vs) click to toggle source

Find common parents of input nodes

@param vs [Array<PGM::Variable>] List of vertices @return [Array<PGM::Variable>] List parent vertices

   # File lib/pgm/model.rb
60 def common_parents(*vs)
61   r = nil
62   vs.each do |v|
63     if r
64       r &= parent_vertices(v)
65     else
66       r = parent_vertices(v)
67     end
68   end
69   r ||= []
70 end
common_parents?(*vs) click to toggle source

Check if there are common parents of input vertices

@param vs [Array<PGM::Variable>] List of variables @return [Boolean] Is there common parents

   # File lib/pgm/model.rb
76 def common_parents?(*vs)
77   return !common_parents(*vs).empty?
78 end
learn(arr) click to toggle source

learn model from array of condition and automatically calculate the conditional probability table. Condition is in a Hash format { var_name1: state_name1, var_name2: state_name2 }

@param arr [Array<Hash{Symbol => Symbol,String}>] Array of data for learning in Hash objects

    # File lib/pgm/model.rb
155 def learn(arr)
156   @variables.each_value do |var|
157     var.learn(arr)
158   end
159 end
model_notation() click to toggle source

Generate string notation of Baysian Network

@return [String] Model notation

    # File lib/pgm/model.rb
142 def model_notation
143   line = []
144   each_vertex do |v|
145     parents = parent_vertices(v)
146     line.push("P(#{v}#{parents.empty? ? '' : "|#{parents.join(',')}"})")
147   end
148   return "P(#{vertices.sort.join(',')}) = #{line.sort.join('')}"
149 end
puts_conditional_probability_table(cellsize = 25) click to toggle source

puts conditional probability table on the screen

@param cellsize [Integer] Size of each cell

    # File lib/pgm/model.rb
194 def puts_conditional_probability_table(cellsize = 25)
195   @variables.each_value do |val|
196     val.puts_conditional_probability_table(cellsize)
197   end
198 end
Also aliased as: puts_cpt
puts_cpt(cellsize = 25)
set_conditional_probability(varname, prob, condition) click to toggle source

Set conditional probability to a random variable Condition is in a Hash format { var_name1: state_name1, var_name2: state_name2 }

@param varname [String, Symbol] Random variable name @param prob [Float] Probability @param condition [Hash{Symbol => Symbol,String}] Data for learning in Hash objects

    # File lib/pgm/model.rb
177 def set_conditional_probability(varname, prob, condition)
178   var(varname.to_sym).set_conditional_probability(prob, condition)
179 end
v_tructure?(*vs) click to toggle source

Check if vertices are in V-Structure

@param vs [Array<PGM::Variable>] List of vertices @return [Boolean] Are vertices in V-Structure

    # File lib/pgm/model.rb
131 def v_tructure?(*vs)
132   if vs.length == 3
133     return parent_vertices(vs[0]).sort == vs[1..-1].sort
134   else
135     return false
136   end
137 end