Skip to contents

Compares 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:

analytic

The score-identity Jacobian (legacy component name retained for compatibility)

numeric

The numerically computed Jacobian (finite differences)

abs_error

Matrix of absolute errors

rel_error

Matrix of relative errors

max_rel_error

Maximum relative error across all entries

status

One of converged, approximate, or failed.

component_status

Named statuses for order refinement and finite-difference agreement.

pass

Compatibility logical equal to status == "converged".

conditioning

The 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]