<jupyter_start><jupyter_text>**Exercise set 2**==============>The goal of this exercise is to investigate Bayes theorem,>and to use ANOVA to determine if a certain factor is indeed>affecting the results of an experiment.**Exercise 2.1**A certain disease occurs randomly in the general population with a probability of $\tfrac{1}{10\,000}$.A test has been developed for this disease which is $99\%$ correct, meaning herethat if a person has the disease, then the test will correctly be positive in $99\%$ of the cases.If a person does not have the disease, the test will correctly be negative in $99\%$ of the cases.**(a)**  Assume that we are testing for this disease in a population of $1\,000\,000$ people. How manyare expected to have the disease? How many are expected to nothave the disease? Further:* (i)  How many people with the disease will have a positive test?* (ii)  How many people with the disease will have a negative test?* (iii)  How many people without the disease will have a positive test? * (iv)  How many people without the disease will have a negative test? Summarize your answers to the four points above in a table of the followingform:|    |    ||Has the disease?||:---|:---|:---:|:---:||||Yes|No||**Test result**| Positive ||||| Negative | ||<jupyter_code># Your code here<jupyter_output><empty_output><jupyter_text>**Your answer to question 2.1(a):** *Double click here* **(b)**  Later in the course, when dealing with classification problems,we will refer to this table as the "confusion matrix". This tablesummarizes the types of errors we are making: the number offalse positives ("FP") and the number offalse negatives ("FN"). In addition, it shows us the numberof true positives("TP") and the number of true negatives ("TN").Identify the location ofthese $4$ labels in the table above. Using these labels, we can define different measures that tell ussomething about the performance of the test. One example of such ameasure is the "precision" which isdefined as the ratio between the number of true positives andthe total number of positives:\begin{equation}\text{precision} = \frac{\text{TP}}{\text{TP} + \text{FP}}\tag{1}\end{equation}We can interpret the precision as the probability of having thedisease, given that the test was positive.Calculate this probability, using the formula given above.Do you have any comments about the size ofthis probability?<jupyter_code># Your code here<jupyter_output><empty_output><jupyter_text>**Your answer to question 2.1(b):** *Double click here* **(c)**  Use Bayes theorem to calculate the probability ofhaving the disease, given that a test was positive. Comparethis to the probability you found in the previous answer.<jupyter_code># Your code here<jupyter_output><empty_output><jupyter_text>**Your answer to question 2.1(c):** *Double click here* **Exercise 2.2**The fertilizer magnesium ammonium phosphate MgNH$_4$PO$_4$ is an effectivesupplier for nutrients necessary for plant growth. In an experiment, you havetested the effect of this fertilizer on the growth of Chrysanthemums bymeasuring the height of the plants after growing them for four weeks.You have considered $4$ different concentrations of the fertilizer (measured in g/bu)and you have measured the mean height by growing $10$ plants per concentration.The measured data is given in Table 1, and isalso given in the file [`Data/fertilizer.txt`](Data/fertilizer.txt).|**50 g/bu** | **100 g/bu** | **200 g/bu** | **400 g/bu** ||:---:|:---:|:---:|:---:||$13.2$ | $16.0$ | $ 7.8$ | $21.0$ ||$12.4$ | $12.6$ | $14.4$ | $14.8$ ||$12.8$ | $14.8$ | $20.0$ | $19.1$ ||$17.2$ | $13.0$ | $15.8$ | $15.8$ ||$13.0$ | $14.0$ | $17.0$ | $18.0$ ||$14.0$ | $23.6$ | $27.0$ | $26.0$ ||$14.2$ | $14.0$ | $19.6$ | $21.1$ ||$21.6$ | $17.0$ | $18.0$ | $22.0$ ||$15.0$ | $22.2$ | $20.2$ | $25.0$ ||$20.0$ | $24.4$ | $23.2$ | $18.2$ |**Table 1:** *Measured plant heights (in cm) as a function of the fertilizer concentration (in g/bu).***(a)**  Here, we will test the hypothesis that the mean heightof the plants is not affected by the amount of fertilizer. Weare going to test this with a specified significance level, $\alpha$.What is the meaning of $\alpha$ in connection with a hypothesis test?<jupyter_code># Your code here<jupyter_output><empty_output><jupyter_text>**Your answer to question 2.2(a):** *Double click here* **(b)**  To test the hypothesis, we will perform ANOVA. Before we do that,it is a good idea to visualize the raw data. Create a suitable plotof the raw data. Does it look like our hypothesis is correct?<jupyter_code># Your code here<jupyter_output><empty_output><jupyter_text>**Your answer to question 2.2(b):** *Double click here* **(c)**  In connection with performing ANOVA, we need to calculate someterms that measure the variance within groups and the variancebetween groups.Calculate these terms ($SST$, $SSA$, and $SSE$).<jupyter_code># Your code here<jupyter_output><empty_output><jupyter_text>**Your answer to question 2.2(c):** *Double click here* **(d)**  Using the terms you calculated in the previous point, obtainthe two estimates for the variance:* (i)  $s_1^2 = \frac{SSA}{k-1}$,* (ii)  $s^2 = \frac{SSE}{k(n-1)}$,and calculate the $f$-statistic: $f=s_1^2/s^2$.<jupyter_code># Your code here<jupyter_output><empty_output><jupyter_text>**Your answer to question 2.2(d):** *Double click here* **(e)**  To perform the actual hypothesis test, we need the critical valuefrom the $F$-distribution. For a specified significance level, $\alpha$,and with $3$ and $36$ degrees of freedom, welabel the critical value as $f_{\alpha}(3, 36)$. Check that I am usingthe correct degrees of freedom here, consistent with theraw data given in table 1.<jupyter_code># Your code here<jupyter_output><empty_output><jupyter_text>**Your answer to question 2.2(e):** *Double click here* **(f)**  Critical $f$ values can be calculated from the distributionfunction(With Python, this can be done with`scipy.stats.f.ppf`from the[SciPy library](https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.f.html).)or it can be found in statistical tables.(One such table can be found[here on-line](https://www.itl.nist.gov/div898/handbook/eda/section3/eda3673.htm).)For the significance levels $\alpha=0.05$ and $\alpha=0.10$, I have found thefollowing two values:* (i)  $f_{\alpha = 0.05}(3, 36) = 2.866$* (ii)  $f_{\alpha=0.10}(3, 36) = 2.243$Check that these two values are correct.<jupyter_code># Your code here<jupyter_output><empty_output><jupyter_text>**Your answer to question 2.2(f):** *Double click here* **(g)**  Based on the calculations you have done,can you conclude on the $0.05$ level of significance thatdifferent concentrations of the fertilizer affect the mean attained heightof the plants? What concentration, if any, appears to give the tallest plants?<jupyter_code># Your code here<jupyter_output><empty_output><jupyter_text>**Your answer to question 2.2(g):** *Double click here* **(h)**  Would your conclusion change with a significance level of $0.10$?<jupyter_code># Your code here<jupyter_output><empty_output><jupyter_text>**Your answer to question 2.2(h):** *Double click here* **(i)**  (Optional) Re-do this exercise, but use the [`anova_lm`](https://www.statsmodels.org/stable/generated/statsmodels.stats.anova.anova\_lm.html) methodfrom the Python package [`statsmodels`](https://www.statsmodels.org), or the[`scipy.stats.f_oneway`](https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.f\_oneway.html) method from the Python package [`SciPy`](https://www.scipy.org/), to run ANOVA.<jupyter_code># Your code here<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1350465451684804, 'fraction_numerical': 0.03146715615576242, 'mean_word_length': 5.237121831561733, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 52, 'https://': 6, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '271070', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 2491, 'n_tokens_neox': 2318, 'n_words': 928, 'path': 'exercises/02_Exercise_Set_2.ipynb', 'repo_name': 'sroet/chemometrics', 'size': '7627'}