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)
set.seed(1)
W = 8
N =15
p_grid <-seq(from =0, to = 1, len =1000)
prob_p <- rep(1,1000)
prob_data <- dbinom(W,N,prob=p_grid)
posterior <-prob_data * prob_p
posterior <- posterior / sum(posterior)

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.

set.seed(1)

samples <- sample (p_grid, prob = posterior, size =1e4, replace =TRUE)
HPDI(samples, prob = 0.9)
##      |0.9      0.9| 
## 0.3393393 0.7247247
# The 90% HPDI is between 0.3393 and 0.7247

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?

set.seed(1)

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

mean(w==8)
## [1] 0.1377
# Chance of observing 8 waters in 15 tosses is around 14%

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

set.seed(1)

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

mean(w ==6)
## [1] 0.1768
# The probability of observing 6 water in 9 tosses is around 18%

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.

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

# Now all posterior probability values have become zero for values less than 0.5,
# The height (probability) of the posterior is larger than previous version, 


#Redo 3M2
samples <- sample(p_grid, prob = posterior, size = 10000, replace = TRUE)
HPDI(samples, prob = .90)
##      |0.9      0.9| 
## 0.5005005 0.7127127
#The 90% HPDI is between 0.50 and 0.71, the lower boundary of the HPDI has been increased, the range has been narrowed , which is more approaching to the true value p= 0.7

#Redo 3M3
w <- rbinom(10000, size = 15, prob = samples)
simplehist(w)

mean(w == 8)
## [1] 0.1597
#The probability of observing 8 water in 15 tosses is around 16%, which only increase a little bit from the previous version

#Redo 3M4
w <- rbinom(10000, size = 9, prob = samples)
simplehist(w)

mean(w == 6)
## [1] 0.2245
#The probability of observing of 6 water in 9 tosses is around 22%, which is higher than the previous version

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?

set.seed(1)
compute_pi_width <- function(N, p) {
  
  p_grid <- seq( from=0 , to=1 , length.out=1000 )
  prior <- rep( 1 , 1000 )
  likelihood <- dbinom( round(N*p), size=N, prob=p_grid )
  posterior <- likelihood * prior
  posterior <- posterior / sum(posterior)
  samples <- sample(p_grid, prob=posterior, size=1e4, replace=TRUE )
  
  interval <- HPDI(samples, prob=0.99)
  diff(interval)
}

true_p <- 0.7

for (n in seq(from = 2001, to =2500, length.out = 500))
{
 result <- compute_pi_width (n, true_p)
 if (result <=0.05){
   print ("Find the times to toss to get make the posterior distribution of p to be only 0.05 wide")
   print (n)
   break
 }
 
}
## [1] "Find the times to toss to get make the posterior distribution of p to be only 0.05 wide"
## [1] 2097
# Comment: We need around 2100 times to toss to make the posterior distribution of p to be only 0.05 wide