# Understanding Bayes for a one-sample proportion

I am reading R. McElreath's "Statistical Rethinking." In this book there is an example of tossing and catching a globe noting a 0 for land and 1 for water. The author then proceeds to describe grid-approximation as a means estimating the posterior. The 'R' code is a follows:

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

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

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

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

#standardize the posterior
posterior <- unstd.posterior/ sum(unstd.posterior)

#check standardization
sum(posterior)

#plot 1
plot(p_grid, posterior, type = "b",
xlab = "Probability of water",
ylab = "Posterior probability")
mtext("Plot 1")


My understanding of the likelihood, is that it represents the prior conditioned on the data. In this case, 6 water hits in 9 trials. If so, why is the value p_grid the variable in the call to dbinom()? In the example, the prior is flat at 1, but it seems to me the prior is contained in p_grid. Lastly, can I interpret the plot as the probability that water covers 100% of the planet when the prior is 1?

Each observation $y_i$ is a binomial random variable with pmf $f(y_i ; p) = p^{y_i}(1-p)^{1-y_i}$, so the likelihood is $$f(y_1, \ldots, y_9 \mid p) = \prod_{i=1}^9 f(y_i \mid p) = p^{\sum_i y_i}(1-p)^{9-\sum_i y_i} = p^6(1-p)^3.$$

...represents the prior conditioned on the data

No, the prior is totally separate from the likelihood. It represents the prior knowledge you have about the unknown parameter $p$, before any data comes. In this example, $p \sim \text{Uniform}(0,1)$, or $$\pi(p) = 1$$ as long as $0<p<1$.

If so, why is the value p_grid the variable in the call to dbinom()?

R's function dbinom is vectorized, so it will evaluate $f(y_1, \ldots, y_9 \mid p)$ for all $p$ in the vector p_grid. The likelihood is a continuous function in $p$, and it can come up with a scalar output for any real number between $0$ and $1$. Since there are an uncountable number of possible inputs, we just pick $20$ of them and put them into p_grid.

They are doing the same with the prior. Because this function always evaluates to $1$, we don't need to call any function to get the prior evaluation. We can just repeat $1$ $20$ times (once for each $p$ in p_grid).

Lastly, can I interpret the plot as the probability that water covers 100% of the planet when the prior is 1?

The posterior is a $\text{Beta}(7,4)$ distribution. The posterior in this case is a density, so its height is not a probability. Areas under this curve are probabilities for the areas over which you are integrating.

Also, it is not for water; it is for $p$. If you integrate over regions, these are regions in the parameter space $(0,1)$. So if you integrate over the interval $(0,.5)$, you found find $P(0 < p < .5 \mid Y_1 = y_1, \ldots, Y_9 = y_9)$, for example.

• What “named” part of the process is represented by “p_grid”? If p_grid is not conditioning the likelihood (seems that it is), not the likelihood itself, nor the prior, then from where do p_grid values arise and enter the inference? Commented Jul 23, 2018 at 4:55
• @Todd you cannot store inside a computer all possible values of $p$, and you can’t store the possible values of either function they map to. P_grid is just a vector of possible values of $p$. See how it’s the x axis in the plot? You are using the word “conditioning” in a weird way, by the way. Commented Jul 23, 2018 at 5:58
• Is this correct: $p$ are the unknowns that I am modeling. My model is comprised of the prior and the likelihood. The likelihood derives from the data and the values of $p$ I want to examine. Commented Jul 23, 2018 at 13:26
• @Todd yeah that sounds pretty good. The only issues I take are with the word “derives,” and the fact that there is only one $p$ (so you should say “value” instead of “values”) but I think that’s right Commented Jul 23, 2018 at 14:42
• There is only one true $p$ and I can examine as many possible values of $p$ as I wish? Commented Jul 23, 2018 at 15:08