Skip to content

Commit

Permalink
More readme changes
Browse files Browse the repository at this point in the history
  • Loading branch information
David Fisher committed Jul 22, 2012
1 parent 0341d62 commit a24119c
Showing 1 changed file with 47 additions and 45 deletions.
92 changes: 47 additions & 45 deletions README.md
Expand Up @@ -21,7 +21,7 @@ http://rubyforge.org/projects/bn4r
Dependencies Dependencies
------ ------


* rgl-0.2.3 ( Ruby Graph Library ), http://rgl.rubyforge.org rgl-0.2.3 ( Ruby Graph Library ), http://rgl.rubyforge.org


Design principles Design principles
------ ------
Expand All @@ -44,69 +44,71 @@ Usage Examples
------ ------


1. Install the gem ( bn4r-0.9.0.gem ) 1. Install the gem ( bn4r-0.9.0.gem )
`gem install bn4r`
gem install bn4r


2. Include the bn4r 2. Include the bn4r
`require 'bn4r'`
require 'bn4r'


3. Create your first bayes net 3. Create your first bayes net


`#Create BayesNet #Create BayesNet
bn_aima = BayesNet.new bn_aima = BayesNet.new


# Create nodes for the Bayes Net (BayesNetNodes) # Create nodes for the Bayes Net (BayesNetNodes)
burglary = BayesNetNode.new("Burglary") burglary = BayesNetNode.new("Burglary")
earthquake = BayesNetNode.new("Earthquake") earthquake = BayesNetNode.new("Earthquake")
alarm = BayesNetNode.new("Alarm") alarm = BayesNetNode.new("Alarm")
john_calls = BayesNetNode.new("JohnCalls") john_calls = BayesNetNode.new("JohnCalls")
mary_calls = BayesNetNode.new("MaryCalls") mary_calls = BayesNetNode.new("MaryCalls")


# Add nodes ( vertex ) to the BayesNet # Add nodes ( vertex ) to the BayesNet
bn_aima.add_vertex(burglary) bn_aima.add_vertex(burglary)
bn_aima.add_vertex(earthquake) bn_aima.add_vertex(earthquake)
bn_aima.add_vertex(alarm) bn_aima.add_vertex(alarm)
bn_aima.add_vertex(john_calls) bn_aima.add_vertex(john_calls)
bn_aima.add_vertex(mary_calls) bn_aima.add_vertex(mary_calls)


# Add relations ( edges ) between nodes in the BayesNet # Add relations ( edges ) between nodes in the BayesNet
bn_aima.add_edge(burglary,alarm) bn_aima.add_edge(burglary,alarm)
bn_aima.add_edge(earthquake,alarm) bn_aima.add_edge(earthquake,alarm)
bn_aima.add_edge(alarm,john_calls) bn_aima.add_edge(alarm,john_calls)
bn_aima.add_edge(alarm,mary_calls) bn_aima.add_edge(alarm,mary_calls)


# Assign probabilities to each node # Assign probabilities to each node
burglary.set_probability_table([], [0.001, 0.999] ) burglary.set_probability_table([], [0.001, 0.999] )
earthquake.set_probability_table([], [0.002, 0.998] ) earthquake.set_probability_table([], [0.002, 0.998] )


alarm.set_probability_table([burglary,earthquake], [0.95, 0.05, 0.94, 0.06, 0.29, 0.71, 0.001,0.999] ) alarm.set_probability_table([burglary,earthquake], [0.95, 0.05, 0.94, 0.06, 0.29, 0.71, 0.001,0.999] )


john_calls.set_probability_table([alarm], [0.90,0.10,0.05,0.95]) john_calls.set_probability_table([alarm], [0.90,0.10,0.05,0.95])
mary_calls.set_probability_table([alarm], [0.70,0.30,0.01,0.99])` mary_calls.set_probability_table([alarm], [0.70,0.30,0.01,0.99])


6. Solve it! 4. Solve it!


`# John and Mary are calling ... # John and Mary are calling ...
john_calls.set_value(true) john_calls.set_value(true)
mary_calls.set_value(true) mary_calls.set_value(true)


# Why? # Why?
is_there_a_burglary = bn_aima.enumeration_ask( burglary, [john_calls, mary_calls] ) is_there_a_burglary = bn_aima.enumeration_ask( burglary, [john_calls, mary_calls] )
puts "Call the police!" if is_there_a_burglary[0] > is_there_a_burglary[1] puts "Call the police!" if is_there_a_burglary[0] > is_there_a_burglary[1]


is_the_alarm_on = bn_aima.enumeration_ask( alarm, [john_calls, mary_calls] ) is_the_alarm_on = bn_aima.enumeration_ask( alarm, [john_calls, mary_calls] )
puts "Run home, your alarm is distubing the neigborhood!" if is_the_alarm_on[0] > is_the_alarm_on[1] puts "Run home, your alarm is distubing the neigborhood!" if is_the_alarm_on[0] > is_the_alarm_on[1]


is_there_a_earthquake = bn_aima.enumeration_ask( earthquake, [john_calls, mary_calls] ) is_there_a_earthquake = bn_aima.enumeration_ask( earthquake, [john_calls, mary_calls] )
puts "Calm yourself, there isn't a earthquake ;)" if is_there_a_earthquake[0] < is_there_a_earthquake[1]` puts "Calm yourself, there isn't a earthquake ;)" if is_there_a_earthquake[0] < is_there_a_earthquake[1]


7. See how your bayes net looks like 5. See how your bayes net looks like


`#In .dot format #In .dot format
bn_aima.to_dot bn_aima.to_dot


# In Microsoft Belief Networks (.xbn) format # In Microsoft Belief Networks (.xbn) format
# (download for free in: http://research.microsoft.com/adapt/MSBNx ) # (download for free in: http://research.microsoft.com/adapt/MSBNx )
bn_aima.to_xbn` bn_aima.to_xbn




Documentation Documentation
Expand Down

0 comments on commit a24119c

Please sign in to comment.