Skip to content

gini_index

cellseg_gsontools.diversity.gini_index(x)

Compute the gini coefficient of inequality for species.

Note

This is based on http://www.statsdirect.com/help/default.htm#nonparametric_methods/gini.htm.

Gini-index: $$ G = \frac{\sum_{i=1}^n (2i - n - 1)x_i} {n \sum_{i=1}^n x_i} $$

where \(x_i\) is the count of species \(i\) and \(n\) is the total count of species.

Parameters:

Name Type Description Default
x Sequence

The input value-vector. Shape (n, )

required

Raises:

Type Description
ValueError

If there are negative input values.

Returns:

Name Type Description
float float

The computed Gini coefficient.

Source code in cellseg_gsontools/diversity.py
def gini_index(x: Sequence) -> float:
    """Compute the gini coefficient of inequality for species.

    Note:
        This is based on
        http://www.statsdirect.com/help/default.htm#nonparametric_methods/gini.htm.

    **Gini-index:**
    $$
    G = \\frac{\\sum_{i=1}^n (2i - n - 1)x_i} {n \\sum_{i=1}^n x_i}
    $$

    where $x_i$ is the count of species $i$ and $n$ is the total count of species.

    Parameters:
        x (Sequence):
            The input value-vector. Shape (n, )

    Raises:
        ValueError:
            If there are negative input values.

    Returns:
        float:
            The computed Gini coefficient.
    """
    if np.min(x) < 0:
        raise ValueError("Input values need to be positive for Gini coeff")

    n = len(x)
    s = np.sum(x)
    nx = n * s + SMALL

    rx = (2.0 * np.arange(1, n + 1) * x[np.argsort(x)]).sum()
    return (rx - nx - s) / nx