import pyAgrum as gum
import pyAgrum.lib.notebook as gnb

“Out of 1 million children, 990,000 get vaccinated, 9,900 have the reaction, and 99 die from it. Meanwhile, 10,000 don’t get vaccinated, 200 get smallpox, and 40 die from the disease. In summary, more children die from vaccination (99) than from the disease (40).”

“Suppose that out of 1 million children, 99% are vaccinated, and 1% are not. If a child is vaccinated, he or she has one chance in one hundred (1%) of developing a reaction, and the reaction has one chance in one hundred (1%) of being fatal. On the other hand, he or she has no chance of developing smallpox. Meanwhile, if a child is not vaccinated, he or she obviously has zero chance of developing a reaction to the vaccine, but he or she has one chance in fifty (2%) of developing smallpox. Finally, let’s assume that smallpox is fatal in one out of five (20%) cases.”

Red dot

from The Book of Why by Judea Pearl

bn = gum.fastBN("v->r->d<-s<-v")
bn
G d d v v s s v->s r r v->r s->d r->d
# No, Yes --> 0, 1

# Vaccinated (v)
bn.cpt("v").fillWith([0.01, 0.99])

# Reaction (r) to the Vaccine
bn.cpt("r")[{"v": 0}] = [1, 0]
bn.cpt("r")[{"v": 1}] = [0.99, 0.01]

# Smallpox (s)
bn.cpt("s")[{"v": 0}] = [0.98, 0.02]
bn.cpt("s")[{"v": 1}] = [1, 0] 

# Death (d)
bn.cpt("d")[{"r": 0, "s": 0}] = [1, 0]
bn.cpt("d")[{"r": 0, "s": 1}] = [0.8, 0.2]
bn.cpt("d")[{"r": 1, "s": 0}] = [0.99, 0.01]
bn.cpt("d")[{"r": 1, "s": 1}] = [0.79, 0.21]
bn.cpt("s")
s
v
0
1
0
0.9800 0.0200
1
1.0000 0.0000
# “What if we had set the vaccination rate to zero?”
gnb.showInference(bn,evs={"v":0})

n = 1000000

print("Have gotten smallpox:", n * (2/100))
print("Have died with no vaccine:", n * (0.4/100))

# According to the book
print("Died from vaccination (99) + from the disease (40):", 99+40)

print("Comparing 'deaths without vaccine' vs. 'with vaccine':", (n * (0.4/100)) - (99+40))
Have gotten smallpox: 20000.0
Have died with no vaccine: 4000.0
Died from vaccination (99) + from the disease (40): 139
Comparing 'deaths without vaccine' vs. 'with vaccine': 3861.0