---
title: "SML201 Intro to Probability"
output: html_document
---

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

### Bernoulli variables

A Bernoulli variable can be thought of as the outcome of a ross of a biased coin. We can get one outcome using

```{r}
rbinom(n = 1, size = 1, prob = 0.8)
```

For Bernoulli variables, `size = 1` always. We'll see situations when `size` is not ` in a second. `n = 1` means we want *one* coin toss. If we want the outcomes of 10 coin tosses, we can go

```{r}
rbinom(n = 10, size = 1, prob = 0.8)
```


### Binomial variables

A binomial variable is the the number of times the coin came up heads out of `size` tosses. For example, here are the outcomes of 100 experiments, where we tossed the coin twice each time:

```{r}
rbinom(n = 100, size = 2, prob = 0.8)  
```
    
    heads: 1, tails :0
    rbinom returns the number of 1's
    size: # of tosses
    n: # of experiments


We can obtain the probability mass function value for a particular value by repeating the same experiment multiple times. For example, suppose we want to know how often the number of times the coin came up heads is 0, when the coin is weighted with P(heads) = 1, and it is tossed twice per experiment.

Here is the proportion of the time that the the coin came up heads 0 times:

```{r}
mean(rbinom(n = 1000000, size = 2, prob = 0.8) == 0)
```


We can obtain this directly using `dbinom`:

```{r}
dbinom(x = 0, size = 2, prob = 0.8)
```

`dbinom` computes the probability mass function for a particular `x` (the number of times the coin came up Heads).

We can use `dbinom` for Bernoulli variables as well:

```{r}
dbinom(x = 1, size = 1, prob = 0.65)
```

(Of course -- the probability of the coin coming up heads once if the probability of heads is 65% is... 65%)

```{r}
dbinom(x = 0, size = 1, prob = 0.65)
```

...and the probability of the coin coming up Tails must be 35%.


We can display the probability mass function by computing the value of `dbinom` for every possible value.

We use a trick where we can plug in multiple values of `x` at the same time:

```{r}
dbinom(x = c(0, 1), size = 1, prob = 0.5)
```

We are now ready to display the pmf for a binomial variable with 20 coin tosses and a fair coin:

```{r}
x <- 0:20
df <- data.frame(x = x, prob = dbinom(x = x, size = 20, prob = 0.5))
ggplot(df) + 
  geom_bar(mapping = aes(x = x, y = prob), stat = "identity")
```


We can get the same kind of visualization by actually performing the experiment 10,000 times and tallying the number of times that each outcome occurred:

```{r}
dat <- data.frame(x = rbinom(n = 10000, size = 20, prob = 0.5))
ggplot(dat) + 
  geom_histogram(mapping = aes(x = x, y = ..count../sum(..count..)), binwidth = 1)
```

### Intro to cumulative probability

One way to compute the probability that the number of times the coin comes up heads is between 15 and 20 is to sum up the probabilities that the coin comes up 15, 16, 17, 18, 19, and 20 times:

```{r}
sum(dbinom(x = c(15, 16, 17, 18, 19, 20), size = 20, prob = 0.5))
```

We can also use cumulative probability for that. `pbinom` computes the cumulative probability function for binomial variables. For example, the probability that the coin will come up heads up to 3 times is:

```{r}
pbinom(q = 3, size = 20, prob = 0.5) # Note: we use q, not x
```

We can also do this "manually" using `dbinom`:

```{r}
sum(dbinom(x = c(0, 1, 2, 3), size  = 20, prob = 0.5))
```

Now, we can compute the probability that a fair coin will come up heads between 15 and 20 times if there are 20 tosses using `pbinom`:

```{r}
pbinom(q = 20, size = 20, prob = 0.5) - pbinom(q = 14, size = 20, prob = 0.5)
```

