Skip to content
DeepForgeby svx
All problems
HardCalculusca-353

Hessian-Vector Product by Finite Differences

Problem

For f(x) = 0.5 * x^T A x, the Hessian-vector product can be estimated from central differences of the gradient: H v approximately (grad(x + eps v) - grad(x - eps v)) / (2 eps), with analytic grad(y) = 0.5 (A + A^T) y.

Given A as nested rows, x, v, and eps (default 1e-5), return the estimate. Use the analytic gradient inside the difference.

Hint

The estimate is exact up to floating-point error because the gradient is linear.

Test cases

case 1
in: [[[1,0],[0,2]],[1,1],[1,0]]
expected: [1.000000000001,0]
case 2
in: [[[0,1],[2,0]],[0.5,-0.5],[1,1]]
expected: [1.4999999999931732,1.4999999999931732]
case 3
in: [[[3,0],[0,1]],[0,0],[0,1]]
expected: [0,1]
Starter codePython
def hessian_vector_fd(a, x, v, eps=1e-5):
    # Your code here
    pass

3 test cases · run your code against them in the editor