Here is a standard sampling-without-replacement problem. Suppose we have \(N\) balls in a box, \(B\) that are black and the remaining \(W = N - B\) balls are white.
You sample \(n\) balls from the box and \(b\) turn out to be black. What have you learned about the number \(B\) that are black in the box?
This is a Bayes’ rule problem. We’ll illustrate it for the case where \(N = 50\).
library(TeachBayes)
bayes_df <- data.frame(B=0:50, Prior=rep(1/51, 51))
dsampling()
.sample_b <- 3
pop_N <- 50
sample_n <- 10
bayes_df$Likelihood <- dsampling(sample_b, pop_N,
bayes_df$B, sample_n)
bayesian_crank()
function and obtain the posterior probabilities for \(B\).bayes_df <- bayesian_crank(bayes_df)
I compare the prior and posterior probabilities for \(B\) graphically.
prior_post_plot(bayes_df)
Here is a 90 percent probability interval for \(B\):
library(dplyr)
discint(select(bayes_df, B, Posterior), 0.90)
## $prob
## [1] 0.9125933
##
## $set
## [1] 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26