Ejemplo n.º 1
0
P[F, T, F, T] = 0.004
P[T, F, T, T] = 0.008
P[T, F, F, T] = 0.032
P[F, F, T, T] = 0.072
P[F, F, F, T] = 0.288
P[T, T, T, F] = 0.054
P[T, T, F, F] = 0.006
P[F, T, T, F] = 0.036
P[F, T, F, F] = 0.004
P[T, F, T, F] = 0.008
P[T, F, F, F] = 0.032
P[F, F, T, F] = 0.072
P[F, F, F, F] = 0.288

# Compute P(Cavity|Toothache=T)  (see the text, page 493).
PC = enumerate_joint_ask('Toothache', {'Rain': T}, P)
print(PC.show_approx())
'''
Answers to 4.2 part a and b
a. 
i.   With rain included, the joint probability distribution contains 16 entries.
ii.  The probabilities should still sum to one as long as the variables cover all possible scenarios
iii. It is possible to use more than just true and false for a probability distribution.  For example,
     if choosing a random location on the American flag, the possible states are red, white, and blue,
     each with a certain probability of being chosen randomly.  As long as every possible state is 
     covered, and the probabilities add to one, states beyond true and false are possible.
iv.  The probabilities that I chose indicate that the value of Rain is independent of the original values
     because the conditional probabilities, e.g. P(Cavity|Catch), are the same for both rain and not rain.
     
b. 
P(Toothache | rain) = P(Toothache && rain)/P(rain) = 
Ejemplo n.º 2
0
P[F, F, T] = 0.144
P[F, F, F] = 0.576
'''
b.
Hand calculation:

P(cavity | catch) = P(cavity & catch) / P(catch)

= (0.108 + 0.072) / (0.108 + 0.072 + 0.016 + 0.144) = 0.18/0.34 

= 0.529

'''

# Compute P(Cavity|Toothache=T)  (see the text, page 493).
PC = enumerate_joint_ask('Cavity', {'Catch': T}, P)
print(PC.show_approx())

# The Joint Probability Distribution Fig. 13.3 (from AIMA Python)
# Probability distribution for 2 coins flips
P = JointProbDist(['Coin1', 'Coin2'])
T, F = True, False
P[T, T] = 0.25
P[F, T] = 0.25
P[T, F] = 0.25
P[F, F] = 0.25

PC = enumerate_joint_ask('Coin2', {'Coin1': T}, P)
print(PC.show_approx())
''' 
c. The answer does confirm what should be true about what will happen when two coins are flipped.
Ejemplo n.º 3
0
P[F, T, F, T] = 0.004
P[T, F, T, T] = 0.008
P[T, F, F, T] = 0.032
P[F, F, T, T] = 0.072
P[F, F, F, T] = 0.288
P[T, T, T, F] = 0.054
P[T, T, F, F] = 0.006
P[F, T, T, F] = 0.036
P[F, T, F, F] = 0.004
P[T, F, T, F] = 0.008
P[T, F, F, F] = 0.032
P[F, F, T, F] = 0.072
P[F, F, F, F] = 0.288

# Compute P(Cavity|Toothache=T)  (see the text, page 493).
PC = enumerate_joint_ask('Cavity', {'Toothache': T}, P)
print(PC.show_approx())

# i) 16
# ii) Yes, probabilities in a Join Distrubution table must add to one. An "axiom of probability".
#   There must be some probability for each combination of T/F values.
# iii) I do not think you can have something other than T/F values. If there is an "inbetween" state with an unclear
#   T/F answer, it can be broken down into a T/F answer with a RV for each "inbetween" state. The table will become quite large.
# iv) Yes, it is 50-50 if there will be rain.

