Skip to content

Commit

Permalink
Added entropy measures
Browse files Browse the repository at this point in the history
  • Loading branch information
Divasco committed Oct 30, 2024
1 parent bfa8695 commit 65e2c63
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
15 changes: 15 additions & 0 deletions garpar/datasets/risso.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
36 changes: 36 additions & 0 deletions garpar/utils/entropy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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

0 comments on commit 65e2c63

Please sign in to comment.