class documentation

class MathHelperFunctions(object):

View In Hierarchy

static convenience math helper functions, if the function name is preceded with an "a", a numpy array is returned

TODO: there is probably no good reason why this should be a class and not a module.

Static Method aclamp Undocumented
Static Method amax Undocumented
Static Method amin Undocumented
Static Method aminmax Undocumented
Static Method apenalty_quadlin Huber-like smooth penality which starts at lower.
Static Method apos clips argument (scalar or array) from below at lower
Static Method cauchy_with_variance_one Undocumented
Static Method chiN approximation of the expectation of norm(randn(dimension)).
Static Method equals_approximately Undocumented
Static Method expms matrix exponential for a symmetric matrix
Static Method huber2 y-shifted Huber function, return huber(x, delta) + delta/2 + eps.
Static Method interdecilerange return 10% to 90% range width
Static Method iqr interquartile range
Static Method logit10 map [lower, upper] -> R such that
Static Method max Undocumented
Static Method min Undocumented
Static Method minmax Undocumented
Static Method norm Undocumented
Static Method prctile prctile(data, 50) returns the median, but p_vals can also be a sequence.
Static Method sround return stochastic round: int(nb) + (rand()<remainder(nb))
Static Method standard_finite_cauchy Undocumented
Static Method vequals_approximately Undocumented
Class Variable _chiN_dict Undocumented
@staticmethod
def aclamp(x, upper):

Undocumented

@staticmethod
def amax(vec, vec_or_scalar):

Undocumented

@staticmethod
def amin(vec_or_scalar, vec_or_scalar2):

Undocumented

@staticmethod
def aminmax(val, min_val, max_val):

Undocumented

@staticmethod
def apenalty_quadlin(x, lower=0, upper=None):

Huber-like smooth penality which starts at lower.

The penalty is zero below lower and affine linear above upper.

Return:

0, if x <= lower
quadratic in x, if lower <= x <= upper
affine linear in x with slope upper - lower, if x >= upper

upper defaults to lower + 1.

@staticmethod
def apos(x, lower=0):

clips argument (scalar or array) from below at lower

@staticmethod
def cauchy_with_variance_one():

Undocumented

@staticmethod
def chiN(dimension):

approximation of the expectation of norm(randn(dimension)).

The exact value can be computed by:

from scipy.special import gamma
return 2**0.5 * gamma((self.dimension+1) / 2) / gamma(self.dimension / 2)

The approximation obeys chin < chin_hat < (1 + 5e-5) * chin.

@staticmethod
def equals_approximately(a, b, eps=1e-12):

Undocumented

@staticmethod
def expms(A, eig=np.linalg.eigh):

matrix exponential for a symmetric matrix

@staticmethod
def huber2(x, delta, eps=0):

y-shifted Huber function, return huber(x, delta) + delta/2 + eps.

huber2 maps x to abs(x) + eps if abs(x) >= delta and otherwise to a quadratic law of x mapping 0 to delta/2 + eps which makes the first derivative of huber2 continuous.

Details: x may be scalar or a numpy array. Setting eps = -delta/2 recovers the Huber loss.

>>> import numpy as np
>>> import cma
>>> hus = cma.utilities.math.Mh.huber2(np.asarray([
...     -1, -0.8, -0.6, -0.4, -0.2, 0, 0.2, 0.4, 0.6, 0.8, 1, 1.2, 1.4, 1.6, 1.8, 2]), 0.5)
>>> ' '.join(['{0:.2}'.format(n) for n in hus])
'1.0 0.8 0.6 0.41 0.29 0.25 0.29 0.41 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0'
@staticmethod
def interdecilerange(data, percentile_function=np.percentile):

return 10% to 90% range width

@staticmethod
def iqr(data, percentile_function=np.percentile):

interquartile range

@staticmethod
def logit10(x, lower=0, upper=1):

map [lower, upper] -> R such that

upper - 10^-x  ->   x, and
lower + 10^-x  ->  -x

for large enough x. By default, simplifies close to log10(x / (1 - x)).

>>> from cma.utilities.math import Mh
>>> l, u = -1, 2
>>> print(Mh.logit10([l+0.01, 0.5, u-0.01], l, u))
[-1.9949189  0.         1.9949189]
@staticmethod
def max(vec, vec_or_scalar):

Undocumented

@staticmethod
def min(a, b):

Undocumented

@staticmethod
def minmax(val, min_val, max_val):

Undocumented

@staticmethod
def norm(vec, expo=2):

Undocumented

@staticmethod
def prctile(data, p_vals=[0, 25, 50, 75, 100], sorted_=False):

prctile(data, 50) returns the median, but p_vals can also be a sequence.

Provides for small samples or extremes IMHO better values than matplotlib.mlab.prctile or np.percentile, however also slower.

@staticmethod
def sround(nb):

return stochastic round: int(nb) + (rand()<remainder(nb))

@staticmethod
def standard_finite_cauchy(size=1):

Undocumented

@staticmethod
def vequals_approximately(a, b, eps=1e-12):

Undocumented

_chiN_dict: dict[int, float] =

Undocumented