Python: Weighted coefficient of variation
Asked Answered
R

1

1

How can I calculate the weighted coefficient of variation (CV) over a NumPy array in Python? It's okay to use any popular third-party Python package for this purpose.

I can calculate the CV using scipy.stats.variation, but it's not weighted.

import numpy as np
from scipy.stats import variation

arr = np.arange(-5, 5)
weights = np.arange(9, -1, -1)  # Same size as arr
cv = abs(variation(arr))  # Isn't weighted
Rattlepate answered 12/12, 2018 at 17:41 Comment(0)
R
2

This can be done using the statsmodels.stats.weightstats.DescrStatsW class in the statsmodels package for calculating weighted statistics.

from statsmodels.stats.weightstats import DescrStatsW

arr = np.arange(-5, 5)
weights = np.arange(9, -1, -1)  # Same size as arr

dsw = DescrStatsW(arr, weights)
cv = dsw.std / abs(dsw.mean)  # weighted std / abs of weighted mean

print(cv)
1.6583123951777001

For a related statistic, i.e. the weighted gini, see this answer.


Credit: This answer is motivated by one on calculating the weighted standard deviation.

Rattlepate answered 12/12, 2018 at 17:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.