Question 5: You are hired as a data analyst by politician A. She wants to know the proportion of people in Metrocity who favor her over politician B. From previous poll numbers, you place a Beta(40,60) prior on the proportion. From polling 200 randomly sampled people in Metrocity, you find that 103 people prefer politician A to politician B. What is the posterior probability that the majority of people prefer politician A to politican B (i.e. P(p>0.5|data))?
k=103
n=200
alpha=k+40
beta=60+n-k
1-pbeta(0.5,shape1=alpha,shape2=beta) # P(p>0.5|data) = 1 - P(P<0.5|data)
## [1] 0.209096
Question 6: An engineer has just finished building a new production line for manufacturing widgets. They have no idea how likely this process is to produce defective widgets so they plan to run two separate runs of 15 widgets each. The first run produces 3 defective widgets and the second 5 defective widgets.
We represent our lack of apriori knowledge of the probability of producing a defective widgets, p, using a flat, uninformative prior -Beta(1,1). What should the posterior distribution of p be after the first run is finished? And after the second?
k1=3
n1=15
alpha1=k1+1
beta1=1+n1-k1
print(c(alpha1,beta1))
## [1] 4 13
k2=5
n2=15
alpha2=k2+alpha1
beta2=beta1+n2-k2
print(c(alpha2,beta2))
## [1] 9 23