10.2 The grid method
The grid method is a simple brute-force approach. Even if you are not able to compute the whole posterior, you may be able to compute the prior and the likelihood point-wise; this is a pretty common scenario, if not the most common one.
Let’s assume we want to compute the posterior for a model with a single parameter. The grid approximation is as follows:
Define a reasonable interval for the parameter (the prior should give you a hint).
Place a grid of points (generally equidistant) on that interval.
For each point in the grid, multiply the likelihood and the prior.
Optionally, we may normalize the computed values, that is, we divide each value in the posterior
array by the total area under the curve, ensuring that the total area equals 1.
The following code block implements the grid method for the coin-flipping model:
Code 10.1
def posterior_grid(grid_points=50, heads=6, tails=9):
"""
...