Get startedGet started for free

Representing uncertainty with priors

1. One more part

It’s time to add one more part that we will need to do

2. Bayes needs this 4

Bayesian inference: a prior probability distribution, which represents how uncertain the model is about the parameters before seeing any data. And ideally, this uncertainty should reflect our uncertainty.

3. rbinom Bayesian model 1

Now, we know we’re going to run 100 ads, so that’s not uncertain, but that an ad gets clicked on 10% of the time, as the social media site claims, that is a curiously precise number. Especially when taking into account that not all ads are the same. Surely some ads perform better than others, and we don’t know how good our ad is. You are still interested in how many site visits the ad is going to generate, but since we really are uncertain about the underlying proportion of clicks we now want to include some of that uncertainty into the binomial model. There are many different probability distributions we could use to represent that uncertainty. For now, we’re going to assume that it’s equally likely that the underlying proportion of clicks could be as low as 0%, like, it’s a horrible ad, and nobody will ever click it, or as high as 20%, twice as good as the 10% claimed by the social media site. These assumptions translate into

4. rbinom Bayesian model 2

a uniform distribution from 0 to 0.2 over the underlying proportion of clicks. Now we need to change the

5. rbinom Bayesian model 2

R code that implements the model. Instead of assigning proportion_clicks a single value we are now going to assign it a large number of random samples from a uniform probability distribution. In R you can do that using

6. runif - Random Uniform samples

the runif function, standing for random uniform. For example,

7. runif - Random Uniform samples

this would sample just 6 values from 0 to 1.0 and put them into proportion_clicks. A neat thing is that because most random number functions are vectorized it’s very easy to chain probability distributions together in R. For example if we now use

8. runif - Random Uniform samples

proportion_clicks as the argument to rbinom, then rbinom will automatically loop over the values in proportion_clicks. If we

9. runif - Random Uniform samples

look at the resulting values in n_clicks, we can see this. The first value in proportion_clicks is used to sample the first value in n_clicks. As it’s just an 0.05 proportion of success the number of successes in n_clicks becomes only 7 out of a 100. The second value in proportion_clicks is used to sample the second in n_clicks, and so on. This means that the variability from runif gets combined with the variability of rbinom. The result is that the samples in n_clicks now also incorporates the uncertainty in proportion_clicks. That got a bit theoretical,

10. Try this in practice!

but now it’s time for you to try this out in practice in a couple of exercises.