In: Computer Science
Question no 3: USE PYTHON
a. Generate a discrete uniform distribution of population size 100 between interval (1,10).)
b Consider the sample size of N=10, Simulate the sampling distribution of the sample mean. (repeat 100 times)
c Consider the sample size of N=30, what is the sample mean and sample standard deviation? (repeat 100 times)
a. Generate a discrete uniform distribution of
population size 100 between interval (1,10).
Solution for part A:
import random
array = [random.randint(1,10) for i in range (100)]
This will produce a uniform distribution of size 100 with values between 1 and 10
b Consider the sample size of N=10, Simulate the sampling distribution of the sample mean. (repeat 100 times)
The simulation of sampling distribution of sample mean showing 100 histogram for each sampling:
import random
import statistics
import matplotlib.pyplot as plt
array = [random.randint(1, 10) for i in range(100)]
print(array)
for i in range(100):
random.shuffle(array)
mean = sum(array[:10]) / 10
result = plt.hist(array[0:10], bins=20, color='c', edgecolor='k', alpha=0.65)
plt.axvline(statistics.mean(array[0:10]), color='k', linestyle='dashed', linewidth=1)
plt.show()
c Consider the sample size of N=30, what is the sample mean and sample standard deviation? (repeat 100 times)
The following program will take 30 samples 100 time and each time print the mean and standard deviation.
import random
import statistics
array = [random.randint(1, 10) for i in range(100)]
print(array)
for i in range(100):
random.shuffle(array)
sample_mean = sum(array[:30]) / 30
standard_dev=statistics.stdev(array[:30])
print("The mean of sample size of 30 is {} and its standard deviation is {}".format("%.2f" % sample_mean,"%.2f" % standard_dev))