import numpy as np
import matplotlib.pyplot as plt
# Set seed for reproducibility
np.random.seed(0)
# Define the population
population = np.linspace(1, 1000, 1000)
# Function to generate random samples and plot histograms
def generate_samples(n, k):
# Generate random samples from the population
samples = np.random.choice(population, size=(n, k), replace=True)
# Calculate mean and standard deviation for each sample
means = np.mean(samples, axis=1)
stds = np.std(samples, axis=1)
# Plot histograms for means and standard deviations
fig, axs = plt.subplots(2, 1, figsize=(8, 8))
axs[0].hist(means, bins=30)
axs[0].set_title("Histogram of Means (n={}, k={})".format(n, k))
axs[1].hist(stds, bins=30)
axs[1].set_title("Histogram of Standard Deviations (n={}, k={})".format(n, k))
# Show the plot
plt.tight_layout()
plt.show()
# Generate samples and plot histograms for different values of n and k
generate_samples(2, 10)
generate_samples(2, 100)
generate_samples(2, 1000)
generate_samples(20, 100)
generate_samples(20, 1000)
generate_samples(200, 100)
generate_samples(200, 1000)
Inside the function, we use the numpy.random.choice function to generate random samples from the population. We calculate the mean and standard deviation for each sample using the np.mean and np.std functions.
Next, we create a figure with two subplots using plt.subplots. We plot a histogram of means in the first subplot and a histogram of standard deviations in the second subplot using axs[0].hist and axs[1].hist respectively. We set appropriate titles for the subplots.
When you run the code, you will see separate histograms for means and standard deviations for each combination of n and k. The histograms will give you an idea of the distribution of means and standard deviations for the random samples generated from the population.