**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 here
that 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 many
are expected to have the disease? How many are expected to not
have 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 following
form:

|    |    ||Has the disease?|
|:---|:---|:---:|:---:|
|||Yes|No|
|**Test result**| Positive |||
|| Negative | ||





```python
# Your code here
```

**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 table
summarizes the types of errors we are making: the number of
false positives ("FP") and the number of
false negatives ("FN"). In addition, it shows us the number
of true positives
("TP") and the number of true negatives ("TN").
Identify the location of
these $4$ labels in the table above. 
Using these labels, we can define different measures that tell us
something about the performance of the test. One example of such a
measure is the "precision" which is
defined as the ratio between the number of true positives and
the 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 the
disease, given that the test was positive.
Calculate this probability, using the formula given above.
Do you have any comments about the size of
this probability?





```python
# Your code here
```

**Your answer to question 2.1(b):** *Double click here*

**(c)**  Use Bayes theorem to calculate the probability of
having the disease, given that a test was positive. Compare
this to the probability you found in the previous answer.




```python
# Your code here
```

**Your answer to question 2.1(c):** *Double click here*



**Exercise 2.2**

The fertilizer magnesium ammonium phosphate MgNH$_4$PO$_4$ is an effective
supplier for nutrients necessary for plant growth. In an experiment, you have
tested the effect of this fertilizer on the growth of Chrysanthemums by
measuring 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 is
also 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 height
of the plants is not affected by the amount of fertilizer. We
are going to test this with a specified significance level, $\alpha$.
What is the meaning of $\alpha$ in connection with a hypothesis test?




```python
# Your code here
```

**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 plot
of the raw data. Does it look like our hypothesis is correct?




```python
# Your code here
```

**Your answer to question 2.2(b):** *Double click here*

**(c)**  In connection with performing ANOVA, we need to calculate some
terms that measure the variance within groups and the variance
between groups.
Calculate these terms ($SST$, $SSA$, and $SSE$).




```python
# Your code here
```

**Your answer to question 2.2(c):** *Double click here*

**(d)**  Using the terms you calculated in the previous point, obtain
the 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$.




```python
# Your code here
```

**Your answer to question 2.2(d):** *Double click here*

**(e)**  To perform the actual hypothesis test, we need the critical value
from the $F$-distribution. For a specified significance level, $\alpha$,
and with $3$ and $36$ degrees of freedom, we
label the critical value as $f_{\alpha}(3, 36)$. Check that I am using
the correct degrees of freedom here, consistent with the
raw data given in table 1.




```python
# Your code here
```

**Your answer to question 2.2(e):** *Double click here*

**(f)**  Critical $f$ values can be calculated from the distribution
function(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 the
following 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.




```python
# Your code here
```

**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 that
different concentrations of the fertilizer affect the mean attained height
of the plants? What concentration, if any, appears to give the tallest plants?




```python
# Your code here
```

**Your answer to question 2.2(g):** *Double click here*

**(h)**  Would your conclusion change with a significance level of $0.10$?




```python
# Your code here
```

**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) method
from 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.



```python
# Your code here
```

**Your answer to question 2.2(i):** *Double click here*

ced85e349b48ab98b3c356c653eb7f93b9470d47
ipynb
Jupyter Notebook
exercises/02_Exercise_Set_2.ipynb
sroet/chemometrics
c797505d07e366319ba1544e8a602be94b88fbb6
CC-BY-4.0
2020-02-04T12:09:19.000Z
2021-08-06T12:28:24.000Z
exercises/02_Exercise_Set_2.ipynb
sroet/chemometrics
c797505d07e366319ba1544e8a602be94b88fbb6
CC-BY-4.0
2020-01-06T10:24:33.000Z
2022-01-21T10:37:46.000Z
exercises/02_Exercise_Set_2.ipynb
sroet/chemometrics
c797505d07e366319ba1544e8a602be94b88fbb6
CC-BY-4.0
2020-01-09T12:04:26.000Z
2021-01-19T10:06:14.000Z
Qwen/Qwen-72B
1. YES
2. YES

