Notes on Richard McElreath’s Statistical Rethinking

Matthew Henderson

24 November 2017

Three Tools for Golem Engineering

Small Worlds and Large Worlds

The garden of forking data

ways <- c(0, 3, 8, 9, 0)
ways/sum(ways)
## [1] 0.00 0.15 0.40 0.45 0.00

Building a model

Components of the model

Making the model go

# define grid
p_grid <- seq(from = 0, to = 1, length.out = 20)

# define prior
prior <- rep(1, 20)

# compute likelihood at each value in the grid
likelihood <- dbinom(6, size = 9, prob = p_grid)

# compute product of likelihood and prior
unstd.posterior <- likelihood * prior

# standardise the posterior, so it sums to 1
posterior <- unstd.posterior / sum(unstd.posterior)
plot(p_grid, posterior, type = "b", xlab = "probability of water", ylab = "posterior probability")
mtext("20 points")

prior <- ifelse(p_grid < 0.5, 0, 1)

prior <- exp(-5*abs(p_grid - 0.5))

library(rethinking)

globe.qa <- map(
  alist(
    w ~ dbinom(9, p),
    p ~ dunif(0, 1)
  ) ,
  data = list(w = 6)
)

precis(globe.qa)
##   Mean StdDev 5.5% 94.5%
## p 0.67   0.16 0.42  0.92
w <- 6
n <- 9
curve(dbeta(x, w + 1, n - w + 1), from = 0, to = 1)
curve(dnorm(x, 0.67, 0.16), lty = 2, add = TRUE)

Practice

2M1

  1. W, W, W
p_grid <- seq(from = 0, to = 1, length.out = 20)
prior <- rep(1, 20)
likelihood <- dbinom(3, size = 3, prob = p_grid)
unstd.posterior <- likelihood * prior
posterior <- unstd.posterior / sum(unstd.posterior)
plot(p_grid, posterior, type = "b", xlab = "probability of water", ylab = "posterior probability")
mtext("20 points")

  1. W, W, W, L
p_grid <- seq(from = 0, to = 1, length.out = 20)
prior <- rep(1, 20)
likelihood <- dbinom(3, size = 4, prob = p_grid)
unstd.posterior <- likelihood * prior
posterior <- unstd.posterior / sum(unstd.posterior)
plot(p_grid, posterior, type = "b", xlab = "probability of water", ylab = "posterior probability")
mtext("20 points")

  1. L, W, W, L, W, W, W
p_grid <- seq(from = 0, to = 1, length.out = 20)
prior <- rep(1, 20)
likelihood <- dbinom(5, size = 7, prob = p_grid)
unstd.posterior <- likelihood * prior
posterior <- unstd.posterior / sum(unstd.posterior)
plot(p_grid, posterior, type = "b", xlab = "probability of water", ylab = "posterior probability")
mtext("20 points")

2M2

p_grid <- seq(from = 0, to = 1, length.out = 20)
prior <- ifelse(p_grid < 0.5, 0, 1)
likelihood <- dbinom(3, size = 3, prob = p_grid)
unstd.posterior <- likelihood * prior
posterior <- unstd.posterior / sum(unstd.posterior)
plot(p_grid, posterior, type = "b", xlab = "probability of water", ylab = "posterior probability")
mtext("20 points")

p_grid <- seq(from = 0, to = 1, length.out = 20)
prior <- ifelse(p_grid < 0.5, 0, 1)
likelihood <- dbinom(3, size = 4, prob = p_grid)
unstd.posterior <- likelihood * prior
posterior <- unstd.posterior / sum(unstd.posterior)
plot(p_grid, posterior, type = "b", xlab = "probability of water", ylab = "posterior probability")
mtext("20 points")

p_grid <- seq(from = 0, to = 1, length.out = 20)
prior <- ifelse(p_grid < 0.5, 0, 1)
likelihood <- dbinom(5, size = 7, prob = p_grid)
unstd.posterior <- likelihood * prior
posterior <- unstd.posterior / sum(unstd.posterior)
plot(p_grid, posterior, type = "b", xlab = "probability of water", ylab = "posterior probability")
mtext("20 points")

2M3

It’s easier to multiply by ten and think about paths.

W L
Earth 7 3
Mars 0 10

There are 13 ways to end up with land. So if we know that we have land then the probability that we’re on Earth is just the number of ways of being on Earth as a proportion of the total number of ways of having land.

\[Pr(\text{Earth}|\text{land}) = 3 / (3 + 10) = 0.23\]

2M4