---
title: "Normal Distribution and Z-Scores"
subtitle: "SD06 — Continuous Distributions"
author: "Samer El Kababji"
output:
  html_document:
    toc: true
format:
  html:
    code-links:
      - text: "Download R Markdown"
        icon: file-earmark-arrow-down
        href: SD06-04-normal-distribution-and-z-scores.Rmd?download=1
---

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

**Course:** Statistics and Probability for Data Science (DS14270).  
**Source deck:** Samer El Kababji, *SD06 — Continuous Distributions*.

This lesson covers the normal density, standardization, probability
calculations, the empirical rule, and linear combinations of normal variables.

## The normal model

If $X\sim N(\mu,\sigma^2)$, then

$$
f(x)=\frac{1}{\sigma\sqrt{2\pi}}
\exp\left[-\frac{(x-\mu)^2}{2\sigma^2}\right],
\qquad -\infty<x<\infty.
$$

Its mean is $\mu$ and its variance is $\sigma^2$.

```{r normal-density}
mu <- 100
sigma <- 15
x <- seq(mu - 4 * sigma, mu + 4 * sigma, length.out = 501)

plot(
  x, dnorm(x, mean = mu, sd = sigma), type = "l", lwd = 2,
  xlab = "x", ylab = "f(x)", main = "N(100, 15^2) density"
)
abline(v = mu, lty = 2, col = "steelblue")
grid()
```

The standard normal variable $Z\sim N(0,1)$ has density $\phi(z)$ and CDF
$\Phi(z)=P(Z\leq z)$.

## Z-scores

Standardization converts a normal variable to the standard normal scale:

$$
Z=\frac{X-\mu}{\sigma}.
$$

A z-score tells how many standard deviations a value lies above or below the
mean. With exam mean 70, standard deviation 10, and score 85,

$$
z=\frac{85-70}{10}=1.5.
$$

```{r exam-z-score}
score <- 85
mu <- 70
sigma <- 10
z <- (score - mu) / sigma
z
```

## Probability by standardization

For any cutoff $x$,

$$
P(X\leq x)=P\left(Z\leq\frac{x-\mu}{\sigma}\right).
$$

In the deck, $X\sim N(100,15^2)$. For $x=120$,

$$
z=\frac{120-100}{15}\approx1.33,
$$

so $P(X\leq120)\approx0.9082$ when a z-table rounds to 1.33. R retains the
unrounded z-score.

```{r normal-probability-example}
z_exact <- (120 - 100) / 15
c(
  exact_z_in_R = pnorm(z_exact),
  rounded_z_table = pnorm(1.33),
  direct = pnorm(120, mean = 100, sd = 15)
)
```

## The 68--95--99.7 rule

For a normal distribution, approximately 68%, 95%, and 99.7% of observations
fall within 1, 2, and 3 standard deviations of the mean.

```{r empirical-rule}
k <- 1:3
exact_probabilities <- pnorm(k) - pnorm(-k)

data.frame(
  interval = paste0("mu +/- ", k, " sigma"),
  exact_probability = exact_probabilities,
  rule_of_thumb = c(0.68, 0.95, 0.997)
)
```

## Quantiles

The $p$th quantile satisfies $P(X\leq x_p)=p$ and is

$$
x_p=\mu+\sigma z_p,
\qquad z_p=\Phi^{-1}(p).
$$

```{r normal-quantiles}
p <- c(0.025, 0.50, 0.975)
data.frame(
  probability = p,
  standard_normal = qnorm(p),
  N_100_15_squared = qnorm(p, mean = 100, sd = 15)
)
```

## Linear transformations and sums

If $X\sim N(\mu,\sigma^2)$ and $Y=aX+b$, then

$$
Y\sim N(a\mu+b,a^2\sigma^2).
$$

If independent $X_1\sim N(\mu_1,\sigma_1^2)$ and
$X_2\sim N(\mu_2,\sigma_2^2)$, then

$$
X_1+X_2\sim N(\mu_1+\mu_2,\sigma_1^2+\sigma_2^2).
$$

```{r normal-linear-combinations}
mu_X <- 10
sd_X <- 3
a <- -2
b <- 5

c(
  transformed_mean = a * mu_X + b,
  transformed_sd = abs(a) * sd_X
)

mu_1 <- 10; sd_1 <- 3
mu_2 <- 4; sd_2 <- 2
c(sum_mean = mu_1 + mu_2, sum_sd = sqrt(sd_1^2 + sd_2^2))
```

## Practice questions

1. If $X\sim N(50,8^2)$, find the z-score of 62.
2. Find $P(X\leq62)$ and $P(42\leq X\leq58)$.
3. Find the 90th percentile of $X$.
4. If independent $X\sim N(10,3^2)$ and $Y\sim N(4,2^2)$, give the
   distribution of $X-Y$.

::: {.callout-note collapse="true"}
### Check your answers

1. $z=(62-50)/8=1.5$.
2. `pnorm(62, 50, 8)` is approximately $0.9332$; the central interval has
   probability `pnorm(58, 50, 8) - pnorm(42, 50, 8)`, approximately $0.6827$.
3. `qnorm(0.90, 50, 8)` is approximately $60.25$.
4. $X-Y\sim N(6,13)$ because means subtract but independent variances add.
:::
