Part 2 - Basic Statistical Concepts and Tools

Basic Statistics

Let’s review some basic statistical concepts and tools.

Sample Mean

Definition 1 (Sample Mean)

The sample mean is defined as,

xˉ=1ni=1nxi\bar{x} = \frac{1}{n} \sum_{i=1}^{n} x_i

where xix_i is the ii-th observation in the sample.

If we’re drawing from a population, the population mean is denoted by E(X)\mathbb{E}(X).

In R, we can calculate the sample mean using the mean() function.

x <- 1:10
> mean(x)
[1] 5.5
> sum(x)/lengt(x)
[1] 5.5

If we assume the components of the data vector are independent and identically distributed, the sample mean is an unbiased estimator of the population mean.

Sample Median

The sample median mm of x1,x2,,xnx_1, x_2, \ldots, x_n is the middle value of the ordered data set.

Let the sorted data be denoted by x(1)x(2)x(n)x_{(1)} \leq x_{(2)} \leq \ldots \leq x_{(n)}. Then,

m={xk+1n=2k+1 (odd)12(xk+xk+1)n=2k (even)m = \begin{cases} x_{k+1} & n = 2k + 1 \text{ (odd)} \newline \frac{1}{2} (x_{k} + x_{k+1}) & n = 2k \text{ (even)} \end{cases}

The sample median is simply median() in R. The median is a special kind of quantile, which we will talk about later. Important to note is that the median and mean can differ significantly, especially when the data is skewed.

The median is insensitive to a few unusually large or unusually small observations, this is called robustness.

Sample Variance

Definition 2 (Sample Variance)

The sample variance is defined as,

s2=1n1i=1n(xixˉ)2s^2 = \frac{1}{n-1} \sum_{i=1}^{n} (x_i - \bar{x})^2

where xˉ\bar{x} is the sample mean.

The sample variance is an unbiased estimator of the population variance.

This will give us an overall sense of the variation in the data, as the name suggests. NB: Remember that, the unit of the variance is squared, so it’s not directly interpretable.

In R, we can calculate the sample variance using the var() function.

Sample Standard Deviation

The sample standard deviation is the square root of the sample variance,

s=s2=1n1i=1n(xixˉ)2s = \sqrt{s^2} = \sqrt{\frac{1}{n-1} \sum_{i=1}^{n} (x_i - \bar{x})^2}

Unlike the variance, the standard deviation is in the same unit as the data, so it’s more interpretable.

In R, we can calculate the sample standard deviation using the sd() function.

If we’re drawing from a population, the population standard deviation is defined as,

Var(X)=E[(XE(X))2]Var(X) = \mathbb{E}[(X - \mathbb{E}(X))^2]

Sample Quantile

For 0p10 \leq p \leq 1, the pp-th quantile for the sample is the data at position pn\approx pn in the sorted data.

Wehn this is not an integer, a weighted average is used.

This value essentially splits the data so that (roughly) 100p%100p\% is smaller and 100(1p)%100(1-p)\% is larger. The median is the 0.50.5 quantile.

The pp-th quantile is also called the 100p100p-th percentile.

For a population, the pp-th quantile for a distribution is defined as F1(p)F^{-1}(p), if FF is strictly increasing so that the inverse exists. Otherwise the definition is a little bit more complicated.

In R, we can calculate the quantiles using the quantile() function.

quantile(x, 0.25) # 25th percentile
quantile(x, (0.25, 0.75)) # 25th and 75th percentiles
Quantiles VS. Proportions

For a data vector xx we can ask two related but inverse questions.

What proportion of the data is less than or equal to a specified value? Or for a specified proporation, what value has this proportion of the data less than or equal?

The latter question is answered by the quantile function.

The inter-quantile range (IQR)

We are interested in the range of the middle 50% of the data, this would be the distance between the 75 percentile and the 25 percentile. Thus, the IQR is a single number.

In R we can calculate the IQR using the IQR() function.

Detecting Outliers

An outlier is an observation that is significantly different from other observations in the data set.

A rule of thumb is:

An observation is an outlier if it is more than 1.5×IQR1.5 \times IQR from the closest quartile.

Also a good rule to remember is:

An outlier is said to be extreme if it is more than 3×IQR3 \times IQR from the closest quartile.

Correlation

Definition 3 (Pearson Correlation Coefficient)

The pearson correlation coefficient rr of two data vectors xx and yy is defined as,

r=cor(x,y)=i=1n(xixˉ)(yiyˉ)i=1n(xixˉ)2i=1n(yiyˉ)2r = cor(x, y) = \frac{\sum_{i=1}^{n} (x_i - \bar{x})(y_i - \bar{y})}{\sqrt{\sum_{i=1}^{n} (x_i - \bar{x})^2 \sum_{i=1}^{n} (y_i - \bar{y})^2}}

The value of rr is between -1 and 1, where 1 indicates a perfect positive linear relationship, -1 indicates a perfect negative linear relationship, and 0 indicates no linear relationship.

Definition 4 (Population Correlation Coefficient)

For a population, when (xi,yi)iidFXY(x_i, y_i) \overset{iid}{\sim} F_{XY}, the population correlation coefficient is defined as,

r=E[(XE(X))(YE(Y))]Var(X)Var(Y)r = \frac{\mathbb{E}[(X - \mathbb{E}(X))(Y - \mathbb{E}(Y))]}{\sqrt{Var(X)Var(Y)}}

In R, we can calculate the correlation coefficient using the cor() function. NB: This is the pearson correlation coefficient, there are other types of correlation coefficients.

Properties of the Correlation Coefficient
  • The vale of rr does not depend on the unit of measurement for each variable.
  • The value of rr does not depend on which of the two variables are labeled xx, i.e. it is symmetric.
  • The value of rr is between -1 and 1.
  • The correlation coefficient is:
    • -1 only when all the points lie on a line with a negative slope.
    • 1 only when all the points lie on a line with a positive slope.
  • The value of rr is a measure of the extent to which xx and yy are linearly related.

Modes and Skew

A mode of a distribution is a peak, or a local maximum, in its density.

It is a visual effect and has no rigorous mathematical definition.

A data set can be characterized by its number of modes. A unimodal distribution has a single mode (e.g Normal distribution), a bimodal distribution has two modes, and so on.

The tail of a distribution are the very large and very small values of the distribution (NB: This is not the best definition, but it will do for now).

A distribution is called long-tailed (or heavy-tailed) if the data set contains values far from the main body of the distribution.

A distribution is skewed if one tail is significantly heavier or longer than the other. Good to remember is that the antonym (opposite) of skew is symmetry.

A distribution with longer left tail is called left-skewed or negatively skewed.

Some good rules of thumb:

  1. When a distribution is skewed positively (to the right) the mean is larger than the median.

  2. When a distribution is skewed negatively (to the left) the mean is smaller than the median.

  3. When a distribution is symmetric, the mean and median are equal.

Distributions

Let’s review some basic distributions.

Gamma Distribution

Definition 5 (Gamma Distribution)

A random variable XX is said to be gamma distributed with parameters (α,β)(\alpha, \beta), denoted as XGamma(α,β)X \sim Gamma(\alpha, \beta), if its PDF is given by,

f(x)={βαeβxxα1Γ(α)x00x<0f(x) = \begin{cases} \frac{\beta^{\alpha} e^{-\beta x} x^{\alpha - 1}}{\Gamma(\alpha)} & x \geq 0 \newline 0 & x < 0 \end{cases}

The gamma function Γ\Gamma is given by,

Γ(α)=0tα1etdt.\Gamma(\alpha) = \int_{0}^{\infty} t^{\alpha - 1} e^{-t} dt.

One important property of Γ\Gamma is that Γ(α)=(α1)Γ(α1)\Gamma(\alpha) = (\alpha - 1) \Gamma(\alpha - 1).

For integer values of α=n\alpha = n, Γ(n)=(n1)!\Gamma(n) = (n-1)!.

When α=1\alpha = 1, the density becomes,

f(x)=βeβxx0f(x) = \beta e^{-\beta x} \quad x \geq 0

we get the exponential distribution. Thus, Gamma(1,β)=Exp(β)Gamma(1, \beta) = Exp(\beta).

With this, if α=n\alpha = n is an integer, XX can be written as sum of nn i.i.d Exp(β)Exp(\beta) random variables.

α\alpha is called the shape parameter and β\beta is called the rate parameter.

The mean and variance of a gamma distribution are given by,

E(X)=αβVar(X)=αβ2\mathbb{E}(X) = \frac{\alpha}{\beta} \newline Var(X) = \frac{\alpha}{\beta^2}

In R, we can generate random variables from a gamma distribution using the rgamma() function.

Beta Distribution

Definition 6 (Beta Distribution)

A random variable XX is said to have a beta distribution if its density is given by,

f(x)={1B(α,β)xα1(1x)β10<x<10otherwisef(x) = \begin{cases} \frac{1}{B(\alpha, \beta)} x^{\alpha - 1} (1 - x)^{\beta - 1} & 0 < x < 1 \newline 0 & \text{otherwise} \end{cases}

Where B(α,β)B(\alpha, \beta) is the beta function,

B(α,β)=01tα1(1t)β1dtB(\alpha, \beta) = \int_{0}^{1} t^{\alpha - 1} (1 - t)^{\beta - 1} dt

so that f(x)f(x) integrates to 1.

The mean and variance of a beta distribution are given by,

E(X)=αα+βVar(X)=αβ(α+β)2(α+β+1)\mathbb{E}(X) = \frac{\alpha}{\alpha + \beta} \newline Var(X) = \frac{\alpha \beta}{(\alpha + \beta)^2 (\alpha + \beta + 1)}

In R, we can generate random variables from a beta distribution using the rbeta() function.

Chi-Squared Distribution

The X2(n)\Chi^2(n) or Xn2\Chi^2_n distribution is a special case of the gamma distribution, with α=n/2\alpha = n/2 and β=1/2\beta = 1/2.

The integer nn is the parameter of the distribution and sometimes called the degree of freedom of the distribution. If XX2(n)X \sim \Chi^2(n), then E(X)=n\mathbb{E}(X) = n and Var(X)=2nVar(X) = 2n.

If ZiiidN(0,1)Z_i \overset{iid}{\sim} N(0, 1), then Z12+Z22++Zn2X2(n)Z_1^2 + Z_2^2 + \ldots + Z_n^2 \sim \Chi^2(n).

Property:

If XX and YY are independent with Xn2\Chi^2_n and Xm2\Chi^2_m distributions, then X+YXn+m2X + Y \sim \Chi^2_{n+m}.

In R, we can generate random variables from a chi-squared distribution using the rchisq() function.

Student’s t-Distribution

We use this t-distribution for CI (confidence intervals) and hypothesis testing. Which we will cover in the next part.

Densitity,

f(x)=Γ(n+12)Γ(n2)nπ(1+x2n)n+12f(x) = \frac{\Gamma(\frac{n+1}{2})}{\Gamma(\frac{n}{2}) \sqrt{n\pi}} \left(1 + \frac{x^2}{n}\right)^{-\frac{n+1}{2}}

Mean and variance,

E(X)=0Var(X)=nn2n>2\mathbb{E}(X) = 0 \newline Var(X) = \frac{n}{n-2} \quad n > 2

If ZN(0,1)Z \sim N(0, 1), XXn2X \sim \Chi^2_n, and XX and ZZ are independant, then nZ/X/ntn\sqrt{n}Z / \sqrt{X/n} \sim t_n.

nn is the parameter of the t-distribution and is called the degree of freedom.

A special case is the t(1)t(1), it is also called cauchy distribution.

Density for Cauchy,

f(x)=1π(1+x2)f(x) = \frac{1}{\pi(1 + x^2)}

In R, we can generate random variables from a t-distribution using the rt() function. For cauchy, we can use the rcauchy() function.