How to Calculate Statistical Moments with NumPy (and SciPy)

How to Calculate Statistical Moments with NumPy
Image by Editor | ChatGPT

Statistical moments are essential for understanding the shape and distribution of a dataset. These moments help in analyzing the central tendency, dispersion, skewness, and kurtosis of data. NumPy, along with its scientific computing companion SciPy, provides several functions to calculate these moments.

In this article, we will explore the four central moments: mean, variance, skewness, and kurtosis.

Understanding Statistical Moments

Statistical moments are quantitative measures used to describe the shape of a data distribution. The most commonly used moments are:

  1. Mean (First Moment): Represents the central value of the data.
  2. Variance (Second Moment): Measures the dispersion of data points around the mean.
  3. Skewness (Third Moment): Describes the asymmetry of the distribution.
  4. Kurtosis (Fourth Moment): Indicates the “tailedness” or heaviness of the tails in the distribution.

Now, let’s explore how to calculate each moment.

First Moment: Mean

The mean, also known as the arithmetic average, represents the central value of a dataset. It’s calculated by summing all the values in the dataset and dividing by the total number of values.

In NumPy, we can compute the mean using numpy.mean():

import numpy as np

# Sample data
data = np.array([10, 15, 20, 25, 30])

# Calculate mean
mean_value = np.mean(data)
print("Mean:", mean_value)

Output:

Mean: 20.0

Second Moment: Variance

Variance is a statistical measure that quantifies the dispersion or spread of a dataset. It indicates how far individual data points deviate from the mean. A high variance means the data points are widely spread out, whereas a low variance indicates that the data points are close to the mean. 

In NumPy, we use numpy.var() to compute the variance:

# Calculate variance
variance_value = np.var(data)
print("Variance:", variance_value)

Output:

Variance: 50.0

Third Moment: Skewness

Skewness is a statistical measure that describes the asymmetry of a probability distribution. While NumPy provides the necessary building blocks to calculate it, the SciPy library offers a direct function for convenience.

Skewness can be classified as:

  • Positive Skewness: The distribution is right-skewed, meaning it has a longer tail on the right side.
  • Negative Skewness: The distribution is left-skewed, meaning the left tail is longer.
  • Zero Skewness: The distribution is symmetric, where the left and right sides are mirror images.
from scipy.stats import skew

# Calculate skewness
skewness_value = skew(data)
print("Skewness:", skewness_value)

Output:

Skewness: 0.0

Our dataset [10, 15, 20, 25, 30] is perfectly symmetrical around its mean of 20.0, so the resulting skewness is 0.0, indicating a symmetrical distribution.

Fourth Moment: Kurtosis

Kurtosis is a statistical measure that describes the shape of a distribution’s tails in relation to a normal distribution. It quantifies how heavy or light the tails are.

It’s important to distinguish between standard kurtosis and “excess kurtosis.” In standard kurtosis, a normal distribution has a value of 3. However, the scipy.stats.kurtosis function calculates excess kurtosis, which is defined as kurtosis – 3. This means a normal distribution has an excess kurtosis of 0.

Excess kurtosis can be classified as:

  • Leptokurtic (Kurtosis > 0): The distribution has heavy tails, meaning it contains more extreme values or outliers than a normal distribution.
  • Mesokurtic (Kurtosis = 0): The distribution is normal, with moderate tails.
  • Platykurtic (Kurtosis < 0): The distribution has light tails, meaning it has fewer extreme values than a normal distribution.
from scipy.stats import kurtosis

# Calculate excess kurtosis
kurtosis_value = kurtosis(data)
print("Kurtosis:", kurtosis_value)

Output:

Kurtosis: -1.3

A kurtosis value less than 0 indicates a platykurtic distribution, which means the distribution is flatter and has lighter tails than a normal distribution. Since our data is evenly spaced with no extreme values, the kurtosis is negative.

Conclusion

Understanding statistical moments helps you analyze data distributions more effectively. NumPy and SciPy provide simple functions to compute these moments. The mean identifies the central value, while variance shows how spread out the data is. Skewness reveals the asymmetry of the distribution, and kurtosis highlights the presence of outliers and the “tailedness” of the data.

Leave a Reply

Your email address will not be published. Required fields are marked *