Chapter 2 - Large Worlds and Small Worlds

The objectives of this problem set is to work with the conceptual mechanics of Bayesian data analysis. The target of inference in Bayesian inference is a posterior probability distribution. Posterior probabilities state the relative numbers of ways each conjectured cause of the data could have produced the data. These relative numbers indicate plausibilities of the different conjectures. These plausibilities are updated in light of observations through Bayesian updating.

Place each answer inside the code chunk (grey box). The code chunks should contain a text response or a code that completes/answers the question or activity requested. Make sure to include plots if the question requests them. Problems are labeled Easy (E), Medium (M), and Hard(H).

Finally, upon completion, name your final output .html file as: YourName_ANLY505-Year-Semester.html and publish the assignment to your R Pubs account and submit the link to Canvas. Each question is worth 5 points

Questions

2E1. Which of the expressions below correspond to the statement: the probability of rain on Monday? (1) Pr(rain) (2) Pr(rain|Monday) (3) Pr(Monday|rain) (4) Pr(rain, Monday)/ Pr(Monday)

# (2) Pr(rain|Monday) and (4) Pr(rain, Monday)/ Pr(Monday) correspond to the statement: the probability of rain on Monday

2E2. Which of the following statements corresponds to the expression: Pr(Monday|rain)? (1) The probability of rain on Monday. (2) The probability of rain, given that it is Monday. (3) The probability that it is Monday, given that it is raining. (4) The probability that it is Monday and that it is raining.

# (3) The probability that it is Monday, given that it is raining corresponds to the expression: Pr(Monday|rain)

2E3. Which of the expressions below correspond to the statement: the probability that it is Monday, given that it is raining? (1) Pr(Monday|rain) (2) Pr(rain|Monday) (3) Pr(rain|Monday) Pr(Monday) (4) Pr(rain|Monday) Pr(Monday)/ Pr(rain) (5) Pr(Monday|rain) Pr(rain)/ Pr(Monday)

# (1) Pr(Monday|rain) and (4) Pr(rain|Monday) Pr(Monday)/ Pr(rain) correspond to the statement: the probability that it is Monday, given that it is raining

2E4. The Bayesian statistician Bruno de Finetti (1906–1985) began his 1973 book on probability theory with the declaration: “PROBABILITY DOES NOT EXIST.” The capitals appeared in the original, so I imagine de Finetti wanted us to shout this statement. What he meant is that probability is a device for describing uncertainty from the perspective of an observer with limited knowledge; it has no objective reality. Discuss the globe tossing example from the chapter, in light of this statement. What does it mean to say “the probability of water is 0.7”?

#Bayesian theory considers the thought that for each parameter there is a value which can be associated with it.Probability provides a relative value or number in case the true answer or value cannot be determined. 0.7" as the value for probability of water is the relative value given the limited information and considered data. It is an estimate of water as the parameter.

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

# (1) W, W, W
p_grid1 <- seq(from = 0, to = 1, length.out = 20)
prob_p1 <- rep(1, 20)
prob_data1 <- dbinom(3, size = 3, prob = p_grid1)
posterior1 <- prob_data1 * prob_p1
posterior1f <- posterior1 / sum(posterior1)
plot(p_grid1, posterior1f, type = "b", xlab = "Probability (Water1)", ylab = "Probability (Posterior1)")

# (2) W, W, W, L
p_grid2 <- seq(from = 0, to = 1, length.out = 20)
prob_p2 <- rep(1, 20)
prob_data2 <- dbinom(3, size = 4, prob = p_grid2)
posterior2 <- prob_data2 * prob_p2
posterior2f <- posterior2 / sum(posterior2)
plot(p_grid2, posterior2f, type = "b", xlab = "Probability (Water2)", ylab = "Probability (Posterior2)")

# (3) L, W, W, L, W, W, W
p_grid3 <- seq(from = 0, to = 1, length.out = 20)
prob_p3 <- rep(1, 20)
prob_data3 <- dbinom(5, size = 7, prob = p_grid3)
posterior3 <- prob_data3 * prob_p3
posterior3f <- posterior3 / sum(posterior3)
plot(p_grid3, posterior3f, type = "b", xlab = "Probability (Water3)", ylab = "Probability (Posterior3)")

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.

