Added some numerical stability to link functions with tests for link functions

This commit is contained in:
Alan Saul 2015-04-10 14:58:02 +01:00
parent 034d141d63
commit 5c9587404d
3 changed files with 197 additions and 27 deletions

View file

@ -6,15 +6,33 @@ from scipy.special import cbrt
from .config import *
_lim_val = np.finfo(np.float64).max
_lim_val_exp = np.log(_lim_val)
_lim_val_square = np.sqrt(_lim_val)
_lim_val_cube = cbrt(_lim_val)
#_lim_val_cube = cbrt(_lim_val)
_lim_val_cube = np.nextafter(_lim_val**(1/3.0), -np.inf)
_lim_val_quad = np.nextafter(_lim_val**(1/4.0), -np.inf)
_lim_val_three_times = np.nextafter(_lim_val/3.0, -np.inf)
def safe_exp(f):
clip_f = np.clip(f, -np.inf, _lim_val_exp)
return np.exp(clip_f)
def safe_square(f):
f = np.clip(f, -np.inf, _lim_val_square)
return f**2
def safe_cube(f):
f = np.clip(f, -np.inf, _lim_val_cube)
return f**3
def safe_quad(f):
f = np.clip(f, -np.inf, _lim_val_quad)
return f**4
def safe_three_times(f):
f = np.clip(f, -np.inf, _lim_val_three_times)
return 3*f
def chain_1(df_dg, dg_dx):
"""
Generic chaining function for first derivative