# b) P(toothache|rain) = P(toothache^rain) / P(rain)
#       = (0.054 + 0.006 + 0.008 + 0.032) / (0.054 + 0.006 + 0.008 + 0.032 + 0.036 + 0.072 + 0.004 + 0.288)
#       = 0.1 / 0.5
#       = 0.2
# P(~toothache|rain) = 1 - 0.2 = 0.8
# *P*(Toothache|rain) = <0.2, 0.8>
Ejemplo n.º 4
0
from probability import JointProbDist, enumerate_joint_ask

# The Joint Probability Distribution Fig. 13.3 (from AIMA Python)
P = JointProbDist(['Toothache', 'Cavity', 'Catch'])
T, F = True, False
P[T, T, T] = 0.108
P[T, T, F] = 0.012
P[F, T, T] = 0.072
P[F, T, F] = 0.008
P[T, F, T] = 0.016
P[T, F, F] = 0.064
P[F, F, T] = 0.144
P[F, F, F] = 0.576

# Compute P(Cavity|Toothache=T)  (see the text, page 493).
PC = enumerate_joint_ask('Cavity', {'Toothache': T}, P)
print(PC.show_approx())
PC = enumerate_joint_ask('Cavity', {'Catch': T}, P)
print(PC.show_approx())
"""
P(Catch) = 0.108+0.072+0.016+0.144 = 0.340
P(Cavity) = 0.108+0.072+0.012+0.008 = 0.200
P(Catch && Cavity) = 0.108+0.72 = 0.180
P(Catch|Cavity) = P(Catch&&Cavity)/P(Cavity) = 0.180/0.200=0.900

P(Cavity|Catch)= P(Catch|Cavity)P(Cavity)/P(Catch) = 0.9*0.2/0.34 = 0.529

"""

# New Coin flipping Probability
P2 = JointProbDist(['head1', 'head2'])
Ejemplo n.º 5
0
from probability import JointProbDist, enumerate_joint_ask

# The Joint Probability Distribution Fig. 13.3 (from AIMA Python)
P = JointProbDist(['Toothache', 'Cavity', 'Catch'])
T, F = True, False
P[T, T, T] = 0.108
P[T, T, F] = 0.012
P[F, T, T] = 0.072
P[F, T, F] = 0.008
P[T, F, T] = 0.016
P[T, F, F] = 0.064
P[F, F, T] = 0.144
P[F, F, F] = 0.576

# Compute P(Cavity|Toothache=T)  (see the text, page 493).
PC = enumerate_joint_ask('Cavity', {'Toothache': T}, P)
print(PC.show_approx())

# Compute P(Cacity|Catch=T)
PC = enumerate_joint_ask('Cavity', {'Catch': T}, P)
print(PC.show_approx())

P1 = JointProbDist(['Coin1', 'Coin2'])
P1[T, T] = 0.25
P1[T, F] = 0.25
P1[F, T] = 0.25
P1[F, F] = 0.25

