**Exercise set 11**
==============


>In connection with experimental design, we have seen two approaches
>for checking if determined effects are important or not. These two
>approaches are based on creating a probability plot and performing ANOVA.
>The goal of this exercise is to learn how we use these two approaches in
>practice.


**Exercise 11.1**

In this part of the exercise, we will deal with the technical aspects of
creating a normal probability plot. Our final aim is here
to check if the data for some measured quantities (given in
the data files: `Data/data1.txt`, `Data/data2.txt`,
`Data/data3.txt`, and `Data/data4.txt`) comes
from a normal distribution.


**(a)**  Before we begin creating normal probability plots, we should
inspect the raw data. Plot histograms for the raw data. Based
on this, would you say that any of the data files contain numbers
that might come from a normal distribution?
You can also, for each data set, try to compare with
a normal distribution (using the mean and standard deviation
you can obtain from the raw data).





```python
# Your code here
```

**Your answer to question 11.1(a):** *Double click here*

**(b)**  For creating the normal probability plot, we need to be able
to obtain certain parameters for the standard normal distribution.
Here, we will investigate some of the functions that can give us
such parameters.

The standard normal probability density function ($\operatorname{PDF}$) is given by,

$\operatorname{PDF}(x) = \frac{1}{\sqrt{2 \pi}} \operatorname{e}^{-\tfrac{x^2}{2}},$

and the cumulative distribution function (CDF) is,

$
\operatorname{CDF}(x) = \int_{-\infty}^{x} \operatorname{PDF} (t)\,\operatorname{d}t = 
\frac{1}{2} \left[ 1 + \operatorname{erf} \left( \frac{x}{\sqrt{2}} \right) \right],
$

where $\operatorname{erf}(\ldots)$ is the error function.
The cumulative distribution function gives the probability of observing a
value less than or equal to $x$: $P(X \leq x) = \operatorname{CDF}(x)$.

We can also turn this equation around: Given a probability $P$ what is
the value of $x$ that gives this probability? To answer this question,
we need the quantile function (also known as the percent-point function),
which is the inverse of the cumulative distribution function.
For the standard normal distribution, the percent-point
function ($\operatorname{PPF}$),
is given by,

$
\operatorname{PPF}(P) = \sqrt{2} \operatorname{erf}^{-1}(2P - 1).
$

If we make use of the `scipy` package, all these functions
are available to us:
```python
from scipy.stats import norm
import numpy as np

x = np.linspace(-2, 2, 100)
pdf = norm.pdf(x)  # Probability density function.
cdf = norm.cdf(x)  # Cumulative probability density function.
ppf = norm.ppf(x)  # Quantile function/percent-point function.
```


Use these methods to answer the following questions for the standard normal distribution:

* (i)  What is the probability of observing a $x \leq 1$?

* (ii)  What is the probability of observing a $x \leq 0$?

* (iii)  What is the probability of observing a $x \leq -2$?

* (iv)  Given that the probability of observing $x \leq \alpha$ is
$10$\%, what is $\alpha$?

* (v)  Given that the probability of observing $x \leq \alpha$ is
$90$\%, what is $\alpha$?

* (vi)  Given that the probability of observing $x \leq \alpha$ is
$99$\%, what is $\alpha$?







```python
# Your code here
```

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

**(c)**  To construct the normal probability plot, we will make use of
the $\operatorname{PPF}$.
If the data we are to investigate contain $n$ points, then
we need to figure out how these $n$ points would be placed
in the distribution we are going to compare with (here: the standard normal distribution).
In the following, we will call the $n$ points we have measured
for $y_1$, $y_2$, $\ldots$, $y_n$, and we assume that we have
sorted them so that $y_1 \leq y_2 \leq \ldots \leq y_n$.

We now need to check
how $n$ points drawn from a normal distribution would
be distributed, and compare this with how our measured data is distributed.
The simplest approach would be
(as we did in lecture $11$) to find the locations in the
normal distribution where the probabilities are $\tfrac{1}{n}$, $\tfrac{2}{n}$,
$\ldots$, $\tfrac{n}{n}$. This we can obtain using the $\operatorname{PPF}$.
If we call these locations $x_1$, $x_2$, $\ldots$,
$x_n$, then we expect to have seen
one data point ($y_1$) for $x \leq x_1$,
two data points ($y_1$ and $y_2$) for $x \leq x_2$, $\ldots$,
and $n$ points for $x \leq x_n$.
So far so good, but we have two complicating factors:
(i) $x_i$ is *not* necessarily the most *probable* location
for the $i$'th smallest point, and (ii) how do we deal with $x_n = \infty$?

To solve these two potential problems, we have to rely on some theoretical
results from statistics. The correct $x_i$'s we are looking for are actually
something called the order statistic medians and these are exactly
related to order statistic medians from a *uniform distribution*, $m_i$,
\begin{equation}
x_i = \operatorname{PPF}(m_i) .
\label{eq:orderstat}
\tag{1}\end{equation}

