Get startedGet started for free

Calculating a joint distribution

The parts of the code we developed in the last video is displayed. It defines a grid over the underlying proportions of clicks (proportion_clicks) and possible outcomes (n_visitors) in pars. It adds to it the prior probability of each parameter combination and the likelihood that each proportion_clicks would generate the corresponding n_visitors.

This exercise is part of the course

Fundamentals of Bayesian Data Analysis in R

View Course

Exercise instructions

  • Add the column pars$probability: The probability of each proportion_clicks and n_visitors combination. As in the video, this should be calculated by multiplying the likelihood by the prior
  • Make sure the column pars$probability sums to 1.0 by normalizing it, that is, by dividing pars$probability by the total sum of pars$probability.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

n_ads_shown <- 100
proportion_clicks <- seq(0, 1, by = 0.01)
n_visitors <- seq(0, 100, by = 1)
pars <- expand.grid(proportion_clicks = proportion_clicks,
                    n_visitors = n_visitors)
pars$prior <- dunif(pars$proportion_clicks, min = 0, max = 0.2)
pars$likelihood <- dbinom(pars$n_visitors, 
    size = n_ads_shown, prob = pars$proportion_clicks)

# Add the column pars$probability and normalize it
pars$probability <- ___
Edit and Run Code