PC1 = enumerate_joint_ask('Coin1', {'Coin2': T}, P1)
print(PC1.show_approx())
"""
Ejemplo n.º 6
0
This implements the simulation of a coin flip of two coins that computes the probability

@author: Gavin Martin
@version feb 28, 2020
'''

from probability import JointProbDist, enumerate_joint_ask

# The Joint Probability Distribution Fig. 13.3 (from AIMA Python)
P = JointProbDist(['Coin1', 'Coin2'])
T, F = True, False
# T symbolizes Heads, while False symbolizes Tails
P[T, T] = .25
P[T, F] = .25
P[F, T] = .25
P[F, F] = .25

# Compute P(Coin2|Coin1=T)  # T means heads
PC = enumerate_joint_ask('Coin2', {'Coin1': T}, P)
print(PC.show_approx())

# Answers:
# It did confirm what I thought was true about flipping coins. If one coin is preset to heads / tails,
# it makes sense that the other coin would have a 50/50 shot of being either.

# For the probability done by hand: I took the probability where (Cavity and Catch are true) / P(Catch is true)
# That came out to (.108 + .072) / (.108 + .016 + .072 + .144) or .529 for P(Cavity | Catch)

# I can see why this would be problematic for a bigger function. The more variables you add,
# the longer and more chaotic the problem woudld get. It isn't very scaeable.
Ejemplo n.º 7
0
from probability import JointProbDist, enumerate_joint_ask

# The Joint Probability Distribution Fig. 13.3 (from AIMA Python)
P = JointProbDist(['Toothache', 'Cavity', 'Catch'])
T, F = True, False
P[T, T, T] = 0.108
P[T, T, F] = 0.012
P[F, T, T] = 0.072
P[F, T, F] = 0.008
P[T, F, T] = 0.016
P[T, F, F] = 0.064
P[F, F, T] = 0.144
P[F, F, F] = 0.576

#4.1b
ans1 = enumerate_joint_ask('Cavity', {'Catch': T}, P)
print("4.1b: P(Cavity|Catch) results\n", ans1.show_approx())

# The Joint Probability Distributino of flipping 2 coins
P2 = JointProbDist(['Coin1', 'Coin2'])
Heads, Tails = True, False
P2[Heads, Heads] = 0.25
P2[Heads, Tails] = 0.25
P2[Tails, Heads] = 0.25
P2[Tails, Tails] = 0.25

#4.1c
ans2 = enumerate_joint_ask('Coin1', {'Coin2': Heads}, P2)
print("4.1c: P(Coin1|Coin2 = Heads) results\n", ans2.show_approx())
Ejemplo n.º 8
0
b.
i. P(Cavity|catch) = P(Cavity ^ catch) / P(catch) = (0.108+0.072) / (0.108+0.016+0.072+0.144) = 0.529
So the distribution is: True: 0.529, False: 0.471
ii. code below, which agrees with my hand-calculated version
"""

# The Joint Probability Distribution Fig. 13.3 (from AIMA Python)
P = JointProbDist(['Toothache', 'Cavity', 'Catch'])
T, F = True, False
P[T, T, T] = 0.108; P[T, T, F] = 0.012
P[F, T, T] = 0.072; P[F, T, F] = 0.008
P[T, F, T] = 0.016; P[T, F, F] = 0.064
P[F, F, T] = 0.144; P[F, F, F] = 0.576

# Compute P(Cavity|Toothache=T)  (see the text, page 493).
PC = enumerate_joint_ask('Cavity', {'Catch': T}, P)
print(PC.show_approx())

"""
1 (cont)
c.
"""
P_coins = JointProbDist(["coin1", "coin2"])
H, T = True, False
P_coins[H, T] = 1/4;
P_coins[T, T] = 1/4;
P_coins[T, H] = 1/4;
P_coins[H, H] = 1/4;

