lib/bayesnet/factor.rb in bayesnet-0.0.1 vs lib/bayesnet/factor.rb in bayesnet-0.0.2
- old
+ new
@@ -13,11 +13,11 @@
end
# Specifies function values for args. Latest args is an function value, all previous are argument values
def val(*args)
args = args[0] if args.size == 1 && args[0].is_a?(Array)
- @vals[args[..-2]] = args[-1]
+ @vals[args[0..-2]] = args[-1]
end
def var_names
@vars.keys
end
@@ -50,10 +50,12 @@
vals.each { |k, v| vals[k] /= norm_factor }
self.class.new(@vars.clone, vals)
end
def limit_by(evidence)
- vars = @vars.except(*evidence.keys)
+ # todo: use Hash#except when Ruby 2.6 support no longer needed
+ evidence_keys_set = evidence.keys.to_set
+ vars = @vars.reject { |k, _| evidence_keys_set.include?(k) }
evidence_vals = evidence.values
indices = evidence.keys.map { |k| index_by_var_name[k] }
vals = @vals.select { |k, v| indices.map { |i| k[i] } == evidence_vals }
vals.transform_keys! { |k| k - evidence_vals }