# File lib/bn4r/bn_algorithms.rb, line 121 def likelihood_weighting( x, e, n, bn = self ) retval = [0.0, 0.0] n.times { w_sample, w = weighted_sample(e) # ask for a weighted_sample with given evidences value = w_sample.select { |v| v.name == x.name }[0].value # select the value for the query variable if value == (x.value || true) # if no value for x, ask for true retval[1] += w else retval[0] += w end } # Normalize results norm = retval[1].to_f / (retval[0]+retval[1]).to_f return [norm, 1-norm] end