--- title: "Scores, Hessians, and varying parameters" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{Scores, Hessians, and varying parameters} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r setup, include=FALSE} knitr::opts_chunk$set(collapse = TRUE, comment = "#>") library(rvinecopulib) set.seed(601) ``` rvinecopulib exposes observation-wise likelihood scores and average Hessians for parametric bivariate and vine copula models. These are the basic inputs for convergence checks, standard errors, sandwich covariance estimates, and gradient-based extensions. For a parameter vector `theta` and observation-wise log-likelihood contributions `ell_i(theta)`, rvinecopulib reports \[ s_i(\theta)=\frac{\partial\ell_i(\theta)}{\partial\theta}, \qquad \bar H(\theta)=\frac{1}{n}\sum_{i=1}^n \frac{\partial^2\ell_i(\theta)}{\partial\theta\partial\theta^\top}. \] Thus `scores()` contains the row vectors `s_i`, while `hessian()` returns `H-bar`. ## Bivariate scores and Hessians Fit a parametric model and evaluate derivatives at its fitted parameters: ```{r bicop-derivatives} u2 <- rbicop(250, "gaussian", 0, 0.6) fit2 <- bicop(u2, family_set = "gaussian") score2 <- scores(u2, fit2) hessian2 <- hessian(u2, fit2) dim(score2) hessian2 colSums(score2) ``` The score matrix has one row per observation and one column per model parameter. At an interior maximum-likelihood estimate, its column sums should be close to zero. The Hessian is averaged over observations; multiply by the sample size when a summed log-likelihood Hessian is required. These derivatives are available for continuous parametric models. Boundary estimates and weakly identified parameters need the same care as in any likelihood analysis. ## Vine parameter ordering For a vine, score columns follow `(tree, edge, parameter)` order: tree 1 from left to right, then tree 2, and so on; multi-parameter pair copulas contribute their parameters in the order shown by `get_parameters()`. ```{r fit-vine} n <- 220 z <- rnorm(n) x <- cbind( z + rnorm(n), 0.7 * z + rnorm(n), -0.5 * z + rnorm(n) ) u <- pseudo_obs(x) fit <- vinecop( u, structure = dvine_structure(1:3), family_set = "gaussian" ) get_all_parameters(fit) ``` The number and ordering of derivative columns can be checked against the edge summary. ```{r vine-scores} score <- scores(u, fit) hess <- hessian(u, fit) dim(score) dim(hess) summary(fit) ``` ## Stepwise and full likelihood derivatives Vine models are usually fitted sequentially. With `step_wise = TRUE` (the default), each pair copula treats the pseudo-observations passed down from earlier trees as fixed. This is the objective optimized by the sequential estimator. With `step_wise = FALSE`, derivatives propagate through the complete h-function cascade and refer to the full joint log-likelihood. For a vine with edge set `E`, an observation contributes \[ \ell_i(\theta)=\sum_{e\in E} \log c_e\{u_{e,1,i}(\theta_{