Search for question
Question

Use the link in the Jupyter Notebook activity to access your Python script. Once you have made your calculations, complete this discussion. The script will output answers to the questions given

below. You must attach your Python script output as an HTML file and respond to the questions below. In this discussion, you will apply the central limit theorem and use principles of the Normal distribution to calculate probabilities. You will demonstrate two key parts of the central limit theorem: The distribution of sample means is approximately Normally distributed (bell-shaped) as the sample size increases and we repeatedly draw these samples, regardless of the shape of the population distribution from which the samples are drawn. The average of all sample means is equal to the population mean. In practice, the average of all sample means will closely approximate the population mean. You will generate a population data set representing total precipitation (TPCP) in tenths of a millimeter using Python's numpy module. The distribution of this data set will be skewed. This data set will be unique to each student, and therefore the answers will be unique as well. Run Step 1 in the Python script to generate your unique population data. In your initial post, address the following items: In the Python script, you created a histogram for the dataset generated in Step 1. Check to make sure that this data distribution is skewed and included in your attachment. See Step 2 in the Python script. What is the mean of the TPCP population data? See Step 3 in the Python script. In the Python script, you selected a random sample with replacement, of size 50 (note that this is a sufficiently large sample), from the TPCP population. What is the mean of your random sample? Does this sample mean closely approximate the TPCP population mean? See Step 4 in the Python script. You also selected 1,000 random samples of size 50 and calculated the mean of each sample. Then you stored those means into a dataframe. Check to make sure the output of this step is in your attachment. See Step 5 in the Python script. Review the plotted data distribution for these 1,000 means. Does this approximate a Normal distribution? Does this confirm the first part of the central limit theorem? Why or why not? See Step 6 in the Python script. What is the "grand" mean and standard deviation of these 1,000 means? Does the grand mean closely approximate (on a relative basis) the mean of the original distribution? Does this confirm the second part of the central limit theorem? Why or why not? See Step 7 in the Python script./nule Two Discussion: The Central Limit Theorem This notebook contains the step-by-step directions for your Module Two discussion. It is very important to run through the steps in order. Some steps depend on the outputs of earlier steps. Once you have completed the steps in this notebook, be sure to answer the questions about this activity in the Discussion for this module. Reminder: If you have not already reviewed the discussion prompt, please do so before beginning this activity. That will give you an idea of the questions you will need to answer with the outputs of this script. Initial post (due Thursday) Step 1: Generating population data This block of Python code will generate unique TPCP population data of size 500 observations. You will use this data set in this week's discussion. The numpy module in Python can be used to create datasets with a skewed distribution by randomly generating data from a gamma distribution. You do not need to know what a gamma distribution is or how a dataset is drawn from it. The dataset will be saved in a Python dataframe that you will use in later calculations. Click the block of code below and hit the Run button above. IN [ ]: import pandas as pd import matplotlib.pyplot as plt import numpy as np import scipy.stats as st ​ # use gamma distribution to randomly generate 500 observations. shape, scale = 1.95, 2.5 tpcp = 100*np.random.gamma (shape, scale, 500) ​ # pandas library can be used to convert the array into a dataframe of rounded figures with the column name TPCP. tpcp_df = pd.DataFrame (tpcp, columns=['TPCP']) tpcp_df = tpcp_df.round(0) ​ # print the dataframe to see the first 5 and last 5 observations (note that the index of dataframe starts at 0). print("TPCP data frame\n") print(tpcp_df) Step 2: Creating a histogram plot of population data You will use the matplotlib module in Python to create a histogram plot of the population data from Step 1. This plot allows you to visualize the population data distribution and confirm that it is skewed. You will use 50 bins in the histogram to display the distribution. Click the block of code below and hit the Run button above. NOTE: If the graph is not created, click the code section and hit the Run button again. IN [ ]: # create a figure for the plot. fig, ax = plt.subplots() ​ # create a histogram plot with 50 bins of TPCP population data. plt.hist(tpcp_df['TPCP'], bins=50) ​ # set a title for the plot, x-axis, and y-axis. plt.title('TPCP population distribution', fontsize=20) ax.set_xlabel('TPCP') ax.set_ylabel('Frequency') ​ # show the plot. plt.show() Step 3: Calculating the population mean This step will calculate the mean for the population data. Click the block of code below and hit the Run button above. IN [ ]: # You can use the "mean" method of a pandas dataframe. pop_mean tpcp_df['TPCP'].mean() print("Population mean =", round(pop_mean, 2)) Step 4: Drawing one random sample from the population data and calculating the sample mean This block of code randomly selects one sample (with replacement) of 50 data points from the population data. Then it calculates the sample mean. You will use the "sample" method of the dataframe to select the sample. Click the block of code below and hit the Run button above. IN [ ]:/n# use sample method of the dataframe to select a random sample, with replacement, of size 50. tpcp_sample_df = tpcp_df.sample (50, replace=True) ​ # print the sample mean. sample_mean = tpcp_sample_df['TPCP'].mean() print("Sample mean =", round(sample_mean, 2)) Step 5: Repeatedly drawing samples and saving the sample mean for each sample You will now essentially repeat Step 4 one thousand times to select 1,000 random samples, with replacement, of size 50 from the population data. The code below contains a loop so that you can do this selection with just one click! You will save the sample mean for each sample in a Python dataframe. Click the block of code below and hit the Run button above. In [ ]: # run a for loop to repeat the process Step 4 one thousand times to select one thousand samples. # save the mean of each sample that was drawn in a Python list called means_list. means_list = [] for i in range (1000): tpcp_sample_df = tpcp_df.sample (50, replace=True) sample_mean = tpcp_sample_df['TPCP'].mean() means_list.append(sample_mean) # create a Python dataframe of means. means_df = pd.DataFrame (means_list, columns=['means']) print("Dataframe of 1000 sample means\n") print (means_df) Step 6: Creating a histogram plot of the sample means from Step 5 Now you will plot the data distribution of the 1,000 means from Step 5. View the plot to confirm that it approximates a Normal distribution (bell-shaped curve). Note that the original data distribution in Step 2 was skewed. However, the distribution of sample means, calculated by repeatedly drawing large samples, is approximately Normally distributed. Click the block of code below and hit the Run button above. NOTE: If the graph is not created, click the code section and hit the Run button again. In [Ā ]: # create a figure for the plot. fig, ax = plt.subplots() ​ # create a histogram plot with 50 bins of 1,000 means. plt.hist (means_df['means'], bins=50) ​ # set a title for the plot, x-axis and y-axis. plt.title('Distribution of 1000 sample means', fontsize=20) # title ax.set_xlabel('Means') ax.set_ylabel('Frequency') ​ # show the plot. plt.show() Step 7: Mean and the standard deviation of the sample mean distribution Now you will calculate the "grand" mean ("grand" because it is the mean of the 1,000 means) and the standard deviation of 1,000 sample means. Note that the distribution of sample means was approximately Normal (bell-shaped) in Step 6. Therefore, calculating the mean and the standard deviation of this distribution will allow us to calculate probabilities and critical values. Click the block of code below and hit the Run button above. IN [ ]: # calculate mean of the 1,000 sample means (this is called the grand mean or mean of the means). mean1000 = means_df['means'].mean() print("Grand Mean (Mean of 1000 sample means) =",round(mean1000, 2)) †< # calculate standard deviation of the 1,000 sample means. std1000 = means_df['means'].std() print("Std Deviation of 1000 sample means =",round(std1000, 2)) ​ # print the probability that a specific mean is 450 or less for a Normal distribution with mean and standard deviation of 1,000 sample means. prob_450_less_or_equal = st.norm.cdf (450, mean1000, std1000) print("Probability that a specific mean is 450 or less =", round(prob_450_less_or_equal,4)) End of initial post Attach the HTML output to your initial post in the Module Two discussion. The html output can be downloaded by clicking File, then Download as, then HTML. Be sure to answer all questions about this activity in the Module Two discussion. Follow-up posts (due Sunday) Return to the Module Two discussion to answer the follow-up questions in your response posts to other students. There are no Python scripts to run for your follow-up posts.

Fig: 1