library(tidyverse)
library(ggthemes)

# set plot theme
old <- theme_set(theme_tufte() + theme(text = element_text(family = "Menlo")))

# number of ads shown
n_ads_shown <- 100

# parameters: number of visitors & proportional clicks
n_visitors <- seq(0, 100, 1)
prop_clicks <- seq(0, 1, 0.01)
pars <- expand.grid(prop_clicks = prop_clicks, 
                    n_visitors = n_visitors) %>% 
  sample_frac(1L) %>% 
  as_tibble()

# setup joint probability distribution
pars <- pars %>% 
  # assume an uniform distribution
  mutate(prior = dunif(prop_clicks, min = 0, max = .2),
         # calc the likelihood of getting x number of visitors given props click
         likelihood = dbinom(n_visitors, size = n_ads_shown, prob = prop_clicks),
         # probability is thus prior * likelihood
         probs = prior * likelihood, probs = probs / sum(probs))
pars
## # A tibble: 10,201 x 5
##    prop_clicks n_visitors prior likelihood    probs
##          <dbl>      <dbl> <dbl>      <dbl>    <dbl>
##  1       0.6           85     0   3.78e- 8 0.      
##  2       0.8           31     0   3.88e-26 0.      
##  3       0.75          23     0   1.46e-27 0.      
##  4       0.07          29     5   2.31e-11 1.10e-12
##  5       0.68          35     0   1.03e-11 0.      
##  6       0.01          19     5   5.86e-19 2.79e-20
##  7       0.48          22     0   5.02e- 8 0.      
##  8       0.71          20     0   5.57e-26 0.      
##  9       0.9           30     0   1.25e-46 0.      
## 10       0.290         68     0   6.90e-16 0.      
## # ... with 10,191 more rows
# update prior based on posterior
pars %>% 
    filter(n_visitors == 6) %>% 
    ggplot(aes(prop_clicks, probs)) + 
    geom_bar(stat = "identity", width = 0.005) + 
    labs(x = "Proportional Clicks", y = "Probability")