Visualization of resampling results

Now you'll visualize the results of your simulation from the previous exercise! You'll continue working with nba_weights, which contains the weights of a group of NBA players in kilograms:

nba_weights = [96.7, 101.1, 97.9, 98.1, 98.1, 
               100.3, 101.0, 98.0, 97.4]

Here is your simulation code from the previous exercise:

simu_weights = []
for i in range(1000):
    bootstrap_sample = random.choices(nba_weights, k=9)
    simu_weights.append(np.mean(bootstrap_sample))
mean_weight = np.mean(simu_weights)
upper = np.quantile(simu_weights, 0.975)
lower = np.quantile(simu_weights, 0.025)
print(mean_weight, lower, upper)

The simu_weights list you generated in the last exercise is loaded for you. Similarly, mean_weight, lower and upper are already defined as the mean and 2.5% and 97.5% quantile values for your confidence interval.

The following packages have already been loaded for you: random, numpy as np, seaborn as sns, and matplotlib.pyplot as plt.

This exercise is part of the course

Monte Carlo Simulations in Python

View Course

Exercise instructions

  • Use sns.displot() to plot the distribution of the simulated weights.
  • Use plt.axvline() to plot two vertical lines for the 95% confidence interval (plot lower followed by upper) in red, and the mean in green.

Hands-on interactive exercise

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

# Plot the distribution of the simulated weights
____

# Plot vertical lines for the 95% confidence intervals and mean
plt.axvline(____, color="red")
plt.axvline(____, color="red")
plt.axvline(____, color="green")
plt.show()