All problems
Hessian-Vector Product Finite Difference
Problem
Approximate a Hessian-vector product by central finite differences for f(x1, x2) = x1^2 + 3*x1*x2 + 2*x2^2.
The analytic gradient is grad(x) = [2*x1 + 3*x2, 3*x1 + 4*x2]. Return (grad(x + h*v) - grad(x - h*v)) / (2*h) as a two-element list, where v is the direction vector and h the difference step.
Hint
Perturb x along v in both directions and difference the gradients.
Test cases
case 1
in: [[1,2],[3,-1],0.0001]
expected: [2.9999999999930083,4.999999999988347]
case 2
in: [[0,0],[1,0],0.0001]
expected: [2,3]
case 3
in: [[-2,0.5],[0,1],0.00001]
expected: [2.9999999999752442,4.000000000004]
case 4
in: [[1,1],[1,1],0.001]
expected: [4.999999999999449,6.999999999999673]
Starter codePython
def hvp_finite_diff(x, v, h):
# Your code here
pass4 test cases · run your code against them in the editor