Chapter 2 Small Worlds and Large Worlds

Exercises from Chapter 2 of the book.

library(tidyverse)
library(patchwork)

2M1. 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

There are three datasets for this problem. Let’s store them in a list for ease of work below.

data_1 <- c("W", "W", "W")
data_2 <- c("W", "W", "W", "L")
data_3 <- c("L", "W", "W", "L", "W", "W", "W")
data_lst <- list(data_1, data_2, data_3)
names(data_lst) <- c("data_1", "data_2", "data_3")
# define grid same for all
p_grid <- seq(from = 0, to = 1, length.out = 1000)

# define prior same for all
prob_p <- rep(1, 1000)

#compute posterior 

posterior <- function(data, p_grid, prob_p, plot = TRUE) {
  
# compute likelihood at each value in grid
prob_data <- dbinom( 
  sum(data == "W"), 
  size=length(data) , 
  prob=p_grid )
# compute product of likelihood and prior
posterior <- prob_data * prob_p
# standardize the posterior, so it sums to 1
posterior <- posterior / sum(posterior)

data_for_plot <- data.frame(param = p_grid, posterior = posterior)

if (plot) {
 p <-  ggplot(data_for_plot) + 
   aes(x = param, y = posterior ) + 
   geom_line() +
   labs(x = "Proportion of Water", y = "Posterior Probability") + 
   theme_minimal()
 p
} else {
  posterior
}

}


plots <- lapply(data_lst, posterior, p_grid, prob_p)

plots[[1]]+ plots[[2]]+ plots[[3]]

2M2. 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.

Exercise 2M2 is basically the same as 2M1. The only difference is the prior. Let’s try it with a more of a tidyverse solution.

data_for_plots <-
  tibble(p_grid = seq(from = 0, to = 1, length.out = 1000)) %>%     
  mutate(prior = if_else(p_grid < 0.5, 0, 1)) %>% 
  mutate(data_1 = dbinom(sum(data_1 == "W"), size=length(data_1), prob = p_grid)) %>%
  mutate(data_2 = dbinom(sum(data_2 == "W"), size=length(data_2), prob = p_grid)) %>%
  mutate(data_3 = dbinom(sum(data_3 == "W"), size=length(data_3), prob = p_grid)) %>%
  pivot_longer(
    cols = starts_with("data"),
    names_to = "data",
    values_to = "observations"
  ) %>% 
  mutate(unstd_posterior = observations * prior) %>%           
  mutate(posterior = unstd_posterior / sum(unstd_posterior))



ggplot(data_for_plots) +
  aes(x = p_grid, y = posterior) +
  geom_line() +
  labs(x = "Proportion of Water", y = "Posterior Probability") +  
  theme_minimal() +
  facet_wrap(vars(data), nrow = 1)

2M3. 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.1

\(Pr(Earth|land) = 0.23\)

\(Pr(Earth) = Pr(Mars) = 0.5\)

\(Pr(land|Earth) = 1 - 0.7 = 0.3\)

\(Pr(land|Mars) = 1\)

Bayes theorem:

\(Pr(Earth|land) = Pr(land|Earth) * Pr(Earth) / Pr(land|Earth) * Pr(Earth) + Pr(land|Mars) * Pr(Mars)\)

pr_earth_given_land <- 0.3 * 0.5 / (0.3 * 0.5 + 1 * 0.5)

pr_earth_given_land
## [1] 0.2307692

2M4. 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).

bb_ways <- 2
wb_ways <- 1
ww_ways <- 0
data <- c(bb_ways, wb_ways, ww_ways)
prior <- c(1, 1, 1)
posterior <- prior * data
posterior <- posterior / sum(posterior)
posterior[1] == 2 / 3
## [1] TRUE

2M5. 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.

bb_ways <- 2
wb_ways <- 1
ww_ways <- 0
data <- c(bb_ways, wb_ways, ww_ways)
prior <- c(2, 1, 1)
posterior <- prior * data
posterior <- posterior / sum(posterior)

posterior[1]
## [1] 0.8

2M6. 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.

bb_ways <- 2
wb_ways <- 1
ww_ways <- 0
data <- c(bb_ways, wb_ways, ww_ways)
prior <- c(1, 2, 3)
posterior <- prior * data
posterior <- posterior / sum(posterior)

posterior[1] == 0.5
## [1] TRUE

2M7. 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.

