In this question, we’re going to explore what happens when we add two normally distributed variables together. You can use numpy.random.normal(mean, standard_deviation, n) to create a random sample of n ob- servations. Submit your code with your solutions. (a) Use the command above to create two numpy arrays of length 10,000 drawn from two different dis- tributions. For the first one, use X „ N(100,152), i.e. μX = 100,σX = 15. For the second one, use Y „ N(50,232), i.e. μY = 50,σY = 23. (b) Create a new variable Z = X + Y by adding the two numpy arrays together. Plot a histogram to show Z. Plot a histogram of Z using matplotlib. (c) Now compute the mean μZ and standard deviation σZ of Z. a (d) Compare your answers to the values μX + μY and σX2 + σY2 . What do you notice?
Sure, let's go through the steps one by one using Python code.
(a) First, we'll create two numpy arrays of length 10,000 drawn from the specified normal distributions.
import numpy as np
import matplotlib.pyplot as plt
# Set the seed for reproducibility
np.random.seed(0)
# Create two numpy arrays of length 10,000
X = np.random.normal(loc=100, scale=15, size=10000)
Y = np.random.normal(loc=50, scale=23, size=10000)
(b) Now, we'll create a new variable Z by adding the two numpy arrays together and plot a histogram of Z.
# Create Z by adding X and Y
Z = X + Y
# Plot a histogram of Z
plt.hist(Z, bins=50, alpha=0.75)
plt.title('Histogram of Z')
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.show()
(c) Next, we'll compute the mean (μZ) and standard deviation (σZ) of Z.
# Compute the mean and standard deviation of Z
mu_Z = np.mean(Z)
sigma_Z = np.std(Z)
print(f"Mean of Z: {mu_Z}")
print(f"Standard Deviation of Z: {sigma_Z}")
(d) Finally, we'll compare these values to the theoretical values μX + μY and √(σX^2 + σY^2).
# Theoretical mean and standard deviation
theoretical_mu_Z = 100 + 50
theoretical_sigma_Z = np.sqrt(15**2 + 23**2)
print(f"Theoretical Mean of Z: {theoretical_mu_Z}")
print(f"Theoretical Standard Deviation of Z: {theoretical_sigma_Z}")
When you run this code, you should see that the empirical mean and standard deviation of Z are close to the theoretical values, which demonstrates the properties of the sum of two normally distributed variables. The mean of the sum is the sum of the means, and the variance of the sum is the sum of the variances. The standard deviation is the square root of the variance.