PH = enumerate_joint_ask('coin2', {'coin1': H}, P_coins)
print(PH.show_approx())
Ejemplo n.º 9
0
'''

import sys
sys.path.append("/home/james/Documents/Calvin/CS-344/cs344-code/tools/aima")
from probability import JointProbDist, enumerate_joint_ask

# The Joint Probability Distribution Fig. 13.3 (from AIMA Python)
P = JointProbDist(['Toothache', 'Cavity', 'Catch'])
T, F = True, False
P[T, T, T] = 0.108; P[T, T, F] = 0.012
P[F, T, T] = 0.072; P[F, T, F] = 0.008
P[T, F, T] = 0.016; P[T, F, F] = 0.064
P[F, F, T] = 0.144; P[F, F, F] = 0.576

# Compute P(Cavity|Toothache=T)  (see the text, page 493).
PC = enumerate_joint_ask('Cavity', {'Toothache': T}, P)
print("P(Cavity|Toothache):")
print(PC.show_approx())

# Compute P(Cavity|Catch=T)
print("P(Cavity|Catch):")
print(PC.show_approx())
PC = enumerate_joint_ask('Cavity', {'Catch': T}, P)
print(PC.show_approx())

# Compute coin probabilities
C = JointProbDist(['Coin1', 'Coin2'])
C['Heads', 'Heads'] = 0.25; C['Tails', 'Tails'] = 0.25
C['Heads', 'Tails'] = 0.25; C['Tails', 'Heads'] = 0.25

print('\nP(Coin2|Coin1=heads)')
Ejemplo n.º 10
0
from probability import JointProbDist, enumerate_joint_ask

# The Joint Probability Distribution Fig. 13.3 (from AIMA Python)
P = JointProbDist(['Toothache', 'Cavity', 'Catch'])
T, F = True, False
P[T, T, T] = 0.108
P[T, T, F] = 0.012
P[F, T, T] = 0.072
P[F, T, F] = 0.008
P[T, F, T] = 0.016
P[T, F, F] = 0.064
P[F, F, T] = 0.144
P[F, F, F] = 0.576

# Compute P(Cavity|Toothache=T)  (see the text, page 493).
PC_t = enumerate_joint_ask('Cavity', {'Toothache': T}, P)
print(PC_t.show_approx())

# P(Cavity|catch) = P(Cavity and catch) / P(catch) = (0.108 + 0.072) / (0.108 + 0.016 + 0.072 + 0.144) = 0.5294
PC_c = enumerate_joint_ask('Cavity', {'Catch': T}, P)
print(PC_c.show_approx())

two_coins = 0.5 * 0.5  # probability of any given result of flipping two coins
C = JointProbDist(['coin1', 'coin2'])
C[T, T] = 0.25
C[T, F] = 0.25
C[F, T] = 0.25
C[F, F] = 0.25

PH_h = enumerate_joint_ask('coin1', {'coin2': T}, C)
print(PH_h.show_approx())
Ejemplo n.º 11
0
From what we know:
P(Cavity|Catch) = ((P(Catch|Cavity)P(Cavity))/P(Catch)

P(Catch) = 0.108 + 0.016 + 0.072 + 0.144 = 0.34
P(Cavity && Catch) = 0.108 + 0.072 = 0.18
P(!Cavity && Catch) = 0.016 + 0.144 = 0.16

P(Cavity|Catch) = P(Cavity && Catch)/P(Catch) = (0.18)/0.34 ~= 0.5294
P(!Cavity|Catch) = P(!Cavity && Catch)/P(Catch) = (0.16)/0.34 ~= 0.4706

*P*(Cavity|Catch) = < 0.5294, 0.4706 >
'''

print("Probability distribution of Cavity given Catch:")
PC = enumerate_joint_ask('Cavity', {'Catch': T}, P)
print(PC.show_approx())

print("\n==============================================\n")

# Joint Probability Dist. for two coin flips
heads, tails = True, False
C = JointProbDist(["coin1", "coin2"])
C[heads, heads] = 0.25
C[heads, tails] = 0.25
C[tails, heads] = 0.25
C[tails, tails] = 0.25

print(
    "Probability distribution of coin2, given coin1 was heads:\n(False = Tails, True = Heads)"
)
Ejemplo n.º 12
0
from probability import JointProbDist, enumerate_joint_ask

# The Joint Probability Distribution Fig. 13.3 (from AIMA Python)
P = JointProbDist(['Toothache', 'Cavity', 'Catch'])
T, F = True, False
P[T, T, T] = 0.108
P[T, T, F] = 0.012
P[F, T, T] = 0.072
P[F, T, F] = 0.008
P[T, F, T] = 0.016
P[T, F, F] = 0.064
P[F, F, T] = 0.144
P[F, F, F] = 0.576

# Compute P(Cavity|Toothache=T)  (see the text, page 493).
PC = enumerate_joint_ask('Cavity', {'Toothache': T}, P)
print(PC.show_approx())

# Exercise 4.1

