Class: EMAlgorithm::Likelihood
- Inherits:
-
CheckMethod
- Object
- CheckMethod
- EMAlgorithm::Likelihood
- Defined in:
- lib/em_algorithm/convergence/likelihood.rb
Constant Summary collapse
- THRESHOLD =
THRESHOLD = 0.0001
0.01
Instance Attribute Summary collapse
-
#history ⇒ Object
Returns the value of attribute history.
Instance Method Summary collapse
-
#calculate(model) ⇒ Object
calculate log likelihood.
- #converged? ⇒ Boolean
- #debug_output ⇒ Object
-
#initialize(data_array) ⇒ Likelihood
constructor
A new instance of Likelihood.
- #value ⇒ Object
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
#history ⇒ Object
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
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_output ⇒ Object
31 32 33 |
# File 'lib/em_algorithm/convergence/likelihood.rb', line 31 def debug_output "Likelihood: #{value}" end |
#value ⇒ Object
22 23 24 |
# File 'lib/em_algorithm/convergence/likelihood.rb', line 22 def value @history.last end |