Skip to contents

second_order_sensitivities(loss, prob, alg, ...) returns the Hessian of loss(solve(prob, alg, ...)) with respect to the parameters, and second_order_sensitivity_product(loss, v, prob, alg, ...) its product with v. Julia's ForwardDiffOverAdjoint runs forward mode automatic differentiation over an adjoint gradient; here the adjoint gradient of solve_gradient() is differentiated by central differences, so the solves should use tight tolerances.

Usage

second_order_sensitivities(
  loss,
  prob,
  alg,
  ...,
  sensealg = ForwardDiffOverAdjoint()
)

second_order_sensitivity_product(
  loss,
  v,
  prob,
  alg,
  ...,
  sensealg = ForwardDiffOverAdjoint()
)

Arguments

loss

A function of the solution, as for solve_gradient().

prob, alg

The problem and algorithm.

...

Options for solve().

sensealg

ForwardDiffOverAdjoint() with a first order method.

v

A vector of the length of the parameters.

Value

A matrix (the Hessian) or a vector (its product with v).

Examples

prob <- ODEProblem(function(u, p, t) -p[1] * u, 1, c(0, 1), 2)
loss <- function(sol) sol$u[[length(sol$t)]]
H <- second_order_sensitivities(loss, prob, Tsit5(), saveat = 1, reltol = 1e-12, abstol = 1e-12)
H - exp(-2)   # d2 u(1) / dp2 = exp(-2)
#>               [,1]
#> [1,] -5.294887e-09