I tried looking for my specific question, but I only found partially related questions here, here, and here. I think my question is much simpler than what was asked and answered in these queries. I'm working through Statistical Rethinking by McElreath and there is a portion where they explain grid approximation for Bayesian statistics. The author uses this code to generate and plot the data, though I've made some minor formatting changes so it's more readable:
#### Define Grid ####
p_grid <- seq( from=0 , to=1 , length.out=20 )
#### Define Prior ####
prior <- rep( 1 , 20 )
#### Compute Likelihood at Each Value in Grid ####
likelihood <- dbinom( 6 , size=9 , prob=p_grid )
#### Compute Product of Likelihood and Prior ####
unstd.posterior <- likelihood * prior
#### Standardize the Posterior (Sums to 1) ####
posterior <- unstd.posterior / sum(unstd.posterior)
#### Plot Grid Approximation ####
plot( p_grid ,
posterior ,
type="b" ,
xlab="probability of water" ,
ylab="posterior probability")
mtext("20 points")
This is the plot:
Conceptually, I get most of what is going on here. The code creates a flat prior, a likelihood of a result based off given arguments, and a resulting prior distribution. But my main question is what the grid here does. I know that p_grid
takes a 20 number sequence from 0 to 1, but I don't quite understand why this is done.
p_grid
, at which values would you evaluatedbinom
? How would you compute the posterior? $\endgroup$p
with 20 discrete hypotheses (p=0, p=.01,...,p=1
) and does inference over them instead, since this is simpler. $\endgroup$