---
author: "Prepared by Andrés Aravena, Ph.D. — Istanbul University"
number: STUDENT_NUMBER
title: "Final Exam"
subtitle: "Computing in Molecular Biology and Genetics II"
date: "June 14, 2021"
---

Please change the file name to your student number. The filename should be something like `040567890.Rmd`. Write also your student number in the correct place at the beginning of the answer file.

If you have any issues or questions, write to me at my official email `andres.aravena@istanbul.edu.tr` (only for questions, not for answers).

# 1. Buying online
We can represent the scoring profile in R with a vector, as follows

```{r}
book_profile <- c(3, 3, 5, 16, 72)
```

## 1.1 Average number of stars
Write a function to calculate the average number of stars.
The function must be called `average_stars` and should receive a vector representing the _profile_.
The function must return the average score, considering the _profile_.
There is no simulation here, just basic statistics.

```{r q1}
average_stars <- function(profile) {
  # write here
}
```

Now you can use this code to test your function 

```{r}
average_stars(book_profile)
```

## 1.2 Standard deviation
Please write a function to calculate the standard deviation of the number of stars.
The function must be called `sdev_stars`, should receive a vector representing the _profile_, and must return the standard deviation of the score.
There is no simulation here, just basic statistics.

```{r q2}
sdev_stars <- function(profile) {
  # write here
}
```

If everything is right, you should get exactly this value.

```{r}
sdev_stars(book_profile)
```

## 1.3 Standard error
Please write a function to calculate the standard error of the average number of stars.
The function must be called `stderr_stars`, must receive a vector representing the _profile_ and the sample size, and must return the standard error of the average score. 

**Hint:** you can use the functions defined in the previous questions as part of this answer.
This is not a simulation, just basic statistics.

```{r q3}
stderr_stars <- function(profile) {
  # write here
}
```

Test code

```{r}
sdev_stars(book_profile)
```

## 1.4 Interval for the Average
Please write the code to find a 95% confidence interval for the _real_ average score, considering that this sample has 43 observations.

```{r q4}
# write here
```

# 2. Simulation

## 2.1 Simulate one sample
Please write a function, called `one_score`, that takes the sample size `s`, and a vector `p` representing the score profile. The function must create a random sample of size `s` with the probabilities given by `p`, and must return the mean score of this simulated review.

```{r q5}
one_score <- function(s, p) {
  # write here
}
```

You can test your function with this code.

```{r}
one_score(100, book_profile)
```

## 2.2 Simulate one hundred thousand samples

Please write the code to replicate one hundred thousand simulations of the average of 100 reviews. Store the resulting one hundred thousand values in the vector `many_scores`.

```{r q6}
# write here
```

Test code:

```{r}
hist(many_scores, nclass = 30)
```

## 2.3 Mean and standard deviation
Write the code to find the mean and the standard deviation of the simulated scores.

```{r q7}
# write here
```

Write your answer using only the pre-defined code chunk. Do not create any new chunk.

## 2.4 Confidence interval
Write the code to build a confidence interval containing 95% of the simulated scores.

**Notice:** This interval is different from the one in Question 1.4.

```{r q8}
# write here
```

# 3. Comparing with another book

The scoring profile for the second book is the following vector
```{r}
second_book <- c(2, 1, 4, 12, 82)
```

## 3.1 Simulate ten thousand reviews
Please write the code to assign one hundred thousand simulated results to the vector `second_scores`.

**Hint:** you can use the functions defined in the previous questions as part of this answer.

```{r q9}
# write here the code to create the vector `second_scores`
```

If everything goes right, you should see something like this

```{r}
hist(second_scores, nclass = 30)
```

## 3.2 Is the second book really better than the first one?
Please write the code to find _the proportion of cases_ when the first book seems better than the second one. The result should be a number between 0 and 1. 

```{r q10}
# write here
```

## 3.3 Effect of the sample size
Please write the code to compare one thousand simulations of the first book's average score for 43 reviewers, against one thousand simulations of the second book's average score for 25919 reviewers.

**Hint:** you can use the functions defined in the previous questions as part of this answer.

```{r q11}
# write here
```

# 4. Why?
_(One of these questions is a bonus. At least two of these questions **must** be answered. If you answer the three, you get extra points.)_

These questions must be answered in plain English. They should not include any computer code. Write only in the designated area.

## 4.1 Why Normal?
Please explain (in plain English) why we can assume that the average values we calculated in Question 2 follow a Normal distribution.

``` q12
# write here
```

## 4.2 Why Questions 1 and 2 are different?
Please explain (in plain English) why the interval in Question 1 is different from the interval in Question 2.

``` q13
# write here
```

## 4.3 Exit survey.
Please fill the survey found following [this link](https://docs.google.com/forms/d/e/1FAIpQLSdTSHw2dTCRKCaSttv24jCcpj4o4MW3ZhP7CbCrw9gn83dQ5w/viewform?usp=sf_link). You can also find the link on the course homepage at <http://www.dry-lab.org/blog/2021/cmb2/>.

All *honest* answers get a good score, especially if they provide constructive criticism. The idea is to improve our course, so no offense will be taken.
On the other side, dishonest answers do not get any score.

Speak your mind.