Class: EMAlgorithm::Likelihood

Inherits:
CheckMethod show all
Defined in:
lib/em_algorithm/convergence/likelihood.rb

Constant Summary collapse

THRESHOLD =

THRESHOLD = 0.0001

0.01

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_array) ⇒ Likelihood

Returns a new instance of Likelihood.



8
9
10
11
# File 'lib/em_algorithm/convergence/likelihood.rb', line 8

def initialize(data_array)
  @data_array = data_array
  @history = []
end

Instance Attribute Details

#historyObject

Returns the value of attribute history.



6
7
8
# File 'lib/em_algorithm/convergence/likelihood.rb', line 6

def history
  @history
end

Instance Method Details

#calculate(model) ⇒ Object

calculate log likelihood



14
15
16
17
18
19
20
# File 'lib/em_algorithm/convergence/likelihood.rb', line 14

def calculate(model)
  likelihood = @data_array.inject(0.0) do |likelihood, x|
    likelihood + log(model.pdf(x))
  end
  @history << likelihood
  likelihood
end

#converged?Boolean

Returns:

  • (Boolean)


26
27
28
29
# File 'lib/em_algorithm/convergence/likelihood.rb', line 26

def converged?
  return false if @history.length == 1
  (@history[-1] - @history[-2]).abs < THRESHOLD
end

#debug_outputObject



31
32
33
# File 'lib/em_algorithm/convergence/likelihood.rb', line 31

def debug_output
  "Likelihood: #{value}"
end

#valueObject



22
23
24
# File 'lib/em_algorithm/convergence/likelihood.rb', line 22

def value
  @history.last
end