2018-01-07 03:05:14 +00:00
|
|
|
"""Measures of diversity."""
|
2018-01-09 03:12:31 +00:00
|
|
|
from math import exp
|
2018-01-07 03:05:14 +00:00
|
|
|
from math import log
|
|
|
|
|
|
|
|
from voting.util import normalize
|
|
|
|
|
|
|
|
|
|
|
|
def berger_parker(groups):
|
|
|
|
r"""Calculate the Berger-Parker index.
|
|
|
|
|
|
|
|
.. math::
|
|
|
|
|
|
|
|
max(p_i)
|
|
|
|
|
|
|
|
:param list group: a list of integers representing populations of groups
|
|
|
|
"""
|
|
|
|
groups = normalize(groups)
|
|
|
|
return max(groups)
|
|
|
|
|
|
|
|
|
2018-01-09 03:12:31 +00:00
|
|
|
def general(groups, q=1):
|
2018-01-07 03:05:14 +00:00
|
|
|
r"""Calculate the general diversity index.
|
|
|
|
|
|
|
|
.. math::
|
|
|
|
|
|
|
|
\left( \sum_{i=1}^n p_i^q \right) ^ {1/(1-q)}
|
|
|
|
|
|
|
|
:param list groups: a list of integers representing populations of groups
|
|
|
|
:param float q: weight value
|
|
|
|
"""
|
2018-01-09 03:12:31 +00:00
|
|
|
if q == 1:
|
|
|
|
return exp(shannon(groups))
|
2018-01-07 03:05:14 +00:00
|
|
|
groups = normalize(groups)
|
|
|
|
return sum([g ** q for g in groups]) ** (1.0 / (1 - q))
|
|
|
|
|
|
|
|
|
|
|
|
def gini_simpson(groups):
|
|
|
|
r"""Calculate the Gini-Simpson index.
|
|
|
|
|
|
|
|
.. math::
|
|
|
|
|
|
|
|
1 - \sum_{i=1}^n p_i^2
|
|
|
|
|
|
|
|
:param list group: a list of integers representing populations of groups
|
|
|
|
"""
|
|
|
|
return 1 - simpson(groups)
|
|
|
|
|
|
|
|
|
|
|
|
def golosov(groups):
|
|
|
|
r"""Calculate the effective number of parties using Golosov.
|
|
|
|
|
|
|
|
.. math::
|
|
|
|
|
|
|
|
\sum_{i=1}^n \frac{p_i}{p_i + p_1^2 - p_i^2}
|
|
|
|
|
|
|
|
where :math:`p_1` is the largest proportion.
|
|
|
|
|
|
|
|
:param list group: a list of integers representing populations of a group
|
|
|
|
"""
|
|
|
|
groups = normalize(groups)
|
|
|
|
p1 = max(groups)
|
|
|
|
return sum([g / (g + p1 ** 2 - g ** 2) for g in groups])
|
|
|
|
|
|
|
|
|
|
|
|
def inverse_simpson(groups):
|
|
|
|
r"""Calculate the Inverse-Simpson index.
|
|
|
|
|
|
|
|
.. math::
|
|
|
|
|
|
|
|
\frac{1}{\sum_{i=1}^n p_i^2}
|
|
|
|
|
|
|
|
:param list group: a list of integers representing populations of groups
|
|
|
|
"""
|
|
|
|
return 1.0 / simpson(groups)
|
|
|
|
|
|
|
|
|
|
|
|
def laakso_taagepera(groups):
|
|
|
|
r"""Calculate the effective number of parties using Laakso-Taagepera.
|
|
|
|
|
|
|
|
.. math::
|
|
|
|
|
|
|
|
\frac{1}{\sum_{i=1}^n p_i^2}
|
|
|
|
|
|
|
|
:param list group: a list of integers representing populations of groups
|
|
|
|
"""
|
|
|
|
groups = normalize(groups)
|
|
|
|
return 1.0 / sum([g ** 2 for g in groups])
|
|
|
|
|
|
|
|
|
2018-01-09 03:12:31 +00:00
|
|
|
def renyi(groups, q=0):
|
2018-01-07 03:05:14 +00:00
|
|
|
r"""Calculate the Renyi entropy.
|
|
|
|
|
|
|
|
.. math::
|
|
|
|
|
|
|
|
\frac{1}{1-q} \ln \left( \sum_{i=1}^n p_i ^ q \right)
|
|
|
|
|
|
|
|
:param list groups: a list of integers representing populations of groups
|
|
|
|
:param float q: weight value
|
|
|
|
|
|
|
|
"""
|
2018-01-09 03:12:31 +00:00
|
|
|
if q == 1:
|
|
|
|
return shannon(groups)
|
2018-01-07 03:05:14 +00:00
|
|
|
groups = normalize(groups)
|
|
|
|
return 1.0 / (1 - q) * log(sum([g ** q for g in groups]))
|
|
|
|
|
|
|
|
|
|
|
|
def shannon(groups):
|
|
|
|
r"""Calculate the Shannon index.
|
|
|
|
|
|
|
|
.. math::
|
|
|
|
|
|
|
|
\sum_{i=1}^n p_i \ln (p_i)
|
|
|
|
|
|
|
|
:param list groups: a list of integers representing populations of groups
|
|
|
|
"""
|
|
|
|
groups = normalize(groups)
|
|
|
|
return sum([g * log(g) for g in groups])
|
|
|
|
|
|
|
|
|
|
|
|
def simpson(groups):
|
|
|
|
r"""Calculate the Simpson index.
|
|
|
|
|
|
|
|
.. math::
|
|
|
|
|
|
|
|
\sum_{i=1}^n p_i^2
|
|
|
|
|
|
|
|
:param list groups: a list of integers representing populations of groups
|
|
|
|
"""
|
|
|
|
groups = normalize(groups)
|
|
|
|
return sum([g ** 2 for g in groups])
|