Skip to content

Instantly share code, notes, and snippets.

@murphyk
Last active September 12, 2023 22:13
Show Gist options
  • Save murphyk/94205bcf335837108ff0e6d51331785a to your computer and use it in GitHub Desktop.
Save murphyk/94205bcf335837108ff0e6d51331785a to your computer and use it in GitHub Desktop.
illustrate tail sensitivity of Bayes (from McElreath)
post_prior <- function(m_pr, m_lik, sd_pr, sd_lik, df_pr, df_lik,
xlim = c(-5, 15), n = 1001, ttl = "",...) {
xvec <- seq(xlim[1], xlim[2], length.out = n)
tfun <- function(x,m,s,df, ...) dt((x-m)/s, df)
prvec <- tfun(xvec, m_pr, sd_pr, df_pr)
likvec <- tfun(xvec, m_lik, sd_lik, df_lik)
postvec <- prvec*likvec/(sum(prvec*likvec)*diff(xvec[1:2]))
matplot(xvec, cbind(prvec, likvec, postvec),
type = "l",
lty = c(2,1,1),
col = c(1,1,2),
lwd = c(1,1,2),
ylab = "density",
...)
title(ttl)
pmax = max(max(likvec), max(prvec))
text(0, pmax, "likelihood")
text(10, pmax, "prior")
}
par(las = 1, bty = "l")
par(mfrow=c(2,2))
post_prior(10, 0, 1, 1, 1000,1000, ttl="prior:N, lik:N") # log = "y")
post_prior(10, 0, 1, 1, 2, 2, ttl="prior:T, lik:T") #, log = "y")
post_prior(10, 0, 1, 1, 2, 1000, ttl="prior:T, lik:N")
post_prior(10, 0, 1, 1, 1000, 2, ttl="prior:N, lik:T")
@murphyk
Copy link
Author

murphyk commented Sep 12, 2023

You are right.
Or we could use a library for probability distributions that takes care of all this for us.
like this:

 pr_prior = tfd.StudentT(df_prior, mean_prior, std_prior)

https://colab.research.google.com/github/gerdm/misc/blob/master/2023-09/bayes-tails.ipynb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment