Coverage for src/sensai/util/math.py: 0%
25 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-13 22:17 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-13 22:17 +0000
1import math
2from typing import List
4from .string import object_repr, ToStringMixin
7class NormalDistribution(ToStringMixin):
8 def __init__(self, mean=0, std=1, unit_max=False):
9 """
10 :param mean: the mean
11 :param std: the standard deviation
12 :param unit_max: if True, scales the distribution's pdf such that its maximum value becomes 1
13 """
14 import scipy.stats
15 self.unitMax = unit_max
16 self.mean = mean
17 self.std = std
18 self.norm = scipy.stats.norm(loc=mean, scale=std)
20 def _tostring_includes(self) -> List[str]:
21 return ["mean", "std", "unitMax"]
23 def pdf(self, x):
24 v = self.norm.pdf(x)
25 if self.unitMax:
26 v /= self.norm.pdf(self.mean)
27 return v
29 def __str__(self):
30 return object_repr(self, ["mean", "std", "unitMax"])
33def sigmoid(x: float, min_value=0, max_value=1, k=1, x0=0):
34 return min_value + (max_value - min_value) / (1 + math.exp(-k * (x - x0)))
37def reverse_sigmoid(x: float, max_value=1, min_value=0, k=1, x0=0):
38 return max_value - sigmoid(x, min_value=0, max_value=max_value - min_value, k=k, x0=x0)
41def reduce_float_precision_decimals(f: float, decimals: int) -> float:
42 return float(format(f, '.%df' % decimals))