Dispersion Measures with R

Last Update: January 14, 2021

Descriptive statistics consists of quantitative or qualitative data population or sample frequency distribution, central tendency measures, dispersion measures, association measures and frequency distribution shape.

This topic is part of Business Statistics with R course. Feel free to take a look at Course Curriculum.

This tutorial has an educational and informational purpose and doesn’t constitute any type of forecasting, business, trading or investment advice. All content, including code and data, is presented for personal educational use exclusively and with no guarantee of exactness of completeness. Past performance doesn’t guarantee future results. Please read full Disclaimer.

1. Dispersion measures

Dispersion measures consist of data population or sample variability. Main dispersion measures are standard deviation, variance and average deviation or mean absolute deviation.

  • Standard deviation consists of squared root of average data population or sample squared dispersion.

\sigma=\sqrt{\frac{1}{n}\sum_{t=1}^{n}(x_{t}-\mu)^2}\;\;or\;\;\bar{\sigma}=\sqrt{\frac{1}{\bar{n}-1}\sum_{t=1}^{\bar{n}}(x_{t}-\bar{\mu})^2}

Where \sigma = population standard deviation, x_{t} = data, \mu = population mean, n = population number of observations, \bar{\sigma} = corrected sample standard deviation, \bar{\mu} = sample mean, \bar{n} = sample number of observations.

  • Variance consists of average data population or sample squared dispersion.

\sigma^2=\frac{1}{n}\sum_{t=1}^{n}(x_{t}-\mu)^2\;\;or\;\;\bar{\sigma}^2=\frac{1}{\bar{n}-1}\sum_{t=1}^{\bar{n}}(x_{t}-\bar{\mu})^2

Where \sigma^2 = population variance, x_{t} = data, \mu = population mean, n = population number of observations, \bar{\sigma}^2 = corrected sample variance, \bar{\mu} = sample mean, \bar{n} = sample number of observations.

  • Mean absolute deviation or average deviation consists of average data population or sample absolute dispersion.

ad=\frac{1}{n}\sum_{t=1}^{n}\left | x_{t}-\mu \right |\;\;or\;\;\bar{ad}=\frac{1}{\bar{n}}\sum_{t=1}^{\bar{n}}\left | x_{t}-\bar{\mu} \right |

Where ad = population mean absolute deviation or average deviation, x_{t} = data, \mu = population mean, n = population number of observations, \bar{ad} = sample mean absolute deviation or average deviation, \bar{\mu} = sample mean, \bar{n} = sample number of observations.

2. R script code example.

2.1. Load R package [1].

library('quantmod')

2.2. Dispersion measures data reading and data daily arithmetic returns calculation.

  • Data: S&P 500® index replicating ETF (ticker symbol: SPY) daily adjusted close prices (2007-2015).
data <- read.csv('Dispersion-Measures-Data.txt',header=T)
spy <- xts(data[,2],order.by=as.Date(data[,1]))
rspy <- dailyReturn(spy)
colnames(rspy) <- 'rspy'

2.3. Dispersion measures data daily arithmetic returns corrected sample standard deviation calculation and output.

In:
sd(rspy)
Out:
[1] 0.01356701

2.4. Dispersion measures data daily arithmetic returns corrected sample variance calculation and output.

In:
var(rspy)
Out:
             rspy
rspy 0.0001840637

2.5. Dispersion measures data daily arithmetic returns sample mean absolute deviation or sample average deviation calculation and output.

In:
mean(abs(rspy-mean(rspy)))
Out:
[1] 0.008663449
3. References.

[1] Jeffrey A. Ryan and Joshua M. Ulrich. “quantmod: Quantitative Financial Modelling Framework”. R package version 0.4-17. 2020.