Simulate beta posterior
In the upcoming few exercises, you will be using the simulate_beta_posterior()
function you saw defined in the last video. In this exercise, you will get a feel for what the function is doing by carrying out the computations it performs.
You are given a list of ten coin tosses, called tosses
, in which 1
stands for heads, 0
for tails, and we define heads as a "success". To simulate the posterior probability of tossing heads, you will use a beta prior. Recall that if the prior is \(Beta(a, b)\), then the posterior is \(Beta(x, y)\), with:
\(x = \text{NumberOfHeads} + a\)
\(y = \text{NumberOfTosses} - \text{NumberOfHeads} + b\)
This exercise is part of the course
Bayesian Data Analysis in Python
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Set prior parameters and calculate number of successes
beta_prior_a = ____
beta_prior_b = ____
num_successes = np.sum(____)
# Generate 10000 posterior draws
posterior_draws = np.random.beta(
____ + ____,
____ - ____ + ____,
10000)
# Plot density of posterior_draws
sns.kdeplot(posterior_draws, shade=True)
plt.show()