Question: Problem # 1 Write a Python function named CentralLimitDemoExp that takes as input arguments: n - the number of samples to be taken from an

Problem #1
Write a Python function named CentralLimitDemoExp that takes as input arguments:
n - the number of samples to be taken from an exponential distribution,
sample_sizes - a 1 x 6 numpy array holding the size of each of the n samples for that value of n different size of each sample for a fixed,
mu - the mean of the exponential random variable.
And returns as output:
fig - a figure displaying six histograms, one for each value of sample_size specified for the given value of n,
sample_means - a numpy array of size 1 x n holding the n sample_means,
sample_means_stdevs - a numpy array of size 1 x 6 holding the standard deviation of the list of sample_means.
Test your function by setting sample_sizes = np.array([4,50,100,500,1000,2000]) and running the command fig1, means1, devs1= CentralLimitDemoExp(1000, sample_sizes, 170).
import numpy as np
import matplotlib.pyplot as plt
import random
import statistics
from numpy.random import seed
def CentralLimitDemoExp(n, sample_sizes, mu):
# This line will re-initialize the random seed everytime the function is run and produce the same
# samples every time, you can comment it out
seed(1)
# Create a figure consisting of 6 subplots arranged into two rows and three columns
fig, axs = plt.subplots(2,3, figsize =(12,8))
fig.subplots_adjust(hspace =0.4)
fig.subplots_adjust(wspace =0.4)
# Figure indeces
axs_row =0
axs_col =0
# Create a numpy array to hold the standard deviation of the list of sample means (1 x 6)
sample_means_stdev = # Fill in code here
# Loop to generate n samples of sizes sample_sizes[0],..., sample_sizes[5]
# keep track of how many of the listed sample_sizes have been completed
k =0
for sample_size in sample_sizes:
# Create an np.array to hold all n means
sample_means = # Fill in code here
# Loop to generate the n samples of size sample_size
for j in range(0,n):
# Create a sample by drawing sample_size elements from an exponential distribution
sample = # Fill in code here
# Find its mean
sample_mean = # Fill in code here
# And add it to the list of sample_means
sample_means[j]= sample_mean
# Find the standard deviation of each of the six lists of n sample means and store them in sample_means_stdev
sample_means_stdev[k]= np.std(sample_means)
# Increase the count of completed sample_sizes
k +=1
# Generate the histogram of the n sample_means of size sample_size
# Histogram
axs[axs_row, axs_col].hist(sample_means, color = "green")
# Plot and axis labels
axs[axs_row, axs_col].set_title("sample_size =%s"% sample_size)
axs[axs_row, axs_col].set_ylabel("Frequency")
axs[axs_row, axs_col].set_xlabel("Observed Sample Mean")
# Figure title
fig.suptitle('Sample Mean Distribution for different values of sample_size')
# Advance figure row and column counters
if axs_col <2:
axs_col +=1
else:
axs_col =0
axs_row +=1
return fig, sample_means, sample_means_stdev

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!