```{r}
#| echo: false
#| message: false
library(tidyverse)
```

# Discrete Random Variable

Let X be the number you get when rolling an unfair die.

| x      | 1   | 2   | 3    | 4   | 5   | 6    |
|:-------|:----|:----|:-----|:----|:----|:-----|
| P(X=x) | 0.2 | 0.1 | 0.15 | 0.1 | 0.3 | 0.15 |

## Probability Mass Function (pmf)

$P(X=1)=f(1)=0.2$

$P(X=2)=f(2)=0.1$

$P(X=3)=f(3)=0.15$

$P(X=4)=f(4)=0.1$

$P(X=5)=f(5)=0.3$

$P(X=6)=f(6)=0.15$

```{r}
#| echo: false
x <- 1:6
y <- c(0.2, 0.1, 0.15, 0.1, 0.3, 0.15)

data <- data.frame(x = x, y = y)

data %>% 
  ggplot() +
  aes(x = x, y = y) +
  geom_segment(data = data, 
               aes(x = x, y = 0, 
                   xend = x, yend = y)) +
  labs(x = expression(paste("x")),
       y = expression(paste("f(x)"))) +
  theme_bw() +
  theme(text = element_text(size = 20)) + 
  scale_x_continuous(breaks = pretty(data$x, n = 6))
```

## Cumulative Distribution Function (cdf)

| x      | 1   | 2   | 3    | 4   | 5   | 6    |
|:-------|:----|:----|:-----|:----|:----|:-----|
| P(X=x) | 0.2 | 0.3 | ? | ? | ? | 1 |

$P(X\leq1)=F(1)=0.2$

$P(X\leq2)=F(2)=0.3$

...

$P(X\leq6)=F(6)=1$

Question 1: $F(0)=?$

Question 2: $F(2.5)=?$

Question 3: $F(10)=?$

```{r, echo=F}
x <- 1:6
y <- cumsum(c(0.2, 0.1, 0.15, 0.1, 0.3, 0.15))
xend <- c(2, 3, 4, 5, 6, 6)

data <- data.frame(x = x,
                   y = y,
                   xend = xend)

ggplot(data = data,
       aes(x = x, y = y, xend = xend, yend = y)) +
  geom_point() +
  geom_segment() +
  scale_x_continuous(breaks=1:6,
                     limits = c(1, 6)) +
  scale_y_continuous(limits = c(-.1, 1.1)) +
  labs(x = "x",
      y = expression(paste("F(x)")))+
  theme_bw() +
  theme(text = element_text(size = 20)) 
```

## Expectation

| x    | 1   | 2   | 3    | 4   | 5   | 6    |
|:-----|:----|:----|:-----|:----|:----|:-----|
| f(x) | 0.2 | 0.1 | 0.15 | 0.1 | 0.3 | 0.15 |

$E[X]=\sum_{x\in S}xf(x)=1\times0.2  +2\times 0.1 +3\times  0.15 +4\times 0.1 +5\times  0.3 +6\times  0.15=3.65$

```{r}
x <- 1:6
f_x <- c(0.2, 0.1, 0.15, 0.1, 0.3, 0.15)
mu <- sum(x * f_x)
mu
```

## Variance

$Var(X)=E[(X-E[X])^2]=E[X^2]-(E[X])^2$, where

$E[X^2]=\sum_{x\in S}x^2f(x)=1^2\times0.2  +2^2\times 0.1 +3^2\times  0.15 +4^2\times 0.1 +5^2\times  0.3 +6^2\times  0.15=16.45$

and

$(E[X])^2=3.65^2=13.3225$

```{r}
mu_2 <- sum(x^2 * f_x)
mu_2 - mu^2
```

## Linear Combination of Random Variables

$E(aX+bY)=a\cdot E(X)+b\cdot E(Y)$

$Var(aX+bY)=a^2\cdot Var(X)+b^2\cdot Var(Y)$ if $X$ and $Y$ are independent.

# Discrete Distributions

## Bernoulli Distribution

The Bernoulli distribution represents the outcome (success/failure) of an event with success probability of $\pi$.

$X\sim Bernoulli(\pi)$

| x    | 0       | 1     |
|:-----|:--------|:------|
| f(x) | $1-\pi$ | $\pi$ |

