Skip to content
Browse files

work on factor graphs

  • Loading branch information...
1 parent 7b4ab29 commit c10f8ae30f4c9212eef3a6c632254df442d3ef72 @larskuhnt larskuhnt committed Apr 12, 2010
View
2 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"
View
19 lib/saulabs/gauss/distribution.rb
@@ -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
@@ -75,6 +74,10 @@ def ==(other)
def equals(other)
self == other
end
+
+ def to_s
+ "[μ=#{'%.4f' % mean}, σ=#{'%.4f' % deviation}]"
+ end
end
end
View
7 lib/saulabs/gauss/factor_graph.rb
@@ -1,7 +0,0 @@
-module Saulabs
- module Gauss
- class FactorGraph
-
- end
- end
-end
View
36 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
View
1 lib/saulabs/gauss/functions.rb
@@ -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
View
2 lib/saulabs/trueskill.rb
@@ -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

0 comments on commit c10f8ae

Please sign in to comment.
Something went wrong with that request. Please try again.