1

I am trying to work through https://rpruim.github.io/Kruschke-Notes/bayes-rule-and-the-grid-method.html and was confused about the normalized posterior and likelihood calculation

library(ggplot)
library(tidyverse)
library(ggformula)
library(triangle)
library(purrr)
x <- 1; n <- 4
CoinsGrid <- 
  expand.grid(
    theta = seq(0, 1, by = 0.001)
  ) %>%
  mutate(
    prior = dtriangle(theta),        # triangle distribution
    likelihood  = map_dbl(theta, ~ dbinom(x = x, size = n, .x)),
    likelihood1 = likelihood / sum(likelihood) / 0.001, # "normalized"
    posterior0 = prior * likelihood,                    # unnormalized
    posterior = posterior0 / sum(posterior0) / 0.001    # normalized
  )

gf_area( prior ~ theta, data = CoinsGrid, alpha = 0.3) %>%
  gf_area( likelihood1 ~ theta, data = CoinsGrid, alpha = 0.3, fill = "green") %>%
  gf_area( posterior ~ theta, data = CoinsGrid, alpha = 0.3, fill = "steelblue") 

I understand why the normalization is needed but unable to get the intuition behind why the author dividing by 0.001?

likelihood1 = likelihood / sum(likelihood) / 0.001

My hunch is that it might be due of the grid size, but i appreciate any help wrt the intuition.

1 Answers1

0

If you want to normalize the likelihood, you want to make the area under the likelihood curve to be 1.

The current Area without normalization= SUM(likelihood * discretized theta space) !=1 (most time). If it is not 1, then divide by the area itself, it will become 1!

The desired Area with normalization=SUM(likelihood1*discretized theta space) = SUM(likelihood1)*discretized theta space

How to make it to be 1? You simply divide the area value, which is simply approximated by SUM(likelihood * discretized theta space), here the discretized theta space=0.001. The sum() will further equal to SUM(likelihood)*discretized theta space(0.001).

So likelihood1 = likelihood / (sum(likelihood) * 0.001)=likelihood / sum(likelihood) / 0.001.

Similar approach was done for posterior probability.