<jupyter_start><jupyter_text>6 Switching to Pyro (a) Use pyro to reimplement the Bayesian network with parameter values you fitted in question 3. Use default iss values and round parameter estimates to 2 decimal places. Show source code.<jupyter_code>import matplotlib.pyplot as plt
import numpy as np
import torch
import pyro
import pyro.distributions as dist

pyro.set_rng_seed(101)
def bn():
    age = pyro.sample("A", dist.Categorical(torch.tensor([0.36, 0.16,0.48])))
    sex = pyro.sample("S", dist.Categorical(torch.tensor([0.55, 0.45])))
    edu_probs = torch.tensor([[[0.64,0.36],[0.84,0.16],[0.16,0.84]],[[0.72,0.28],[0.89,0.11],[0.81,0.19]]])
    education = pyro.sample("E", dist.Categorical(edu_probs[sex][age]))
    occ_probs = torch.tensor([[0.98,0.02],[0.97,0.03]])
    occupation = pyro.sample("O", dist.Categorical(occ_probs[education]))
    res_probs = torch.tensor([[0.72,0.28],[0.94,0.06]])
    residence = pyro.sample("R", dist.Categorical(res_probs[education]))
    trav_probs = torch.tensor([[[0.71,0.14,0.15],[0.69,0.16,0.15]],[[0.55,0.08,0.37],[0.73,0.25,0.02]]])
    travel = pyro.sample("T", dist.Categorical(trav_probs[residence][occupation]))<jupyter_output><empty_output><jupyter_text>(b) You observe a person with a university degree. What is your prediction of this person’s means of travel? Provide either a MAP estimate or a histogram of the marginal on the variable “T”.<jupyter_code>conditioned_bn1 = pyro.condition(bn, data={'E':torch.tensor(1)})
posterior = pyro.infer.Importance(conditioned_bn1, num_samples=1000).run()
marginal = pyro.infer.EmpiricalMarginal(posterior, "T") 
travel_samples = np.array([marginal().item() for _ in range(1000)])
t_unique, t_counts = np.unique(travel_samples,return_counts=True)
plt.xlabel("Travel Method")
plt.ylabel("Count")
plt.bar(t_unique, [t/1000 for t in t_counts]) 
plt.yticks([0.0,0.2,0.4,0.6, 0.8,1.0])
plt.xticks(ticks=t_unique,labels=["Car", "Other", "Train"])
plt.title("Travel|Education=University")
plt.show()<jupyter_output><empty_output><jupyter_text>(c) You observe a self-employed person who lives in a big city. What is your prediction of this person’s age? Provide either a MAP estimate or a histogram of the marginal on the variable “A”.<jupyter_code>conditioned_bn2 = pyro.condition(bn, data={'O':torch.tensor(1),'R':torch.tensor(0)})
posterior2 = pyro.infer.Importance(conditioned_bn2, num_samples=1000).run()
marginal2 = pyro.infer.EmpiricalMarginal(posterior2, "A") 
age_samples = np.array([marginal2().item() for _ in range(1000)])
a_unique, a_counts = np.unique(age_samples,return_counts=True)
plt.xlabel("Age")
plt.ylabel("Count")
plt.bar(a_unique, [a*.001 for a in a_counts]) 
plt.xticks(ticks=a_unique,labels=["Adult", "Old", "Young"])
plt.yticks([0.0,0.2,0.4,0.6, 0.8,1.0])
plt.title("Age|Occupation=Self-employed, Residence=Big city");<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17317073170731706, 'fraction_numerical': 0.06097560975609756, 'mean_word_length': 6.286802030456853, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '215621', 'license': 'MIT', 'n_tokens_mistral': 1206, 'n_tokens_neox': 1084, 'n_words': 212, 'path': 'HW/hw1_answers/hw1_pyro.ipynb', 'repo_name': 'smruthiramesh/causalML', 'size': '2870'}