<font color=blue>NOTE: Videos and graphs do not display immediately on all devices.  If this is the case for you, click the "Cell" menu option, and select "Run All".  If you still do not see the videos/graphs, try a different browser.  If that also does not work, please let your instructor know by email.</font>

# Measures of Central Tendency and Spread

In this notebook, we continue our study of continuous random variables (such as the height of a randomly chosen MATH 136 student, or the cholesterol level of a randomly chosen female living in Edmonton).  We turn our attention to the determination of summary statistics from probability density functions.  We focus on two types of statistics, namely <i>measures of central tendency</i> (summarizing typical outcomes of an experiment) and <i>measures of spread</i> (summarizing how similar/varied the outcome of an experiment are).

Three common measures of central tendency are <i>mean</i> (which the same as average, also known as expected value), <i>median</i>, and <i>mode</i>.  Two common measures of spread are <i>variance</i> and <i>standard deviation</i>.

### 1. Review: Mean or Average of a Discrete Random Variable

Recall that the average of a set of discrete values is the sum of the values divided by the number of values in the set.  Mathematically, if there are $n$ values in the set, and the values are denoted $a_i$ for $i = 1, 2, \ldots, n$, then the average is $$a_{ave} = \frac{a_1 + a_2 + \ldots + a_n}{n} = \frac{1}{n} \sum_{i=1}^n a_i.$$

In preparation for the next section, it is helpful to consider a specific example with repeated values.  Suppose that in a group of 10 people:
- 5 people are 40 years old,
- 3 people are 12 years old, and
- 2 people are 7 years old.

The average age of the 10 people in the group is 
$$A_{ave} = \frac{ 5(40) + 3(12) + 2(7) }{ 10 } = 25.$$
We can rewrite this calculation as follows:
$$A_{ave} = 40 \frac{5}{10} + 12 \frac{3}{10} + 7 \frac{2}{10}.$$
Note that the fraction 5/10 represents the proportion of people in the group with age 40, that is, the probability that a randomly selected individual from the group is 40 years old.  Similarly, the fraction 3/10 represents the probability that a randomly selected individual from the group is 12 years old, etc.  

In the last calculation, the expression on the right-hand side is written in the form of <font color=blue>the sum of "outcome times probability of that outcome"</font>.  

This form motivates the definition of average of a continuous random variable.

### 2. Mean or Average of a Continuous Random Variable

For the purposes of developing the formula of the <i>mean</i> or average of a continuous random variable, consider the probability density function $f(x)$ for a continuous random variable $X$ defined on the interval $[a,b]$.  

We will build a Riemann sum:
- divide the interval $[a,b]$ into $n$ subintervals of length $\displaystyle \Delta x = \frac{b-a}{n},$ and 
- let $x_i = a + i \cdot \Delta x$ for $i = 0, 1, 2, \ldots, n$, 

as shown in the figure below.



Focus on determining the probability that $X$ takes on a value between $x_{i-1}$ and $x_i$.  This is the area under the probability density function between $x_{i-1}$ and $x_i$, which we approximate by the area of the rectangle with width $\Delta x$ and height $f(x_i)$, that is, 
$$P( x_{i-1} \le X \le x_{i} ) = \int_{x_{i-1}}^{x_i} f(s) \ ds \approx f(x_i) \Delta x.$$
Roughly speaking, $f(x_i) \Delta x$ is the probability that $X$ takes on a value approximately equal to $x_i$.

Motivated by the form for the average value from the previous section, namely that the average is <font color=blue>the sum of "outcome times probability of that outcome"</font>, we thus have 
\begin{align}
\text{Average} & = x_0 \cdot f(x_0) \Delta x + x_1 \cdot f(x_1) \Delta x + \ldots + x_n \cdot f(x_n) \Delta x \\ 
& = \sum_{i=1}^n x_i f(x_i) \Delta x.
\end{align}

In the limit, as $n \to \infty$, and introducing the notation $\mu$ for mean or average and $E[X]$ for expected value, we obtain
$$\mu = E[X] = \int_a^b x f(x) \ dx.$$

In general, the mean (average) or expected value of the continuous random variable $X$ is 
$$\mu = E[X] = \int_{-\infty}^{\infty} x f(x) \ dx.$$

### 3. Two Additional Measures of Central Tendency: Median and Mode

The <i>median</i>, denoted by $\alpha$, is the point at which exactly half the area under the probability density function lies to the left and the other half lies to the right, that is, the median is  
$$\alpha \text{ such that} \int_{\alpha}^{\infty} f(x) \ dx = \frac{1}{2}.$$

