lib/bn4r/bn_algorithms.rb in bn4r-0.1.2 vs lib/bn4r/bn_algorithms.rb in bn4r-0.9.0
- old
+ new
@@ -43,12 +43,17 @@
# Intelligence, A Modern Approach", 2nd Edition. pp 511-512
#
# The input are the nodes of the bn ordered by dependencies see nodes_ordered_by_dependencies
def prior_sample(nodes_ordered = nodes_ordered_by_dependencies)
sample = Array.new
- nodes_ordered.each { |v|
- value = rand < v.get_probability(true)
+ nodes_ordered.each { |v|
+ value = nil
+ prob = 0.0; r_prob = rand
+ v.outcomes.each { |outcome|
+ prob += v.get_probability(outcome)
+ value = outcome and break if r_prob < prob
+ }
v.set_value(value)
sample << v.copy
}
# leave the bn clear of values.
nodes_ordered.each { |v| v.clear_value }
@@ -179,10 +184,15 @@
node_actual = [e] if e.class == BayesNetNode and e.name == v.name
if !node_actual.nil? and node_actual.size == 1
value = node_actual[0].value
w = w * v.get_probability(value)
else
- value = rand < v.get_probability(true)
+ rand_sample = rand; i_tmp = 0.0
+ v.outcomes.each { |outcome|
+ value = outcome
+ i_tmp += v.get_probability(value)
+ break if i_tmp > rand_sample
+ }
end
v.set_value(value)
sample << v.copy
}
\ No newline at end of file