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(rethinker)
number_points <- 1e3
probability_grid <- seq(from=0, to=1, length.out=number_points)
probability_p <- rep(1, number_points)
number_water <- 8
number_tosses <-15
Probability <- dbinom(number_water, size = number_tosses,prob = probability_grid)
posterior <- Probability * probability_p
posterior <- posterior / sum(posterior)
plot(x=probability_grid, y=posterior, main="Posterior of the Experiment of Globe Tossing", xlab="Probability of Water", ylab="Posterior Probability")

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

Number_Samples <- 1e4
set.seed(42)
Samples <- sample(probability_grid, prob=posterior, size = Number_Samples, replace = TRUE)
HPDI_val <- c(Samples, prob=0.9)
cat("The 90% Highest Posterior Density Interval is between,", HPDI_val[1], 'and', HPDI_val[2])
## The 90% Highest Posterior Density Interval is between, 0.4304304 and 0.5575576

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?

Water <- rbinom(Number_Samples, size = number_tosses, prob = Samples)
hist(Water)

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

New_number_tosses <- 9
New_number_water <-6
New_water_probability <-rbinom(Number_Samples, size = New_number_tosses, prob = Samples)
hist(New_water_probability)

probability_new_water <- sum(New_water_probability==New_number_water) / Number_Samples
cat('The Probability of observing 6 water in 9 tosses is', probability_new_water)
## The Probability of observing 6 water in 9 tosses is 0.1711

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.

number_points <1e3
## [1] FALSE
probability_grid <-seq(from=0, to=1, length.out=number_points)
prior <- ifelse(probability_grid < 0.5, 0, 1)
number_water <- 8
number_tosses <- 15
probability <- dbinom(number_water, size = number_tosses, prob = probability_grid)
posterior <- probability * prior
posterior <- posterior /sum(posterior)
plot(x=probability_grid, y=posterior, main = 'Posterior of the Experiment', xlab = 'Probability of Water', ylab = 'Posterior Probability')

Number_Samples <-1e4
samples <- c(probability_grid, prob = posterior, size=Number_Samples, replace=TRUE)
HPDI_val <- c(Samples, prob=0.9)
cat('The 90% Highest Posterior Density Interval is between', HPDI_val[1], 'and', HPDI_val[2], '\n')
## The 90% Highest Posterior Density Interval is between 0.4304304 and 0.5575576
water <- rbinom(Number_Samples, size = number_tosses, prob = Samples)
hist(Water)

probability_new_water <- sum(New_water_probability==New_number_water) / Number_Samples
cat('The Probability of observing 6 water in 9 tosses is', probability_new_water, '\n')
## The Probability of observing 6 water in 9 tosses is 0.1711

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_perc_interval_width <- function(number_tosses, true_prob)
number_points <- 1e3
probability_grid <-seq(from=0, to=1, length.out=number_points)
True_probability <-0.7
probability <- dbinom(round(number_tosses * True_probability), size = number_tosses, prob = probability_grid)
posterior <- probability * prior
posterior <- posterior / sum(posterior)
Number_Samples <- 1e6
probability_limit <- 0.99
samples <- c(probability_grid, prob=posterior, size=Number_Samples, replace=TRUE)
perc_interval <- c(samples, prob=probability_limit)
names(perc_interval) <- NULL
diff <- diff(perc_interval)
True_probability <-0.7
number_tosses <-c(2100:2200)
width_list <-c(1:length(number_tosses))
for (toss in number_tosses) {width_list[toss-(number_tosses[1]-1)] <-compute_perc_interval_width(toss, True_probability)}
diff_of_diff <-width_list - 0.05
num_of_tosses <-number_tosses[1]
min_loc <-which.min(diff_of_diff)
number_tosses <-number_tosses[1] + min_loc
cat('The number of tosses needed to get to a 99% interval with a PI width of 0.05 and is', num_of_tosses)
## The number of tosses needed to get to a 99% interval with a PI width of 0.05 and is 2100