Skip to content

Effortlessly Generate Python Sample with Replacement

[

Python Sample with Replacement

In this Python tutorial, we will learn about sampling with replacement and how to calculate confidence intervals using Monte Carlo simulations. We will use the random.choices() function from the random module and the numpy library to perform the calculations.

Sampling with Replacement

Sampling with replacement is a technique used in Monte Carlo simulations where we randomly select elements from a dataset, allowing for the possibility of selecting the same element multiple times. This simulates the process of drawing samples from a population with replacement.

Let’s begin by defining a list of weights of NBA players:

import random
import numpy as np
nba_weights = [96.7, 101.1, 97.9, 98.1, 98.1, 100.3, 101.0, 98.0, 97.4]

Now, we can use the random.choices() function to sample nine heights from this list with replacement. We will perform this sampling 1000 times to obtain a distribution of sample means:

sample_means = []
num_samples = 1000
for _ in range(num_samples):
sample = random.choices(nba_weights, k=9)
sample_mean = np.mean(sample)
sample_means.append(sample_mean)

Next, we can calculate the mean and the 95% confidence interval for our simulation results. We will use the numpy library for this calculation:

mean = np.mean(sample_means)
lower = np.percentile(sample_means, 2.5)
upper = np.percentile(sample_means, 97.5)

The np.percentile() function calculates the values that correspond to the given percentiles, in this case, the lower and upper percentiles for a 95% confidence interval.

Finally, we can print the results:

print("Mean: {:.2f}".format(mean))
print("95% Confidence Interval: [{:.2f}, {:.2f}]".format(lower, upper))

Complete Code

import random
import numpy as np
nba_weights = [96.7, 101.1, 97.9, 98.1, 98.1, 100.3, 101.0, 98.0, 97.4]
sample_means = []
num_samples = 1000
for _ in range(num_samples):
sample = random.choices(nba_weights, k=9)
sample_mean = np.mean(sample)
sample_means.append(sample_mean)
mean = np.mean(sample_means)
lower = np.percentile(sample_means, 2.5)
upper = np.percentile(sample_means, 97.5)
print("Mean: {:.2f}".format(mean))
print("95% Confidence Interval: [{:.2f}, {:.2f}]".format(lower, upper))

Running the above code will produce the mean and the 95% confidence interval of the mean weight of NBA players based on the given list.