Medium

2M1

Plot the posterior distribution of multiple values of n and w

write the example as a function:

plot_posterior_distribution <- function(w=6, n=9, k=20){
  p_grid = seq(0,1,length=k)
  prior=rep(1,k)
  
  likelyhood = dbinom(w,n,prob=p_grid)
  unst.posterior = likelyhood*prior
  posterior = unst.posterior/sum(unst.posterior)
  
  plot(y=posterior, x=p_grid, type="b")
  mtext(paste0("w=",w,", n=",n))
}

par(mfrow=c(2,2), mar=c(2,3,2,0))

# example values
plot_posterior_distribution()
# a
plot_posterior_distribution(3,3)
# bb
plot_posterior_distribution(3,4)
# c
plot_posterior_distribution(5,7)

2M2

plot_posterior_distribution <- function(w=6, n=9, k=20){
  p_grid = seq(0,1,length=k)
  prior=ifelse(p_grid<0.5,0,2)
  
  likelyhood = dbinom(w,n,prob=p_grid)
  unst.posterior = likelyhood*prior
  posterior = unst.posterior/sum(unst.posterior)
  
  plot(y=posterior, x=p_grid, type="b")
  mtext(paste0("w=",w,", n=",n))
}

par(mfrow=c(2,2), mar=c(2,3,2,0))

# example values
plot_posterior_distribution()
# a
plot_posterior_distribution(3,3)
# b
plot_posterior_distribution(3,4)
# c
plot_posterior_distribution(5,7)

2M3

P_land_Earth = 0.3
P_Earth = 0.5
P_land = 0.5*0.3 + 0.5*1

P_Earth_land = P_land_Earth * P_Earth / P_land
P_Earth_land
## [1] 0.2307692

2M4

The black/black card has 2 ways of giving a black side up, the black/white 1 way and the white/white card 0 ways. Thus, 2/3 probability of being the bb card and 1/3 prob of being the bw card.

2M5

Update the previous question: 2/4 chance of bb card * 2/3 chance of bb card given black side is up 1/4 chance of bw card * 1/3 chance of wb card given black side is up

4/12 / (1/12 + 4/12) = 4/5 = 0.8

2M6

Update 2M4 1 chance of bb card * 2/3 chance of bb card given black side is up 2 chance of bw card * 1/3 chance of wb card given black side is up

2/3 / (2/3 + 2/3) = 1/2

2M7

Chance that first card is black/black, given that first card has black side and second white side.

BB + BW = 2 possible ways (BB card can be either side, BW card is white side up) BB + WW = 4 ways (both can be either side) BW + WW = 2 ways (WW can be either side)

Other combinations have 0 probability. 6/8 combinations have the BB card first -> P=0.75

Hard

2H1

P_twins_A = 0.1
P_twins = mean( c(0.1,0.2) )
P_A = 0.5

P_A_twins = P_twins_A * P_A / P_twins

P_A_twins * 0.1 + (1-P_A_twins)*0.2
## [1] 0.1666667

Better way of notation: (from https://github.com/cavaunpeu/statistical-rethinking/blob/master/chapter-2/homework.R)

A.likelihood <- .1
B.likelihood <- .2
likelihood <- c(A.likelihood, B.likelihood)
prior <- c(1, 1)
unstandardized.posterior <- likelihood * prior
posterior <- unstandardized.posterior / sum(unstandardized.posterior)

# probability next birth is set of twins
posterior[1] * .1 + posterior[2] * .2
## [1] 0.1666667

2H2

This was part of the previous question

P_A_twins
## [1] 0.3333333
P_A_twins == posterior[1]
## [1] TRUE

2H3

Use the posterior from last question as prior. Then incorporate new singleton birth

prior <- posterior

A.likelihood <- .9
B.likelihood <- .8
likelihood <- c(A.likelihood, B.likelihood)

unstandardized.posterior <- likelihood * prior
posterior <- unstandardized.posterior / sum(unstandardized.posterior)
posterior
## [1] 0.36 0.64

2H4

P_PosA_A <- 0.8 # chance of a positive test for A if truly A
P_PosA_B <- 1 - 0.65 # Chance of positive test if actually B

P_A = 0.5
P_PosA = P_A*P_PosA_A + (1-P_A)*P_PosA_B

P_A_PosA = P_PosA_A * P_A / P_PosA
P_A_PosA
## [1] 0.6956522
# Or calculate like this:

likelihood <- c(P_PosA_A, P_PosA_B)
prior <- c(1, 1)
unstandardized.posterior <- likelihood * prior
posterior_2 <- unstandardized.posterior / sum(unstandardized.posterior)

posterior_2
## [1] 0.6956522 0.3043478

Now use posterior from the births as prior with the genetic test to get updated posterior.

prior = posterior # from 2H3

unstandardized.posterior <- likelihood * prior
posterior_3 <- unstandardized.posterior / sum(unstandardized.posterior)

posterior_3
## [1] 0.5625 0.4375