covered p by water N times sim_globe <- function( p=0.7 , N=9 ) { sample(c("W","L"),size=N,prob=c(p,1-p),replace=TRUE) } /PUIJOH IBQQFOT VOUJM XF DBMM UIF GVODUJPO CZ JUT OBNF 3 DPEF sim_globe() [1] "L" "W" "W" "W" "L" "L" "L" "W" "L" 3FQFBU DBMMJOH UIF GVODUJPO UP TFF UIBU JU TJNVMBUFT B EJČFSFOU TBNQMF FBDI UJNF "OE CZ OBNJOH UIF QSPQPSUJPO PG XBUFS p BOE OVNCFS PG UPTTFT N JO UIF GVODUJPO EFĕOJUJPO XF DBO FBTJMZ DIBOHF UIFTF WBMVFT XIFO XF DBMM UIF GVODUJPO Bę 26"-*5: "4463"/$& # function to compute posterior distribution compute_posterior <- function( the_sample , poss=c(0,0.25,0.5,0.75,1) ) { W <- sum(the_sample=="W") # number of W observed L <- sum(the_sample=="L") # number of L observed ways <- sapply( poss , function(q) (q*4)^W * ((1-q)*4)^L ) post <- ways/sum(ways) bars <- sapply( post, function(q) make_bar(q) ) data.frame( poss , ways , post=round(post,3) , bars ) } 5P VTF UIJT GVODUJPO ZPV OFFE UP HJWF JU B TBNQMF "OE XF DBO KVTU FNCFE UIF QSFWJPVT TJNVMBUJPO GVODUJPO JOTJEF JU 3 DPEF compute_posterior( sim_globe() ) poss ways post bars 1 0.00 0 0.000 2 0.25 243 0.291 ###### 3 0.50 512 0.612 ############ 4 0.75 81 0.097 ## 5 1.00 0 0.000 SBę 26"-*5: "4463"/$& # function to compute posterior distribution compute_posterior <- function( the_sample , poss=c(0,0.25,0.5,0.75,1) ) { W <- sum(the_sample=="W") # number of W observed L <- sum(the_sample=="L") # number of L observed ways <- sapply( poss , function(q) (q*4)^W * ((1-q)*4)^L ) post <- ways/sum(ways) bars <- sapply( post, function(q) make_bar(q) ) data.frame( poss , ways , post=round(post,3) , bars ) } 5P VTF UIJT GVODUJPO ZPV OFFE UP HJWF JU B TBNQMF "OE XF DBO KVTU FNCFE UIF QSFWJPVT TJNVMBUJPO GVODUJPO JOTJEF JU 3 compute_posterior( sim_globe() ) poss ways post bars 1 0.00 0 0.000 2 0.25 243 0.291 ###### 3 0.50 512 0.612 ############ 4 0.75 81 0.097 ## 5 1.00 0 0.000 3FQFBU UIJT GVODUJPO DBMM B GFX UJNFT UP TIPX UIBU BT UIF TBNQMF WBSJFT TP UPP EPFT UIF QPTUF SJPS EJTUSJCVUJPO ę 4."-- 803-% 0 0.2 0.4 0.6 0.8 1 proportion water posterior probability 0.00 0.05 0.10 0.15 0.20 0.25 0.30 11 possibilities 1 2 3 4