```{r}
#| message: false
#| warning: 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=F}
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\leq 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$

_Exercise 1: Fill in the cdf table using the pmf table._

_Exercise 2: If we were only given the completed cdf table, are we able to know the pmf of the random variable?_

_Exercise 3: What are $F(0), F(2.5), 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)=\pi$

$\sigma^2=Var(X)=\pi(1-\pi)$

_Example: From a deck of 52 cards, randomly pick a card, let the event be that the suit of the card picked is heart._

## Geometric Distribution

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

$X\sim Geometric(\pi)$

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

$\mu=E(X)=\frac{1-\pi}{\pi}$

$\sigma^2=Var(X)=\frac{1-\pi}{\pi^2}$

```{r, echo=F, warning=F}
# Parameters
p_values <- c(0.1, 0.3, 0.5)  # Probability values
x <- 0:10  # Range of x values (number of failures before the first success)

# Create a data frame for ggplot
data <- data.frame(
  x = rep(x, times = length(p_values)),
  p = unlist(lapply(p_values, function(p) dgeom(x, prob = p))),
  prob = rep(p_values, each = length(x))
)

# Plot using ggplot2
ggplot(data, aes(x = x, y = p, color = factor(prob), group = prob)) +
  geom_line(size=1) +             # Add lines
  geom_point(size=2) +            # Add points
  scale_color_manual(
    values = c("red", "blue", "green"),
    name = "p"
  ) + 
  scale_x_continuous(breaks=0:10) +
  labs(
    title = "PMF of Geometric Distribution",
    x = "Number of Failures (x)",
    y = "Probability"
  ) +
  theme_minimal(base_size = 16)  # Increase overall font size
```

_Exercise: From a deck of 52 cards, randomly pick a card, if the suit of the card picked is not heart, then replace the card into the deck, shuffle the deck and pick a new card. Repeat the procedure until the first heart is picked. What is the probability that the number of trials before the last one equals to 3? What is the probability that the number of trials before the last one is smaller or equal to 3?_

```{r, include=T}
# f(3)
dgeom(x = 3, prob = 0.25)

# Calculate F(3) by f(0) + f(1) + f(2) + f(3)
dgeom(x = 0, prob = 0.25) + dgeom(x = 1, prob = 0.25)  + dgeom(x = 2, prob = 0.25) + dgeom(x=3, prob=0.25)
sum(dgeom(x=0:3, prob=0.25))

# Directly calculate F(3)
pgeom(q = 3, prob = 0.25) 
```



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

4.14 on page 166 (a-c)

_Remember the book uses a different definition for geometric distribution_

__4.14 Defective Rate.__ A machine that produces a special type of transistor (a component of computers) has a 2\% defective rate. The production is considered a random process where each transistor is independent of the others. 

(a) What is the probability that the $10^{th}$ transistor produced is the first with a defect?

(b) What is the probability that the machine produces no defective transistors in a batch of 100?

(c) On average, how many transistors would you expect to be produced before the first with a defect? What is the standard deviation?

Solutions:

Let $X$ be a discrete random variable representing the number of transistors before the first defective transistor. We can say that $X \sim Geom(0.02)$.

(a) $P(X = 9) = f(9)$

```{r, include=T}

#f(9)
dgeom(x = 9, prob = 0.02)

```

(b) $P(X \geq 100) = 1 - P(X < 100) = 1 - P(X \leq 99) = 1 - F(99)$

```{r, include=T}

#1 - F(99)
1 - pgeom(q = 99, prob = 0.02)

```

(c) $E[X] = \frac{1-0.02}{0.02}$, $Var(X) = \frac{1-0.02}{0.02^2}$

The standard deviation can be calculated by taking the square root of the variance.

## 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)=n\pi$

$Var(X)=n\pi(1-\pi)$

```{r, echo=F, warning=F}
# Binomial, fixed n=10
n <- 10  # Number of trials
p_values <- c(0.2, 0.5, 0.8)  # Probability of success
x <- 0:n  # Range of x values (number of successes)

# Create a data frame for ggplot
data <- data.frame(
  x = rep(x, times = length(p_values)),
  p = unlist(lapply(p_values, function(p) dbinom(x, size = n, prob = p))),
  prob = rep(p_values, each = length(x))
)

# Plot using ggplot2
ggplot(data, aes(x = x, y = p, color = factor(prob), group = prob)) +
  geom_line(size = 1) +             # Add lines
  geom_point(size = 2) +            # Add points
  scale_color_manual(
    values = c("red", "blue", "green"),
    name = "p"
  ) + 
  scale_x_continuous(breaks = 0:n) +
  labs(
    title = "PMF of Binomial Distribution, fixed n=10",
    x = "Number of Successes (x)",
    y = "Probability"
  ) +
  theme_minimal(base_size = 16)     # Increase overall font size
```

```{r, warning=F, echo=F}
p <- 0.5  # Fixed probability of success
n_values <- c(10, 20, 40)  # Different values for number of trials
x_values <- lapply(n_values, function(n) 0:n)  # Range of x values for each n

# Create a data frame for ggplot
data <- data.frame(
  x = unlist(x_values),
  p = unlist(lapply(seq_along(n_values), function(i) dbinom(x_values[[i]], size = n_values[i], prob = p))),
  n = factor(rep(n_values, times = sapply(x_values, length)))  # Add n as a factor
)

