Class: EMAlgorithm::Gaussian

Inherits:
Model
  • Object
show all
Defined in:
lib/em_algorithm/models/gaussian.rb

Constant Summary

Constants inherited from Model

Model::DIGIT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Model

#pdf, #value_distribution, #value_distribution_to_gnuplot

Constructor Details

#initialize(mu = 0.0, sigma = 1.0) ⇒ Gaussian

Returns a new instance of Gaussian.



5
6
7
8
# File 'lib/em_algorithm/models/gaussian.rb', line 5

def initialize(mu = 0.0, sigma = 1.0)
  @mu = mu
  @sigma = sigma
end

Instance Attribute Details

#dimObject

Returns the value of attribute dim.



3
4
5
# File 'lib/em_algorithm/models/gaussian.rb', line 3

def dim
  @dim
end

#muObject

Returns the value of attribute mu.



3
4
5
# File 'lib/em_algorithm/models/gaussian.rb', line 3

def mu
  @mu
end

#sigmaObject

Returns the value of attribute sigma.



3
4
5
# File 'lib/em_algorithm/models/gaussian.rb', line 3

def sigma
  @sigma
end

Instance Method Details

#probability_density_function(x) ⇒ Object



10
11
12
# File 'lib/em_algorithm/models/gaussian.rb', line 10

def probability_density_function(x)
  exp(-((x-@mu)**2.0)/(2.0*@sigma**2))/(sqrt(2.0*PI)*@sigma)
end

#probability_density_function_with_observation_weight(x_with_weight) ⇒ Object



14
15
16
17
18
# File 'lib/em_algorithm/models/gaussian.rb', line 14

def probability_density_function_with_observation_weight(x_with_weight)
  x = x_with_weight[0]
  observation_weight = x_with_weight[1]
  observation_weight * probability_density_function(x)
end

#to_gnuplotObject



39
40
41
# File 'lib/em_algorithm/models/gaussian.rb', line 39

def to_gnuplot
  "exp(-((x-(#{@mu.round(DIGIT)}))**2.0)/(2.0*#{(@sigma ** 2).round(DIGIT)}))/(sqrt(2.0*pi)*#{@sigma.round(DIGIT)})"
end

#to_gnuplot_with_title(weight) ⇒ Object



43
44
45
# File 'lib/em_algorithm/models/gaussian.rb', line 43

def to_gnuplot_with_title(weight)
  to_gnuplot + " w l axis x1y2 lw 3 title '#{weight.round(DIGIT)}*N(#{@mu.round(DIGIT)},#{@sigma.round(DIGIT)})'"
end

#update_average!(data_array, temp_weight, temp_weight_per_datum) ⇒ Object



20
21
22
23
24
25
# File 'lib/em_algorithm/models/gaussian.rb', line 20

def update_average!(data_array, temp_weight, temp_weight_per_datum)
  data_sum = (0..(data_array.size-1)).inject(0.0) do |sum, di|
    sum + temp_weight_per_datum[di] * data_array[di]
  end
  @mu = data_sum / temp_weight
end

#update_parameters!(data_array, temp_weight, temp_weight_per_datum) ⇒ Object



34
35
36
37
# File 'lib/em_algorithm/models/gaussian.rb', line 34

def update_parameters!(data_array, temp_weight, temp_weight_per_datum)
  update_average!(data_array, temp_weight, temp_weight_per_datum)
  update_sigma!(data_array, temp_weight, temp_weight_per_datum)
end

#update_sigma!(data_array, temp_weight, temp_weight_per_datum) ⇒ Object



27
28
29
30
31
32
# File 'lib/em_algorithm/models/gaussian.rb', line 27

def update_sigma!(data_array, temp_weight, temp_weight_per_datum)
  data_sum = (0..(data_array.size-1)).inject(0.0) do |sum, di|
    sum + temp_weight_per_datum[di] * (data_array[di] - @mu) ** 2
  end
  @sigma = sqrt(data_sum / temp_weight)
end