def sensitivity_numerical(omega, direction, model): ''' Returns the derivative of 1) optimal value of SDP 2) derivative primal and dual solution: d/dx([vec(M); vec(Y_0); ...; vec(Y_k)]) at omega when perturbing the second moment matrix across the given direction ''' fmin = np.min(model.predict_f(model.X.value)[0]) def solution(om): ''' Return [vec(M); vec(Y_0); ...; vec(Y_k)] at omega=om ''' M, Y = sdp(om, fmin)[1:3] solution = M.flatten() for i in range(len(Y)): y = Y[i][0] / Y[i][0, 0]**.5 solution = np.concatenate((solution, y)) return solution d_opt_val = nd.Derivative(lambda x: sdp(omega + x * direction, fmin)[0])(0) d_solution = nd.Derivative(lambda x: solution(omega + x * direction))(0) return d_opt_val, d_solution
def get_Hessian(self, One_Dimension=True): if One_Dimension: f = nd.Derivative(partial(self.hmmtract.objective_1D, False), n=1) ff = nd.Derivative(partial(self.hmmtract.objective_1D, False), n=2) grad = -f(self.hmmtract.x[1:]) hess = -ff(self.hmmtract.x[1:]) else: f = nd.Derivative(self._loglikelihood, n=1) ff = nd.Hessdiag(self._loglikelihood) grad = -f(self.x) hess = -ff(self.x) return grad, hess
def test_derivative_exp(self): # derivative of exp(x), at x == 0 dexp = nd.Derivative(np.exp) self.assertAlmostEqual(dexp(0), np.exp(0)) dexp.n = 2 t = dexp(0) self.assertAlmostEqual(t, np.exp(0))
def test_derivative_poly1d(self): # Specify the step size (default stepsize = 0.1) p0 = np.poly1d(range(1, 6)) fd = nd.Derivative(p0, n=4, romberg_terms=0) #, step_max=3, step_num=10) p4 = p0.deriv(4) self.assertAlmostEqual(fd(1), p4(1), places=5)
def test_identity_derivative(self): l = Identity() dl = nd.Derivative(l) for x in np.linspace(-100, 100, 5): assert_array_almost_equal(l.derivatives(x), dl(x))
def test_hyperbolic_tangent_derivative(self): l = HyperbolicTangent() dl = nd.Derivative(l) for x in np.linspace(-100, 100, 5): assert_array_almost_equal(l.derivatives(x), dl(x))
def get_curvature(D, Hr, k, n): '''Calculate d^2 E / d k^2 along kx and ky directions at band n. Assumes there are no band crossings in the region sampled, so that the single index n can be used for all sampled ks. ''' curvature = [] for d in range(2): def Er_d(kd): kr = [] for dp in range(3): if dp == d: kr.append(kd) else: kr.append(k[dp]) H_kr = Hk_Cart(kr, Hr, D.T) w, U = np.linalg.eigh(H_kr) Er = w[n] return Er fd = numdifftools.Derivative(Er_d, n=2) curvature_d = fd(k[d]) curvature.append(curvature_d) return curvature
def test_high_order_derivative_cos(self): # Higher order derivatives (third derivative) # Truth: 1 d3cos = nd.Derivative(np.cos, n=3) y = d3cos(np.pi / 2.0) small = np.abs(y - 1.0) < d3cos.error_estimate self.assertTrue(small)
def partial_derivative(f,input): nbinput=input.shape[0] ret = np.empty(nbinput) for i in range(nbinput): fg = lambda x:partial_function(f,input,i,x) ret[i] = nd.Derivative(fg)(input[i]) return ret
def test_policy_gradient(env): from util.util import softmax_policy_checkfunc policy = FL.RandomDiscreteActionChooser(env.nA) rdata = FL.rollout(env, policy, 100) print rdata s_n = rdata[ 'observations'] # Vector of states (same as observations since MDP is fully-observed) a_n = rdata['actions'] # Vector of actions (each is an int in {0,1,2,3}) n = a_n.shape[0] # Length of trajectory q_n = np.random.randn( n) # Returns (random for the sake of gradient checking) f_sa = np.random.randn( env.nS, env.nA) # Policy parameter vector. explained shortly. # Compute numerical gradients and compare to the analytical computation from # todo.softmax_policy_gradient in order to verify your implementation stepdir = np.random.randn(*f_sa.shape) auxfunc = lambda x: softmax_policy_checkfunc(f_sa + stepdir * x, s_n, a_n, q_n) numgrad = ndt.Derivative(auxfunc)(0) g = todo.softmax_policy_gradient(f_sa, s_n, a_n, q_n) # TODO anagrad = (stepdir * g).sum() assert abs(numgrad - anagrad) < 1e-10 print(numgrad) print(anagrad)
def test_logistic_derivative(self): l = Logistic() dl = nd.Derivative(l) for x in np.linspace(-100, 100, 5): assert_array_almost_equal(l.derivatives(x), dl(x))
def derivative(params, p, output, best_parameters=None, partial=False): F = threebody.Fitter(**params) if p not in F.parameters: raise ValueError("Parameter %s not among options: %s" % (p, F.parameters)) if best_parameters is not None: F.best_parameters = pickle.load(open(best_parameters, "rb")) print(F.goodness_of_fit(F.best_parameters)) if partial: vals, names = F.compute_linear_parts() for (n, v) in zip(names, vals): F.best_parameters[n] = v bs = base_steps.get(p, 1e-3) sg = numdifftools.MaxStepGenerator(bs, use_exact_steps=True) def fres(v): bp = F.best_parameters.copy() print(p, bp[p], v, end="\t") bp[p] += v try: r = F.residuals(bp, linear_fit=False) print(F.goodness_of_fit(bp)) return r except ValueError: print(np.inf) return np.inf * np.ones_like(F.mjds) nl_der = numdifftools.Derivative(fres, step=sg)(0) np.save(output, nl_der)
def GetDerivative(self, param_name): x_cur = self.params_val[param_name] dur = [] params_val = deepcopy(self.params_val) del params_val[param_name] ur = lambda x: self.pot.Potential( self.r, dict(params_val.items() + [(param_name, x)])) dur = nd.Derivative(ur) return dur(x_cur)
def test_logistic_derivative_vector(self): l = Logistic() dl = nd.Derivative(l) x = np.linspace(-100, 100, 5) derivative_numeric = np.zeros(x.shape) for i, xi in enumerate(x): derivative_numeric[i] = dl(xi) assert_array_almost_equal(l.derivatives(x), derivative_numeric)
def test_derivative_sin(self): # Evaluate the indicated (default = first) # derivative at multiple points dsin = nd.Derivative(np.sin) x = np.linspace(0, 2. * np.pi, 13) y = dsin(x) small = np.abs(y - np.cos(x)) < dsin.error_estimate * 100 # print np.abs(y - np.cos(x)) # print dsin.error_estimate # print small self.assertTrue(np.all(small))
def test_hyperbolic_tangent_derivative_vector(self): l = HyperbolicTangent() dl = nd.Derivative(l) x = np.linspace(-100, 100, 5) derivative_numeric = np.zeros(x.shape) for i, xi in enumerate(x): derivative_numeric[i] = dl(xi) assert_array_almost_equal(l.derivatives(x), derivative_numeric)
def test_central_n_forward_derivative_log(self): # Although a central rule may put some samples in the wrong places, it # may still succeed dlog = nd.Derivative(np.log, method='central') x = 0.001 small = np.abs(dlog(x) - 1.0 / x) < dlog.error_estimate self.assertTrue(small) # But forcing the use of a one-sided rule may be smart anyway dlog.method = 'forward' small = np.abs(dlog(x) - 1 / x) < dlog.error_estimate self.assertTrue(small)
def __init__(self, f, g, dg=None, a=-1, b=1, basis=chebyshev_basis, s=1, precision=10, endpoints=True, full_output=False): self.f = f self.g = g self.dg = nd.Derivative(g) if dg is None else dg self.basis = basis self.a = a self.b = b self.s = s self.endpoints = endpoints self.precision = precision self.full_output = full_output
def _test_numdifftools_helper(self, f, x): extra_args = [1] * f.extras args = extra_args + [x] for n in range(1, 5): #print 'n:', n ya = f(*args, n=n) #print 'ya:', ya f_part = functools.partial(f, *extra_args) yb = numdifftools.Derivative(f_part, n=n)(x) #print 'yb:', yb # detect only gross errors assert_allclose_or_small(ya, yb, rtol=1e-2, zerotol=1e-2)
def test_forward_derivative_tan(self): # Control the behavior of Derivative - forward 2nd order method, with only 1 Romberg term # Compute the first derivative, also return the final stepsize chosen #[deriv,err,fdelta] = derivest(@(x) tan(x),pi,'deriv',1,'Style','for','MethodOrder',2,'RombergTerms',1) dtan = nd.Derivative(np.tan, n=1, order=2, method='forward', romberg_terms=1) y = dtan(np.pi) abserr = dtan.error_estimate self.assertTrue(np.abs(y - 1) < abserr) dtan.final_delta # Control the behavior of DERIVEST - forward 2nd order method, with only 1 Romberg term # Compute the first derivative, also return the final stepsize chosen dtan = nd.Derivative(np.tan, n=1, method='forward', order=2, romberg_terms=1) self.assertAlmostEqual(dtan(np.pi), 1.0)
def fisher_matrix(covariance, dndz_sliced, ell): """calculate fisher matrix for 7 cosmological parameters ['omega_m', 'sigma_8', 'n_s', 'w_0', 'w_a', 'omega_b', 'h'] for given covariance matrix of lensing signal, and galaxy-redshift distribution in divided redshift bins.""" funcs = { 'Omega_b': getC_ellOfOmegab, 'sigma_8': getC_ellOfSigma8, 'h': getC_ellOfh, 'n_s': getC_ellOfn_s, 'Omega_m': getC_ellOfOmegam, 'w_0': getC_ellOfw0, 'w_a': getC_ellOfwa} vals = { 'sigma_8': sigma8, 'Omega_b': Omega_b, 'h': h, 'n_s': n_s, 'Omega_m': Omega_m, 'w_0': w_0, 'w_a': w_a} derivs_sig = {} for var in funcs.keys(): if vals[var] == 0: f = nd.Derivative(funcs[var], full_output=True, step=0.05) else: f = nd.Derivative(funcs[var], full_output=True, step=float(vals[var])/10) val, info = f(vals[var], dndz_sliced, ell) derivs_sig[var] = val param_order = ['Omega_m', 'sigma_8', 'n_s', 'w_0', 'w_a', 'Omega_b', 'h'] fisher = np.zeros((7,7)) for i, var1 in enumerate(param_order): for j, var2 in enumerate(param_order): f = [] for l in range(derivs_sig[var1].shape[1]): res = derivs_sig[var1][:, l].T @ np.linalg.inv(covariance[l]) @ derivs_sig[var2][:, l] f.append(res) fisher[i][j] = sum(f) return fisher
def test_derivative_equals_numerical(parfile, param): if param == "H3": pytest.xfail( "PINT's H3 code is known to use inconsistent approximations") model, toas, phase = get_model_and_toas( os.path.join(os.path.dirname(__file__), "datafile", parfile)) units = getattr(model, param).units def f(value): m = copy.deepcopy(model) p = getattr(m, param) if isinstance(p, pint.models.parameter.MJDParameter): p.value = value else: p.value = value * p.units with quiet(): warnings.simplefilter("ignore") try: dphase = m.phase(toas) - phase except ValueError: return np.nan * np.zeros_like(phase.frac) return dphase.int + dphase.frac if param == "ECC": e = model.ECC.value stepgen = numdifftools.MaxStepGenerator(min(e, 1 - e) / 2) elif param == "H3": h3 = model.H3.value stepgen = numdifftools.MaxStepGenerator(abs(h3) / 2) elif param == "FB0": stepgen = numdifftools.MaxStepGenerator(np.abs(model.FB0.value) * 1e-2) elif param == "FB1": stepgen = numdifftools.MaxStepGenerator(np.abs(model.FB1.value) * 1e3) elif param == "FB2": stepgen = numdifftools.MaxStepGenerator(np.abs(model.FB2.value) * 1e5) elif param == "FB3": stepgen = numdifftools.MaxStepGenerator(np.abs(model.FB3.value) * 1e7) else: stepgen = None df = numdifftools.Derivative(f, step=stepgen) a = model.d_phase_d_param(toas, delay=None, param=param).to_value(1 / units) b = df(getattr(model, param).value) if param.startswith("FB"): assert np.amax(np.abs(a - b)) / np.amax(np.abs(a) + np.abs(b)) < 1e-6 else: assert_allclose(a, b, atol=1e-4, rtol=1e-4)
def test_derivative_cube(self): '''Test for Issue 7''' cube = lambda x: x * x * x dcube = nd.Derivative(cube) shape = (3, 2) x = np.ones(shape) * 2 dx = dcube(x) self.assertListEqual(list(dx.shape), list(shape), 'Shape mismatch') for i, (val, tval) in enumerate(zip(dx.ravel(), (3 * x**2).ravel())): self.assertAlmostEqual( val, tval, places=12, msg= 'First differing element %d\n value = %g, \n true value = %g' % (i, val, tval))
def test_high_order_derivative_sin(self): # Higher order derivatives (second derivative) # Truth: 0 d2sin = nd.Derivative(np.sin, n=2, step_max=0.5) self.assertAlmostEqual( d2sin(np.pi), 0.0, ) # Higher order derivatives (up to the fourth derivative) # Truth: sqrt(2)/2 = 0.707106781186548 d2sin.n = 4 y = d2sin(np.pi / 4) small = np.abs(y - np.sqrt(2.) / 2.) < d2sin.error_estimate self.assertTrue(small)
def newton_raphson(f, x0, maxSteps=10, tol=10.**(-6)): import numdifftools as nd fp = nd.Derivative(f) i = 1 a = x0 while (i < maxSteps): b = a - f(a) / fp(a) if (b - a) < tol: return b a = b i += 1 # end while return b
def band_curvatures(H_k0s, phis, Pzs, band_indices, curv_warn=None): '''Returns -(d^2 E_{k0;n} / dq_c^2) \equiv [hbar^2 / (2 * mstar_c{k0;n})]^{-1} for c = x, y. result[k0_index][n0] = [x, y] [eV Bohr^2] ''' def band_energy(H_k0_phi, n, c, q): q_subst = [] for cp in range(2): if cp == c: q_subst.append(q) else: q_subst.append(0.0) q_subst.append(0.0) Es, U = np.linalg.eigh(H_k0_phi(np.array(q_subst))) return Es[n] result = [] for H_k0_phi, band_indices_k0 in zip(H_k0_phis(H_k0s, phis, Pzs), band_indices): result.append([]) for n in band_indices_k0: curvatures = [] for c in range(2): curvatures.append( nd.Derivative(partial(band_energy, H_k0_phi, n, c), n=2, order=16)(0.0)) cx, cy = curvatures[0], curvatures[1] threshold = 0.05 if (curv_warn is None or n in curv_warn ) and abs(cx - cy) > threshold * max(abs(cx), abs(cy)): print("WARNING: cx = {}, cy = {}, relative diff = {}".format( cx, cy, abs(cx - cy) / max(abs(cx), abs(cy)))) result[-1].append([cx, cy]) return result
def optimize_biases(model_dat, bias_ind): """numerical optimize modification indicator for single equation""" # optimizations parameters bias_start = 0 method = 'SLSQP' # BFGS, SLSQP, Nelder-Mead, Powell, TNC, COBYLA, CG print("\nEstimation of bias for {}:".format(model_dat["yvars"][bias_ind])) out = minimize(sse_bias, bias_start, args=(bias_ind, model_dat), method=method) bias = out.x sse = out.fun if hasattr(out, 'hess_inv'): hess_i = inv(out.hess_inv) print("Scalar Hessian from method {}.".format(method)) else: hess_i = nd.Derivative(sse_bias, n=2)(bias, bias_ind, model_dat) print("Scalar Hessian numerically.") return bias, hess_i, sse
import numpy as np import numdifftools as nd import matplotlib.pyplot as plt x = np.linspace(-2, 2, 100) for i in range(10): df = nd.Derivative(np.tanh, n=i) y = df(x) h = plt.plot(x, y / np.abs(y).max()) plt.show() def rosen(x): return (1 - x[0])**2 + 105. * (x[1] - x[0]**2)**2 hessian = nd.Hessian(rosen) print hessian([1, 1])
for i in cfile_index: cfile[i,:] = C[index1,:] index1 += 1 cout = reshape(cfile,(23*8,1)) np.savetxt("Atrap_el5_fourthorder.txt", cout) # <codecell> import numdifftools as nd for en in elec_name: i = int(en) - 1 w.set_voltage(en, C[3,i]) pot = lambda z : w.compute_total_dc_potential([xp,yp,z]) dpot = nd.Derivative(pot,n=2) dlist = [] for z_i in xrange(Nz): dlist.append(dpot(Z[z_i])) plot(Z,dlist) # <codecell> for en in elec_name: i = int(en) - 1 w.set_voltage(en, C[4,i]-3.1*C[3,i]) pot = lambda z : w.compute_total_dc_potential([xp,yp,z]) dpot = nd.Derivative(pot,n=2) dlist4 = []
def mgf_moment(self, i: int, n: int, lag: int) -> float: """Compute ith moment of C(n, lag) using MGF""" derivative = nd.Derivative(partial(self.mgf, n=n, lag=lag), n=i, full_output=True) moment, info = derivative(0) return moment