class MathHelperFunctions(object):
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 |
Huber-like smooth penality which starts at lower. |
Static Method | apos |
clips argument (scalar or array) from below at lower |
Static Method | cauchy |
Undocumented |
Static Method | chi |
approximation of the expectation of norm(randn(dimension)). |
Static Method | equals |
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 |
Undocumented |
Static Method | vequals |
Undocumented |
Class Variable | _chi |
Undocumented |
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.
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.
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'
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]