File size: 755 Bytes
0fba077
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# Copyright 2023 by Jan Philip Wahle, https://jpwahle.com/
# All rights reserved.

import numpy as np


def calculate_gini_simpson(dictionary):
    """
    Function to Calculate Gini Simpson's Diversity Index
    """
    total = sum(dictionary.values())
    sum_squares = sum((n / total) ** 2 for n in dictionary.values())
    return 1 - sum_squares


def calculate_gini(frequencies):
    """
    Function to Calculate Gini's Diversity Index
    """
    frequencies = np.array(frequencies)
    if len(frequencies) == 0 or np.mean(frequencies) == 0:
        return None
    total = sum(
        np.sum(np.abs(xi - frequencies[i:]))
        for i, xi in enumerate(frequencies[:-1], 1)
    )
    return total / (len(frequencies) ** 2 * np.mean(frequencies))