Class: PGM::DiscreateVariable
Overview
A class for discrete random variable
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parents ⇒ Object
readonly
Returns the value of attribute parents.
-
#states ⇒ Object
readonly
Returns the value of attribute states.
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer
compare variables.
-
#add_data(condition) ⇒ Object
learn model from data.
-
#calculate_conditional_probability_table ⇒ Object
(also: #cal_cpt)
calculate conditional probability table.
-
#conditional_probability(condition) ⇒ Float
return conditional probability of the specified condition.
-
#initialize(model, name, states, parents: [], children: []) ⇒ DiscreateVariable
constructor
Instantiate DiscreateVariable.
-
#learn(arr) ⇒ Object
learn model from array of condition and automatically calculate the conditional probability table.
-
#puts_conditional_probability_table(cellsize = 25) ⇒ Object
(also: #puts_cpt)
puts conditional probability table on the screen.
-
#set_conditional_probability(prob, condition) ⇒ Object
(also: #set_cp)
assign conditional probability for specified condition.
-
#to(other) ⇒ Object
assign link from self to other.
Constructor Details
#initialize(model, name, states, parents: [], children: []) ⇒ DiscreateVariable
Instantiate DiscreateVariable
33 34 35 36 37 38 39 40 |
# File 'lib/pgm/variable.rb', line 33 def initialize(model, name, states, parents: [], children: []) @name = name.to_sym @model = model @states = states.map{|x| x.to_sym}.sort @parents = parents @children = children self.create_conditional_probability_table end |
Instance Attribute Details
#children ⇒ Object (readonly)
Returns the value of attribute children.
24 25 26 |
# File 'lib/pgm/variable.rb', line 24 def children @children end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
24 25 26 |
# File 'lib/pgm/variable.rb', line 24 def model @model end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
24 25 26 |
# File 'lib/pgm/variable.rb', line 24 def name @name end |
#parents ⇒ Object (readonly)
Returns the value of attribute parents.
24 25 26 |
# File 'lib/pgm/variable.rb', line 24 def parents @parents end |
#states ⇒ Object (readonly)
Returns the value of attribute states.
24 25 26 |
# File 'lib/pgm/variable.rb', line 24 def states @states end |
Instance Method Details
#<=>(other) ⇒ Integer
compare variables
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/pgm/variable.rb', line 46 def <=>(other) if other.is_a?(PGM::Variable) if @model == other.model return @name <=> other.name else return @model <=> other.model end else raise TypeError, "#{other} is not PGM::Variable" end end |
#add_data(condition) ⇒ Object
learn model from data. Condition is in a Hash format { var_name1: state_name1, var_name2: state_name2 }
82 83 84 |
# File 'lib/pgm/variable.rb', line 82 def add_data(condition) @count_table[*self.data_coor(condition)] += 1 end |
#calculate_conditional_probability_table ⇒ Object Also known as: cal_cpt
calculate conditional probability table
87 88 89 |
# File 'lib/pgm/variable.rb', line 87 def calculate_conditional_probability_table @probability_table = @count_table.floor / @count_table.sum(1).to_f end |
#conditional_probability(condition) ⇒ Float
return conditional probability of the specified condition. Condition is in a Hash format { var_name1: state_name1, var_name2: state_name2 }
121 122 123 |
# File 'lib/pgm/variable.rb', line 121 def conditional_probability(condition) return @probability_table[*self.data_coor(condition)] end |
#learn(arr) ⇒ Object
learn model from array of condition and automatically calculate the conditional probability table
71 72 73 74 75 76 |
# File 'lib/pgm/variable.rb', line 71 def learn(arr) arr.each do |item| self.add_data(item) end self.calculate_conditional_probability_table end |
#puts_conditional_probability_table(cellsize = 25) ⇒ Object Also known as: puts_cpt
puts conditional probability table on the screen
96 97 98 99 100 101 |
# File 'lib/pgm/variable.rb', line 96 def puts_conditional_probability_table(cellsize = 25) puts "#{@name.to_s.center(cellsize)}|#{@adjoint_var_list.map{|x| "P(#{x})".center(cellsize)}.join('|')}" 0.upto(@states.length - 1).each do |ind| puts "#{@states[ind].to_s.rjust(cellsize)}|#{@probability_table[true, ind].to_a.map{|x| x.round(5).to_s.rjust(cellsize)}.join('|')}" end end |
#set_conditional_probability(prob, condition) ⇒ Object Also known as: set_cp
assign conditional probability for specified condition. Condition is in a Hash format { var_name1: state_name1, var_name2: state_name2 }
110 111 112 |
# File 'lib/pgm/variable.rb', line 110 def set_conditional_probability(prob, condition) @probability_table[*self.data_coor(condition)] = prob end |
#to(other) ⇒ Object
assign link from self to other
61 62 63 64 65 |
# File 'lib/pgm/variable.rb', line 61 def to(other) other.add_parent(self) self.add_child(other) @model.add_edge(@name, other.name) end |