---
title: "Chapter 4.4 Normal random variables"
author: "Name"
output: pdf_document
---

**Situation** The most widely used continuous distribution is the normal distribution, a distribution with the familiar "bell" shape. Many characteristics in nature exhibit this shape: 

* heights of humans, trees, wombats
* failure of mechanical parts due to wear and tear
* random noise in electrical circuits

The normal distribution is an extremely important distribution and we will discuss some of its properties in this section.  There are three main reasons why the normal distribution is so important:

1.

2. 

3. 


**Random variable:** Let $X$ be a random variable from a Normal distribution defined by the parameters $\mu$ for the mean, and $\sigma^{2]}$ for the variance. 

**Distributional Notation:** $X \sim N(\mu, \sigma^{2})$. 

**pmf:** 
$$
f(x)=\frac{1}{\sqrt{2\pi\sigma^{2}}}e^{-\big[\frac{(x-\mu)}{2\sigma}\big]^{2}}
$$

**Mean and variance:** $E(X) = \mu \qquad Var(X) = \sigma^{2}$

**R commands:**

Note that R uses the _standard deviation_, NOT the variance. 

* `dnorm(x, mu, sd)` to compute $P(X == x)$ 
* `pnorm(x, mu, sd)` to compute $P(X\leq x)$ (the cdf)
* `rnorm(N, mu, sd)` to randomly draw N samples from a $X \sim N(\mu, \sigma^{2})$ distribution. 


**Visualizing the shape:**
```{r, fig.width=10, fig.height=3, fig.align='center'}
par(mfrow=c(1,3))
hist(rnorm(1000, 0, 1))
hist(rnorm(1000, 5, 2))
hist(rnorm(1000, 100, 8))
```

\newpage

## Standard Normal Random Variable
The _standard normal random variable Z_ is a special case of the Normal distribution with mean $\mu = 0$ and variance $\sigma^{2}=1$. The PDF then simplifies to

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

### Example
Use R to evaluate the following integrals under the Standard Normal $Z$ distribution. In each case, draw a diagram of $f_{Z}(z)$ and shade the area that corresponds to the integral. 

1. $\frac{1}{\sqrt{2\pi}}\int^{1.33}_{-.44} e^{-z^{2}/2}$



2. $\frac{1}{\sqrt{2\pi}}\int^{.94}_{-\infty} e^{-z^{2}/2}$

### Example
Use `pnorm` to calculate the theoretical probability for each question, and confirm via simulation using `rnorm`.

1. $P(Z > 1.3)$



2. $P(-0.15 < Z < 1.5)$ 



### You try it
Use R for all steps, do not do these by hand or using Z tables. Use the `integrate`, `pnorm` and `rnorm` functions. 

1. $\frac{1}{\sqrt{2\pi}}\int^{2}_{-1} e^{-z^{2}/2}$



2. $\frac{1}{\sqrt{2\pi}}\int^{2.1}_{-\infty} e^{-z^{2}/2}$



3. $P(Z < 0.9)$



4. $P(1.1 < Z < 2.5)$ 



5. $P(Z > 0.9)$

\newpage

### Example
A normally distributed population of lemming body weights has a mean of 63.5 g and a standard deviation 12.2 g. 

1. Define the random variable. 


2. Draw a picture of the distribution


For each question below, write the question in math notation, sketch a picture, calculate the theoretical probability using `pnorm`, and simulate the probability using `rnorm`. 

1. What proportion of this population is 78.0 g or larger?



2. What proportion of this population is 78 g or smaller?


3. What is the probability of choosing at random from  this population a weight smaller than 41 g?


4. What is the probability of choosing at random from this population a weight between 60 and 70 g?

\newpage

### You try it
1. According to a recent study, the carapace length for adult males of a certain species of tarantula are normally distributed with a mean of 17.45 mm and a standard deviation of 1.85 mm. Answer these questions using both simulation (`rnorm`) and the R function `pnorm`.

a. Would a tarantula that had a carapace longer than 21 mm be unusual?


b. What is the probability that the length of a carapace is between 16mm and 18 mm.

## Inverse Normal

It is often of interest to calculate a quantile of the normal distribution.  For instance, maybe we want to know the score you would need on the SAT exam to be in the *top 10th percentile*. For this sort of problem we would want to use the `qnorm(p,mu,sigma)` where $p$ is the area to the left of a certain value of interest.

> A _quantile_ divides the range of a probability distribution into intervals of equal probability. 



### Example
Let's look at those lemmings again. Recall the weight of a lemming can be described as $X \sim N(63.5, 12.1^{2})$. What lemming weight corresponds to the 80th percentile? 



* Translate question into mathematical notation


* Draw picture 


* Find quantile $x$ using `qnorm`



### You try it
Reconsider the tarantula example above. What carapace length corresponds to the **top 20th percentile**.



