Compares the analytically computed Jacobian (via score identities) against numerical finite differences to validate the implementation.
Value
A named list with components:
analyticThe analytically computed Jacobian
numericThe numerically computed Jacobian (finite differences)
abs_errorMatrix of absolute errors
rel_errorMatrix of relative errors
max_rel_errorMaximum relative error across all entries
passLogical; TRUE if max relative error < 0.01
Details
Uses central finite differences: $$\frac{\partial f}{\partial a} \approx \frac{f(a+\epsilon) - f(a-\epsilon)}{2\epsilon}$$
Important: This is a SECONDARY verification method because both the analytical Jacobian and the finite differences use the same quadrature layer. For independent verification, compare against adaptive integration (scipy.integrate.quad in Python).
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.24553449 dM1/db = -4.13558517
#> dV/da = 2.94445788 dV/db = -13.03822850
#>
#> Numeric Jacobian (finite diff):
#> dM1/da = 2.24552030 dM1/db = -4.13558517
#> dV/da = 2.94463271 dV/db = -13.03822849
#>
#> Relative Errors:
#> dM1/da: 6.32e-06 dM1/db: 1.29e-11
#> dV/da: 5.94e-05 dV/db: 6.13e-10
#>
#> Max Relative Error: 5.94e-05 [PASS]