Chapter 3 - Sampling the Imaginary

This chapter introduced the basic procedures for manipulating posterior distributions. Our fundamental tool is samples of parameter values drawn from the posterior distribution. These samples can be used to produce intervals, point estimates, posterior predictive checks, as well as other kinds of simulations. Posterior predictive checks combine uncertainty about parameters, as described by the posterior distribution, with uncertainty about outcomes, as described by the assumed likelihood function. These checks are useful for verifying that your software worked correctly. They are also useful for prospecting for ways in which your models are inadequate.

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.

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

3-1. Suppose the globe tossing data had turned out to be 8 water in 15 tosses. Construct the posterior distribution, using grid approximation. Plot the posterior. Use the same flat prior as before.

library(rethinking)
p_grid <- seq(from = 0, to = 1, length.out = 1e3)
prior <- rep(1, 1e3)
likelihood <- dbinom(8, size = 15, prob = p_grid)
posterior <- likelihood * prior
posterior <- posterior / sum(posterior)
#We plot the posterior as below
plot(x = p_grid, y = posterior, type = "l")

3-2. Draw 10,000 samples from the grid approximation from above. Then use the samples to calculate the 90% HPDI for p.

samples <- sample(p_grid, prob = posterior, size = 1e4, replace = TRUE)
HPDI(samples, prob = 0.90)
##      |0.9      0.9| 
## 0.3363363 0.7247247
#From the results, we can infer that for 90% HDPI,p lies between 0.34 and 0.72

3-3. Construct a posterior predictive check for this model and data. This means simulate the distribution of samples, averaging over the posterior uncertainty in p. What is the probability of observing 8 water in 15 tosses?

#To simulate the distribution of samples, we use the rbinom() function and provide the appropriate probability distribution.


w <- rbinom(1e4, size = 15, prob = samples)
simplehist(w)

mean(w == 8)
## [1] 0.1474
# From the result, we note that the probablity of observing 8 water in 15 tosses is 0.158

3-4. Using the posterior distribution constructed from the new (8/15) data, now calculate the probability of observing 6 water in 9 tosses.

# Similar to the code executed above 
w <- rbinom(1e4, size = 9, prob = samples)
simplehist(w)

mean(w == 6)
## [1] 0.1771
#In this case, the probability of observing of 6 water in 9 tosses is 0.177.

3-5. Start over at 3-1, but now use a prior that is zero below p = 0.5 and a constant above p = 0.5. This corresponds to prior information that a majority of the Earth’s surface is water. Repeat each problem above and compare the inferences. Plot the posterior. What difference does the better prior make? If it helps, compare inferences (using both priors) to the true value p = 0.7.

# Updated 3-1
p_grid <- seq(from = 0, to = 1, length.out = 1e3)
prior <- ifelse(p_grid < .5, 0, 1)
likelihood <- dbinom(8, size = 15, prob = p_grid)
posterior <- likelihood * prior
posterior <- posterior / sum(posterior)
plot(x = p_grid, y = posterior, type = "l")

#This plot looks similar to before (from 3-1) with two notable exceptions: (1) all posterior probability values have become zero for values of p<0.5, and (2) the height of the remainder of the curve (p>0.5) has increased (as evidenced by the scaling of the y-axis increasing). Thus, the probability mass that had been assigned to be below p=0.5 appears to have been shifted up to the remainder of the curve.

# Updated 3-2
samples <- sample(p_grid, prob = posterior, size = 1e4, replace = TRUE)
HPDI(samples, prob = .90)
##      |0.9      0.9| 
## 0.5005005 0.7127127
#From the results, we can note that the prior has a big impact on the lower bound of the HPDI (i.e., increasing it from 0.33 to 0.5). Also, it does not influence the upper bound very much. So the probability mass that was below p=0.5 must have shifted to be in the range p=0.5 and p=0.71 otherwise the upper bound would have changed as well.

# Updated 3-3
w <- rbinom(1e4, size = 15, prob = samples)
simplehist(w)

mean(w == 8)
## [1] 0.1518
#The histogram is now less symmetrical (being left-skewed), corresponding to a lower probability of observing few water tosses. The modal predicted value has also shifted from 8 to 9. Combining these effects, the probability of observing 8 water in 15 tosses has increased a little from 15.08% to 15.47%.



#Updated 3-4
w <- rbinom(1e4, size = 9, prob = samples)
simplehist(w)

mean(w == 6)
## [1] 0.2336
#The modal value has shifted from 5 to 6, and the probability of observing 6 water in 9 tosses has increased from 17.72 % to 23.64%.

3-6. Suppose you want to estimate the Earth’s proportion of water very precisely. Specifically, you want the 99% percentile interval of the posterior distribution of p to be only 0.05 wide. This means the distance between the upper and lower bound of the interval should be 0.05. How many times will you have to toss the globe to do this?

compute_pi_width <- function(N, true_p) {
  likelihood <- dbinom( round(N*true_p), size = N, prob = p_grid )
  posterior <- likelihood * prior
  posterior <- posterior / sum(posterior)
  samples <- sample(p_grid, prob = posterior, size = 1e4, replace = TRUE )
  interval <- PI(samples, prob = 0.99)
  names(interval) <- NULL
  diff( interval )
}
compute_pi_width(1500,0.7)
## [1] 0.06206206
compute_pi_width(2000,0.7)
## [1] 0.05205205
compute_pi_width(2200,0.7)
## [1] 0.05105105
#We would have to toss the globe at least 2200 times so as to achieve a significant p-value<0.05