```{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 |

Quick check: $0.2+0.1+0.15+0.1+0.3+0.15=1$.

## 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$

For $x$ between integers, the cdf is flat (right-continuous). For example,
$F(2.5)=P(X\le2.5)=P(X\le2)=0.3$.

_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?_
Yes. For a discrete rv with support at integers, $f(x)=P(X=x)=F(x)-F(x-1)$, the size of the jump at $x$.

_Exercise 3: What are $F(0), F(2.5), F(10)$?_

Answer: $F(0)=0,\ F(2.5)=0.3,\ F(10)=1$.

```{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$
Interpretation: the long-run average outcome of many rolls.

```{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$

So $Var(X)=16.45-13.3225=3.1275$.

Interpretation: how spread out the outcomes are around the mean.

```{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.

In general, $Var(aX+bY)=a^2Var(X)+b^2Var(Y)+2ab\,Cov(X,Y)$.

**Example (stocks)** from OpenIntro

Leonard invests $6000 in Caterpillar (CAT) and $2000 in Exxon Mobil (XOM).
Let $X$ be the next month's return on CAT and $Y$ be the next month's return
on XOM, in decimal form (e.g., 1% = 0.01, -1% = -0.01). The dollar gain/loss is
$$Z = 6000X + 2000Y.$$

Then
$$E(Z)=6000E(X)+2000E(Y),$$
and if $X$ and $Y$ are independent,
$$Var(Z)=6000^2Var(X)+2000^2Var(Y).$$

**Example (commute time)**

A student’s daily commute time (in minutes) consists of two independent parts:

- **\(X\)** = time spent walking from home to the bus stop  
  - E(X) = 8 minutes  
  - Var(X) = 4 minutes$^2$

- **\(Y\)** = time spent riding the bus  
  - E(Y) = 22 minutes  
  - Var(Y) = 16 minutes$^2$

On some days, construction causes the walking portion to take **twice as long**, while the bus ride remains unchanged.

Define the total commute time on a construction day as  
\[
T = 2X + Y.
\]


**a)** Find the expected total commute time \(E(T)\).  
*(Show how the expectation rule applies.)*

**b)** Assuming \(X\) and \(Y\) are independent, find the variance of the total commute time \(Var(T)\).  
*(Show how the variance rule applies.)*


