hw2 problem 5

This commit is contained in:
snoopy0328
2025-02-20 15:26:52 -05:00
parent a5ab1314f5
commit 70ebbd3759
2 changed files with 108 additions and 0 deletions
+67
View File
@@ -0,0 +1,67 @@
import numpy as np
# SIMULATION CALCULATIONS
def simulate_culprit(N, pSuperman):
return np.random.rand(N) < pSuperman
def simulate_crumbs(N, supermanProb, batmanProb, culprit):
randomDraw = np.random.rand(N, 3)
supermanCrumbs = (randomDraw < supermanProb)
batmanCrumbs = (randomDraw < batmanProb)
return np.where(culprit[:, None], supermanCrumbs, batmanCrumbs)
def combination(crumbResults, culprit):
combinations = {}
for couch in [False, True]:
for kitchen in [False, True]:
for gym in [False, True]:
for culprit_label, culprit_val in [("Superman", True), ("Batman", False)]:
mask = (crumbResults[:, 0] == couch) & (crumbResults[:, 1] == kitchen) & (crumbResults[:, 2] == gym) & (culprit == culprit_val)
combinations[(couch, kitchen, gym, culprit_label)] = np.sum(mask)
return combinations
def print_probabilities(combinations, N):
print("Couch:\t Kitchen: Gym:\t Culprit:")
for k, v in combinations.items():
couch, kitchen, gym, culprit_label = k
#formatting
couch_str = 'True ' if couch else 'False'
kitchen_str = 'True ' if kitchen else 'False'
gym_str = 'True ' if gym else 'False'
if culprit_label == 'Batman': culprit_label = 'Batman '
print(f"{couch_str}\t {kitchen_str}\t {gym_str} {culprit_label}: {(v / N) * 100:.2f}%")
# ANALYTIC CALCULATIONS
def analytic_probabilities(pSuperman, pBatman, supermanProb, batmanProb):
combinations = {}
for couch in [False, True]:
for kitchen in [False, True]:
for gym in [False, True]:
#Superman
prob_superman = pSuperman
prob_superman *= supermanProb[0] if couch else (1 - supermanProb[0])
prob_superman *= supermanProb[1] if kitchen else (1 - supermanProb[1])
prob_superman *= supermanProb[2] if gym else (1 - supermanProb[2])
combinations[(couch, kitchen, gym, "Superman")] = prob_superman
#Batman
prob_batman = pBatman
prob_batman *= batmanProb[0] if couch else (1 - batmanProb[0])
prob_batman *= batmanProb[1] if kitchen else (1 - batmanProb[1])
prob_batman *= batmanProb[2] if gym else (1 - batmanProb[2])
combinations[(couch, kitchen, gym, "Batman")] = prob_batman
return combinations
def print_analytic_probabilities(combinations):
print("Couch:\t Kitchen: Gym:\t Culprit:")
for k, v in combinations.items():
couch, kitchen, gym, culprit_label = k
#formatting
couch_str = 'True ' if couch else 'False'
kitchen_str = 'True ' if kitchen else 'False'
gym_str = 'True ' if gym else 'False'
if culprit_label == 'Batman': culprit_label = 'Batman '
print(f"{couch_str}\t {kitchen_str}\t {gym_str} {culprit_label}: {v * 100:.2f}%")
+41
View File
@@ -0,0 +1,41 @@
from calculateHW2P5 import simulate_culprit, simulate_crumbs, combination, print_probabilities, analytic_probabilities, print_analytic_probabilities
import numpy as np
if __name__ == "__main__":
#N = 100000 #used while testing
NSize = [1000, 10000, 100000]
#Priors for each suspect
pSuperman = 0.5
pBatman = 0.5
#Likelihoods of crumbs on each location
supermanProb = np.array([0.3, 0.7, 0.2])
batmanProb = np.array([0.4, 0.6, 0.3])
#Simulate
'''
culprit = simulate_culprit(N, pSuperman)
crumbResults = simulate_crumbs(N, supermanProb, batmanProb, culprit)
print("Simulation:")
print_probabilities(combination(crumbResults, culprit), N)
'''
print("Simulation:")
for N in NSize:
print(f"N = {N}")
# Simulate culprit and crumbs
culprit = simulate_culprit(N, pSuperman)
crumbResults = simulate_crumbs(N, supermanProb, batmanProb, culprit)
# Count combinations and print probabilities
print("Simulated Probabilities:")
print_probabilities(combination(crumbResults, culprit), N)
print("\n")
#Analytic
print("Analytic:")
print_analytic_probabilities(analytic_probabilities(pSuperman, pBatman, supermanProb, batmanProb))
'''
As the value of N increases, the simulated probabilities get closer to the analytic probabilities.
'''