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

# Recap of set theory

The set $S$ is the full set of all American states. The sets $A$ and $B$ are subsets of $S$ defined as follows:

A is the set of states starting with M

B is the set of states ending with s.

Now, the complement of a set refers to all the elements in the universal set $S$ that are not in the given set. So, $A^c$ represents all states not starting with M, and $B^c$ represents all states not ending with S. 

The expression $A^c \cup B^c$ represents all states that either do not start with M or do not end with S, or both. Mathematically, this can be written as:
$A^c \cup B^c = \{ x \in S : x \notin A \text{ or } x \notin B \}$

### Multiple Choice Question:

Which of the following expressions represents the set of all states that either do not start with the letter M or do not end with the letter S?

a) $A \cap B$

b) $A \cup B$

c) $A^c \cap B^c$

d) $A^c \cup B^c$

- Answer: d)


# Probability Density Function (pdf)

For a continuous random variable  $X$, the pdf is denoted by $f(x)$ and satisfies:

1. $f(x)\geq0$ for all $x$,
2. $\int_{-\infty}^{\infty} f(x) \, dx = 1$.

```{r}
# Example: f(x) = 2x for x in (0, 1)
x <- seq(0, 1, by = 0.01)
y <- 2*x
plot(x, y, type = "l", lwd = 2, ylab = "Density", xlab = "x", main = "PDF of f(x)=2x in (0,1)")
```

# Cumulative Distribution Function (CDF)

The CDF of $X$ is given by $F(x) = \int_{-\infty}^{x} f(t) \, dt$.

```{r}
# Example: f(x) = 2x for x in (0, 1)
y_cdf <- x^2
plot(x, y_cdf, type = "l", lwd = 2, ylab = "Probability", xlab = "x", main = "CDF of f(x)=2x in (0,1)")
```

# Expected Value

The expected value of $X$ is given by $E(X) = \int_{-\infty}^{\infty} x \cdot f(x) \, dx$.

```{r}
# Example: f(x) = 2x for x in (0, 1)
mean_val <- integrate(function(x) x * 2*x, 0, 1)$value
mean_val
```

# Variance

The variance of $X$ is given by $\text{Var}(X) = E((X - E(X))^2) = \int_{-\infty}^{\infty} (x - E(X))^2 \cdot f(x) \, dx$.

```{r}
# Example: f(x) = 2x for x in (0, 1)
var_val <- integrate(function(x) (x - mean_val)^2 * 2*x, 0, 1)$value
var_val
```


# Uniform Distribution

The Uniform distribution is defined by its two parameters, $a$ and $b$, which are the minimum and maximum values, respectively. The pdf of a Uniform distribution is given by:

$f(x) = \frac{1}{b-a} \, \text{ for } x \in [a, b]$

```{r}
# Example: Uniform distribution U(0,1)
x <- seq(0, 1, by = 0.01)
y <- dunif(x, min = 0, max = 1)
plot(x, y, type = "l", lwd = 2, ylab = "Density", xlab = "x", main = "PDF of Uniform(0,1)")
```

```{r}
# find the f(0.5)
dunif(x = 0.5, min = 0, max = 1)
```

```{r}
# find the F(0.5)
punif(q = 0.5, min = 0, max = 1)
```

Example:

Imagine a spinner with a continuous circular scale ranging from 0 to 1. 

1. What is the probability that it landing on point 0.5?

2. What is the probability that it landing on a value smaller than 0.5?

# Exponential Distribution

The Exponential distribution is defined by its rate parameter $\lambda$. The pdf of an Exponential distribution is given by:

$f(x) = \lambda e^{-\lambda x} \, \text{ for } x \geq 0$

```{r}
# Example: Exponential distribution with lambda = 1
x <- seq(0, 5, by = 0.01)
y <- dexp(x, rate = 1)
plot(x, y, type = "l", lwd = 2, ylab = "Density", xlab = "x", main = "PDF of Exponential(1)")
```

```{r}
# find the f(2)
dexp(x = 2, rate = 1)
```

```{r}
# find the F(2)
pexp(q = 2, rate = 1)
```

Example:

Suppose a bus arrives at a certain bus stop every 20 minutes on average.

1. What is the distribution of the time a person have to wait?

2.Find the probability that a bus arrives within 10 minutes

If a bus arrives at a certain bus stop every 20 minutes on average, then the rate parameter $\lambda$ is $1/20$ buses per minute.

Now, if we want to find the probability that a bus arrives within 10 minutes, we can substitute these values into the cumulative distribution function (CDF) of the exponential distribution:

$F(x;\lambda) = 1 - e^{-\lambda x}$

Plugging the values, we get:

$F(10; 0.05) = 1 - e^{-(0.05)(10)} = 1 - e^{-0.5} \approx 1 - 0.606 = 0.394$

So, there is approximately a 39.4% chance that a bus will arrive within the next 10 minutes at this bus stop given the new average arrival time of 20 minutes.


# Normal Distribution

The Normal distribution is defined by its mean $\mu$ and standard deviation $\sigma$. The pdf of a Normal distribution is given by:

$f(x) = \frac{1}{\sigma \sqrt{2\pi}} e^{-(x-\mu)^2 / (2\sigma^2)}$

```{r}
# Example: Normal distribution with mu = 0 and sigma = 1
x <- seq(-5, 5, by = 0.01)
y <- dnorm(x, mean = 0, sd = 1)
plot(x, y, type = "l", lwd = 2, ylab = "Density", xlab = "x", main = "PDF of Normal(0,1)")
```

```{r}
# find the f(2)
dnorm(x = 3, mean = 0, sd = 1)
```

```{r}
# find the F(2)
pnorm(q = 3, mean = 0, sd = 1)
```

Example: 4.4 on page 160, 4.7 on page 161

