# Example, prior and posterior beta ---- makepdf = F fname = '../pdftex/img/class20-stopping.pdf' if (makepdf) { wd = 5.5 ht = 3 scale = .8 pdf(fname, width=wd*scale,height= ht*scale) } par(mar=c(3.5,2.5,.4,.3)) txtxpos = .43 txtwd = .02 linewd = .13 cex.txt = 1.1 x = seq(0,1,.01) # Set up limits etc, plot type ='n' plot(c(0,1),c(0,3.5), type='n', mgp=c(2.5,1,0), ylab='', xaxt='n', xlab=expression(theta), cex.lab=2) axis(1, at=c(0,.25, .50, .75, 1.0), labels=c('0','.25','.50','.75','1.0'), cex.axis=1.2) prior.a = 3; prior.b = 3 col = 'cyan' txtht = 3 lines(x,dbeta(x,prior.a, prior.b), col=col, lwd=2) s = sprintf('Prior Beta(%d,%d)', prior.a, prior.b) text(txtxpos, txtht, s, pos=2, cex=cex.txt) lines(c(txtxpos+txtwd,txtxpos+txtwd+linewd),c(txtht,txtht), col=col,lwd=3) posterior.a = 8; posterior.b = 4 col = 'red' txtht = 2.5 lines(x,dbeta(x, posterior.a, posterior.b), col=col, lwd=2) s = sprintf('Posterior Beta(%d,%d)', posterior.a, posterior.b) text(txtxpos, txtht, s, pos=2, cex=cex.txt) lines(c(txtxpos+txtwd,txtxpos+txtwd+linewd),c(txtht,txtht), col=col,lwd=3) # 50% probability interval col = 'blue' probInt.50 = qbeta(c(.25,.75),posterior.a,posterior.b) ht = .15 lines(probInt.50,c(ht,ht), col=col, lwd=3) ht = 0 probInt.90 = qbeta(c(.05,.95),posterior.a,posterior.b) lines(probInt.90,c(ht,ht), col=col, lwd=3) s = sprintf("50%% probabilitly interval [%.2f, %.2f]", probInt.50[1], probInt.50[2]) print(s) s = sprintf("90%% probabilitly interval [%.2f, %.2f]", probInt.90[1], probInt.90[2]) print(s) s = sprintf("P(theta > .5) = %.2f", 1- pbeta(.5,posterior.a,posterior.b)) print(s) if (makepdf) dev.off()