class PGM::DiscreateVariable

A class for discrete random variable

Attributes

children[R]
model[R]
name[R]
parents[R]
states[R]

Public Class Methods

new(model, name, states, parents: [], children: []) click to toggle source

Instantiate DiscreateVariable

@param model [PGM::BayesianNet] Model for variable to be added @param name [String, Symbol] Name of variable @param states [Array<String, Symbol>] List of variable states @param parents [Array<PGM::Variable>] List of parent vertices @param children [Array<PGM::Variable>] List of child vertices

   # File lib/pgm/variable.rb
33 def initialize(model, name, states, parents: [], children: [])
34   @name = name.to_sym
35   @model = model
36   @states = states.map{|x| x.to_sym}.sort
37   @parents = parents
38   @children = children
39   self.create_conditional_probability_table
40 end

Public Instance Methods

<=>(other) click to toggle source

compare variables

@param other [PGM::Variable] Model for variable to be added @return [Integer] List of child vertices

   # File lib/pgm/variable.rb
46 def <=>(other)
47   if other.is_a?(PGM::Variable)
48     if @model == other.model
49       return @name <=> other.name
50     else
51       return @model <=> other.model
52     end
53   else
54     raise TypeError, "#{other} is not PGM::Variable"
55   end
56 end
add_data(condition) click to toggle source

learn model from 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 Hash objects

   # File lib/pgm/variable.rb
82 def add_data(condition)
83   @count_table[*self.data_coor(condition)] += 1
84 end
calculate_conditional_probability_table() click to toggle source

calculate conditional probability table

   # File lib/pgm/variable.rb
87 def calculate_conditional_probability_table
88   @probability_table = @count_table.floor / @count_table.sum(1).to_f
89 end
Also aliased as: cal_cpt
conditional_probability(condition) click to toggle source

return conditional probability of the specified condition. Condition is in a Hash format { var_name1: state_name1, var_name2: state_name2 }

@param condition [Hash{Symbol => Symbol,String}] condition in Hash objects @return [Float] probability

    # File lib/pgm/variable.rb
121 def conditional_probability(condition)
122   return @probability_table[*self.data_coor(condition)]
123 end
learn(arr) click to toggle source

learn model from array of condition and automatically calculate the conditional probability table

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

   # File lib/pgm/variable.rb
71 def learn(arr)
72   arr.each do |item|
73     self.add_data(item)
74   end
75   self.calculate_conditional_probability_table
76 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/variable.rb
 96 def puts_conditional_probability_table(cellsize = 25)
 97   puts "#{@name.to_s.center(cellsize)}|#{@adjoint_var_list.map{|x| "P(#{x})".center(cellsize)}.join('|')}"
 98   0.upto(@states.length - 1).each do |ind|
 99     puts "#{@states[ind].to_s.rjust(cellsize)}|#{@probability_table[true, ind].to_a.map{|x| x.round(5).to_s.rjust(cellsize)}.join('|')}"
100   end
101 end
Also aliased as: puts_cpt
puts_cpt(cellsize = 25)
set_conditional_probability(prob, condition) click to toggle source

assign conditional probability for specified condition. Condition is in a Hash format { var_name1: state_name1, var_name2: state_name2 }

@param prob [Float] probability @param condition [Hash{Symbol => Symbol,String}] condition in Hash objects

    # File lib/pgm/variable.rb
110 def set_conditional_probability(prob, condition)
111   @probability_table[*self.data_coor(condition)] = prob
112 end
Also aliased as: set_cp
set_cp(prob, condition)
to(other) click to toggle source

assign link from self to other

@param other [PGM::Variable] Variable to be linked

   # File lib/pgm/variable.rb
61 def to(other)
62   other.add_parent(self)
63   self.add_child(other)
64   @model.add_edge(@name, other.name)
65 end

Protected Instance Methods

add_child(var) click to toggle source
    # File lib/pgm/variable.rb
169 def add_child(var)
170   @children.push(var)
171   @children.sort!
172 end
add_parent(var) click to toggle source
    # File lib/pgm/variable.rb
163 def add_parent(var)
164   @parents.push(var)
165   @parents.sort!
166   self.create_conditional_probability_table
167 end
create_conditional_probability_table() click to toggle source
    # File lib/pgm/variable.rb
147 def create_conditional_probability_table
148   list = []
149   @parents.each do |parent|
150     list.push(parent.state_notation)
151   end
152   if list.length == 0
153     @adjoint_var_list = [@name]
154   elsif list.length == 1
155     @adjoint_var_list = list[0]
156   else
157     @adjoint_var_list = list[0].product(*list[1..-1]).map{|x| x.join(',')}
158   end
159   @count_table = NArray.sfloat(@adjoint_var_list.length, @states.length).fill(0.0000001)
160   @probability_table = NArray.sfloat(@adjoint_var_list.length, @states.length)
161 end
data_coor(condition) click to toggle source
    # File lib/pgm/variable.rb
127 def data_coor(condition)
128   #[column, row]
129   notation = []
130   @parents.map{|x| x.name.to_sym}.each do |name|
131     notation.push("#{name}=#{condition[name]}")
132   end
133   return [
134             @adjoint_var_list.include?(notation.join(',')) ? @adjoint_var_list.index(notation.join(',')) : true,
135             @states.include?(condition[@name]) ? @states.index(condition[@name]) : true
136           ]
137 end
state_notation() click to toggle source
    # File lib/pgm/variable.rb
139 def state_notation
140   notations = []
141   self.states.each do |state|
142     notations.push("#{name}=#{state}")
143   end
144   return notations
145 end