This module implements a simple classroom example of probabilistic inference over the full joint distribution specified by AIMA, Figure 13.3. It is based on the code from AIMA probability.py. Added the joint probability distribution for 2 coin flips. @author: kvlinden @edited: ees32 @date: 02-28-20 @version Jan 1, 2013 ''' 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 ''' b. Hand calculation: P(cavity | catch) = P(cavity & catch) / P(catch)
''' This module implements a simple classroom example of probabilistic inference over the full joint distribution specified by AIMA, Figure 13.3. It is based on the code from AIMA probability.py. @author: kvlinden & Jason Pruim @version Jan 1, 2013 ''' import sys sys.path.insert(0, "/home/jrp27/Documents/cs344-code/tools/aima") sys.path.insert(0, "/home/jrp27/Documents/cs344-code/tools/paip") 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()) """
''' ''' Copied from joint.py This module implements a simple classroom example of probabilistic inference over the full joint distribution specified by AIMA, Figure 13.3. It is based on the code from AIMA probability.py. @author: kvlinden @version Jan 1, 2013 ''' from probability import JointProbDist, enumerate_joint_ask # The Joint Probability Distribution Fig. 13.3 (from AIMA Python) P = JointProbDist(['Toothache', 'Cavity', 'Catch', 'Rain']) T, F = True, False P[T, T, T, T] = 0.054 P[T, T, F, T] = 0.006 P[F, T, T, T] = 0.036 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
''' 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.
""" Exercise 4.1 a. joint.py computes the probability distrubtion for Cavity, given that toothache = True. It computes this by using the AIMA class that allows it to calculate the conditional probability from the table. In this case, it would do that by (P being the probability, ProbDist being the distribution): P(Cavity|toothache) = P(Cavity ^ toothache) / P(toothache) = (0.108+0.012)/(0.108+0.012+0.016+0.064) Therefore, the distribution is as the code outputs, with False: 0.4, True: 0.6 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"])
''' lab04 @author: Enoch Mwesigwa @version March 1, 2020 ''' from probability import JointProbDist, enumerate_joint_ask #### EXERCISE 4.2A ### P = JointProbDist(['Toothache', 'Cavity', 'Catch', "Rain"]) T, F = True, False # P(rain) = .1 P(¬ rain) = .9 P[T, T, T, T] = 0.0108; P[T, T, T, F] = 0.0972 P[T, T, F, T] = 0.0012; P[T, T, F, F] = 0.0108 P[F, T, T, T] = 0.0072; P[F, T, T, F] = 0.0648 P[F, T, F, T] = 0.0008; P[F, T, F, F] = 0.0072 P[T, F, T, T] = 0.0016; P[T, F, T, F] = 0.0144 P[T, F, F, T] = 0.0064; P[T, F, F, F] = 0.0576 P[F, F, T, T] = 0.0144; P[F, F, T, F] = 0.1296 P[F, F, F, T] = 0.0576; P[F, F, F, F] = 0.5184 ''' i. There twice as many entries as before: 16 ii. Yes they do. A Probability distribution tables display every possible outcome with the probability of it occuring. By that definition, these
from probability import JointProbDist, enumerate_joint_ask 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|Catch=T) PC = enumerate_joint_ask('Cavity', {'Catch': T}, P) print(PC.show_approx()) P = JointProbDist(['Coin 1', 'Coin 2']) P[T, T] = 0.25 P[T, F] = 0.25 P[F, T] = 0.25 P[F, F] = 0.25 # Compute P(Cavity|Catch=T) PC = enumerate_joint_ask('Coin 2', {'Coin 1': T}, P) print(PC.show_approx()) # Computer P(Cavity|CatchT) by hand # P(Cavity|Catch)=P(Cavity^Catch)/P(Catch)=(.108+.072)/(.108+.072+.016+.144)=.529
''' Exercise 4.1 ''' 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())