F: 0.2 }), ('Test2', 'Cancer', { T: 0.9, F: 0.2 }), ]) """ Calculations: ∝ * P(Test1, Test2 | Cancer) * P(Cancer) = ∝ * P(Test1 | Cancer) * P(Test2 | C) * P(Cancer) = ∝ * 0.9 * 0.9 * 0.01 = 0.17, 0.83 """ print('P(Cancer | positive results on both tests): ') print(enumeration_ask('Cancer', dict(Test1=T, Test2=T), cancer).show_approx()) """ Calculations: ∝ * P(Test1, ¬Test2 | Cancer) * P(Cancer) = ∝ * P(Test1 | Cancer) * P(¬Test2 | Cancer) * P(Cancer) = ∝ * 0.9 * 0.2 * 0.01 = 0.00565, 0.99435 """ print( '\nP(Cancer | a positive result on test 1, but a negative result on test 2): ' ) print(enumeration_ask('Cancer', dict(Test1=T, Test2=F), cancer).show_approx()) """ The results do make sense because there is a low chance(0.01) of having cancer and the probability for each test determining if there is cancer is high (0.9).
('Happy', 'Sunny Raise', { (T, T): 1.0, (T, F): 0.7, (F, T): 0.9, (F, F): 0.1 })]) #A) """ Both Raise and Sunny are both independent, meaning that both do not influence each other P(Raise | Sunny) = P(Raise) = 0.01, 0.99 """ print('P(Raise | sunny): ') print(enumeration_ask('Raise', dict(Sunny=T), sunny).show_approx()) """ ∝ * P(Happy | Sunny, Raise) * P(Raise) = ∝ 1.0 * 0.01 = 0.0142, 0.986 """ print('\nP(Raise | happy ∧ sunny): ') print(enumeration_ask('Raise', dict(Happy=T, Sunny=T), sunny).show_approx()) #B) print('\nP(Raise | happy): ') print(enumeration_ask('Raise', dict(Happy=T), sunny).show_approx()) print('\nP(Raise | happy ∧ ¬sunny): ') print(enumeration_ask('Raise', dict(Happy=T, Sunny=F), sunny).show_approx()) """
(T, T): 0.95, (T, F): 0.94, (F, T): 0.29, (F, F): 0.001 }), ('JohnCalls', 'Alarm', { T: 0.90, F: 0.05 }), ('MaryCalls', 'Alarm', { T: 0.70, F: 0.01 })]) # Compute P(Alarm | burglary and -earthquake) using the enumeration-ask and elimination-ask algorithms print("P(Alarm | burglary and -earthquake)") print( enumeration_ask('Alarm', dict(Burglary=T, Earthquake=F), burglary).show_approx()) print( elimination_ask('Alarm', dict(Burglary=T, Earthquake=F), burglary).show_approx()) ''' i. This computation solves the probability of alarm going off or not given that there is a burglary and no earthquake ''' # Compute P(John | burglary and -earthquake) using the enumeration-ask and elimination-ask algorithms print("\nP(John | burglary and -earthquake)") print("Enumeration-ask -> " + enumeration_ask( 'JohnCalls', dict(Burglary=T, Earthquake=F), burglary).show_approx()) print("Elimination-ask -> " + elimination_ask( 'JohnCalls', dict(Burglary=T, Earthquake=F), burglary).show_approx()) ''' ii. This computation solves the probability of John calling or not given that there is a burglary and no earthquake.
F: 0.20 }), ('Sprinkler', 'Cloudy', { T: 0.10, F: 0.50 }), ('WetGrass', 'Sprinkler Rain', { (T, T): 0.99, (T, F): 0.90, (F, T): 0.90, (F, F): 0.00 })]) # Compute P(Cloudy) print('P(Cloudy)') print("Enumeration-ask -> " + enumeration_ask('Cloudy', dict(), weatherA).show_approx()) # Compute P(Sprinkler | cloudy) print('\nP(Sprinkler | cloudy)') print("Enumeration-ask -> " + enumeration_ask('Sprinkler', dict(Cloudy=T), weatherA).show_approx()) # Compute P(Cloudy| the sprinkler is running and it’s not raining) print('\nP (Cloudy | sprinker and -rain') print("Enumeration-ask -> " + enumeration_ask( 'Cloudy', dict(Sprinkler=T, Rain=F), weatherA).show_approx()) # Compute P(WetGrass | it’s cloudy, the sprinkler is running and it’s raining) print('\nP (WetGrass | cloudy and sprinkler and rain') print("Enumeration-ask -> " + enumeration_ask( 'WetGrass', dict(Cloudy=T, Sprinkler=T, Rain=T), weatherA).show_approx())
('Sprinkler', 'Cloudy', { T: 0.1, F: 0.5 }), ('Rain', 'Cloudy', { T: 0.8, F: 0.2 }), ('WetGrass', 'Sprinkler Rain', { (T, T): 0.99, (T, F): 0.90, (F, T): 0.90, (F, F): 0.00 })]) # i print("i.", enumeration_ask('Cloudy', dict(), bayes_net).show_approx()) # ii print("ii.", enumeration_ask('Sprinkler', dict(Cloudy=T), bayes_net).show_approx()) # iii print( "iii.", enumeration_ask('Cloudy', dict(Sprinkler=T, Rain=T), bayes_net).show_approx()) # iv print( "iv.", enumeration_ask('WetGrass', dict(Cloudy=T, Sprinkler=T, Rain=T), bayes_net).show_approx()) # v print("v.",
from tools.aima.probability import BayesNet, enumeration_ask # Utility variables T, F = True, False # create the Bayes Network happy = BayesNet([ ('Sunny', '', 0.7), ('Raise', '', 0.01), ('Happy', 'Sunny Raise', {(T, T): 1.0, (T, F): 0.7, (F, T): 0.9, (F, F): 0.1}) ]) print("Exercise 5.3") # a.i. P(Raise | sunny) print("a. i. ", enumeration_ask('Raise', dict(Sunny=T), happy).show_approx()) # a.ii. P(Raise | happy ^ sunny) print(" ii. ", enumeration_ask('Raise', dict(Happy=T, Sunny=T), happy).show_approx()) # b.i. P(Raise | happy) print("b. i. ", enumeration_ask('Raise', dict(Happy=T), happy).show_approx()) # b.ii. P(Raise | happy ^ -sunny) print(" ii. ", enumeration_ask('Raise', dict(Happy=T, Sunny=F), happy).show_approx()) """ The results don't makes sense to me; this is the case for diagnostic statistics. """
# Utility variables T, F = True, False # From AIMA code (probability.py) - tow-cause happiness example happiness = BayesNet([('Sunny', '', 0.7), ('Raise', '', 0.01), ('Happy', 'Sunny Raise', { (T, T): 1.0, (T, F): 0.7, (F, T): 0.9, (F, F): 0.1 })]) # a. i) Compute P(Raise | sunny) using the enumeration-ask and elimination-ask algorithms print("P(Raise | sunny)") print("Enumeration-ask -> " + enumeration_ask('Raise', dict(Sunny=T), happiness).show_approx()) print("Elimination-ask -> " + elimination_ask('Raise', dict(Sunny=T), happiness).show_approx()) # a. ii) Compute P(Raise | happy and sunny) using the enumeration-ask and elimination-ask algorithms print("\nP(Raise | happy and sunny)") print( "Enumeration-ask -> " + enumeration_ask('Raise', dict(Happy=T, Sunny=T), happiness).show_approx()) print( "Elimination-ask -> " + elimination_ask('Raise', dict(Happy=T, Sunny=T), happiness).show_approx()) ''' Hand-work a. i) P(Raise | sunny) = <P(raise | sunny), P(-raise | sunny)>
}), ('Rain', 'Cloudy', { T: 0.8, F: 0.2 }), ('WetGrass', 'Sprinkler Rain', { (T, T): 0.99, (T, F): 0.9, (F, T): 0.9, (F, F): 0.0 })]) ''' i. P(Cloudy) = 0.5 = 0.5, 0.5 ''' print(enumeration_ask('Cloudy', dict(), climate).show_approx()) ''' ii.P(Sprinker | cloudy) = 0.1 = 0.1, 0.9 ''' print(enumeration_ask('Sprinkler', dict(Cloudy=T), climate).show_approx()) ''' iii. P(Cloudy| the sprinkler is running and it’s not raining) = ∝ * P(Sprinkler, ¬Rain | Cloudy * P(Cloudy) = ∝ * 0.1 * 0.2 * 0.5 = ∝ * 0.01, 0.2 = 0.0476, 0.952 ''' print( enumeration_ask('Cloudy', dict(Sprinkler=T, Rain=F), climate).show_approx())