dbinom(6, size=9, prob=0.5)
[1] 0.1640625
The likelihood of the data: 6 W in 9 tosses, under the probability of 0.5
dbinom(6, size=9, prob=0.5)
[1] 0.1640625
For the globe tossing example, let’s make a grid of 20 points
# change this for more precision. however, there will not be much change in inference after 100 points
= 20
num_of_points
# 1. define grid
<- seq(from=0, to=1, length.out=num_of_points)
p_grid
# 2. define prior
<- rep(1, num_of_points)
prior
# 3. compute likelihood at each value in the grid
<- dbinom(6, size=9, prob=p_grid)
likelihood
# 4. compute product of likelihood and prior
<- likelihood * prior
unstd.posterior
# 5. standarize the posterior, so it sums to 1
<- unstd.posterior / sum(unstd.posterior) posterior
Let’s display the posterior distribution:
plot(p_grid, posterior, type="b", xlab = "prob. of water", ylab="posterior prob.")
Different prior #1:
= 20
num_of_points
# 1. define grid
<- seq(from=0, to=1, length.out=num_of_points)
p_grid
# 2. define prior
<- ifelse(p_grid < 0.5, 0, 1)
prior
# 3. compute likelihood at each value in the grid
<- dbinom(6, size=9, prob=p_grid)
likelihood
# 4. compute product of likelihood and prior
<- likelihood * prior
unstd.posterior
# 5. standarize the posterior, so it sums to 1
<- unstd.posterior / sum(unstd.posterior)
posterior
# 6. plot
plot(p_grid, posterior, type="b", xlab = "prob. of water", ylab="posterior prob.")
Different prior #2:
= 20
num_of_points
# 1. define grid
<- seq(from=0, to=1, length.out=num_of_points)
p_grid
# 2. define prior
<- exp(-5*abs(p_grid - 0.5))
prior
# 3. compute likelihood at each value in the grid
<- dbinom(6, size=9, prob=p_grid)
likelihood
# 4. compute product of likelihood and prior
<- likelihood * prior
unstd.posterior
# 5. standarize the posterior, so it sums to 1
<- unstd.posterior / sum(unstd.posterior)
posterior
# 6. plot
plot(p_grid, posterior, type="b", xlab = "prob. of water", ylab="posterior prob.")
library(rethinking)
Loading required package: cmdstanr
This is cmdstanr version 0.7.1
- CmdStanR documentation and vignettes: mc-stan.org/cmdstanr
- CmdStan path: C:/Users/H/Documents/.cmdstan/cmdstan-2.34.1
- CmdStan version: 2.34.1
A newer version of CmdStan is available. See ?install_cmdstan() to install it.
To disable this check set option or environment variable CMDSTANR_NO_VER_CHECK=TRUE.
Loading required package: posterior
This is posterior version 1.5.0
Attaching package: 'posterior'
The following objects are masked from 'package:stats':
mad, sd, var
The following objects are masked from 'package:base':
%in%, match
Loading required package: parallel
rethinking (Version 2.40)
Attaching package: 'rethinking'
The following object is masked from 'package:stats':
rstudent
<- quap(
globe.qa
alist(
~ dbinom(W+L, p), # binomial likelihood
W ~ dunif(0, 1) # uniform prior
p
),
data=list(W=6, L=3)
)
# print a summary of quadratic approx.
precis(globe.qa)
mean sd 5.5% 94.5%
p 0.6666667 0.1571338 0.4155366 0.9177968
The output refers to the properties of the posterior, assuming it is Gaussian. Let’s compare this approximation with the real posterior distribution (it has a Beta distribution analytically)
<- 6
W <- 3
L
# from the previous outputs
<- 0.67
globe.qa.mean <- 0.16
globe.qa.sd
# the real posterior distribution
curve(dbeta(x, W+1, L+1), from=0, to=1)
# the quadratic/gaussian approximation
curve(dnorm(x, globe.qa.mean, globe.qa.sd), lty=2, add=TRUE)
<- 1000
n_samples <- rep(NA, n_samples)
p 1] <- 0.5
p[
<- 6
W <- 3
L
for (i in 2:n_samples) {
<- rnorm(1, p[i-1], 0.1)
p_new if (p_new < 0) p_new <- ans(p_new)
if (p_new > 1) p_new <- 2 - p_new
<- dbinom(W, W+L, p[i-1])
q0 <- dbinom(W, W+L, p_new)
q1 <- ifelse(runif(1) < q1/q0, p_new, p[i-1])
p[i]
}
# compare the MCMC approximation with the analytical posterior (beta)
dens(p, xlim=c(0,1))
curve(dbeta(x, W+1, L+1), lty=2, add=TRUE)