Get startedGet started for free

Simulating posterior draws

You have just decided to use a Beta(5, 2) prior for the efficacy rate. You are also using the binomial distribution to model the data (curing a sick patient is a "success", remember?). Since the beta distribution is a conjugate prior for the binomial likelihood, you can simply simulate the posterior!

You know that if the prior is \(Beta(a, b)\), then the posterior is \(Beta(x, y)\), with:

\(x = NumberOfSuccesses + a\),

\(y = NumberOfObservations - NumberOfSuccesses + b\).

Can you simulate the posterior distribution? Recall that altogether you have data on 22 patients, 19 of whom have been cured. numpy and seaborn have been imported for you as np and sns, respectively.

This exercise is part of the course

Bayesian Data Analysis in Python

View Course

Exercise instructions

  • Assign the numbers of patients treated and cured to num_patients_treated and num_patients_cured, respectively.
  • Use the appropriate numpy function to sample from the posterior distribution and assign the result to posterior_draws.
  • Plot the posterior distribution using the appropriate seaborn function.

Hands-on interactive exercise

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

# Define the number of patients treated and cured
num_patients_treated = ____
num_patients_cured = ____

# Simulate 10000 draws from the posterior distribuition
posterior_draws = ____(____ + ____, ____ - ____ + ____, 10000)

# Plot the posterior distribution
____(____, shade=True)
plt.show()
Edit and Run Code