def test_fixed_step(self, vals): htrue = self._hfun(vals) methods = [ 'central2', 'central', 'multicomplex', 'complex', 'forward', 'backward' ] for order in range(2, 7, 2): steps = nd.MinStepGenerator(num_steps=order + 1, use_exact_steps=True, step_ratio=3., offset=0) note('order = {}'.format(order)) for method in methods: h_fun = nd.Hessdiag(self._fun, step=steps, method=method, order=order, full_output=True) h_val, _info = h_fun(vals) _error = np.abs(h_val - htrue) note('error = {}, error_est = {}'.format( _error, _info.error_estimate)) assert_allclose(h_val, htrue, rtol=1e-5, atol=100 * max(_info.error_estimate))
def test_fixed_base_step(): desired = 0.1 step_gen = nd.MinStepGenerator(base_step=desired, num_steps=1, scale=2, offset=0) h = [h for h in step_gen(0)] assert_array_almost_equal((h[0] - desired) / desired, 0)
def test_central_and_forward_derivative_on_log(self): # Although a central rule may put some samples in the wrong places, it # may still succeed epsilon = nd.MinStepGenerator(num_steps=15, offset=0, step_ratio=2) dlog = nd.Derivative(np.log, method='central', step=epsilon) x = 0.001 assert_allclose(dlog(x), 1.0 / x) # But forcing the use of a one-sided rule may be smart anyway dlog = nd.Derivative(np.log, method='forward', step=epsilon) assert_allclose(dlog(x), 1 / x)
def test_default_generator(): step_gen = nd.MinStepGenerator(base_step=None, num_steps=10, step_ratio=4, offset=-1) h = np.array([h for h in step_gen(0)]) desired = np.array([ 3.58968236e-02, 8.97420590e-03, 2.24355147e-03, 5.60887869e-04, 1.40221967e-04, 3.50554918e-05, 8.76387295e-06, 2.19096824e-06, 5.47742059e-07, 1.36935515e-07 ]) assert_array_almost_equal((h - desired) / desired, 0)
def test_complex(self): htrue = np.array([0., 2., 18.]) method = 'complex' for num_steps in range(3, 7, 1): steps = nd.MinStepGenerator(num_steps=num_steps, use_exact_steps=True, step_ratio=2.0, offset=4) h_fun = nd.Hessdiag(self._fun, step=steps, method=method, full_output=True) h_val, _info = h_fun([1, 2, 3]) assert_allclose(h_val, htrue)
def test_complex(self): htrue = np.array([0., 2., 18.]) method = 'complex' for num_steps in range(3, 7, 1): steps = nd.MinStepGenerator(num_steps=num_steps, use_exact_steps=True, step_ratio=2.0, offset=4) Hfun = nd.Hessdiag(self._fun, step=steps, method=method, full_output=True) hd, _info = Hfun([1, 2, 3]) _error = hd - htrue assert_array_almost_equal(hd, htrue)
def test_min_step_generator_with_base_step01(): desired = 0.1 step_gen = nd.MinStepGenerator(base_step=desired, num_steps=1, offset=0) methods = ['forward', 'backward', 'central', 'complex'] for n in range(1, 5): for order in [1, 2, 4, 6, 8]: min_length = n + order - 1 lengths = [ min_length, min_length, max(min_length // 2, 1), max(min_length // 4, 1) ] for m, method in zip(lengths, methods): h = [h for h in step_gen(0, method=method, n=n, order=order)] # print(len(h), n, order, method) assert_array_almost_equal((h[-1] - desired) / desired, 0) assert_equal(m, len(h))
def test_fixed_step(self): htrue = np.array([0., 2., 18.]) methods = ['multicomplex', 'complex', 'central', 'forward', 'backward'] for order in range(2, 7, 2): steps = nd.MinStepGenerator(num_steps=order + 1, use_exact_steps=True, step_ratio=3., offset=0) for method in methods: Hfun = nd.Hessdiag(self._fun, step=steps, method=method, order=order, full_output=True) hd, _info = Hfun([1, 2, 3]) _error = hd - htrue assert_array_almost_equal(hd, htrue)
def test_hessian_cos_x_y_at_0_0(): # cos(x-y), at (0,0) def fun(xy): return np.cos(xy[0] - xy[1]) htrue = [[-1., 1.], [1., -1.]] methods = [ 'multicomplex', 'complex', 'central', 'central2', 'forward', 'backward' ] for num_steps in [10, 1]: step = nd.MinStepGenerator(num_steps=num_steps) for method in methods: h_fun = nd.Hessian(fun, method=method, step=step, full_output=True) h_val, _info = h_fun([0, 0]) # print(method, (h_val-np.array(htrue))) assert_allclose(h_val, htrue)
def test_hessian_cosIx_yI_at_I0_0I(): # cos(x-y), at (0,0) def fun(xy): return np.cos(xy[0] - xy[1]) htrue = [[-1., 1.], [1., -1.]] methods = [ 'multicomplex', 'complex', 'central', 'central2', 'forward', 'backward' ] for num_steps in [10, 1]: step = nd.MinStepGenerator(num_steps=num_steps) for method in methods: Hfun2 = nd.Hessian(fun, method=method, step=step, full_output=True) h2, _info = Hfun2([0, 0]) # print(method, (h2-np.array(htrue))) assert_array_almost_equal(h2, htrue)
def test_default_base_step(): step_gen = nd.MinStepGenerator(num_steps=1, offset=0) h = [h for h in step_gen(0)] desired = nd.EPS**(1. / 2.5) assert_array_almost_equal((h[-1] - desired) / desired, 0)
def test__min_step_generator_with_step_nom1(): step_gen = nd.MinStepGenerator(num_steps=1, step_nom=1.0, offset=0) h = [h for h in step_gen(0)] desired = EPS**(1. / 2.5) assert_array_almost_equal((h[-1] - desired) / desired, 0)