Skip to content

Commit

Permalink
small optimisations
Browse files Browse the repository at this point in the history
  • Loading branch information
glevv committed Jan 4, 2024
1 parent 4c7b7ca commit 0ef7cbf
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/obscure_stats/kurtosis/kurtosis.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def moors_kurt(x: np.ndarray) -> float:
The meaning of kurtosis: Darlington reexamined.
The American Statistician, 40 (4): 283-284,
"""
return np.nanvar(np.square(stats.zscore(x, nan_policy="omit"))) + 1
return np.nanvar(stats.zscore(x, nan_policy="omit") ** 2) + 1


def moors_octile_kurt(x: np.ndarray) -> float:
Expand Down
9 changes: 5 additions & 4 deletions src/obscure_stats/variation/variation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Module for measures of categorical variations."""

import math
from collections import Counter

import numpy as np
Expand Down Expand Up @@ -103,7 +104,7 @@ def gibbs_m1(x: np.ndarray) -> float:
Special case of Tsallis entropy (alpha = 2).
"""
freq = np.asarray(list(Counter(x).values())) / len(x)
return 1 - np.sum(np.square(freq))
return 1 - np.sum(freq**2)


def gibbs_m2(x: np.ndarray) -> float:
Expand Down Expand Up @@ -133,7 +134,7 @@ def gibbs_m2(x: np.ndarray) -> float:
"""
freq = np.asarray(list(Counter(x).values())) / len(x)
k = len(freq)
return (k / (k - 1)) * (1 - np.sum(np.square(freq)))
return (k / (k - 1)) * (1 - np.sum(freq**2))


def b_index(x: np.ndarray) -> float:
Expand Down Expand Up @@ -162,7 +163,7 @@ def b_index(x: np.ndarray) -> float:
"""
n = len(x)
freq = np.asarray(list(Counter(x).values())) / n
return 1 - np.sqrt(1 - np.square(stats.gmean(freq * len(freq) / n)))
return 1 - (1 - (stats.gmean(freq * len(freq) / n)) ** 2) ** 0.5


def avdev(x: np.ndarray) -> float:
Expand Down Expand Up @@ -231,7 +232,7 @@ def renyi_entropy(x: np.ndarray, alpha: float = 2) -> float:
if alpha == 1:
# return Shannon entropy to avoid division by 0
return -np.sum(freq * np.log2(freq))
return 1 / (1 - alpha) * np.log2(np.sum(freq**alpha))
return 1 / (1 - alpha) * math.log2(np.sum(freq**alpha))


def negative_extropy(x: np.ndarray) -> float:
Expand Down

0 comments on commit 0ef7cbf

Please sign in to comment.