CommencerCommencer gratuitement

Sampling with replacement

Bootstrapping is great for calculating confidence intervals for means; you'll now practice doing just that!

nba_weights 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]

You are interested in calculating the 95% confidence interval of the mean weight of NBA players using this list.

The following have been imported for you: random, and numpy as np.

Cet exercice fait partie du cours

Monte Carlo Simulations in Python

Afficher le cours

Instructions

  • Use random.choices() to sample nine heights from the list 1,000 times, with replacement.
  • Calculate the mean and 95% confidence interval for your simulation results, assigning the lower end of the confidence interval to lower and the upper end to upper.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

simu_weights = []

# Sample nine values from nba_weights with replacement 1000 times
for i in range(____):
    bootstrap_sample = ____
    simu_weights.append(np.mean(bootstrap_sample))

# Calculate the mean and 95% confidence interval of the mean for your results
mean_weight = ____
upper = ____
lower = ____
print(mean_weight, lower, upper)
Modifier et exécuter le code