Skip to content

Instantly share code, notes, and snippets.

@twolodzko
Created June 10, 2020 07:19
Show Gist options
  • Save twolodzko/0436b702939f0fa2307fceba5bf6cf9a to your computer and use it in GitHub Desktop.
Save twolodzko/0436b702939f0fa2307fceba5bf6cf9a to your computer and use it in GitHub Desktop.
Examples of prior having impact on posterior
# code accompanying answer: https://stats.stackexchange.com/a/471245/35989
n <- 22
x <- 13
lik <- function(p) dbinom(x, n, p) * 20
prior <- function(p) dbeta(p, alpha, beta)
post <- function(p) dbeta(p, alpha+x, beta+(n-x))
op <- par(mfrow=c(1,3), mar=c(3,1,1,1), cex=0.9)
for (prior_size in c(5, 25, 100)) {
prior_prob <- 0.3
alpha <- prior_prob * prior_size
beta <- (1-prior_prob) * prior_size
plot(NA, ylim=c(0,9), xlim=c(0,1), axes=FALSE, xlab="", ylab="")
curve(lik, 0, 1, add=TRUE, col="red", axes=FALSE, xlab="", ylab="", ylim=5)
curve(post, 0, 1, add=TRUE, col="violet")
curve(prior, 0, 1, add=TRUE, col="blue")
axis(1)
legend("topright", legend=c("prior", "likelihood", "posterior"),
lty=1, col=c("blue", "red", "violet"), bty="n")
}
par(op)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment