We have learned how to think like a Bayesian:
We have learned three conjugate families:
Once we have a posterior model, we must be able to apply the results.
f(\theta|y) = \frac{f(\theta) L(\theta|y)}{f(y)} \propto f(\theta)L(\theta|y)
f(y) = \int_{\theta_1} \int_{\theta_2} \cdot \cdot \cdot \int_{\theta_k} f(\theta) L(\theta|y) d\theta_k \cdot \cdot \cdot d\theta_2 d\theta_1
\left \{ \theta^{(1)}, \theta^{(2)}, ..., \theta^{(N)} \right \}
These \theta_i will have properties that reflect those of the posterior model for \theta.
To help us, we will apply these simulation techniques to Beta-Binomial and Gamma-Poisson models.
Suppose there is an image that you can’t view in its entirety.
We can see snippets along a grid that sweeps from left to right across the image.
The finer the grid, the clearer the image; if the grid is fine enough, the result is a good approximation.
This is the general idea behind Bayesian grid approximation.
Our target image is the posterior pdf, f(\theta|y).
\left\{ \theta^{(1)}, \theta^{(2)}, ..., \theta^{(N)} \right\},
from a discretized approximation of the posterior pdf, f(\theta|y).
\begin{align*} Y|\pi &\sim \text{Bin}(10, \pi) \\ \pi &\sim \text{Beta}(2, 2) \end{align*}
\begin{align*} Y|\pi &\sim \text{Bin}(10, \pi) \\ \pi &\sim \text{Beta}(2, 2) \\ \pi | Y &\sim \text{Beta}(11, 3) \end{align*}
Instead of using the posterior we know, let’s approximate it using grid approximation.
First step: define a discrete grid of possible \theta values.
Instead of using the posterior we know, let’s approximate it using grid approximation.
First step: define a discrete grid of possible \theta values.
| pi_grid |
|---|
| 0.0 |
| 0.2 |
| 0.4 |
| 0.6 |
| 0.8 |
| 1.0 |
Instead of using the posterior we know, let’s approximate it using grid approximation.
Second step: evaluate the prior pdf, f(\theta), and the likelihood function, L(\theta|y) at each \theta grid value.
dbeta() and dbinom() to evaluate the \text{Beta}(2,2) prior and \text{Bin}(10, \pi) likelihood with Y=9 at each \pi in pi_grid.Instead of using the posterior we know, let’s approximate it using grid approximation.
Second step: evaluate the prior pdf, f(\theta), and the likelihood function, L(\theta|y) at each \theta grid value.
dbeta() and dbinom() to evaluate the \text{Beta}(2,2) prior and \text{Bin}(10, \pi) likelihood with Y=9 at each \pi in pi_grid.| pi_grid | prior | likelihood |
|---|---|---|
| 0.0 | 0.00 | 0.0000000 |
| 0.2 | 0.96 | 0.0000041 |
| 0.4 | 1.44 | 0.0015729 |
| 0.6 | 1.44 | 0.0403108 |
| 0.8 | 0.96 | 0.2684355 |
| 1.0 | 0.00 | 0.0000000 |
Instead of using the posterior we know, let’s approximate it using grid approximation.
Third step: obtain a discrete approximation of the posterior pdf, f(\theta|y) by calculating the product f(\theta) L(\theta|y) at each \theta grid value and normalizing the products to sum to 1 across all \theta.
Instead of using the posterior we know, let’s approximate it using grid approximation.
Third step: obtain a discrete approximation of the posterior pdf, f(\theta|y) by calculating the product f(\theta) L(\theta|y) at each \theta grid value and normalizing the products to sum to 1 across all \theta.
Instead of using the posterior we know, let’s approximate it using grid approximation.
Third step: obtain a discrete approximation of the posterior pdf, f(\theta|y) by calculating the product f(\theta) L(\theta|y) at each \theta grid value and normalizing the products to sum to 1 across all \theta.
Verifying our calculations,
Instead of using the posterior we know, let’s approximate it using grid approximation.
Third step: obtain a discrete approximation of the posterior pdf, f(\theta|y) by calculating the product f(\theta) L(\theta|y) at each \theta grid value and normalizing the products to sum to 1 across all \theta.
Verifying our calculations,
| sum(unnormalized) | sum(posterior) |
|---|---|
| 0.3180144 | 1 |
Instead of using the posterior we know, let’s approximate it using grid approximation.
We now have a glimpse into the actual posterior pdf.
As we increase the number of possible \theta values, the better we can “see” the resulting posterior.
What happens if we try the following: n=50, n=100, n=500, n=1000?
Markov chain Monte Carlo (MCMC) is an application of Markov chains to simulate probability models.
MCMC samples are not taken directly from the posterior pdf, f(\theta | y)… and they are not independent.
Suppose we have an N-length MCMC sample, \left\{ \theta^{(1)}, \theta^{(2)}, \theta^{(3)}, ..., \theta^{(N)} \right\}
f\left( \theta^{(i+1)} | \theta^{(i)}, y \right)
f\left( \theta^{(i+1)} | \theta^{(i)}, y \right) \ne f\left(\theta^{(i+1)}|y \right)
rstanrstan:
rstan notationdata: in our example, Y is the observed number of successes in 10 trials.
rstan that Y is an integer between 0 and 10.parameters: in our example, our model depends on \pi.
rstan that \pi can be any real number between 0 and 1.model: in our example, we need to specify Y \sim \text{Bin}(10, \pi) and \pi \sim \text{Beta}(2,2).rstanrstanmodel code: the character string defining the model (in our case, bb_model).data: a list of the observed data.
chains: how many parallel Markov chains to run.
iter: desired number of iterations, or length of Markov chain.
seed: used to set the seed of the RNG.rstan# STEP 2: simulate the posterior
bb_sim <- stan(model_code = bb_model, data = list(Y = 9),
chains = 4, iter = 5000*2, seed = 84735)
SAMPLING FOR MODEL 'anon_model' NOW (CHAIN 1).
Chain 1:
Chain 1: Gradient evaluation took 2e-06 seconds
Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.02 seconds.
Chain 1: Adjust your expectations accordingly!
Chain 1:
Chain 1:
Chain 1: Iteration: 1 / 10000 [ 0%] (Warmup)
Chain 1: Iteration: 1000 / 10000 [ 10%] (Warmup)
Chain 1: Iteration: 2000 / 10000 [ 20%] (Warmup)
Chain 1: Iteration: 3000 / 10000 [ 30%] (Warmup)
Chain 1: Iteration: 4000 / 10000 [ 40%] (Warmup)
Chain 1: Iteration: 5000 / 10000 [ 50%] (Warmup)
Chain 1: Iteration: 5001 / 10000 [ 50%] (Sampling)
Chain 1: Iteration: 6000 / 10000 [ 60%] (Sampling)
Chain 1: Iteration: 7000 / 10000 [ 70%] (Sampling)
Chain 1: Iteration: 8000 / 10000 [ 80%] (Sampling)
Chain 1: Iteration: 9000 / 10000 [ 90%] (Sampling)
Chain 1: Iteration: 10000 / 10000 [100%] (Sampling)
Chain 1:
Chain 1: Elapsed Time: 0.01 seconds (Warm-up)
Chain 1: 0.011 seconds (Sampling)
Chain 1: 0.021 seconds (Total)
Chain 1:
SAMPLING FOR MODEL 'anon_model' NOW (CHAIN 2).
Chain 2:
Chain 2: Gradient evaluation took 0 seconds
Chain 2: 1000 transitions using 10 leapfrog steps per transition would take 0 seconds.
Chain 2: Adjust your expectations accordingly!
Chain 2:
Chain 2:
Chain 2: Iteration: 1 / 10000 [ 0%] (Warmup)
Chain 2: Iteration: 1000 / 10000 [ 10%] (Warmup)
Chain 2: Iteration: 2000 / 10000 [ 20%] (Warmup)
Chain 2: Iteration: 3000 / 10000 [ 30%] (Warmup)
Chain 2: Iteration: 4000 / 10000 [ 40%] (Warmup)
Chain 2: Iteration: 5000 / 10000 [ 50%] (Warmup)
Chain 2: Iteration: 5001 / 10000 [ 50%] (Sampling)
Chain 2: Iteration: 6000 / 10000 [ 60%] (Sampling)
Chain 2: Iteration: 7000 / 10000 [ 70%] (Sampling)
Chain 2: Iteration: 8000 / 10000 [ 80%] (Sampling)
Chain 2: Iteration: 9000 / 10000 [ 90%] (Sampling)
Chain 2: Iteration: 10000 / 10000 [100%] (Sampling)
Chain 2:
Chain 2: Elapsed Time: 0.01 seconds (Warm-up)
Chain 2: 0.011 seconds (Sampling)
Chain 2: 0.021 seconds (Total)
Chain 2:
SAMPLING FOR MODEL 'anon_model' NOW (CHAIN 3).
Chain 3:
Chain 3: Gradient evaluation took 0 seconds
Chain 3: 1000 transitions using 10 leapfrog steps per transition would take 0 seconds.
Chain 3: Adjust your expectations accordingly!
Chain 3:
Chain 3:
Chain 3: Iteration: 1 / 10000 [ 0%] (Warmup)
Chain 3: Iteration: 1000 / 10000 [ 10%] (Warmup)
Chain 3: Iteration: 2000 / 10000 [ 20%] (Warmup)
Chain 3: Iteration: 3000 / 10000 [ 30%] (Warmup)
Chain 3: Iteration: 4000 / 10000 [ 40%] (Warmup)
Chain 3: Iteration: 5000 / 10000 [ 50%] (Warmup)
Chain 3: Iteration: 5001 / 10000 [ 50%] (Sampling)
Chain 3: Iteration: 6000 / 10000 [ 60%] (Sampling)
Chain 3: Iteration: 7000 / 10000 [ 70%] (Sampling)
Chain 3: Iteration: 8000 / 10000 [ 80%] (Sampling)
Chain 3: Iteration: 9000 / 10000 [ 90%] (Sampling)
Chain 3: Iteration: 10000 / 10000 [100%] (Sampling)
Chain 3:
Chain 3: Elapsed Time: 0.01 seconds (Warm-up)
Chain 3: 0.01 seconds (Sampling)
Chain 3: 0.02 seconds (Total)
Chain 3:
SAMPLING FOR MODEL 'anon_model' NOW (CHAIN 4).
Chain 4:
Chain 4: Gradient evaluation took 1e-06 seconds
Chain 4: 1000 transitions using 10 leapfrog steps per transition would take 0.01 seconds.
Chain 4: Adjust your expectations accordingly!
Chain 4:
Chain 4:
Chain 4: Iteration: 1 / 10000 [ 0%] (Warmup)
Chain 4: Iteration: 1000 / 10000 [ 10%] (Warmup)
Chain 4: Iteration: 2000 / 10000 [ 20%] (Warmup)
Chain 4: Iteration: 3000 / 10000 [ 30%] (Warmup)
Chain 4: Iteration: 4000 / 10000 [ 40%] (Warmup)
Chain 4: Iteration: 5000 / 10000 [ 50%] (Warmup)
Chain 4: Iteration: 5001 / 10000 [ 50%] (Sampling)
Chain 4: Iteration: 6000 / 10000 [ 60%] (Sampling)
Chain 4: Iteration: 7000 / 10000 [ 70%] (Sampling)
Chain 4: Iteration: 8000 / 10000 [ 80%] (Sampling)
Chain 4: Iteration: 9000 / 10000 [ 90%] (Sampling)
Chain 4: Iteration: 10000 / 10000 [100%] (Sampling)
Chain 4:
Chain 4: Elapsed Time: 0.01 seconds (Warm-up)
Chain 4: 0.012 seconds (Sampling)
Chain 4: 0.022 seconds (Total)
Chain 4:
rstan, , parameters = pi
chains
iterations chain:1 chain:2 chain:3 chain:4
[1,] 0.8278040 0.7523560 0.6386998 0.9627572
[2,] 0.9087546 0.8922386 0.6254761 0.9413518
[3,] 0.6143859 0.8682356 0.7222360 0.9489624
[4,] 0.8462156 0.8792812 0.8100192 0.9413812
Remember, these are not a random sample from the posterior!
They are also not independent!
Each chain forms a dependent 5,000 length Markov chain of \left\{ \pi^{(1)}, \pi^{(2)}, ..., \pi^{(5000)}\right\}
rstanmcmc_trace() from bayesplot package) to see what the values did longitudinally.rstanmcmc_hist() and mcmc_dens() functions,Let’s now consider a smaller simulation, where n=50 (recall, overall n=100, but half is for burn-in).
Run the following code:
bb_sim_short <- stan(model_code = bb_model, data = list(Y = 9),
chains = 4, iter = 50*2, seed = 84735)
SAMPLING FOR MODEL 'anon_model' NOW (CHAIN 1).
Chain 1:
Chain 1: Gradient evaluation took 1e-06 seconds
Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.01 seconds.
Chain 1: Adjust your expectations accordingly!
Chain 1:
Chain 1:
Chain 1: WARNING: There aren't enough warmup iterations to fit the
Chain 1: three stages of adaptation as currently configured.
Chain 1: Reducing each adaptation stage to 15%/75%/10% of
Chain 1: the given number of warmup iterations:
Chain 1: init_buffer = 7
Chain 1: adapt_window = 38
Chain 1: term_buffer = 5
Chain 1:
Chain 1: Iteration: 1 / 100 [ 1%] (Warmup)
Chain 1: Iteration: 10 / 100 [ 10%] (Warmup)
Chain 1: Iteration: 20 / 100 [ 20%] (Warmup)
Chain 1: Iteration: 30 / 100 [ 30%] (Warmup)
Chain 1: Iteration: 40 / 100 [ 40%] (Warmup)
Chain 1: Iteration: 50 / 100 [ 50%] (Warmup)
Chain 1: Iteration: 51 / 100 [ 51%] (Sampling)
Chain 1: Iteration: 60 / 100 [ 60%] (Sampling)
Chain 1: Iteration: 70 / 100 [ 70%] (Sampling)
Chain 1: Iteration: 80 / 100 [ 80%] (Sampling)
Chain 1: Iteration: 90 / 100 [ 90%] (Sampling)
Chain 1: Iteration: 100 / 100 [100%] (Sampling)
Chain 1:
Chain 1: Elapsed Time: 0 seconds (Warm-up)
Chain 1: 0 seconds (Sampling)
Chain 1: 0 seconds (Total)
Chain 1:
SAMPLING FOR MODEL 'anon_model' NOW (CHAIN 2).
Chain 2:
Chain 2: Gradient evaluation took 0 seconds
Chain 2: 1000 transitions using 10 leapfrog steps per transition would take 0 seconds.
Chain 2: Adjust your expectations accordingly!
Chain 2:
Chain 2:
Chain 2: WARNING: There aren't enough warmup iterations to fit the
Chain 2: three stages of adaptation as currently configured.
Chain 2: Reducing each adaptation stage to 15%/75%/10% of
Chain 2: the given number of warmup iterations:
Chain 2: init_buffer = 7
Chain 2: adapt_window = 38
Chain 2: term_buffer = 5
Chain 2:
Chain 2: Iteration: 1 / 100 [ 1%] (Warmup)
Chain 2: Iteration: 10 / 100 [ 10%] (Warmup)
Chain 2: Iteration: 20 / 100 [ 20%] (Warmup)
Chain 2: Iteration: 30 / 100 [ 30%] (Warmup)
Chain 2: Iteration: 40 / 100 [ 40%] (Warmup)
Chain 2: Iteration: 50 / 100 [ 50%] (Warmup)
Chain 2: Iteration: 51 / 100 [ 51%] (Sampling)
Chain 2: Iteration: 60 / 100 [ 60%] (Sampling)
Chain 2: Iteration: 70 / 100 [ 70%] (Sampling)
Chain 2: Iteration: 80 / 100 [ 80%] (Sampling)
Chain 2: Iteration: 90 / 100 [ 90%] (Sampling)
Chain 2: Iteration: 100 / 100 [100%] (Sampling)
Chain 2:
Chain 2: Elapsed Time: 0 seconds (Warm-up)
Chain 2: 0 seconds (Sampling)
Chain 2: 0 seconds (Total)
Chain 2:
SAMPLING FOR MODEL 'anon_model' NOW (CHAIN 3).
Chain 3:
Chain 3: Gradient evaluation took 0 seconds
Chain 3: 1000 transitions using 10 leapfrog steps per transition would take 0 seconds.
Chain 3: Adjust your expectations accordingly!
Chain 3:
Chain 3:
Chain 3: WARNING: There aren't enough warmup iterations to fit the
Chain 3: three stages of adaptation as currently configured.
Chain 3: Reducing each adaptation stage to 15%/75%/10% of
Chain 3: the given number of warmup iterations:
Chain 3: init_buffer = 7
Chain 3: adapt_window = 38
Chain 3: term_buffer = 5
Chain 3:
Chain 3: Iteration: 1 / 100 [ 1%] (Warmup)
Chain 3: Iteration: 10 / 100 [ 10%] (Warmup)
Chain 3: Iteration: 20 / 100 [ 20%] (Warmup)
Chain 3: Iteration: 30 / 100 [ 30%] (Warmup)
Chain 3: Iteration: 40 / 100 [ 40%] (Warmup)
Chain 3: Iteration: 50 / 100 [ 50%] (Warmup)
Chain 3: Iteration: 51 / 100 [ 51%] (Sampling)
Chain 3: Iteration: 60 / 100 [ 60%] (Sampling)
Chain 3: Iteration: 70 / 100 [ 70%] (Sampling)
Chain 3: Iteration: 80 / 100 [ 80%] (Sampling)
Chain 3: Iteration: 90 / 100 [ 90%] (Sampling)
Chain 3: Iteration: 100 / 100 [100%] (Sampling)
Chain 3:
Chain 3: Elapsed Time: 0 seconds (Warm-up)
Chain 3: 0 seconds (Sampling)
Chain 3: 0 seconds (Total)
Chain 3:
SAMPLING FOR MODEL 'anon_model' NOW (CHAIN 4).
Chain 4:
Chain 4: Gradient evaluation took 0 seconds
Chain 4: 1000 transitions using 10 leapfrog steps per transition would take 0 seconds.
Chain 4: Adjust your expectations accordingly!
Chain 4:
Chain 4:
Chain 4: WARNING: There aren't enough warmup iterations to fit the
Chain 4: three stages of adaptation as currently configured.
Chain 4: Reducing each adaptation stage to 15%/75%/10% of
Chain 4: the given number of warmup iterations:
Chain 4: init_buffer = 7
Chain 4: adapt_window = 38
Chain 4: term_buffer = 5
Chain 4:
Chain 4: Iteration: 1 / 100 [ 1%] (Warmup)
Chain 4: Iteration: 10 / 100 [ 10%] (Warmup)
Chain 4: Iteration: 20 / 100 [ 20%] (Warmup)
Chain 4: Iteration: 30 / 100 [ 30%] (Warmup)
Chain 4: Iteration: 40 / 100 [ 40%] (Warmup)
Chain 4: Iteration: 50 / 100 [ 50%] (Warmup)
Chain 4: Iteration: 51 / 100 [ 51%] (Sampling)
Chain 4: Iteration: 60 / 100 [ 60%] (Sampling)
Chain 4: Iteration: 70 / 100 [ 70%] (Sampling)
Chain 4: Iteration: 80 / 100 [ 80%] (Sampling)
Chain 4: Iteration: 90 / 100 [ 90%] (Sampling)
Chain 4: Iteration: 100 / 100 [100%] (Sampling)
Chain 4:
Chain 4: Elapsed Time: 0 seconds (Warm-up)
Chain 4: 0 seconds (Sampling)
Chain 4: 0 seconds (Total)
Chain 4:
The more a dependent Markov chain behaves like an independent sample, the smaller the error in the resulting posterior approximation.
Effective sample size (N_{\text{eff}}): the number of independent sample values it would take to produce an equivalently accurate posterior approximation.
Effective sample size ratio:
\frac{N_{\text{eff}}}{N}
We will use the neff_ratio() function to find this ratio.
In our example data,
[1] 0.3462257
[1] 0.4950171
Autocorrelation allows us to evaluate if our Markov chain sufficiently mimics the behavior of an independent sample.
Autocorrelation:
Autocorrelation allows us to evaluate if our Markov chain sufficiently mimics the behavior of an independent sample.
Strong autocorrelation or dependence is a bad thing.
No obvious patterns in the trace plot; dependence is relatively weak.
Autocorrelation plot quickly drops off and is effectively 0 by lag 5.
Confirmation that our Markov chain is mixing quickly.
This is an “unhealthy” Markov chain.
Trace plot shows strong trends \to autocorrelation in the Markov chain values.
Slow decrease in autocorrelation plot indicates that the dependence between chain values does not quickly fade away.
\hat{R} \approx \sqrt{\frac{\text{var}_{\text{combined}}}{\text{var}_{\text{within}}}}
\hat{R} \approx \sqrt{\frac{\text{var}_{\text{combined}}}{\text{var}_{\text{within}}}}
rhat() function from the bayesplot package to find \hat{R}.We can see that our simulation is stable.
If we were to find \hat{R} for the other (obviously bad) simulation, it would be 5.35 😱
Take a break!
Back in 10 minutes.
Now that we know how to find posterior distributions, we will discuss how to use them to make inference.
Imagine you find yourself standing at the Museum of Modern Art (MoMA) in New York City, captivated by the artwork in front of you.
Let \pi denote the proportion of artists represented in major U.S. modern art museums that are Gen X or younger.
| artist | country | birth | death | alive | genx | gender | count | year_acquired_min | year_acquired_max |
|---|---|---|---|---|---|---|---|---|---|
| Ad Gerritsen | dutch | 1940 | 2015 | FALSE | FALSE | male | 1 | 1981 | 1981 |
| Kirstine Roepstorff | danish | 1972 | NA | TRUE | TRUE | female | 3 | 2005 | 2005 |
| Lisa Baumgardner | american | 1958 | 2015 | FALSE | FALSE | female | 2 | 2016 | 2016 |
| David Bates | american | 1952 | NA | TRUE | FALSE | male | 1 | 2001 | 2001 |
| Simon Levy | american | 1946 | NA | TRUE | FALSE | male | 1 | 2012 | 2012 |
| Pierre Mercure | canadian | 1927 | 1966 | FALSE | FALSE | male | 8 | 2008 | 2008 |
\begin{align*} Y|\pi &\sim \text{Bin}(100,\pi) \\ \pi &\sim \text{Beta}(4,6) \\ \pi | (Y = 14) &\sim \text{Beta(18,92)} \end{align*}
| model | alpha | beta | mean | mode | var | sd |
|---|---|---|---|---|---|---|
| prior | 4 | 6 | 0.4000000 | 0.3750000 | 0.0218182 | 0.1477098 |
| posterior | 18 | 92 | 0.1636364 | 0.1574074 | 0.0012330 | 0.0351137 |
P\left[ \theta \in (\theta_{0.025}, \theta_{0.975})|Y=y \right] = \int_{\theta_{0.025}}^{\theta_{0.975}} f(\theta|y) d\theta = 0.95
P\left[ \pi < 0.2 | Y = 14 \right] = \int_0^{0.2} f(\pi|y = 14) d\pi
P\left[ \pi < 0.2 | Y = 14 \right] = \int_0^{0.2} f(\pi|y = 14) d\pi
pbeta() function:pbeta() function:P\left[ \pi < 0.2 | Y = 14 \right] = 0.849
P\left[ \pi < 0.2 \right] = \int_0^{0.2} f(\pi) d\pi
pbeta() function:pbeta() function:P\left[ \pi < 0.2 \right] = 0.086
| Hypotheses | Prior Probability | Posterior Probability |
|---|---|---|
| H_0: \pi \ge 0.2 | P[H_0] = 0.914 | P[H_0 \mid Y = 14] = 0.151 |
| H_1: \pi < 0.2 | P[H_1] = 0.086 | P[H_1 \mid Y = 14] = 0.849 |
\text{posterior odds} = \frac{P\left[ H_1 | Y = y \right]}{P\left[ H_0 | Y = y \right]}, \ \ \ \ \ \ \ \text{prior odds} = \frac{P\left[ H_1 \right]}{P\left[ H_0 \right]}
| Hypotheses | Prior Probability | Posterior Probability |
|---|---|---|
| H_0: \pi \ge 0.2 | P[H_0] = 0.914 | P[H_0 \mid Y = 14] = 0.151 |
| H_1: \pi < 0.2 | P[H_1] = 0.086 | P[H_1 \mid Y = 14] = 0.849 |
\begin{align*} \text{posterior odds} &= \frac{P\left[ H_1 | Y = 14 \right]}{P\left[ H_0 | Y = 14 \right]} \\ &= \frac{0.849}{0.151} \\ &\approx 5.62 \end{align*}
\begin{align*} \text{posterior odds} &= \frac{P\left[ H_1 | Y = 14 \right]}{P\left[ H_0 | Y = 14 \right]} \\ &= \frac{0.849}{0.151} \\ &\approx 5.62 \end{align*}
| Hypotheses | Prior Probability | Posterior Probability |
|---|---|---|
| H_0: \pi \ge 0.2 | P[H_0] = 0.914 | P[H_0 \mid Y = 14] = 0.151 |
| H_1: \pi < 0.2 | P[H_1] = 0.086 | P[H_1 \mid Y = 14] = 0.849 |
\begin{align*} \text{prior odds} &= \frac{P\left[ H_1 \right]}{P\left[ H_0 \right]} \\ &= \frac{0.086}{0.914} \\ &\approx 0.093 \end{align*}
\begin{align*} \text{prior odds} &= \frac{P\left[ H_1 \right]}{P\left[ H_0 \right]} \\ &= \frac{0.086}{0.914} \\ &\approx 0.093 \end{align*}
\text{Bayes Factor} = \frac{\text{posterior odds}}{\text{prior odds}} = \frac{P\left[H_1 | Y\right] / P\left[H_0 | Y\right]}{P\left[H_1\right] / P\left[H_0\right]}
\text{Bayes Factor} = \frac{\text{posterior odds}}{\text{prior odds}} = \frac{P\left[H_1 | Y\right] / P\left[H_0 | Y\right]}{P\left[H_1\right] / P\left[H_0\right]}
| Hypotheses | Prior Probability | Posterior Probability |
|---|---|---|
| H_0: \pi \ge 0.2 | P[H_0] = 0.914 | P[H_0 \mid Y = 14] = 0.151 |
| H_1: \pi < 0.2 | P[H_1] = 0.086 | P[H_1 \mid Y = 14] = 0.849 |
\begin{align*} \text{Bayes Factor} &= \frac{\text{posterior odds}}{\text{prior odds}} = \frac{P\left[H_1 | Y\right] / P\left[H_0 | Y\right]}{P\left[H_1\right] / P\left[H_0\right]} \\ &= \frac{5.62}{0.09} \\ &\approx 62.44 \end{align*}
| Hypotheses | Prior Probability | Posterior Probability |
|---|---|---|
| H_0: \pi \ge 0.2 | P[H_0] = 0.914 | P[H_0 \mid Y = 14] = 0.151 |
| H_1: \pi < 0.2 | P[H_1] = 0.086 | P[H_1 \mid Y = 14] = 0.849 |
It’s time to draw a conclusion!
Hypotheses:
Evidence:
Conclusion:
\begin{align*} H_0&: \ \pi = 0.3 \\ H_1&: \ \pi \ne 0.3 \end{align*}
\begin{align*} H_0&: \ \pi = 0.3 \\ H_1&: \ \pi \ne 0.3 \end{align*}
P\left[ \pi =0.3 | Y = 14 \right] = \int_{0.3}^{0.3} f(\pi|y = 14) d\pi = 0
P\left[ \pi =0.3 | Y = 14 \right] = \int_{0.3}^{0.3} f(\pi|y = 14) d\pi = 0
\text{posterior odds} = \frac{P\left[ H_1 | Y = 14 \right]}{P\left[ H_0 | Y = 14 \right]} = \frac{1}{0}
Welp.
Let’s think about the 95% posterior credible interval for \pi: (0.10, 0.24).
\begin{align*} H_0&: \ \pi \in (0.25, 0.35) \\ H_1&: \ \pi \not\in (0.25, 0.35) \end{align*}
In addition to estimating the posterior and using the posterior distribution for hypothesis testing, we may be interested in predicting the outcome in a new dataset.
Suppose we get our hands on data for 20 more artworks displayed at the museum.
Based on the posterior understanding of \pi that we’ve developed throughout this chapter, what number would you predict are done by artists that are Gen X or younger?
Suppose we get our hands on data for 20 more artworks displayed at the museum.
Based on the posterior understanding of \pi that we’ve developed throughout this chapter, what number would you predict are done by artists that are Gen X or younger?
Y'|\pi \sim \text{Bin}(20, \pi)
f(y'|\pi) = P[Y' = y'|\pi] = {20\choose{y'}} \pi^{y'} (1-\pi)^{20-y'}
f(y'|\pi) f(\pi|y=14)
f(y'|y) = \int f(y'|\pi) f(\pi|y) \ d \pi
# STEP 2: SIMULATE the posterior
art_sim <- stan(model_code = art_model,
data = list(Y = 14),
chains = 4,
iter = 5000*2,
seed = 84735)
SAMPLING FOR MODEL 'anon_model' NOW (CHAIN 1).
Chain 1:
Chain 1: Gradient evaluation took 4e-06 seconds
Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.04 seconds.
Chain 1: Adjust your expectations accordingly!
Chain 1:
Chain 1:
Chain 1: Iteration: 1 / 10000 [ 0%] (Warmup)
Chain 1: Iteration: 1000 / 10000 [ 10%] (Warmup)
Chain 1: Iteration: 2000 / 10000 [ 20%] (Warmup)
Chain 1: Iteration: 3000 / 10000 [ 30%] (Warmup)
Chain 1: Iteration: 4000 / 10000 [ 40%] (Warmup)
Chain 1: Iteration: 5000 / 10000 [ 50%] (Warmup)
Chain 1: Iteration: 5001 / 10000 [ 50%] (Sampling)
Chain 1: Iteration: 6000 / 10000 [ 60%] (Sampling)
Chain 1: Iteration: 7000 / 10000 [ 70%] (Sampling)
Chain 1: Iteration: 8000 / 10000 [ 80%] (Sampling)
Chain 1: Iteration: 9000 / 10000 [ 90%] (Sampling)
Chain 1: Iteration: 10000 / 10000 [100%] (Sampling)
Chain 1:
Chain 1: Elapsed Time: 0.01 seconds (Warm-up)
Chain 1: 0.01 seconds (Sampling)
Chain 1: 0.02 seconds (Total)
Chain 1:
SAMPLING FOR MODEL 'anon_model' NOW (CHAIN 2).
Chain 2:
Chain 2: Gradient evaluation took 0 seconds
Chain 2: 1000 transitions using 10 leapfrog steps per transition would take 0 seconds.
Chain 2: Adjust your expectations accordingly!
Chain 2:
Chain 2:
Chain 2: Iteration: 1 / 10000 [ 0%] (Warmup)
Chain 2: Iteration: 1000 / 10000 [ 10%] (Warmup)
Chain 2: Iteration: 2000 / 10000 [ 20%] (Warmup)
Chain 2: Iteration: 3000 / 10000 [ 30%] (Warmup)
Chain 2: Iteration: 4000 / 10000 [ 40%] (Warmup)
Chain 2: Iteration: 5000 / 10000 [ 50%] (Warmup)
Chain 2: Iteration: 5001 / 10000 [ 50%] (Sampling)
Chain 2: Iteration: 6000 / 10000 [ 60%] (Sampling)
Chain 2: Iteration: 7000 / 10000 [ 70%] (Sampling)
Chain 2: Iteration: 8000 / 10000 [ 80%] (Sampling)
Chain 2: Iteration: 9000 / 10000 [ 90%] (Sampling)
Chain 2: Iteration: 10000 / 10000 [100%] (Sampling)
Chain 2:
Chain 2: Elapsed Time: 0.01 seconds (Warm-up)
Chain 2: 0.011 seconds (Sampling)
Chain 2: 0.021 seconds (Total)
Chain 2:
SAMPLING FOR MODEL 'anon_model' NOW (CHAIN 3).
Chain 3:
Chain 3: Gradient evaluation took 0 seconds
Chain 3: 1000 transitions using 10 leapfrog steps per transition would take 0 seconds.
Chain 3: Adjust your expectations accordingly!
Chain 3:
Chain 3:
Chain 3: Iteration: 1 / 10000 [ 0%] (Warmup)
Chain 3: Iteration: 1000 / 10000 [ 10%] (Warmup)
Chain 3: Iteration: 2000 / 10000 [ 20%] (Warmup)
Chain 3: Iteration: 3000 / 10000 [ 30%] (Warmup)
Chain 3: Iteration: 4000 / 10000 [ 40%] (Warmup)
Chain 3: Iteration: 5000 / 10000 [ 50%] (Warmup)
Chain 3: Iteration: 5001 / 10000 [ 50%] (Sampling)
Chain 3: Iteration: 6000 / 10000 [ 60%] (Sampling)
Chain 3: Iteration: 7000 / 10000 [ 70%] (Sampling)
Chain 3: Iteration: 8000 / 10000 [ 80%] (Sampling)
Chain 3: Iteration: 9000 / 10000 [ 90%] (Sampling)
Chain 3: Iteration: 10000 / 10000 [100%] (Sampling)
Chain 3:
Chain 3: Elapsed Time: 0.01 seconds (Warm-up)
Chain 3: 0.011 seconds (Sampling)
Chain 3: 0.021 seconds (Total)
Chain 3:
SAMPLING FOR MODEL 'anon_model' NOW (CHAIN 4).
Chain 4:
Chain 4: Gradient evaluation took 1e-06 seconds
Chain 4: 1000 transitions using 10 leapfrog steps per transition would take 0.01 seconds.
Chain 4: Adjust your expectations accordingly!
Chain 4:
Chain 4:
Chain 4: Iteration: 1 / 10000 [ 0%] (Warmup)
Chain 4: Iteration: 1000 / 10000 [ 10%] (Warmup)
Chain 4: Iteration: 2000 / 10000 [ 20%] (Warmup)
Chain 4: Iteration: 3000 / 10000 [ 30%] (Warmup)
Chain 4: Iteration: 4000 / 10000 [ 40%] (Warmup)
Chain 4: Iteration: 5000 / 10000 [ 50%] (Warmup)
Chain 4: Iteration: 5001 / 10000 [ 50%] (Sampling)
Chain 4: Iteration: 6000 / 10000 [ 60%] (Sampling)
Chain 4: Iteration: 7000 / 10000 [ 70%] (Sampling)
Chain 4: Iteration: 8000 / 10000 [ 80%] (Sampling)
Chain 4: Iteration: 9000 / 10000 [ 90%] (Sampling)
Chain 4: Iteration: 10000 / 10000 [100%] (Sampling)
Chain 4:
Chain 4: Elapsed Time: 0.01 seconds (Warm-up)
Chain 4: 0.01 seconds (Sampling)
Chain 4: 0.02 seconds (Total)
Chain 4:
As we saw previously, the posterior was Beta(18, 92).
We will use the tidy() function from the broom.mixed package.
| term | estimate | std.error | conf.low | conf.high |
|---|---|---|---|---|
| pi | 0.1635453 | 0.0353673 | 0.1000882 | 0.2391218 |
The approximate middle 95% CI for \pi is (0.100, 0.239).
Our approximation of the actual posterior median is 0.162.
mcmc_areas() function from the bayesrules package to get a corresponding graph,tidy() does not give everything we may be interested in.
art_chains_df %>%
summarize(post_mean = mean(pi),
post_median = median(pi),
post_mode = sample_mode(pi),
lower_95 = quantile(pi, 0.025),
upper_95 = quantile(pi, 0.975))| post_mean | post_median | post_mode | lower_95 | upper_95 |
|---|---|---|---|---|
| 0.1635453 | 0.1615222 | 0.1583033 | 0.1000882 | 0.2391218 |
tidy() (and then some!)Now that we have saved the Markov chain values, we can use them to answer questions about the data.
Recall, we were interested in testing the claim that fewer than 20% of major museum artists are Gen X.
| exceeds | n | percent |
|---|---|---|
| FALSE | 3061 | 0.15305 |
| TRUE | 16939 | 0.84695 |
From this, we can see that MCMC gave us an accurate approximation.
We should use this as “proof” that the approximations are “reliable” for non-conjugate families.
STA6349 · Applied Bayesian Analysis · Summer 2026