field-time-diversity / metrics.py
jpwahle's picture
Add remaining app files
0fba077
raw
history blame contribute delete
755 Bytes
# 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))