Пример #1
0
'''
@author: Ian Park, for CS344, Lab05
@version March 7, 2020
'''

from aima.probability import BayesNet, enumeration_ask, elimination_ask, gibbs_ask

# Utility variables
T, F = True, False

cancer = BayesNet([
    ('Cancer', '', 0.01),
    ('Test1', 'Cancer', {
        T: 0.90,
        F: 0.20
    }),
    ('Test2', 'Cancer', {
        T: 0.90,
        F: 0.20
    }),
])

# P(Cancer | positive results on both tests)
print(enumeration_ask('Cancer', dict(Test1=T, Test2=T), cancer).show_approx())
# P(Cancer | Test1 ∧ Test2) = alpha * <P(Cancer) * P(Test1 | Cancer) * P(Test2 | Cancer), P(¬Cancer) * P(Test1 | ¬Cancer) * P(Test2 | ¬Cancer)>
#                           = alpha * <0.9 * 0.9 * 0.01, 0.2 * 0.2 * 0.99>
#                           = alpha * <0.0081, 0.0396>
#                           = 1 / 0.0477 * <0.0081, 0.0396>
#                           = <0.1698, 0.8302>

# P(Cancer | a positive result on test 1, but a negative result on test 2)
Пример #2
0
@version March 7, 2020
'''

from aima.probability import BayesNet, enumeration_ask, elimination_ask, gibbs_ask

# Utility variables
T, F = True, False

# From AIMA code (probability.py) - Fig. 14.2 - burglary example
burglary = BayesNet([('Burglary', '', 0.001), ('Earthquake', '', 0.002),
                     ('Alarm', 'Burglary Earthquake', {
                         (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
                     })])

# P(Alarm | burglary ∧ ¬earthquake)
print(
    enumeration_ask('Alarm', dict(Burglary=T, Earthquake=F),
                    burglary).show_approx())

# P(John | burglary ∧ ¬earthquake)
print(
    enumeration_ask('JohnCalls', dict(Burglary=T, Earthquake=F),
Пример #3
0
It's taken from the AIMA Python code.

@author: kvlinden
@version Jan 2, 2013
'''

from aima.probability import BayesNet, enumeration_ask, elimination_ask, gibbs_ask

# Utility variables
T, F = True, False

# From AIMA code (probability.py) - Fig. 14.2 - burglary example
cancer = BayesNet([('Cancer', '', 0.01), ('Test1', 'Cancer', {
    T: 0.9,
    F: 0.2
}), ('Test2', 'Cancer', {
    T: 0.9,
    F: 0.2
})])

# Exercise 5.2
# P(Cancer | positive results on both tests)
print(enumeration_ask('Cancer', dict(Test1=T, Test2=T),
                      cancer).show_approx())  # False: 0.83, True: 0.17
print(elimination_ask('Cancer', dict(Test1=T, Test2=T),
                      cancer).show_approx())  # False: 0.83, True: 0.17
print(gibbs_ask('Cancer', dict(Test1=T, Test2=T),
                cancer).show_approx())  # False: 0.837, True: 0.163

# P(Cancer | a positive result on test 1, but negative result on test2)
print(enumeration_ask('Cancer', dict(Test1=T, Test2=F),
Пример #4
0
@author: kvlinden
@version Jan 2, 2013
'''

from aima.probability import BayesNet, enumeration_ask, elimination_ask, gibbs_ask

# Utility variables
T, F = True, False

# From AIMA code (probability.py) - Fig. 14.2 - burglary example
emotion = 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
    }),
])

# Exercise 5.3 a. in respective order
print(enumeration_ask('Raise', dict(Sunny=T),
                      emotion).show_approx())  # False: 0.99, True: 0.01
print(elimination_ask('Raise', dict(Sunny=T),
                      emotion).show_approx())  # False: 0.99, True: 0.01
print(gibbs_ask('Raise', dict(Sunny=T),
                emotion).show_approx())  # False: 0.996, True: 0.004

print(enumeration_ask('Raise', dict(Happy=T, Sunny=T),
                      emotion).show_approx())  # False: 0.986, True: 0.0142
Пример #5
0
'''
@author: Ian Park, for CS344, Lab05
@version March 7, 2020
'''

from aima.probability import BayesNet, enumeration_ask, elimination_ask, gibbs_ask

# Utility variables
T, F = True, False

happiness = BayesNet([
    ('Sunny', '', 0.70),
    ('Raise', '', 0.01),
    ('Happy', 'Sunny Raise', {
        (T, T): 1.0,
        (T, F): 0.7,
        (F, T): 0.9,
        (F, F): 0.1
    }),
])

# P(Raise | Sunny)
print(enumeration_ask('Raise', dict(Sunny=T), happiness).show_approx())
# P(Raise | Sunny) = alpha * <P(Raise) * P(Sunny), P(¬Raise) * P(Sunny)>
#                  = alpha * P(Sunny) * <0.01, 0.99>
#                  = <0.01, 0.99>
# The probability of raise does not depend on sunny. So, since we have the probability of a raise, we can easily find the probability.

# P(Raise | Happy ∧ Sunny)
print(
    enumeration_ask('Raise', dict(Happy=T, Sunny=T), happiness).show_approx())