From 65e2c630a9ceddfb17c644a3941174149a04a3ff Mon Sep 17 00:00:00 2001 From: Divasco Date: Wed, 30 Oct 2024 19:57:27 -0300 Subject: [PATCH] Added entropy measures --- garpar/datasets/risso.py | 15 +++++++++++++++ garpar/utils/entropy.py | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/garpar/datasets/risso.py b/garpar/datasets/risso.py index 1d0842b..0f732ca 100644 --- a/garpar/datasets/risso.py +++ b/garpar/datasets/risso.py @@ -143,6 +143,21 @@ def get_window_loss_probability(self, window_size, entropy): float Loss probability that corresponds to the nearest candidate entropy value to the target entropy. + + Example + -------- + If we run this function with window_size=3 and entropy=.99 + >>> # Example with a sliding window size of 3 and target entropy of 0.99 + >>> get_window_loss_probability(window_size=3, entropy=0.99) + We get the following data + Candidates + [1.18666621e-14 9.18295834e-01 9.18295834e-01 1.18666621e-14] + Entropy + 0.99 + Loss probabilities + [2.22044605e-16 3.33333333e-01 6.66666667e-01 1.00000000e+00] + Loss probability + 0.3333333333333333 """ h_candidates, loss_probabilities = self.candidate_entropy(window_size) idx = argnearest(h_candidates, entropy) diff --git a/garpar/utils/entropy.py b/garpar/utils/entropy.py index b642ebd..e206b72 100644 --- a/garpar/utils/entropy.py +++ b/garpar/utils/entropy.py @@ -9,7 +9,10 @@ import warnings +import numpy as np + from scipy import stats +from scipy.optimize import minimize def shannon(prices, window_size=None, **kwargs): @@ -59,3 +62,36 @@ def risso(prices, window_size, **kwargs): """ if not window_size or window_size < 0: raise ValueError("'window_size' must be >= 0") + + + +def HOne (weights): + weights = np.asarray(weights) + return 1 - np.sum(weights**2) + +def HInf (weights): + weights = np.asarray(weights) + return 1 - np.max(weights) + +def yagerOne (weights): + """ + Computes Yager's entropy for a fuzzy set. + + Parameters: + - weights: array-like, list of membership degrees (values in [0, 1]) + - p: float, parameter to control sensitivity (default = 1) + + Returns: + - entropy: float, Yager's entropy + """ + + n = len(weights) + weights = np.asarray(weights) + + return (sum((weights - 1/n) ** p)) ** (1/p) + +def yagerInf (weigths): + weigths = np.asarray(weigths) + n = len(weigths) + + return np.max(weigths) - 1/n