Skip to content

Commit

Permalink
work on factor graphs
Browse files Browse the repository at this point in the history
  • Loading branch information
larskuhnt committed Apr 12, 2010
1 parent 7b4ab29 commit c10f8ae
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 16 deletions.
2 changes: 1 addition & 1 deletion lib/saulabs/gauss.rb
@@ -1,3 +1,3 @@
require "#{File.dirname(__FILE__)}/gauss/functions.rb"
require "#{File.dirname(__FILE__)}/gauss/distribution.rb"
require "#{File.dirname(__FILE__)}/gauss/factor_graph.rb"
require "#{File.dirname(__FILE__)}/gauss/factors.rb"
19 changes: 11 additions & 8 deletions lib/saulabs/gauss/distribution.rb
Expand Up @@ -35,18 +35,17 @@ def absolute_difference(x, y)

def log_product_normalisation(x, y)
return 0.0 if x.precision == 0.0 || y.precision == 0.0
vsum = x.variance + y.variance
mdiff = x.mean - y.mean
-0.91893853320467267 - Math.log(vsum) / 2.0 - mdiff * mdiff / 2.0 * vsum
variance_sum = x.variance + y.variance
mean_diff = x.mean - y.mean
-Functions::LOG_SQRT_2PI - (Math.log(variance_sum) / 2.0) - (mean_diff**2 / 2.0 * variance_sum)
end

def log_ratio_normalisation(x, y)
return 0.0 if x.precision == 0.0 || y.precision == 0.0
v2 = y.variance
vdiff = v2 - x.variance
return 0.0 if vdiff == 0.0
mdiff = x.mean - y.mean
Math.log(v2) + 0.91893853320467267 - Math.log(vdiff) / 2.0 + mdiff * mdiff / 2.0 * vdiff
variance_diff = y.variance - x.variance
return 0.0 if variance_diff == 0.0
mean_diff = x.mean - y.mean
Math.log(y.variance) + Functions::LOG_SQRT_2PI - (Math.log(variance_diff) / 2.0) + (mean_diff**2 / 2.0 * variance_diff)
end

end
Expand Down Expand Up @@ -75,6 +74,10 @@ def ==(other)
def equals(other)
self == other
end

def to_s
"[μ=#{'%.4f' % mean}, σ=#{'%.4f' % deviation}]"
end

end
end
Expand Down
7 changes: 0 additions & 7 deletions lib/saulabs/gauss/factor_graph.rb

This file was deleted.

36 changes: 36 additions & 0 deletions lib/saulabs/gauss/factors.rb
@@ -0,0 +1,36 @@
module Saulabs
module Gauss
module Factors

class BaseFactor

attr_accessor :messages, :binding, :variables

def initialize
@messages = []
@binding = {}
@variables = []
end

def send_message(message, variable)
log_z = Distribution.log_product_normalisation(message.value, variable.value)
variable.value = message.value * variable.value
return log_z
end

end

class GreaterThanFactor < BaseFactor

attr_accessor :epsilon, :vaiable

def initialize(epsilon, variable)
@epsilon = epsilon
@variable = variable
end

end

end
end
end
1 change: 1 addition & 0 deletions lib/saulabs/gauss/functions.rb
Expand Up @@ -4,6 +4,7 @@ class Functions

SQRT2 = Math.sqrt(2).freeze
INV_SQRT_2PI = (1 / Math.sqrt(2 * Math::PI)).freeze
LOG_SQRT_2PI = Math.log(Math.sqrt(2 * Math::PI)).freeze

class << self

Expand Down
2 changes: 2 additions & 0 deletions lib/saulabs/trueskill.rb
Expand Up @@ -15,6 +15,8 @@ def self.update_skills(tau, beta, draw_prob, skills)
num_players = skills.size
epsilon = -Math.sqrt(2.0 * beta2) * Gauss::Functions.inv_cdf((1.0 - draw_prob) / 2.0)
factors = []
variables = []

end

end
Expand Down

0 comments on commit c10f8ae

Please sign in to comment.