3.3 Discrete Cases
Suppose you have a string of numbers \([1,1,1,1,0,0,1,1,1,0]\) (7 ones and 3 zeros) produced by a Bernoulli random number generator. What parameter \(p\) was used in the Bernoulli function?2
<- c(1, 1, 1, 1, 0, 0, 1, 1, 1, 0) D
Your best guess is \(p = 0.7\), but how confident are you? Posit eleven competing priors, \(\theta = [.0, .1, \ldots, 1]\) with equal prior probabilities, \(P(\theta) = [1/11, \ldots]\).
<- seq(0, 1, by = 0.1)
theta <- rep(1/11, 11) prior
Using a Bernoulli generative model, the likelihood of observing 7 ones and 3 zeros are \(P(D|\theta) = \theta^7 + (1-\theta)^3.\)
<- theta^7 * (1 - theta)^3
likelihood
data.frame(theta, likelihood) %>%
ggplot() +
geom_segment(aes(x = theta, xend = theta, y = 0, yend = likelihood),
linetype = 2, color = "steelblue") +
geom_point(aes(x = theta, y = likelihood), color = "steelblue", size = 3) +
scale_x_continuous(breaks = theta) +
theme_minimal() +
theme(panel.grid.minor = element_blank()) +
labs(title = expression(paste("Maximum likelihood of observing D is at ", theta, " = 0.7.")),
x = expression(theta), y = expression(f[theta](D)))
The posterior probability is the likelihood divided by the marginal probability of observing \(D\) multiplied by the prior, \(P(\theta|D) = \frac{P(D|\theta)}{P(D)}\cdot P(\theta).\) In this case, the marginal probability is straight-forward to calculate: it is the sum-product of the priors and their associated likelihoods.
<- likelihood / sum(likelihood * prior) * prior posterior
What would the posterior look like if we started with an educated guess on \(P(\theta)\) that more heavily weights \(\theta = 0.7\)?
<- c(.05, .05, .05, .05, .05, .10, .15, .20, .15, .10, .05)
prior <- likelihood / sum(likelihood * prior) * prior posterior
What if we now employ a larger data set? To see, generate a sample of 100 Bernoulli(.7) observations.
<- rbernoulli(100, p = 0.7) %>% as.numeric() D100
## Warning: `rbernoulli()` was deprecated in purrr 1.0.0.
<- theta^sum(D100) * (1 - theta)^(length(D100)-sum(D100))
likelihood <- likelihood / sum(likelihood * prior) * prior posterior
What would it look like if it also had more competing hypotheses, \(\theta \in (0, .01, .02, \ldots, 1)\).
<- seq(0, 1, by = .01)
theta <- rep(1/100, 101)
prior <- theta^sum(D100) * (1 - theta)^(length(D100)-sum(D100))
likelihood <- likelihood / sum(likelihood * prior) * prior posterior
Example borrowed from chris’ sandbox↩︎