The beta distribution has a form of \( \theta^{a-1} (1 - \theta)^{b-1} / B(a,b) \), where the denominator is a normalizing constant.
This is the same form as the Bernoulli likelihood function: \( p(\{y_1,...,Y_N\}|\theta) = \prod_i \theta^{y_i}(1-\theta)^{(1-y_i)} \).
Thus, the posterior distribution which is proportional to the product of the prior distribution and likelihood will also be a beta distribution if the prior distribution is a beta distribution.
## Functions that returns probability density
BetaDist <- function(x, a, b) {
dbeta(x = x, shape1 = a, shape2 = b)
}
## Create data
dat <- expand.grid(a = c(0.5, 1, 2, 3, 4),
b = c(0.5, 1, 2, 3, 4),
x = seq(from = 0.01, to = 1, by = 0.01))
## Create probability density
dat <- within(dat, {
BetaDist <- BetaDist(x, a, b)
})
library(ggplot2)
p1 <- ggplot(data = dat, mapping = aes(x = x, y = BetaDist)) +
layer(geom = "line",
stat = "identity") +
scale_y_continuous(name = "Probability density of theta", limits = c(0, 3)) +
scale_x_continuous(name = "theta", breaks = c(0,0.5,1), labels = c("0",".5","1")) +
facet_grid(b ~ a)
library(grid)
VP <- viewport(width = 0.975, height = 0.975, x = 0.0, y = 0.0, just = c(0,0))
InPlot <- p1 + labs(title = "a")
print(InPlot, vp = VP)
grid.text(label = "b", x = 0.975, y = 0.5, rot = 270)
When a = 1 and b = 1, the prior is uninformative. A larger \( a \) causes a right shift in the peak, whereas a larger \( b \) causes a left shift in the peak.