---
title: "PSYC 201A Lab 03: Probability"
author: "Wenhao (James) Qi"
date: "2021-10-13"
---

```{r}
library(tidyverse)
```

# 1 Basic probability rules (base rate neglect)

Suppose that 1% of competitive cyclists use performance-enhancing drugs and that a particular drug test has a 5% false positive rate and a 10% false negative rate.

```{r}
p.drug <- 0.01
p.pos.given.legit <- 0.05
p.neg.given.drug <- 0.1
```

# 1.1 (5 min)

What is the probability that a cyclist is not taking drugs given the test was negative?

```{r}

```

What is the probability that a cyclist is taking drugs given the test was positive?

```{r}

```

# 2 Binomial distribution

In this section, we use a bent coin P(H) = 0.65.

```{r}
p.head <- 0.65
```

## 2.1 Sampling (5 min)

Sample the results of 10 coin flips and count the number of heads.

Hint: `runif`

```{r}

```

Take 100 such samples.

Hint: `replicate`

```{r}

```

Do the same thing with `rbinom`.

```{r}

```

## 2.2 PMF (10 min)

Calculate analytically the probability that there are 3 heads in the 10 coin flips.

Hint: `choose`

```{r}

```

Do the same thing with `dbinom`:

```{r}

```

Calculate analytically the probability mass function and plot it:

```{r}

```

Do the same thing with `dbinom`:

```{r}

```

## 2.3 CDF (10 min)

Calculate analytically the probability that there are 3 or fewer heads in the 10 coin flips.

```{r}

```

Do the same thing with `pbinom`.

```{r}

```

Calculate analytically the cumulative distribution function and plot it.

```{r}

```

Do the same thing with `pbinom`.

```{r}

```

## 2.4 Quantile (5 min)

What's smallest integer n such that the number of heads is less than or equal to n at least 50% of the time?  Calculate analytically.

```{r}

```

Do the same thing with `qbinom`.

```{r}

```

## 2.5 Moments (10 min)

Calculate analytically the expected value of the number of heads.

```{r}

```

Simulate the expected value with 10000 samples.

```{r}

```

Calculate analytically the standard deviation of the distribution.

```{r}

```

Simulate the standard deviation with 10000 samples.

```{r}

```

# 3 Normal distribution (15 min)

In this section we assume people's IQ follows a normal distribution with mean = 100 and sd = 15.

Take 10 samples of IQ.

```{r}

```

Plot the probability density function of IQ.

```{r}

```

What proportion of people have an IQ lower than 120?

```{r}

```

What proportion of people have an IQ between 80 and 120?

```{r}

```

Plot the cumulative probability function of IQ.

```{r}

```

What's the value x such that 90% of people have a lower IQ than x?

```{r}

```

Simulate the mean of IQ with 10000 samples.

```{r}

```

Simulate the standard deviation of IQ with 10000 samples.

```{r}

```

# 4 (Bonus challenge) Central limit theorem

Use numerical samples to demonstrate that the sampling distribution of the sample mean approaches a normal distribution as the sample size increases, for beta(0.01,0.01), unif(0,1) and gamma(1,1).

```{r}

```
