Verify Jacobian Against Finite Differences
verify_jacobian.RdCompares the score-identity Jacobian against numerical finite differences to validate the implementation.
Usage
verify_jacobian(
J,
a,
b,
eps = 1e-06,
M = .QUAD_NODES_VERIFICATION,
verbose = TRUE,
abs_tol = .JACOBIAN_DERIV_ABS_TOL,
rel_tol = .JACOBIAN_DERIV_REL_TOL
)Arguments
- J
Integer; sample size.
- a
Numeric; shape parameter.
- b
Numeric; rate parameter.
- eps
Positive relative step for five-point finite differences (default: 1e-6).
- M
Integer; number of quadrature nodes (default: 200 for verification).
- verbose
Logical; if TRUE, print detailed comparison.
- abs_tol, rel_tol
Non-negative mixed-error tolerances; at least one must be positive.
Value
A named list with components:
analyticThe score-identity Jacobian (legacy component name retained for compatibility)
numericThe numerically computed Jacobian (finite differences)
abs_errorMatrix of absolute errors
rel_errorMatrix of relative errors
max_rel_errorMaximum relative error across all entries
statusOne of
converged,approximate, orfailed.component_statusNamed statuses for order refinement and finite-difference agreement.
passCompatibility logical equal to
status == "converged".conditioningThe structured conditioning result from the score-identity Jacobian.
Details
Uses the fourth-order, five-point central formula at a fixed recorded quadrature order. The algebra is independent of the score identity, although both computations use the package's marginal-moment integration layer.
Important: Release tests additionally compare both methods against adaptive log-Gamma integration with separately coded conditional moments.
Examples
# Verify Jacobian for a specific case
result <- verify_jacobian(J = 50, a = 2.0, b = 1.0, verbose = TRUE)
#> Jacobian Verification (J=50, a=2.00, b=1.00, M=200)
#> ------------------------------------------------------------
#>
#> Analytic Jacobian (score-based):
#> dM1/da = 2.24552026 dM1/db = -4.13558517
#> dV/da = 2.94463308 dV/db = -13.03822850
#>
#> Numeric Jacobian (finite diff):
#> dM1/da = 2.24552030 dM1/db = -4.13558517
#> dV/da = 2.94463270 dV/db = -13.03822850
#>
#> Relative Errors:
#> dM1/da: 1.64e-08 dM1/db: 1.92e-10
#> dV/da: 1.29e-07 dV/db: 4.50e-11
#>
#> Max Relative Error: 1.29e-07
#> Max Budget Ratio: 6.38e-05 [CONVERGED]