bb_ways <- 2*3
wb_ways <- 1*2
ww_ways <- 0*1
data <- c(bb_ways, wb_ways, ww_ways)
prior <- c(1, 1, 1)
posterior <- prior * data
posterior <- posterior / sum(posterior)

posterior[1] == 0.75
## [1] TRUE

2H1. 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?

Прашањето е, која е веројатноста второто раѓање да е близнаци, кога првото било близнаци, или:

\(Pr(twins2|twins1) = ?\)

Треба да пресметаме:

\(Pr(twins2|twins1) = Pr(twins1, twins2) / Pr(twins)\)

Знаме дека:

\(Pr(twins|A) = 0.1\)

\(Pr(twins|B) = 0.2\)

\(Pr(A) = Pr(B) = 0.5\)

Треба да пресметаме и:

\(Pr(twins) = Pr(twins|A)*Pr(A) + Pr(twins|B)*Pr(B)\)

$Pr(twins1, twins2) = Pr(twins|A) * Pr(twins|A) * Pr(A) + Pr(twins|B) * Pr(twins|B) * Pr(B) $

pr_twins <- 0.1 * 0.5 + 0.2 * 0.5

pr_twins1_and_twins2 <- 0.1 * 0.1 * 0.5 + 0.2 * 0.2 * 0.5 

pr_twins_2_given_twins1 <- pr_twins1_and_twins2 / pr_twins

pr_twins_2_given_twins1
## [1] 0.1666667

2H2. 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.

Ова прашање е:

\(Pr(A|twins1) = ?\)

Баезова теорема:

\(Pr(A|twins1) = Pr(twins1|A) * Pr(A) / Pr(twins)\)

Ова може лесно да го пресметаме како:

pr_twins_given_a = 0.1 * 0.5 / pr_twins

pr_twins_given_a
## [1] 0.3333333

Слично, може да креираме пресметка со која одеднаш ќе ги добиеме двете веројатности, т.е.:

\(Pr(A|twins1)\) и \(Pr(B|twins1)\).

p_twins_A <- 0.1
p_twins_B <- 0.2
data_twins <- c(p_twins_A, p_twins_B)
prior <- c(1, 1)
posterior <- prior * data_twins
posterior_2H2 <- posterior / sum(posterior)
names(posterior_2H2) = c("pr_A_twins", "pr_B_twins")
posterior_2H2
## pr_A_twins pr_B_twins 
##  0.3333333  0.6666667

2H3. 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.

Сега прашањето е за веројатноста пандата да е вид А ако родила прво близнаци, па потоа едно мече:

\(Pr(A|single) = ?\)

Овде може да се искористи bayеsian updating бидејќи веќе имаме веројатност за А при раѓање близанци од претходната вежба, што ќе биде prior т.е.:

\(Pr(A|twins) = 0.3\)

Новородената панда всушност ја третираме како нови податоци во пресметката

data_single <- c(1 - p_twins_A, 1 - p_twins_B)
prior_2H3 <- posterior_2H2
posterior <- prior_2H3 * data_single
posterior_2H3 <- posterior / sum(posterior)
names(posterior_2H3) = c("pr_A_single", "pr_B_single")
posterior_2H3
## pr_A_single pr_B_single 
##        0.36        0.64

2H4. 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.

Првото прашање е:

\(Pr(A|testA) = ?\)

Знаеме дека:

\(Pr(testA|A) = 0.8\) \(Pr(testA|B) = 1 - 0.65 = 0.35\) тестот точно предвидува вид B 0.65 од случаите, значи, 0.35 се А.

test_correct_A <- 0.8
test_correct_B <- 0.35
data <- c(test_correct_A, test_correct_B)
prior <- c(1, 1)
posterior <- prior * data
posterior_2H4 <- posterior / sum(posterior)
names(posterior_2H4) = c("panda_is_A", "panda_is_B")
posterior_2H4
## panda_is_A panda_is_B 
##  0.6956522  0.3043478

Тука, слично како претходно, како prior го сметаме резултатот т.е. posterior од пресметката со раѓања.

prior <- posterior_2H3
posterior <- prior * data
posterior_2H4_2 <- posterior / sum(posterior)
names(posterior_2H4_2) = c("panda_is_A", "panda_is_B")
posterior_2H4_2
## panda_is_A panda_is_B 
##     0.5625     0.4375

На крај:

\(Pr(A|testA,twins,single) = 0.56\)

Веројатноста пандата да е вид А при точен тест за вид А, родени близнаци и потоа едно пандиче е околу 0.56.