Class: PGM::DiscreateVariable

Inherits:
Variable
  • Object
show all
Defined in:
lib/pgm/variable.rb

Overview

A class for discrete random variable

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, name, states, parents: [], children: []) ⇒ DiscreateVariable

Instantiate DiscreateVariable

Parameters:

  • model (PGM::BayesianNet)

    Model for variable to be added

  • name (String, Symbol)

    Name of variable

  • states (Array<String, Symbol>)

    List of variable states

  • parents (Array<PGM::Variable>) (defaults to: [])

    List of parent vertices

  • children (Array<PGM::Variable>) (defaults to: [])

    List of child vertices



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

#childrenObject (readonly)

Returns the value of attribute children.



24
25
26
# File 'lib/pgm/variable.rb', line 24

def children
  @children
end

#modelObject (readonly)

Returns the value of attribute model.



24
25
26
# File 'lib/pgm/variable.rb', line 24

def model
  @model
end

#nameObject (readonly)

Returns the value of attribute name.



24
25
26
# File 'lib/pgm/variable.rb', line 24

def name
  @name
end

#parentsObject (readonly)

Returns the value of attribute parents.



24
25
26
# File 'lib/pgm/variable.rb', line 24

def parents
  @parents
end

#statesObject (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

Parameters:

Returns:

  • (Integer)

    List of child vertices



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 }

Parameters:

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

    Data for learning in Hash objects



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_tableObject 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 }

Parameters:

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

    condition in Hash objects

Returns:

  • (Float)

    probability



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

Parameters:

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

    Array of data for learning in Hash objects



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

Parameters:

  • cellsize (Integer) (defaults to: 25)

    Size of each cell



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 }

Parameters:

  • prob (Float)

    probability

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

    condition in Hash objects



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

Parameters:



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