Recall the globe tossing model from the chapter. Compute and plot the grid approximate posterior distribution for each of the following sets of observations. In each case, assume a uniform prior for p. (1) W, W, W (2) W, W, W, L (3) L, W, W, L, W, W, W
W, W, W
# define grid
p_grid <- seq(from = 0, to = 1, length.out = 20)
# define prior, assume we don't know everything about population so we make an assumptions fairly as much possibly
prior <- rep(1, times = 20)
# compute likelihood at each value in grid
likelihood <- dbinom(3, size = 3, prob = p_grid)
# compute product of likelihood and prior
unstd_posterior <- likelihood * prior
# standardize the posterior, so it sums to 1
posterior <- unstd_posterior/sum(unstd_posterior)
plot(p_grid, posterior, type = "b",
xlab = "Proportion of water", ylab = "Posterior probability")
mtext("W, W, W")W, W, W, L
# define p_grid
p_grid <- seq(from = 0, to = 1, length.out = 20)
# define prior, assume we don't know everything about distribution of p_grid so we make an assumption fairly as much possibly
prior <- rep(1, times = 20)
# compute likelihood at each value in grid
likelihood <- dbinom(x = 3, size = 4, prob = p_grid)
# compute product of likelihood and prior
unstd_posterior <- likelihood * prior
# standardize the posterior, so it sums to 1
posterior <- unstd_posterior / sum(unstd_posterior)
plot(p_grid, posterior, type = "b",
xlab = "Proportion of water", ylab = "Posterior probability")
mtext("W, W, W, L")L, W, W, L, W, W, W
n_water <- 5
n_land <- 2
n_sample <- n_water + n_land
# define p_grid -> all possible value of p
p_grid <- seq(from = 0, to = 1, length.out = 50)
# define prior, assume we dont know everything about distribution of p_grid, so we make an assumption fairly as much possibly
prior <- rep(1, 50)
# compute likelihood by dbinom
likelihood <- dbinom(x = n_water, size = n_sample, prob = p_grid)
# compute posterior by product of likelihood and prior
unstd_posterior <- likelihood * prior
# standardize posterior
posterior <- unstd_posterior / sum(unstd_posterior)
plot(p_grid, posterior, type = "b",
xlab = "Proportion of water", ylab = "Posterior probability")
mtext("L, W, W, L, W, W, W")Now assume a prior for p that is equal to zero when p < 0.5 and is a positive constant when p ≥ 0.5. Again compute and plot the grid approximate posterior distribution for each of the sets of observations in the problem just above.
W, W, W
# define p_grid
p_grid <- seq(from = 0, to = 1, length.out = 50)
# define prior
prior <- ifelse(p_grid < 0.5, 0, 1)
# compute likelihood
likelihood <- dbinom(x = 3, size = 3, prob = p_grid)
# compute unstandardized posterior by product of likelihood and prior
unstd_posterior <- likelihood * prior
# compute standardized posterior
posterior <- unstd_posterior / sum(unstd_posterior)
plot(p_grid, posterior, type = "b",
xlab = "Proportion of water", ylab = "Posterior probability")
mtext("W, W, W")W, W, W, L
# define p_grid
p_grid <- seq(from = 0, to = 1, length.out = 100)
# define prior
prior <- ifelse(p_grid < 0.5, 0, 2)
# compute likelihood
likelihood <- dbinom(x = 3, size = 4, prob = p_grid)
# compute unstandardized posterior by product of likelihood and prior
unstd_posterior <- likelihood * prior
# compute standardized posterior
posterior <- unstd_posterior / sum(unstd_posterior)
plot(p_grid, posterior, type = "b",
xlab = "Proportion of water", ylab = "Posterior probability")
mtext("W, W, W")L, W, W, L, W, W, W
n_water <- 5
n_land <- 2
n_sample <- n_water + n_land
# define p_grid -> all possible value of p
p_grid <- seq(from = 0, to = 1, length.out = 100)
# define prior, assume we dont know everything about distribution of p_grid, so we make an assumption fairly as much possibly
prior <- ifelse(p_grid < 0.5, 0, 2)
# compute likelihood by dbinom
likelihood <- dbinom(x = n_water, size = n_sample, prob = p_grid)
# compute posterior by product of likelihood and prior
unstd_posterior <- likelihood * prior
# standardize posterior
posterior <- unstd_posterior / sum(unstd_posterior)
plot(p_grid, posterior, type = "b",
xlab = "Proportion of water", ylab = "Posterior probability")
mtext("L, W, W, L, W, W, W")Overall, the index of maximum value of posterior is the same with problem 1, the only difference that the value of posterior, because the prior shrinks so the posterior shrinks too, narrower and taller.
Suppose there are two globes, one for Earth and one for Mars. The Earth globe is 70% covered in water. The Mars globe is 100% land. Further suppose that one of these globes—you don’t know which—was tossed in the air and produced a “land” observation. Assume that each globe was equally likely to be tossed. Show that the posterior probability that the globe was the Earth, conditional on seeing “land” (Pr(Earth|land)), is 0.23.
\[ \text{Pr(Earth | land)} = \frac{\text{Pr(land|Earth) Pr(Earth)}}{\sum} = \frac{0.3*1/2}{0.3*1/2+1*1/2} = 0.23 \]
# proportion tossing result Water given globe is Earth
p_wE <- 0.7
# proportion tossing result Land given globe is Earth
p_lE <- 1 - p_wE
# proportion tossing result Land given globe is Mars
p_lM <- 1
# prior of 2 globe, equally
prior <- rep(1, 2)
likelihood <- c(p_lE, p_lM)
unstd_posterior <- likelihood * prior
posterior <- unstd_posterior / sum(unstd_posterior)
posterior## [1] 0.2307692 0.7692308
Suppose you have a deck with only three cards. Each card has two sides, and each side is either black or white. One card has two black sides. The second card has one black and one white side. The third card has two white sides. Now suppose all three cards are placed in a bag and shuffled. Someone reaches into the bag and pulls out a card and places it flat on a table. A black side is shown facing up, but you don’t know the color of the side facing down. Show that the probability that the other side is also black is 2/3. Use the counting method (Section 2 of the chapter) to approach this problem. This means counting up the ways that each card could produce the observed data (a black side facing up on the table).
# likelihood of Black side is shown facing up
card_BB <- 2
card_BW <- 1
card_WW <- 0
# prior
prior <- rep(1, 3)
# likelihood
likelihood <- c(card_BB, card_BW, card_WW)
# unstandarized posterior
unstd_posterior <- likelihood * prior
# posterior
posterior <- unstd_posterior / sum(unstd_posterior)
posterior## [1] 0.6666667 0.3333333 0.0000000
Now suppose there are four cards: B/B, B/W, W/W, and another B/B. Again suppose a card is drawn from the bag and a black side appears face up. Again calculate the probability that the other side is black.
Notice in this case prior of card_BB changed
# likelihood of Black side is shown facing up
card_BB <- 2
card_BW <- 1
card_WW <- 0
# prior
prior <- rep(c(2, 1), times = c(1, 2))
# likelihood
likelihood <- c(card_BB, card_BW, card_WW)
# unstandarized posterior
unstd_posterior <- likelihood * prior
# posterior
posterior <- unstd_posterior / sum(unstd_posterior)
posterior## [1] 0.8 0.2 0.0
Imagine that black ink is heavy, and so cards with black sides are heavier than cards with white sides. As a result, it’s less likely that a card with black sides is pulled from the bag. So again assume there are three cards: B/B, B/W, and W/W. After experimenting a number of times, you conclude that for every way to pull the B/B card from the bag, there are 2 ways to pull the B/W card and 3 ways to pull the W/W card. Again suppose that a card is pulled and a black side appears face up. Show that the probability the other side is black is now 0.5. Use the counting method, as before.
Notice in this case prior of all cards changed
# likelihood of Black side is shown facing up
card_BB <- 2
card_BW <- 1
card_WW <- 0
# prior
prior <- rep(c(1, 2, 3), times = 1)
# likelihood
likelihood <- c(card_BB, card_BW, card_WW)
# unstandarized posterior
unstd_posterior <- likelihood * prior
# posterior
posterior <- unstd_posterior / sum(unstd_posterior)
posterior## [1] 0.5 0.5 0.0
Assume again the original card problem, with a single card showing a black side face up. Before looking at the other side, we draw another card from the bag and lay it face up on the table. The face that is shown on the new card is white. Show that the probability that the first card, the one showing a black side, has black on its other side is now 0.75. Use the counting method, if you can. Hint: Treat this like the sequence of globe tosses, counting all the ways to see each observation, for each possible first card.
Notice in this case I use Bayesian updating
Because the order of tossing is not matter, so we compute posterior for tossing first card is white. And then use 1 - this value as prior for tossing second card is black.
# likelihood of White side is shown facing up
W_card_BB <- 0
W_card_BW <- 1
W_card_WW <- 2
# prior
prior <- rep(1, 3)
# likelihood
likelihood <- c(W_card_BB, W_card_BW, W_card_WW)
# unstandarized posterior
unstd_posterior <- likelihood * prior
# posterior
posterior <- unstd_posterior / sum(unstd_posterior)
# likelihood of Black side is shown facing up
B_card_BB <- 2
B_card_BW <- 1
B_card_WW <- 0
# prior <- 1 - before posterior (because we draw without replacement)
new_prior <- 1-posterior
# new likelihood
new_likelihood <- c(B_card_BB, B_card_BW, B_card_WW)
# new unstandarized posterior
new_unstd_posterior <- new_likelihood * new_prior
# new posterior
new_posterior <- new_unstd_posterior / sum(new_unstd_posterior)
new_posterior## [1] 0.75 0.25 0.00
Suppose there are two species of panda bear. Both are equally common in the wild and live in the same places. They look exactly alike and eat the same food, and there is yet no genetic assay capable of telling them apart. They differ however in their family sizes. Species A gives birth to twins 10% of the time, otherwise birthing a single infant. Species B births twins 20% of the time, otherwise birthing singleton infants. Assume these numbers are known with certainty, from many years of field research.
Now suppose you are managing a captive panda breeding program. You have a new female panda of unknown species, and she has just given birth to twins. What is the probability that her next birth will also be twins?
twins_A <- 0.1
twins_B <- 0.2
# prior
prior <- rep(1, 2)
# likelihood
likelihood <- c(twins_A, twins_B)
# unstandarized posterior
unstd_posterior <- likelihood * prior
# posterior
posterior <- unstd_posterior / sum(unstd_posterior)
# new prior
new_prior <- posterior
# likelihood
likelihood <- c(twins_A, twins_B)
# unstandarized posterior
unstd_posterior <- likelihood * new_prior
# prob
prob_next_birth_be_twins <- sum(unstd_posterior)
prob_next_birth_be_twins## [1] 0.1666667
Recall all the facts from the problem above. Now compute the probability that the panda we have is from species A, assuming we have observed only the first birth and that it was twins.
twins_A <- 0.1
twins_B <- 0.2
# prior
prior <- rep(1, 2)
# likelihood
likelihood <- c(twins_A, twins_B)
# unstandarized posterior
unstd_posterior <- likelihood * prior
# posterior
posterior <- unstd_posterior / sum(unstd_posterior)
posterior## [1] 0.3333333 0.6666667
Continuing on from the previous problem, suppose the same panda mother has a second birth and that it is not twins, but a singleton infant. Compute the posterior probability that this panda is species A.
twins_A <- 0.1
twins_B <- 0.2
# prior
prior <- rep(1, 2)
# likelihood
twins_likelihood <- c(twins_A, twins_B)
# unstandarized posterior
unstd_posterior <- likelihood * prior
# posterior
posterior <- unstd_posterior / sum(unstd_posterior)
# new prior
new_prior <- posterior
# likelihood
sing_likelihood <- 1 - twins_likelihood
# unstandarized posterior
unstd_posterior <- sing_likelihood * new_prior
# posterior
posterior <- unstd_posterior / sum(unstd_posterior)
posterior## [1] 0.36 0.64
A common boast of Bayesian statisticians is that Bayesian inference makes it easy to use all of the data, even if the data are of different types.
So suppose now that a veterinarian comes along who has a new genetic test that she claims can identify the species of our mother panda. But the test, like all tests, is imperfect. This is the information you have about the test:
The probability it correctly identifies a species A panda is 0.8.
The probability it correctly identifies a species B panda is 0.65.
The vet administers the test to your panda and tells you that the test is positive for species A. First ignore your previous information from the births and compute the posterior probability that your panda is species A. Then redo your calculation, now using the birth data as well.
Solution from https://sr2-solutions.wjakethompson.com/index.html
# use Bayes' Theorem to determine the probability of species A, given a positive
# test
p_ap <- (0.8 * 0.5) / ((0.5 * 0.8) + (0.5 * 0.35))
# Now include test data with observed births
# likelihood for each species is Pr(twins) * Pr(singleton)
a_likelihood <- 0.1 * (1 - 0.1)
b_likelihood <- 0.2 * (1 - 0.2)
# compute posterior probabilities, using test result as prior
likelihood <- c(a_likelihood, b_likelihood)
prior <- c(p_ap, (1 - p_ap))
posterior <- likelihood * prior
posterior <- posterior / sum(posterior)
posterior## [1] 0.5625 0.4375