pmf: $f(x)=\pi^x(1-\pi)^{1-x},\ x\in \{0,1\}$


$\mu=E(X)=$ ?

$\sigma^2=Var(X)=$ ?

## Geometric Distribution

The Geometric distribution represents the number of failures before the first success when running independent trials with success probability $p$.

$X\sim Geometric(p)$

pmf: $f(x)=(1-p)^xp\ ,x=0,1,2,...$

$\mu=E(X)=$ ?

$\sigma^2=Var(X)=$ ?

```{r, echo=F}
x <- seq(0,30)
y <- dgeom(x, 0.1)

data <- data.frame(x = x,
                   y = y)
data %>% 
  ggplot() +
  aes(x = x, y = y) +
  geom_segment(data = data, 
               aes(x = x, y = 0, 
                   xend = x, yend = y)) +
  labs(x = expression(paste("x")),
       y = expression(paste("f(x)"))) +
  ggtitle("Geometric pmf when p=0.1")+
  theme_bw() +
  theme(text = element_text(size = 20)) 
```

```{r, echo=F}
x <- seq(0,30)
y <- dgeom(x, 0.3)

data <- data.frame(x = x,
                   y = y)
data %>% 
  ggplot() +
  aes(x = x, y = y) +
  geom_segment(data = data, 
               aes(x = x, y = 0, 
                   xend = x, yend = y)) +
  labs(x = expression(paste("x")),
       y = expression(paste("f(x)"))) +
  ggtitle("Geometric pmf when p=0.3")+
  theme_bw() +
  theme(text = element_text(size = 20)) 
```

```{r}
# f(1)
dgeom(x = 1, prob = 0.1)
```

```{r}
# F(2) = f(0) + f(1) + f(2)
dgeom(x = 0, prob = 0.1) + dgeom(x = 1, prob = 0.1)  + dgeom(x = 2, prob = 0.1) 
pgeom(q = 2, prob = 0.1) 
```



### Exercises from [the book](https://www.openintro.org/go/?id=os4_for_screen_readers&referrer=/book/os/index.php)

4.14 on page 166

## Binomial Distribution

The Binomial distribution represents the number of successes out of $n$ independent trials with success probability $\pi$.

$X\sim Binomial(n,\pi)$

pmf: $f(x)={n \choose x}\pi^x(1-\pi)^{n-x},\ x\in \{0,1,2,...,n\}$

$E(X)=$ ?

$Var(X)=$ ?

```{r}
# f(1)
dbinom(x = 1, size = 3, prob = 0.5)
# F(1)
pbinom(q = 1, size = 3, prob = 0.5)
```

### Exercises from [the book](https://www.openintro.org/go/?id=os4_for_screen_readers&referrer=/book/os/index.php)

4.22 on page 176

## Poisson Distribution

The Poisson distribution can represent the number of events occurring within a fixed period of time.

$X\sim Poisson(\lambda)$

pmf: $f(x)=\frac{\lambda^x}{x!}e^{-\lambda},\ x=0,1,2,...$

$E(X)=$ ?
$Var(X)=$ ?

```{r, echo=F}
theme_set(theme_gray(base_size = 22))

x <- rep(seq(0, 20), 4)
lambda <- c(rep(1, 21), rep (2, 21), rep (5, 21), rep(10, 21))


y <- dpois(x, lambda)

data <- data.frame(x = x,
                   y = y,
                   lambda = lambda)

data$lambda <- 
  factor(data$lambda,
         levels = c("1", "2", "5", "10"),
         labels =
           c(expression(paste(lambda, " = 1")),
             expression(paste(lambda, " = 2")),
             expression(paste(lambda, " = 5")),
             expression(paste(lambda, " = 10"))))

data %>% 
  ggplot() +
  aes(x = x, y = y) +
  geom_segment(data = data, 
               aes(x = x, y = 0, 
                   xend = x, yend = y)) +
  facet_wrap(data$lambda, 
             labeller = label_parsed) +
  labs(x = expression(paste("x")),
       y = expression(paste("f(X = x)")))
```

```{r}
# f(2)
dpois(x = 2, lambda = 5)
# F(2)
ppois(q = 2, lambda = 5)
```



### Exercises from [the book](https://www.openintro.org/go/?id=os4_for_screen_readers&referrer=/book/os/index.php)

4.32 on page 185