# (1) W, W, W
p_grid11 <- seq(from = 0, to = 1, length.out = 20)
prob_p11 <- ifelse(p_grid11 < 0.5, 0, 1)
prob_data11 <- dbinom(3, size = 3, prob = p_grid11)
posterior11 <- prob_data11 * prob_p11
posterior11f <- posterior11 / sum(posterior11)
plot(p_grid11, posterior11f, type = "b", xlab = "Probability (Water11)", ylab = "Probability (Posterior11)")

# (2) W, W, W, L
p_grid22 <- seq(from = 0, to = 1, length.out = 20)
prob_p22 <- ifelse(p_grid22 < 0.5, 0, 1)
prob_data22 <- dbinom(3, size = 4, prob = p_grid22)
posterior22 <- prob_data22 * prob_p2
posterior2f <- posterior2 / sum(posterior2)
plot(p_grid2, posterior2f, type = "b", xlab = "Probability (Water22)", ylab = "Probability (Posterior22)")

# (3) L, W, W, L, W, W, W
p_grid33 <- seq(from = 0, to = 1, length.out = 20)
prob_p33 <- ifelse(p_grid33 < 0.5, 0, 1)
prob_data33 <- dbinom(5, size = 7, prob = p_grid33)
posterior33 <- prob_data33 * prob_p33
posterior33f <- posterior33 / sum(posterior33)
plot(p_grid33, posterior33f, type = "b", xlab = "Probability (Water33)", ylab = "Probability (Posterior33)")

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.

# Working on data given
# P(Land|Earth) = 1-0.7 =0.3
# P(Land|Mars) = 1
# P(Mars) = P(Earth)  = 0.5
# P(Land) = P(Land|Earth)P(Earth) + P(Land|Mars)P(Mars) = 0.3 * 0.5 + 1 * 0.5 = 0.65
# P(Earth|land)= P(Land|Earth)P(Earth)/P(Land) = 0.3 * 0.5 / 0.65 = 0.23

PE <- 0.5
PM <- 0.5
PLE <- 0.3
PLM <- 1.0
PL <- PE*PLE + PM*PLM

PEL <- (PE*PLE)/PL
PEL
## [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).

# Since one side is black, we can eliminate the card with both white sides.
# Card   Ways 
# BB      2
# WW      0 
# BW      1
# P(BB) = BB / (BB + BW) = 2 / (2+1)= 2/3

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.

# Since one side of the card is black, we can eliminate the card with both white sides.
# Card   Ways 
# BB      2
# BB      2
# BW      1
# WW      0
# P(BB) = (BB + BB) / (BB + BB + BW) = (2 + 2) / (2+2+1) = 4/5

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.

# Card   Ways    Prior   Likelihood
# BB      2        1        2
# WW      0        3        0
# BW      1        2        2
# P(BB) = BB / (BB + BW + WW) = 2 / (2 + 0 + 2) = 0.5

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.

# Card   Ways 
# BW      2
# WW      0
# BB      6
# P(BB) = BB / (BB + BW) = (6) / (2 + 6) = 0.75

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?

# Calling species A as A
# Species B as B 
# Twins as T

# P(T|A) = 0.1 
# P(T|B) = 0.2

# P(A)=0.5
# P(B)=0.5

# P(T) = Pr(T|A) * P(A) + P(T|B) * P(B) = 0.1 * 0.5 + 0.2 * 0.5 = 0.15
# P(A|T) = P(T|A) * P(A) / P(T) = 0.1 * 0.5 / 0.15 = 1/3
# P(B|T) = P(T|B) * P(B)/P(T) = 0.2 * 0.5 / 0.15 = 2/3

# Since first were twins, P(A) = 1/3 and P(B) = 2/3

# P(T) = P(T|A) * P(A) + P(T|B) * P(B) = 1/3 * 0.1 + 2/3 * 0.2 = 1/6