How can I calculate Weighted Standard Deviation in Python?

Weighted Standard Deviation can be calculated in Python using the statistics package. The package provides a weighted_stdev() function which takes the data points and their corresponding weights as input and returns the Weighted Standard Deviation. This function can be used to efficiently calculate the Weighted Standard Deviation of a given dataset.


The weighted standard deviation is a useful way to measure of values in a dataset when some values in the dataset have higher weights than others.

The formula to calculate a weighted standard deviation is:

where:

  • N: The total number of
  • M: The number of non-zero weights
  • wi: A vector of weights
  • xi: A vector of data values
  • x: The weighted mean

The easiest way to calculate a weighted standard deviation in Python is to use the function from the statsmodels package:

DescrStatsW(values, weights=weights, ddof=1).std

The following example shows how to use this function in practice.

Example: Weighted Standard Deviation in Python

Suppose we have the following array of data values and corresponding weights:

#define data values 
values = [14, 19, 22, 25, 29, 31, 31, 38, 40, 41]

#define weights
weights = [1, 1, 1.5, 2, 2, 1.5, 1, 2, 3, 2]

The following code shows how to calculate the weighted standard deviation for this array of data values:

from statsmodels.stats.weightstats import DescrStatsW

#calculate weighted standard deviation
DescrStatsW(values, weights=weights, ddof=1).std

8.570050878426773

The weighted standard deviation turns out to be 8.57.

Note that we can also use var to quickly calculate the weighted variance as well:

from statsmodels.stats.weightstats import DescrStatsW

#calculate weighted variance
DescrStatsW(values, weights=weights, ddof=1).var

73.44577205882352

The weighted variance turns out to be 73.446.

The following tutorials explain how to calculate weighted standard deviation in other statistical software:

x