Compute Hessian matrix via forward-over-reverse automatic differentiation
hessian.RdComputes the Hessian matrix (matrix of second partial derivatives) of a scalar function with respect to a vector of parameters. Uses forward-mode AD on top of reverse-mode AD for efficient, accurate computation.
Value
A numeric matrix of dimension (n x n) where n = length(params), containing the Hessian d²f/dθᵢdθⱼ
Details
The Hessian is computed using forward-over-reverse mode AD:
For each parameter i, create dual numbers where:
Primal = value object holding parameter value
Tangent = value object (1 for param i, 0 for others)
Run the loss function with these dual-value inputs
The tangent of the loss is df/dθᵢ (as a value expression)
Call backward() on the tangent loss
Each tangent parameter's gradient gives d²f/dθⱼdθᵢ
This is the "forward-over-reverse" pattern: forward-mode (dual numbers) computes df/dθᵢ symbolically, then reverse-mode differentiates that to get d²f/dθⱼdθᵢ.