# Plot using ggplot2
ggplot(data, aes(x = x, y = p, color = n, group = n)) +
  geom_line(size = 1) +             # Add lines
  geom_point(size = 2) +            # Add points
  scale_color_manual(
    values = c("red", "blue", "green"),
    name = "n"
  ) + 
  labs(
    title = "PMF of Binomial Distribution, fixed p=0.5",
    x = "Number of Successes (x)",
    y = "Probability"
  ) +
  theme_minimal(base_size = 16)     # Increase overall font size
```

_Exercise: From a deck of 52 cards, randomly pick a card and record the suit, then replace the card into the deck, shuffle the deck, pick a new card and record the suit. Repeat the procedure 10 times. What is the probability that 8 of the 10 suits recorded are hearts? What is the probability that at least 8 of the 10 suits recorded are hearts?_

```{r, include=T}
# f(8)
dbinom(x = 8, size = 10, prob = 0.25)

# f(8) + f(9) + f(10)
dbinom(x = 8, size = 10, prob = 0.25) + dbinom(x = 9, size = 10, prob = 0.25) + dbinom(x = 10, size = 10, prob = 0.25)
sum(dbinom(x = 8:10, size = 10, prob = 0.25))

# 1 - F(7)
1 - pbinom(q = 7, size = 10, prob = 0.25)
```

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

4.22 on page 176 (a-c)

__4.22 Arachnophobia.__ A Gallup Poll found that 7\% of teenagers (ages 13 to 17) suffer from arachnophobia and are extremely afraid of spiders. At a summer camp there are 10 teenagers sleeping in each tent. Assume that these 10 teenagers are independent of each other.

(a) Calculate the probability that at least one of them suffers from arachnophobia.

(b) Calculate the probability that exactly 2 of them suffer from arachnophobia

(c) Calculate the probability that at most 1 of them suffers from arachnophobia

Solutions:

Let $X$ be a discrete random variable representing the number of teenagers at the summer camp suffering from arachnophobia. We can say that $X \sim Binom(10, 0.07)$.

(a) $P(X \geq 1) = 1 - P(X = 0) = 1 - f(0)$

```{r, include=T}

#f(0)
dbinom(x = 0, size = 10, prob = 0.07)

```

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

```{r, include=T}

#f(2)
dbinom(x = 2, size = 10, prob = 0.07)

```

(c) $P(X \leq 1) = F(1)$

```{r, include=T}

#F(1)
pbinom(q = 1, size = 10, prob = 0.07)

#f(0) + f(1)
dbinom(x = 0, size = 10, prob = 0.07) + dbinom(x = 1, size = 10, prob = 0.07)
```

## 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)=\lambda$

$Var(X)=\lambda$

```{r, echo=F, warning=F}
# Parameters for Poisson distribution
lambda_values <- c(1, 2, 5, 10)  # Lambda values
x <- 0:20  # Range of x values

# Create a data frame for ggplot
data <- data.frame(
  x = rep(x, times = length(lambda_values)),
  p = unlist(lapply(lambda_values, function(lambda) dpois(x, lambda = lambda))),
  lambda = rep(lambda_values, each = length(x))
)

# Plot using ggplot2
ggplot(data, aes(x = x, y = p, color = factor(lambda), group = lambda)) +
  geom_line(size = 1) +             # Add lines
  geom_point(size = 2) +            # Add points
  scale_color_manual(
    values = c("red", "blue", "green", "purple"),
    name = expression(lambda)
  ) + 
  scale_x_continuous(breaks = 0:20) +
  labs(
    title = "PMF of Poisson Distribution",
    x = "Number of Events (x)",
    y = "Probability"
  ) +
  theme_minimal(base_size = 16)     # Increase overall font size
```

_Exercise: It is estimated that one bus arrives at the stop every 6 minuts. What is the probability that 10 buses will arrive at the stop during the next hour? What is the probability that the number of buses that arrive next hour is between 4 and 15 (both included)?_
```{r, include=T}
lambda = 60 / 6

# f(10)
dpois(x = 10, lambda = lambda)

# f(4) + f(5) + ... + f(15)
sum(dpois(x = 4:15, lambda = lambda))

# F(15) - F(3)
ppois(q = 15, lambda = lambda) - ppois(q = 3, lambda = lambda)
```



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

4.32 on page 185 (a,b,d)

__4.32 Stenographer's typos.__ A very skilled court stenographer makes one typographical error (typo) per hour on average.

(a) What probability distribution is most appropriate for calculating the probability of a given number of typos this stenographer makes in an hour?

(b) What are the mean and the standard deviation of the number of typos this stenographer makes?

(d) Calculate the probability that this stenographer makes at most 2 typos in a given hour.

Solutions:

(a) Let $X$ be a discrete random variable representing the number of typos the stenographer makes in one hour. We can say that $X \sim Poisson(1)$.

(b) The mean and standard deviation of a $Poisson(1)$ random variable are both 1.

(d) $P(X \leq 2) = F(2)$

```{r, include=T}

# F(2)
ppois(q = 2, lambda = 1)


# f(0) + f(1) + f(2)
dpois(x = 0, lambda = 1) + dpois(x = 1, lambda = 1) + dpois(x = 2, lambda = 1)

```

