## Moments

Moments are quantitative measures related to the shape of the function’s graph. For probability distribution, the first moment is the expected value (mean), the second central moment is the variance, the third standardized moment is skewness and the fourth standardized moment is kurtosis. They are all ways of looking at data by combining the data into certain non-linear ways. Higher moments, those beyond the 4th moment, also follow this concept. For all \(k\), the \(k\)-th raw sample moment is defined as

\(\bar{X}_{n}^{k}=\frac{1}{n}\sum_{i=1}^{n}{x_{i}^{k}}\)

It can be seen that the first sample moment is the sample mean.

The \(k\)-th central sample moment is defined as

\(M_{n}^{k}=\frac{1}{n}\sum_{i=1}^{n}{(x_{i}-\bar{x}_{n}^{1})^{k}}\)

It can be seen that the first central moment \(M_{n}^{1}=0\) the second central moment is the biased sample variance.

For higher-order moments beyond the 4th moment, similar to variance, skewness and kurtosis, they are non-linear combinations of the data. The can be used for description or explanation of further shape parameters. The higher the moment, the harder it is to estimate as larger samples are required to obtain estimates of similar quality. This is due to the exces degrees of freedom consumed by the higher orders. The higher-order moments are also more difficult to understand. For example, the 5th-order moment can be understood as measuring “relative importance of tails versus center (shoulders and mode) in causing skew”. For a given skew, high 5th moment indicates heavy tail and little movement of mode, while low 5th moment indicates more change in shoulders.

**Code**

In NM Dev, the class Moments computes the sample moments for a data set.

` ````
```// create an array of doubles for our dataset
val values = doubleArrayOf(
1.050339964176429, 0.906121669295144,
0.116647826876888, 4.579895872370673,
1.714264543643022, 0.436467861756682,
0.860735191921604, 1.771233864044571,
0.623149028640023, 1.058291583279980
)
// create Moments object
val moments = Moments(6) // up to the 6th moment
// add data to the Moments object
val n = values.size
for (i in 0..n-1) {
moments.addData(values[i])
}
println("Sample size: " + moments.N())
println("1st central moment: " + moments.centralMoment(1) + " OK")
println("2nd central moment: " + moments.centralMoment(2) + " OK")
println("3rd central moment: " + moments.centralMoment(3) + " off, not enough data")
println("4th central moment: " + moments.centralMoment(4) + " way off, not enough data")
println("5th central moment: " + moments.centralMoment(5) + " meaningless, not enough data")
println("6th central moment: " + moments.centralMoment(6) + " meaningless, not enough data")

` ````
```Sample size: 10
1st central moment: 1.3117147406005016 OK
2nd central moment: 1.422300520892388 OK
3rd central moment: 3.217342528890056 off, not enough data
4th central moment: 11.708334307448762 way off, not enough data
5th central moment: 36.97401011137274 meaningless, not enough data
6th central moment: 122.20276981014142 meaningless, not enough data