Instructions (read all instructions carefully before proceeding, they differ from the Midterm):
This is Part II of the final. It consists of 24 parts organized into three questions. The total point value is 150 points (43% of final exam grade), including 10 points for group participation, which will be assessed individually over CCLE.
Please enter all of your answers either as code or markdown text into the appropriate place in a copy of this notebook on CoCalc (similar to homework). Some problems will require Python programming.
Then, you will only submit one version of the midterm, for your entire group, via Gradescope. The Gradescope assignment for the Midterm allows group participation, so make sure you select and enter all members of your group.
Since you are completing this on CoCalc but uploading to Gradescope, we recommend that you wait to upload until you have completed the full exam on CoCalc. At that point, on CoCalc, select File / Download as… / PDF. CoCalc will convert to a PDF that you can download. This entire PDF can then be easily uploaded to Gradescope, and you can manually select where each answer is for each question.
Please be aware, when uploading PDFs and your answers to Gradescope, that page-breaks can accidentally hide text. Additionally, if you answer questions via code comments (as opposed to markdown text), unless you manually create line breaks in your comment, the full text answer may not be legible. After uploading to Gradescope, please double-check all of your answers to make sure that they are legible and clear.
While screen shots are an acceptable alternative to uploading PDF, due to low resolution, we do not recommend taking photos of your computer screen with your phone, unless absolutely necessary.
Again, we will not grade any material left on CoCalc. In order to receive a grade, you must submit – as a group – to Gradescope. Don’t forget to add group members to your submission on Gradescope!
You may use your notes, assignments, slides, readings, solutions, and other resources on our LS 40 CCLE site and your CoCalc project (but not elsewhere on the internet).
However, as always, you must show all of your work to receive full credit for each problem.
If you have a clarifying question about the exam at any point during the exam period, email Professor Tingley. Questions about content or your own progress will not be answered.
For technical glitches with Python, try "Kernel menu > Restart kernel" or Backups in files view first.
Gradescope will forbid uploads after 3:00 pm Pacific Time on Thursday, March 18, 2021. Please plan accordingly.
Needing fuel before your exam, you open a single bag of regular M&M candies, pouring the contents out onto your desk. There are 35 candies of the following colors:
a. You first wonder whether the probability of getting any given color of M&M is more likely than any other color. What single statistical test you would use to test this hypothesis. If multiple suitable tests are possible, justify your choice. (5 points)
This would be chi squared goodness of fit test. This is because this statistical test is the one that compares the observed frequency with the theoretical frequency, which is needed in this situation.
b. What would be the observed test statistic for the appropriate test suggested in (a)? [note: in your calculations, do not round to whole M&Ms, fractions of M&Ms are OK] (5 points)
Chi squared value:
c. Calculate the probability of observing a test statistic of equal or greater magnitude purely due to random chance under the null hypothesis that frequencies are equal. (5 points)
sim=np.zeros(10000)null=10*["B"]+10*["O"]+10*["G"]+10*["Y"]+10*["R"]+10*["BR"]foriinrange(10000):sample=np.random.choice(null,35)b=np.sum(sample=="B")o=np.sum(sample=="O")g=np.sum(sample=="G")y=np.sum(sample=="Y")r=np.sum(sample=="R")br=np.sum(sample=="BR")chi=((b-5.83)**2/5.83)+((o-5.83)**2/5.83)+((g-5.83)**2/5.83)+((y-5.83)**2/5.83)+((r-5.83)**2/5.83)+((br-5.83)**2/5.83)sim[i]=chip=sns.displot(sim)plt.axvline(6.7,color="red")pval=(np.sum(sim>=6.7))/10000print("The p value for our simulation is:",pval)
The p value for our simulation is: 0.2359
d. Is the probability calculated in part (c) one-tailed or two-tailed? Why or why not? (5 points)
This will be one tailed, right tailed, because chi squared is only positive values and represents sums of distances from expected value. The observed data is equal to the expected data, this shows the chi squared statistic will be 0 because the two values are identical under the null hypothesis. The chi squared statistic will increase as the differences between the observed and expected becomes more extreme.
e. What is your inference from the probability calculated in part (c)? (5 points)
From the pvalue we calculated of 0.2359, we cannot reject the null hypothesis of M & M packs having the same proportions of color of candy because it is not under the significance threshold of 0.05. This shows us that our observed color distribution was likely just due to random chance.
f. Emotionally invested in this discovery, you show your results to your roommate. Looking at the sorted colors of M&Ms on your desk, she comments, “it seems unlikely that you would draw 10 orange and only 2 green just by chance if the probabilities are truly equal. Can you just calculate that specific probability for me?” Knowing everything you learned in LS40 and based off the preceding analysis, do you comply with her request? If yes, explain how you would calculate the requested probability. If not, defend your choice. (5 points)
Yes, we can find that probability. We can use a basic big box null hypothesis statistical test. This is because we want to find the probability of getting 10 orange and 2 green m&ms in the same bag. With a big box, we can resample the same sample size of 35 m&ms and find the probability of that specific combination. The null hypothesis for this request would be that there is no difference in the ratios of each m&m color, but we could see how statistically significant the probability of getting 10 orange and 2 green in the same batch is.
After a night of google searches, you discover that all M&Ms are manufactured at just two different plants in the United States, each packaging a slightly different proportion of colors. Each plant produces approximately 50% of all M&Ms sold in the United States per year. The color frequencies for these two factories are shown in the table below:
But which of these factories sends M&Ms to your local store, you wonder. Determined to find out, you do more statistics.
g. These new factory %s have changed your expectations. Using the same analytic framework as before, calculate two observed test statistics based on the data from your original bag of M&Ms. One test statistic should relate to the expectations from the New Jersey factory and the other test statistic should relate to the expectations from the Tennessee factory. [note: again, do not round to whole M&Ms] (8 points)
#new jerseynewjersey=250*["B"]+250*["O"]+125*["G"]+125*["Y"]+125*["R"]+125*["BR"]newjer_chi_obs=((7-8.75)**2/8.75)+((10-8.75)**2/8.75)+((2-4.375)**2/4.375)+((5-4.375)**2/4.375)+((4-4.375)**2/4.375)+((7-4.375)**2/4.375)print("Our new chi squared for new jersey is:",newjer_chi_obs)tennessee=207*["B"]+205*["O"]+198*["G"]+135*["Y"]+131*["R"]+124*["BR"]tenn_chi_obs=((7-7.245)**2/7.245)+((10-7.175)**2/7.175)+((2-6.93)**2/6.93)+((5-4.725)**2/4.725)+((4-4.585)**2/4.585)+((7-4.34)**2/4.34)print("Our new chi squared for tennessee is:",tenn_chi_obs)
Our new chi squared for new jersey is: 3.5142857142857142
Our new chi squared for tennessee is: 6.348735833832281
h. Calculate the probability of opening a bag of M&Ms and finding the frequency of colors that you observed (or a frequency distribution more extreme), given the expected frequencies from each factory. Your answer should calculate two probabilities, one for each factory. [hint: For your box model, imagine each factory produces 1000 M&Ms of the expected proportions. Refer to homework 7 solutions if you’re having trouble coding it.] (8 points)
#new jersey factorysim_nj=np.zeros(10000)newjersey=250*["B"]+250*["O"]+125*["G"]+125*["Y"]+125*["R"]+125*["BR"]foriinrange(10000):sample=np.random.choice(newjersey,35)b=np.sum(sample=="B")o=np.sum(sample=="O")g=np.sum(sample=="G")y=np.sum(sample=="Y")r=np.sum(sample=="R")br=np.sum(sample=="BR")chi=((b-8.75)**2/8.75)+((o-8.75)**2/8.75)+((g-4.375)**2/4.375)+((y-4.375)**2/4.375)+((r-4.375)**2/4.375)+((br-4.375)**2/4.375)sim_nj[i]=chip=sns.displot(sim_nj)plt.axvline(newjer_chi_obs,color="red")pval_nj=(np.sum(sim_nj>=newjer_chi_obs))/10000print("The p value for our simulation for new jersey is:",pval_nj)
The p value for our simulation for new jersey is: 0.6439
#tennesseesim_ten=np.zeros(10000)tennessee=207*["B"]+205*["O"]+198*["G"]+135*["Y"]+131*["R"]+124*["BR"]foriinrange(10000):sample=np.random.choice(tennessee,35)b=np.sum(sample=="B")o=np.sum(sample=="O")g=np.sum(sample=="G")y=np.sum(sample=="Y")r=np.sum(sample=="R")br=np.sum(sample=="BR")chi=((b-7.245)**2/7.245)+((o-7.175)**2/7.175)+((g-6.93)**2/6.93)+((y-4.725)**2/4.725)+((r-4.585)**2/4.585)+((br-4.34)**2/4.34)sim_ten[i]=chip=sns.displot(sim_ten)plt.axvline(tenn_chi_obs,color="red")pval_tenn=(np.sum(sim_ten>=tenn_chi_obs))/10000print("The p value for our simulation for tennessee is:",pval_tenn)
The p value for our simulation for tennessee is: 0.2675
i. Based on your calculations, which factory is more likely to produce a color frequency matching your bag of M&Ms? How certain are you? Can you rule out one of the two factories as the producer? Support your answer only with information from your previous calculations. (5 points)
The new jersey factory showed a pvalue of about 0.6439. This shows us we cannot reject the null hypothesis because it is over the significane threshold of 0.05. Additionally, the new jersey pvalue is much higher than the pvalue for the tennesse factory, which was 0.2675,. This tells us there is a higher chance of getting m&m packs from the new jersey factory. We are very certain of this because the distributions produced in new jersey are very closely aligned with our bag, but we cannot explicitly rule out tennessee because both factories are above the significance threshold, which tells us these proportions came out to random chance. With random chance, either factory is technically possible.
j. Given your calculations, what is the probability that your bag of M&Ms was made in Tennessee? [Hint: Note the difference between this question and that asked in part h. Remember, from above, that the factories produce equal numbers.] (6 points)
We will make a new data frame and allocate 50% to each factory location. We then run a simulation for 10,000 times to see what the probability is to get the bag from the tennessee factory. This would be a big box test.
k. Determined to know the truth, you open 100 more bags, carefully counting up the total numbers of each color. You re-do your simulations, eventually finding that the expected color frequency from Tennessee would produce your observed frequency 85% of the time, while the expected color frequency from New Jersey would produce your observed frequency only 4% of the time. Given this new knowledge, what is the probability that the M&Ms you purchased were made in Tennessee? [Assume that all M&Ms purchased came from the same factory.] (6 points)
We would use the same simulation as in J but now we have new data that we must account for so we will alter our observed frequencies to match the findings in the 100 new bags. This will give us an altered probability of the chance our bag was made in tennessee.
l. Explain why your probability changed from part (j) to (k), referring explicitly to the “reallocation of credibility”. (5 points)
Reallocation of credibility tells us that when our circumstances or situation change, so does our probablity we calculated. The more information we gather, the mre we have to make adjustments to "reallocate" our credibility from our old data to the updated one to have the most accuarate probabilities. This is relevant to j and k because in j we just use 1 bag of candy to calculate data but in k we have now 100 more bags of candy. This gives us new knowledge to integrate into our calculations and more accuracy as well.
2. Masking Results
The following passage was published in the Journal of Hospital Infection (2020, vol. 105, pp. 104–105) on the role of N95 masks in preventing transmission of the virus SARS-CoV-2:
a. For each of the bolded and underlined numbers, classify each into one of the following categories: effect size, p-value, confidence interval, sample size. (8 points)
The effect size is 4.65%
The pvalue is 2.2e-16
the confidence interval is 95% and from (1.75% -infinite)
The sample size is 491 medical staff.
b. For each p-value in part a, give a plausible null hypothesis that is being tested. (5 points)
There is no difference in the infection rate of 2019-nCOV medical staff between no mask group and the N95 respirator group.
c. Note that the authors report something as “infinite.” Based on the information provided in the above passage, why did the authors calculate the upper bound as “infinite”? Is infinite a plausible upper bound? (5 points)
The authors calculate the upper bound will be infinite because at the time of the study, the scientists did not have enough data to actually set an uppper bound for this study. This is because infection rate can fluctuate and there were many other factors about the novel virus that were not yet know. Infinite upper bound is not very plausible but was used as a place holder for the time being.
3. (S)querulous Correlations
A set of wildlife biologists set out to study the relationship between tail length and tail bushy-ness in UCLA squirrels. Below are sets of x-y measurements (x = length in cm, y = bushy-ness in mm) for each of 11 individual squirrels:
The observed correlation coefficient is 0.8162365060002428
The p-value is 0.0005
c. In part (b), did you calculate a 1- or 2-sided p-value? Justify your decision in the context of the data, the test, and the research goals. (5 points)
This pvalue is two-sided because we calculated the statistical significance in opposite directions. We are testing for the possibility of a relationship between tail length and tail bushyness in UCLA squirrels in either direction. This is either a positive or negative direction. This helps further the goals of the study.
d. Encouraged by the results, the researchers are interested in learning how much bushier UCLA squirrel tails are for every cm of length. Calculate this statistic and provide an appropriate confidence interval. [Assume that tail lengths are measured and reported exactly.] (6 points)
reg=linregress(df["X1"],df["Y1"])slope=reg.slopeintercept=reg.interceptX_plot=np.linspace(4,13,100)Y_plot=slope*X_plot+interceptp=sns.lmplot(x="X1",y="Y1",data=df,fit_reg=False)plt.plot(X_plot,Y_plot)print("The slope for the regression line is",slope)print("The y-intercept for the regression line is",intercept)
The slope for the regression line is 0.5000000000000001
The y-intercept for the regression line is 3.000909090909089
The 99% confidence intervals are (0.007389162561576401, 0.9844117647058825)
the statistic shows that with every cm increase in tail length, the bushyness will increase by around 0.5 mm. The 99% confidence interval is from (0.002417218543046662 to 0.9637800687285226)
e. Part (d) asks you to assume that tail lengths are measured and reported exactly. How would your analysis in part (d) change if tail length measurements had instead been rounded to the nearest cm? [You don't have to conduct this alternative analysis, just describe how your analysis would change] (5 points)
This would change our analysis because we know that the tail lengths are rounded and not exact measures. Ordinary Least Squares requires exact tail length values. But since we are rounding now, this is when we use orthogonal regression because we assume that we are rounding the tail length in centimeters, resulting in errors in the tail length. This would increase our slope because we are rounding and giving a steeper slope.
f. A competing set of wildlife biologists from USC decide to replicate the squirrel study and come up with the following measurements from 11 individuals:
reg=linregress(df2["X2"],df2["Y2"])slope=reg.slopeintercept=reg.interceptX_plot2=np.linspace(4,13,100)Y_plot2=slope*X_plot+interceptp2=sns.lmplot(x="X2",y="Y2",data=df2,fit_reg=False)plt.plot(X_plot2,Y_plot2)print("The slope for the regression line is",slope)print("The y-intercept for the regression line is",intercept)
The slope for the regression line is 0.4997272727272729
The y-intercept for the regression line is 3.002454545454544
g. From these two studies, what can we conclude about whether longer tails cause bushier tails in squirrels? (5 points)
We can conclude that longer tails and bushier tails in squirrels are highly correlated because of the high correlation coefficient of about 0.816 for each study. This data, however, does not allow us to conlcude anything about causation. Because this is an observational study, not an experimental one.
h. Looking at the data produced, the UCLA and USC scientists come together and conclude that the relationship between tail length and tail bushy-ness in squirrels is the same on both campuses. You don’t agree. Convince them otherwise, using at least one appropriate graph to support your position. (10 points)
The graphs as shown above show that we cannot conclude that the squirrel tail length and tail bushyness relationship is the same between the UCLA and USC campus because when we graph our data, the UCLA graph is not very closely correlated with the line of regression, showing a lower correlation relationship while the USC graph has data points very close to the line showing a higher correlation relationship.
i. Thanks to your persuasive evidence, the two teams of researchers conclude that the relationship between tail length and bushy-ness is not the same between the two schools. In order for them to not make the same mistake again, what final lesson would you impart? [Note: this final lesson should be considered as one of the most enduring mantras of LS40!] (5 points)
We cannot automatically conclude the relationship is the same by just looking at significant pvalue that is less than 0.05 and correlation coefficient. This tells us we should always visualize data before we make any conclusions and that pvalue being less than 0.05 is not the end all statistic. More tests can always be done to create the most accurate data visualization.