```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Exercise 1

Given a binomial random variable X, with probability of success $\theta = 0.6$, compute, for 10 trials (this is specified in R with size = 10):

- $Prob(X\leq 6)$
- $Prob(X\geq 4)$
- $Prob(7 \leq  X \leq 9)$

### Answer

```{r echo=FALSE,eval=FALSE}
theta<-0.6
pbinom(6,size=10,prob=theta)
1-pbinom(3,size=10,prob=theta)
## cross-check
sum(dbinom(4:10,size=10,prob=theta))
pbinom(9,size=10,prob=theta)-pbinom(6,size=10,prob=theta)
## cross-check
sum(dbinom(7:9,size=10,prob=theta))
```

## Exercise 2

Given a binomial random variable X, with probability of success $\theta = 0.5$, what is the quantile k such that $Prob(X\leq k) = 0.5$. The number of trials is 10.

### Answer

```{r echo=FALSE,eval=FALSE}
qbinom(0.5,size=10,prob=0.5)
```

## Exercise 3

Do the following 1000 times (hint: this can be done with a single command; in R, n refers to the number of experiments): 

- Run an experiment with 10 trials from a binomial (probability of success 0.6). You should get the number of successes in 10 trials. If you do 1000 experiments, then you will get 1000 instances of the number of successes in each of the 1000 experiments. The command for running 1000 experiments:

```{r echo=FALSE,eval=FALSE}
## 1000 experiments, each with 10 trials, prob of success is 0.6:
successes<-rbinom(n=1000,size=10,prob=0.6)
```

- Save the 1000 instances as an R object (say, successes).
- Compute the variance of the successes vector (use the command var()).


- Compute the variance of the number of successes analytically using the formula $n \theta (1-\theta)$ shown in the slides. Confirm that the two estimates (the one based on simulation and the one based on the analytical calculation) approximately match.


- Plot the distribution of the number of successes for the 1000 experiments.


```{r echo=FALSE,eval=FALSE}
successes<-rbinom(n=1000,size=10,prob=0.6)
var(successes)
10*0.6*0.4
```


```{r echo=FALSE,eval=FALSE}
hist(successes,freq=FALSE)
```

# Exercise 4

Given a normal random variable X with mean 500 and variance (not standard deviation!) $50^2$, compute

- $Prob(X>550)$
- $Prob(X<450)$
- $Prob(450<X<550)$
- $Prob(450\leq X\leq 550)$

### Answer

```{r echo=FALSE,eval=FALSE}
pnorm(550,mean=500,sd=50,lower.tail=FALSE)
pnorm(450,mean=500,sd=50)
pnorm(550,mean=500,sd=50)-pnorm(450,mean=500,sd=50)
```

# Exercise 5

Given a normal random variable X with mean 500 and variance $50^2$, 

- what is the quantile q such that $Prob(X<q) = 0.80$?
- what is the quantile q such that $Prob(X>q) = 0.30$?
- what are the two quantiles q1 and q2 such that they are equidistant from the mean 500 and $Prob(q1<X<q2)=0.95$?

### Answer

```{r echo=FALSE,eval=FALSE}
qnorm(0.80,mean=500,sd=50)
qnorm(0.30,mean=500,sd=50)

qnorm(0.975,mean=500,sd=50)
qnorm(0.025,mean=500,sd=50)
## cross check:
500+1.96*50
500-1.96*50
```

# Exercise 6

Do the following 1000 times:

- Generate 100 data points from a normal distribution with mean 500 and variance
$50^2$.
- Each time, compute the mean of the 100 data points and save it in an object. Some R code to help you do this:

### Answer

```{r echo=FALSE,eval=FALSE}
n<-100
nsim<-1000
means<-rep(NA,nsim)
for(i in 1:nsim){
  means[i]<-mean(rnorm(n,mean=500,sd=50))
}
```
- Confirm that the mean of the vector of means is close to 500.

```{r echo=FALSE,eval=FALSE}
mean(means)
```

- Confirm that the standard deviation of the vector of means is approximately the analytically computed standard error $\frac{50}{\sqrt{100}}$. 

```{r echo=FALSE,eval=FALSE}
50/sqrt(100)
sd(means)
```


- Plot the distribution of the means.

```{r echo=FALSE,eval=FALSE}
hist(means,freq=FALSE)
```
