def test_jacobian(self): print('Test Jacobian') x = self.test_x analytic_solution = para_est.Rosenbrock() h = self.fd_step # Analytic j_analytic = analytic_solution.j(x) # numdifftools j_ndifftools = nd.Jacobian(analytic_solution.f_vector)(x) j_scipy = ndscipy.Jacobian(analytic_solution.f_vector, h)(x) # para_est self.ps.set_objective_function(analytic_solution.f_vector, metric='e') j_ps = self.ps.evaluate_derivatives(x, h, evaluate='jacobian') # Log print(' j analytic : {0}'.format(j_analytic)) print(' j_ndifftools: {0}'.format(j_ndifftools)) print(' j scipy : {0}'.format(j_scipy)) print(' j ps : {0}'.format(j_ps)) print(' ps function evaluation count: {0}'.format( self.ps.logger.get_f_eval_count())) self.assertEqual( True, np.all(np.isclose(j_ps, j_analytic, rtol=1.e-14, atol=1.e-2)))
def test_on_scalar_function(): def fun(x): return x[0] * x[1] * x[2] + np.exp(x[0]) * x[1] for method in ['forward', 'backward', "central", "complex"]: j_fun = nd.Jacobian(fun, method=method) x = j_fun([3., 5., 7.]) assert_allclose(x, [135.42768462, 41.08553692, 15.])
def main(problem_sizes=(4, 8, 16, 32, 64, 96)): fixed_step = MinStepGenerator(num_steps=1, use_exact_steps=True, offset=0) epsilon = MaxStepGenerator(num_steps=14, use_exact_steps=True, step_ratio=1.6, offset=0) adaptiv_txt = '_adaptive_{0:d}_{1!s}_{2:d}'.format(epsilon.num_steps, str(epsilon.step_ratio), epsilon.offset) gradient_funs = OrderedDict() hessian_funs = OrderedDict() hessian_fun = 'Hessdiag' hessian_fun = 'Hessian' if nda is not None: nda_method = 'forward' nda_txt = 'algopy_' + nda_method gradient_funs[nda_txt] = nda.Jacobian(1, method=nda_method) hessian_funs[nda_txt] = getattr(nda, hessian_fun)(1, method=nda_method) ndc_hessian = getattr(nd, hessian_fun) order = 2 for method in ['forward', 'central', 'complex']: method2 = method + adaptiv_txt options = dict(method=method, order=order) gradient_funs[method] = nd.Jacobian(1, step=fixed_step, **options) gradient_funs[method2] = nd.Jacobian(1, step=epsilon, **options) hessian_funs[method] = ndc_hessian(1, step=fixed_step, **options) hessian_funs[method2] = ndc_hessian(1, step=epsilon, **options) hessian_funs['forward_statsmodels'] = nds.Hessian(1, method='forward') hessian_funs['central_statsmodels'] = nds.Hessian(1, method='central') hessian_funs['complex_statsmodels'] = nds.Hessian(1, method='complex') gradient_funs['forward_statsmodels'] = nds.Jacobian(1, method='forward') gradient_funs['central_statsmodels'] = nds.Jacobian(1, method='central') gradient_funs['complex_statsmodels'] = nds.Jacobian(1, method='complex') gradient_funs['forward_scipy'] = nsc.Jacobian(1, method='forward') gradient_funs['central_scipy'] = nsc.Jacobian(1, method='central') gradient_funs['complex_scipy'] = nsc.Jacobian(1, method='complex') run_gradient_and_hessian_benchmarks(gradient_funs, hessian_funs, problem_sizes)
def test_on_vector_valued_function(self): xdata = np.arange(0, 1, 0.1) ydata = 1 + 2 * np.exp(0.75 * xdata) def fun(c): return (c[0] + c[1] * np.exp(c[2] * xdata) - ydata)**2 for method in ['forward', 'backward', "central", "complex"]: j_fun = nd.Jacobian(fun, method=method) J = j_fun([1, 2, 0.75]) # should be numerically zero assert_allclose(J, np.zeros((ydata.size, 3)), atol=1e-6)
def test_scalar_to_vector(val): def fun(x): dtype = complex if isinstance(x, complex) else float out = np.zeros((3, ), dtype=dtype) out[0] = x out[1] = x**2 out[2] = x**3 return out for method in [ 'backward', 'forward', "central", ]: # TODO: "complex" does not work j0 = nd.Jacobian(fun, method=method)(val).T assert_allclose(j0, [[1., 2 * val, 3 * val**2]], atol=1e-6)
def test_issue_25(self): def g_fun(x): out = np.zeros((2, 2), dtype=float) out[0, 0] = x[0] out[0, 1] = x[1] out[1, 0] = x[0] out[1, 1] = x[1] return out dg_dx = nd.Jacobian(g_fun) # TODO: method='reverse' fails x = np.array([1, 2]) tv = [[[1., 0.], [0., 1.]], [[1., 0.], [0., 1.]]] _EPS = np.MachAr().eps epsilon = _EPS**(1. / 4) assert_allclose(nd.approx_fprime(x, g_fun, epsilon), tv) dg = dg_dx(x) assert_allclose(dg, tv)
def test_on_matrix_valued_function(self): def fun(x): f0 = x[0]**2 + x[1]**2 f1 = x[0]**3 + x[1]**3 s0 = f0.size s1 = f1.size out = np.zeros((2, (s0 + s1) // 2), dtype=float) out[0, :] = f0 out[1, :] = f1 return out x = np.array([(1, 2, 3, 4), (5, 6, 7, 8)], dtype=float) y = fun(x) assert_allclose(y, [[26., 40., 58., 80.], [126., 224., 370., 576.]]) for method in [ 'forward', ]: # TODO: 'reverse' fails jaca = nd.Jacobian(fun, method=method) assert_allclose(jaca([1, 2]), [[[2., 4.]], [[3., 12.]]]) assert_allclose(jaca([3, 4]), [[[6., 8.]], [[27., 48.]]]) assert_allclose(jaca([[1, 2], [3, 4]]), [[[2., 0., 6., 0.], [0., 4., 0., 8.]], [[3., 0., 27., 0.], [0., 12., 0., 48.]]]) val = jaca(x) assert_allclose(val, [[[2., 0., 0., 0., 10., 0., 0., 0.], [0., 4., 0., 0., 0., 12., 0., 0.], [0., 0., 6., 0., 0., 0., 14., 0.], [0., 0., 0., 8., 0., 0., 0., 16.]], [[3., 0., 0., 0., 75., 0., 0., 0.], [0., 12., 0., 0., 0., 108., 0., 0.], [0., 0., 27., 0., 0., 0., 147., 0.], [0., 0., 0., 48., 0., 0., 0., 192.]]])
for method in ['forward', 'central', 'complex']: method2 = method + adaptiv_txt options = dict(method=method, order=order) gradient_funs[method] = nd.Jacobian(1, step=fixed_step, **options) gradient_funs[method2] = nd.Jacobian(1, step=epsilon, **options) hessian_funs[method] = ndc_hessian(1, step=fixed_step, **options) hessian_funs[method2] = ndc_hessian(1, step=epsilon, **options) hessian_funs['forward_statsmodels'] = nds.Hessian(1, method='forward') hessian_funs['central_statsmodels'] = nds.Hessian(1, method='central') hessian_funs['complex_statsmodels'] = nds.Hessian(1, method='complex') gradient_funs['forward_statsmodels'] = nds.Jacobian(1, method='forward') gradient_funs['central_statsmodels'] = nds.Jacobian(1, method='central') gradient_funs['complex_statsmodels'] = nds.Jacobian(1, method='complex') gradient_funs['forward_scipy'] = nsc.Jacobian(1, method='forward') gradient_funs['central_scipy'] = nsc.Jacobian(1, method='central') gradient_funs['complex_scipy'] = nsc.Jacobian(1, method='complex') def _compute_benchmark(functions, problem_sizes): result_list = [] for n in problem_sizes: print('n=', n) num_methods = len(functions) results = np.zeros((num_methods, 3)) ref_g = None f = BenchmarkFunction(n) for i, (_key, function) in enumerate(functions.items()): t = timeit.default_timer() function.fun = f