The <i>mode</i> represents the most frequent value(s) of $X$.  If the probability density function $f(x)$ has a single peak, then the mode is the value of $x$ at which $f(x)$ has the absolute maximum.  In some cases, the probability density function $f(x)$ has multiple peaks, in which we report multiple modes, each located at a local maximum of $f(x)$.   

For <i>symmetric</i> probability density functions with a single peak, the mean, median, and mode are the same.  As we have seen, probability density functions often are not symmetric, and it is common for the mean, median, and density to be different.  Each measure of central tendency has its advantages and disadvantages.  One often determines all three measures of central tendency to obtain insight into the characteristics of a given continuous random variable.  

> Mean, median, and mode are not the only measures of central tendency.  If you are curious about additional measures of central tendency, you may find it interesting to check out this Wikipedia page on Average: https://en.wikipedia.org/wiki/Average

In the following video, we work through the details of determining the mean, median, and mode for a continuous random variable with a given probability density function.


```python
from IPython.display import YouTubeVideo
YouTubeVideo('GLwkwyGwM-4')
```










### 4. Variance and Standard Deviation of a Continuous Random Variable

Variance is a measure of how much a random variable is likely to deviate from the mean.  The higher the variance, the more spread out the graph of the probability and vice versa.  

The <i>variance</i> of a continuous random variable $X$ with probability density function $f(x)$ and mean $\mu$ is defined as the expected value of the square of the deviation of a random variable from the mean, that is, 
\begin{align}
\text{Var}[X] & = E[ ( X - \mu )^2 ] \\ 
& = \int_{-\infty}^{\infty} (x-\mu)^2 f(x) \ dx = \int_{-\infty}^{\infty} x^2 f(x) \ dx - \mu^2.
\end{align}

The latter two forms of the variance are equivalent; the last one generally is the easiest to work with in calculations.  

The <i>standard deviation</i>, denoted by $\sigma$ or SD[$X$], simply is the square root of the variance, that is, 
$$\sigma = \text{SD}[X] = \sqrt{ \text{Var}[X] }.$$

In the following video, we work through the details of determining the variance and standard deviation for a continuous random variable with a given probability density function.


```python
from IPython.display import YouTubeVideo
YouTubeVideo('xw-Gf3z8f9k')
```










### 5. Summary 

<b>Measures of Central Tendency</b>

A continuous random variable $X$ with probability density function $f(x)$ has the following measures of central tendency:

- The <i>mean</i>, also known as the average or expected value, is 
$$\mu = E[X] = \int_{-\infty}^{\infty} x f(x) \ dx.$$

- The <i>median</i> is 
$$\alpha \text{ such that} \int_{\alpha}^{\infty} f(x) \ dx = \frac{1}{2}.$$

- The <i>modes</i> are all values of $x$ where $f(x)$ has a local maximum.

<b>Measures of Spread</b>

A continuous random variable $X$ with probability density function $f(x)$ has the following measures of spread:

- The <i>variance</i> is 
\begin{align}
\text{Var}[X] & = \int_{-\infty}^{\infty} \left( x - E[X] \right)^2 f(x) \ dx = \int_{-\infty}^{\infty} \left( x - \mu \right)^2 f(x) \ dx\\
& = \int_{-\infty}^{\infty} x^2 f(x) \ dx - (E[X])^2 = \int_{-\infty}^{\infty} x^2 f(x) \ dx - \mu^2.
\end{align}

- The <i>standard deviation</i> is 
$$\sigma = \text{SD}[X] = \sqrt{ \text{Var}[X] }.$$

### 6. Further Study

Please refer to Section 12.5 in the textbook for additional treatment of this topic.  Note: If you have a hardcopy of the textbook, there is no Chapter 12.  The eBook that you have access to through WebAssign does have Chapter 12.  

### 7. Don't Forget

Don't forget to return to eClass to complete the pre-class quiz.

d2833f5534f45a059e2d2f50f0651860a011b79b
ipynb
Jupyter Notebook
MeasuresOfCentralTendencyAndSpread.ipynb
gerdadevries/MATH136Notebooks
f5090835743f5a8992be9f769fdb5ed1d8c74570
Apache-2.0
MeasuresOfCentralTendencyAndSpread.ipynb
gerdadevries/MATH136Notebooks
f5090835743f5a8992be9f769fdb5ed1d8c74570
Apache-2.0
MeasuresOfCentralTendencyAndSpread.ipynb
gerdadevries/MATH136Notebooks
f5090835743f5a8992be9f769fdb5ed1d8c74570
Apache-2.0
Qwen/Qwen-72B
1. YES
2. YES