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.
Diese Übung ist Teil des Kurses
Monte Carlo Simulations in Python
Anleitung zur Übung
- 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
lowerand the upper end toupper.
Interaktive Übung
Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.
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)