# b) i)
# P(Cavity | catch) = P(Cavity and catch) / P(catch)
# P(Cavity | catch) = (.108 + .072) / (.108 + .016 + .072 + .144)
# P(Cavity | catch) = .18 / .34 = .529

# b) ii)
# Compute the P(Cavity | catch = T)
PCC = enumerate_joint_ask('Cavity', {'Catch': T}, P)
print(PCC.show_approx())

# c)
Ejemplo n.º 13
0
from probability import JointProbDist, enumerate_joint_ask

# The Joint Probability Distribution Fig. 13.3 (from AIMA Python)
P = JointProbDist(['Toothache', 'Cavity', 'Catch'])
T, F = True, False
P[T, T, T] = 0.108
P[T, T, F] = 0.012
P[F, T, T] = 0.072
P[F, T, F] = 0.008
P[T, F, T] = 0.016
P[T, F, F] = 0.064
P[F, F, T] = 0.144
P[F, F, F] = 0.576

# Compute P(Cavity|Toothache=T)  (see the text, page 493).
PC = enumerate_joint_ask('Cavity', {'Toothache': T}, P)
print(PC.show_approx())

# Exercise 4.1b
# P(Cavity|catch) = P(Cavity^catch) / P(catch) = (0.108 + 0.072) / (0.108 + 0.072 + 0.016 + 0.144) = 0.529...
p_cavity_given_catch = (0.108 + 0.072) / (0.108 + 0.072 + 0.016 + 0.144)
print(p_cavity_given_catch)
PCC = enumerate_joint_ask('Cavity', {'Catch': T}, P)
print(PCC.show_approx())

# Exercise 4.1c
# coins
P = JointProbDist(['coin_1', 'coin_2'])
H, T = True, False
P[H, H] = 0.25
P[H, T] = 0.25
Ejemplo n.º 14
0
from probability import JointProbDist, enumerate_joint_ask

# The Joint Probability Distribution Fig. 13.3 (from AIMA Python)
P = JointProbDist(['Toothache', 'Cavity', 'Catch'])
T, F = True, False
P[T, T, T] = 0.108
P[T, T, F] = 0.012
P[F, T, T] = 0.072
P[F, T, F] = 0.008
P[T, F, T] = 0.016
P[T, F, F] = 0.064
P[F, F, T] = 0.144
P[F, F, F] = 0.576

# Compute P(Cavity|Toothache=T)  (see the text, page 493).
PC = enumerate_joint_ask('Cavity', {'Toothache': T}, P)

# Compute P(Cavity | catch)
ProbCatch = enumerate_joint_ask('Cavity', {'Catch': T}, P)
print("Probability of a Cavity when there is a Toothache:")
print(PC.show_approx())
'''
P( Cavity | catch ) = P(Cavity && Catch) / P(Catch)

P(Cavity && Catch) = .108 + .072
P(Catch) = .108 + .016 + .072 + .144

P(Cavity && Catch) / P(Catch) = (.108 + .072) / (.108 + .016 + .072 + .144)

= .529
Ejemplo n.º 15
0
Archivo: lab_1.py Proyecto: agg9/cs344
@version Feb 21, 2013
'''

"""
4.1

Value of P(Cavity|catch): . 529
    math: P(a ^ b) / P(b)
        (.108 +.072)  / (.108+.072+.016+.144)

P(Coin2 | Coin1 =heads):
    True: .05       False: .05

    -Yes this confirmed by belief that flipping coins is a 50/50 chance.
"""



from probability import JointProbDist, enumerate_joint_ask

# The Joint Probability Distribution Fig. 13.3 (from AIMA Python)
P = JointProbDist(['Coin1', 'Coin2'])
Heads, Tails = True, False
P[Heads, Heads] = 0.25; P[Tails, Tails] = 0.25
P[Heads, Tails] = 0.25; P[Tails, Heads] = 0.25


PC = enumerate_joint_ask('Coin2', {'Coin1': Heads}, P)
print(PC.show_approx())