Computing measures of central tendency using statsmodels and DescrStatsW in Python

The class DescrStatsW has the attribute mean which is initialised during the creation and initialisation of an DescrStatsW object. The Python example uses a 2-dimensional ndarray as sample data and computes the mean using DescrStatsW

Statistical Analysis Updated 2 Nov 2019 3 min read

Measures of Central Tendency

Measures of central tendency in statistics are representational metrics to describe a particular dataset or distribution or sample or population. Measures of central tendency compute the prime or central or middle value of a given distribution. There are three major measures of central tendency - Mean, Median and Mode.

Mean

Mean or a simple average or expected value is calculated as the sum of all elements in a data distribution (which can be a sample or population) divided by the total number of elements in the sample or population. Mean of a set of elements is denoted by x̅. Let us consider a random sample of data a1, a2, a3, a4,....anthen the mean of the sample is given by,

x̅ = a1+a2+ a3+ a4,....+ an/4

Median

Median is defined as the centre-most element in the dataset. Median is defined distinctly for even number of elements and odd number of elements.

  • For a dataset or sample with odd number of elements, median is computed as follows,
    • Sort the data in an ascending fashion
    • Median is the middle-most value in the dataset
  • For a dataset with even number of element, median is computed as follows,
    • Sort the data in an ascending fashion
    • Median is calculated as the mean of the two middle elements in the dataset

Mode

Mode is defined as the element that most frequently appears in a data distribution. A data distribution can have a single mode i.e., Uni-modal distribution or more than one mode i.e., Multi-modal distribution.

DescrStatsW()

statsmodels computes the measures of the central tendency of a distribution using DescrStatsW class.

Example

Let us compute the mean of a sample from an ndarray of dimension 5x5, without using any weights for the sample members. While the mean atrribute of DescrStatsW class supports computing the mean for a one dimensional or a two-dimensional array, the mean() method of the module statistics from the Python Standard Library supports computing mean for python sequences like list, tuple and others.

As median and mode more not yet part of the DescrStatsW the methods median() and mode() from the Python Standard Library can be used as long as the distributions are Python sequences like list and tuple.

python
# Example Python program using statsmodels to compute the mean of a distribution
import numpy as np
import statsmodels.api as statmod
# ndarray of dimension - 5x5
data = np.ndarray((5, 5));
# Fill the ndarray
for x in range(0, data.shape[0]):
    for y in range(0, data.shape[1]):
        data[x][y] = x * y * 2.5;
# Construct a DescrStatsW instance
descriptive = statmod.stats.DescrStatsW(data);
# Print the ndarray
print("Sample data:");
print(data);
# Print the mean of each column
print("Mean:");
print(descriptive.mean);

Output

Output
Sample data:

[[ 0. 0. 0. 0. 0. ]

[ 0. 2.5 5. 7.5 10. ]

[ 0. 5. 10. 15. 20. ]

[ 0. 7.5 15. 22.5 30. ]

[ 0. 10. 20. 30. 40. ]]

Mean:

[ 0. 5. 10. 15. 20.]