# poll 10 people, get 6 yes answers # with a uniform prior, the posterior is Beta(7,5) x=seq(from=0,to=1,by=.001) plot(x,dbeta(x,7,5),type="l") # posterior density plot 7/12 # posterior mean # with a noninformative "Beta(0,0)" prior, the posterior is Beta(6,4) lines(x,dbeta(x,6,4),lty=2) 6/10 # Jeffreys prior is Beta(0.5,0.5), and gives a Beta(6.5,4.5) posterior lines(x,dbeta(x,6.5,4.5),lty=3) 6.5/11 # what if we started with a Beta(1,3) prior? posterior is Beta(7,7) lines(x,dbeta(x,7,7),lty=4) 7/14 # what about a Beta(3,7) prior? posterior is Beta(9,11) lines(x,dbeta(x,9,11),lty=5) 9/20 # now suppose we had polled 100 people and got 60 yes answers # what are the resulting posterior densities and posterior means? plot(x,dbeta(x,61,41),type="l") # Beta(1,1) prior 61/102 lines(x,dbeta(x,60,40),lty=2) # Beta(0,0) 60/100 lines(x,dbeta(x,60.5,40.5),lty=3) # Beta(0.5,0.5) 60.5/101 lines(x,dbeta(x,61,43),lty=4) # Beta(1,3) 61/104 lines(x,dbeta(x,63,47),lty=5) # Beta(3,7) 63/110 # for the cookies example, we counted 855 chips in 39 cookies for an # average of 21.92 chips per cookie # class prior was Gamma(10,1) x=seq(from=.5,to=30,by=.1) x2=seq(from=18,to=26,by=.01) plot(x,dgamma(x,10,1),type="l",ylim=c(0,.55)) likelihood=numeric(length(x2)) for(i in 1:length(x2)) likelihood[i]=prod(dpois(cookies,x2[i])) lines(x2,likelihood*3.5e48) lines(x2,dgamma(x2,10+855,1+39),lty=2) # compare posteriors from different priors plot(x2,dgamma(x2,10+855,1+39),type="l",ylim=c(0,1)) # class Gamma(10,1) prior lines(x2,dgamma(x2,0+855,0+39),lty=2) # noninformative "Gamma(0,0)" prior lines(x2,dgamma(x2,100+855,10+39),lty=3) # more informative class Gamma(100,10) lines(x2,dgamma(x2,220+855,10+39),lty=4) # more informative Gamma(220,10) lines(x2,dgamma(x2,2200+855,100+39),lty=5) #way more informative Gamma(2200,100)