Class: PGM::BayesianNet

Inherits:
RGL::DirectedAdjacencyGraph
  • Object
show all
Includes:
Bidirectional
Defined in:
lib/pgm/model.rb

Overview

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')

Instance Method Summary collapse

Methods included from Bidirectional

#create_link, #create_var, #has_var?, #initialize, #initialize_copy, #var

Instance Method Details

#add_data(condition) ⇒ Object

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

Parameters:

  • condition (Hash{Symbol => Symbol,String})

    Data for learning in a Hash objects



165
166
167
168
169
# File 'lib/pgm/model.rb', line 165

def add_data(condition)
  @variables.each_value do |var|
    var.add_data(condition)
  end
end

#ancestral_orderingObject

Order random variable by ancestral order



204
205
206
# File 'lib/pgm/model.rb', line 204

def ancestral_ordering
  return self.topsort_iterator.to_a
end

#calculate_conditional_probability_tableObject Also known as: cal_cpt

Calculate conditional probability table from learned data



183
184
185
186
187
# File 'lib/pgm/model.rb', line 183

def calculate_conditional_probability_table
  @variables.each_value do |var|
    var.calculate_conditional_probability_table
  end
end

#cascade?(*vs) ⇒ Boolean

Check if vertices are in cascading topology

Parameters:

Returns:

  • (Boolean)

    Are vertices in cascading topology



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/pgm/model.rb', line 108

def cascade?(*vs)
  prev_children = nil
  r = true
  if vs.length > 2
    vs.each do |v|
      prev_children ||= [v]
      if !prev_children.include?(v)
        r = false
        break
      else
        prev_children = adjacent_vertices(v)
      end
    end
  else
    r = false
  end
  return r
end

#common_children(*vs) ⇒ Array<PGM::Variable>

Find common children of input vertices

Parameters:

Returns:



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/pgm/model.rb', line 84

def common_children(*vs)
  r = nil
  vs.each do |v|
    if r
      r &= adjacent_vertices(v)
    else
      r = adjacent_vertices(v)
    end
  end
  r ||= []
end

#common_children?(*vs) ⇒ Boolean

Check if there are common children of input vertices

Parameters:

Returns:

  • (Boolean)

    Is there common children



100
101
102
# File 'lib/pgm/model.rb', line 100

def common_children?(*vs)
  return !common_children(*vs).empty?
end

#common_parents(*vs) ⇒ Array<PGM::Variable>

Find common parents of input nodes

Parameters:

Returns:



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/pgm/model.rb', line 60

def common_parents(*vs)
  r = nil
  vs.each do |v|
    if r
      r &= parent_vertices(v)
    else
      r = parent_vertices(v)
    end
  end
  r ||= []
end

#common_parents?(*vs) ⇒ Boolean

Check if there are common parents of input vertices

Parameters:

Returns:

  • (Boolean)

    Is there common parents



76
77
78
# File 'lib/pgm/model.rb', line 76

def common_parents?(*vs)
  return !common_parents(*vs).empty?
end

#learn(arr) ⇒ Object

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 }

Parameters:

  • arr (Array<Hash{Symbol => Symbol,String}>)

    Array of data for learning in Hash objects



155
156
157
158
159
# File 'lib/pgm/model.rb', line 155

def learn(arr)
  @variables.each_value do |var|
    var.learn(arr)
  end
end

#model_notationString

Generate string notation of Baysian Network

Returns:

  • (String)

    Model notation



142
143
144
145
146
147
148
149
# File 'lib/pgm/model.rb', line 142

def model_notation
  line = []
  each_vertex do |v|
    parents = parent_vertices(v)
    line.push("P(#{v}#{parents.empty? ? '' : "|#{parents.join(',')}"})")
  end
  return "P(#{vertices.sort.join(',')}) = #{line.sort.join('')}"
end

#puts_conditional_probability_table(cellsize = 25) ⇒ Object Also known as: puts_cpt

puts conditional probability table on the screen

Parameters:

  • cellsize (Integer) (defaults to: 25)

    Size of each cell



194
195
196
197
198
# File 'lib/pgm/model.rb', line 194

def puts_conditional_probability_table(cellsize = 25)
  @variables.each_value do |val|
    val.puts_conditional_probability_table(cellsize)
  end
end

#set_conditional_probability(varname, prob, condition) ⇒ Object

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

Parameters:

  • varname (String, Symbol)

    Random variable name

  • prob (Float)

    Probability

  • condition (Hash{Symbol => Symbol,String})

    Data for learning in Hash objects



177
178
179
# File 'lib/pgm/model.rb', line 177

def set_conditional_probability(varname, prob, condition)
  var(varname.to_sym).set_conditional_probability(prob, condition)
end

#v_tructure?(*vs) ⇒ Boolean

Check if vertices are in V-Structure

Parameters:

Returns:

  • (Boolean)

    Are vertices in V-Structure



131
132
133
134
135
136
137
# File 'lib/pgm/model.rb', line 131

def v_tructure?(*vs)
  if vs.length == 3
    return parent_vertices(vs[0]).sort == vs[1..-1].sort
  else
    return false
  end
end