Thus we can potentially find $x_i$ by first obtaining the corresponding
$m_i$. Unfortunately, no analytical expression for $m_i$ exist and
we have to rely on approximate estimates. One such approximation
was
suggested by [Filliben](https://doi.org/10.1080/00401706.1975.10489279),

\begin{equation}
m_i = 
\begin{cases}
1 - 0.5^{1/n} & \text{if } i = 1, \\
\frac{i - 0.3175}{n + 0.365} & \text{if } i = 2, 3, \ldots, n-1, \\
0.5^{1/n} & \text{if } i=n,
\end{cases}
\label{eq:uniformorderstat}
\tag{2}
\end{equation}

Thus, in summary, to create the normal probability plot we do the following:

* (i)  We sort our original data.

* (ii)  For each sorted data point, we calculate its
uniform statistic median, $m_i$,
using Eq. \eqref{eq:orderstat}.

* (iii)  For each sorted data point, we calculate its most
probable location, $x_i$, in a normal distribution using
Eq. \eqref{eq:uniformorderstat} and
the $m_i$ value we found in the previous step. 

* (iv)  We plot the sorted data against the most probable locations
found in the previous step. That is, we plot the pairs ($x_i$, $y_i$).



Create the normal probability plots for the four data sets given
in `Data/data1.txt`, `Data/data2.txt`,
`Data/data3.txt`, and `Data/data4.txt`. Which
of these would you say are numbers that could originate from a
normal distribution?





```python
# Your code here
```

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

**(d)**  The method we have described above works for any distribution,
not just the normal distribution. We can create similar plots
for other distributions by changing the $\operatorname{PPF}$ function
to the corresponding function for the distribution we wish to check for.
Repeat the previous step, but use the `Gumbel distribution`
(in `scipy` this is available by
`from scipy.stats import gumbel_r`) in place of the
normal distribution. Based on the plots you create, would you say
that any of the data sets may contain numbers from a Gumbel distribution?



```python
# Your code here
```

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


**Exercise 11.2**

After running a set of experiments, you determine the effects
given in Table 1 for $4$ factors: A, B, C, and D.
Use a normal probability plot to identify the important effects among
the ones listed in this table.

|**Factor** | **Effect** |
|:---:|---:|
|A    | $-8.00$ |
|B    | $24.00$ |
|C    | $-2.25$ |
|D    | $-5.50$ |
|AB   | $ 1.00$ |
|AC   | $ 0.75$ |
|AD   | $ 0.00$ |
|BC   | $-1.25$ |
|BD   | $ 4.50$ |
|CD   | $-0.25$ |
|ABC  | $-0.75$ |
|ABD  | $ 0.50$ |
|ACD  | $-0.25$ |
|BCD  | $-0.75$ |
|ABCD | $-0.25$ |

| |
|---|
|**Table 1:** *Effects determined in a set of experiments.*|





```python
# Your code here
```

**Your answer to question 11.2:** *Double click here*

**Exercise 11.3**

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 2, and is
also given in the file `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 2:** *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 11.3(a):** *Double click here*

**(b)**  Based on this data, 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 11.3(b):** *Double click here*

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



```python
# Your code here
```

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


The relevant critical values for the $f$ distribution with $3$ and $36$ degrees of
freedom  are: $f_{\alpha = 0.05}(3, 36) = 2.866$ and $f_{\alpha=0.10}(3, 36) = 2.243$.


**Exercise 11.4**

From a $2^2$ factorial experiment replicated three times you have obtained
the data given in Table 3. We use here a short-hand notation
for the $4$ possible combinations of the variables: $(1)$, $a$, $b$, and $ab$.
In this notation $(1)$ is the experiment where all factors are at their low level. For the
other cases, the absence of a letter means that the corresponding factor was at a low level, and
the presence of a letter means that the corresponding factor was at a high level. 

|**Experiment** | **Replicate 1** | **Replicate 2** | **Replicate 2** |
|:---:|:---:|:---:|:---:|
|$(1)$ | $9$  | $10$ | $11$ |
|$a$   | $30$ | $31$ | $29$ |
|$b$   | $19$ | $20$ | $21$ |
|$ab$  | $5$  | $6$  | $4$  |

| |
|---|
|**Table 3:** *Results from a $2^2$ factorial experiment, repeated $3$ times.*|



**(a)**  Calculate the effects. Here you should average the effects over the
replicates.




```python
# Your code here
```

**Your answer to question 11.4(a):** *Double click here*

**(b)**  Use ANOVA to investigate which effects are important in this case. For
a significance level of $\alpha = 0.01$, the relevant critical
$f$-value is $f_{\alpha=0.01}(1, 8) = 11.259$ with $1$ and $8$ degrees
of freedom.



```python
# Your code here
```

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






095b7d90d00b39c6a10d69cc6febfd9aa0a788c7
ipynb
Jupyter Notebook
exercises_2020/11_Exercise_Set_11.ipynb
sroet/chemometrics
c797505d07e366319ba1544e8a602be94b88fbb6
CC-BY-4.0
2020-02-04T12:09:19.000Z
2021-08-06T12:28:24.000Z
exercises_2020/11_Exercise_Set_11.ipynb
sroet/chemometrics
c797505d07e366319ba1544e8a602be94b88fbb6
CC-BY-4.0
2020-01-06T10:24:33.000Z
2022-01-21T10:37:46.000Z
exercises_2020/11_Exercise_Set_11.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