Exact (analytic) derivatives of loglike_defm() with respect to the model parameters.

loglike_defm_grad(m, par, ncores = 1L)

loglike_defm_hess(m, par, ncores = 1L)

Arguments

m

An object of class DEFM

par

A vector of parameters of length nterms_defm(m).

ncores

Integer scalar. Number of threads to use (only relevant if the package was compiled with OpenMP).

Value

  • loglike_defm_grad() returns a numeric vector of length nterms_defm(m).

  • loglike_defm_hess() returns a square numeric matrix of size nterms_defm(m).

Details

Let \(s_a\) be the observed sufficient statistics of each array (observation), and \(Z_u\) the normalizing constant of each unique support \(u\), shared by \(m_u\) arrays. The gradient is the observed minus the expected statistics, $$\nabla\ell(\theta) = \sum_a s_a - \sum_u m_u E_\theta[t | u],$$ and the Hessian is minus the (weighted) sum of the covariance of the statistics within each support, $$\nabla^2\ell(\theta) = -\sum_u m_u Cov_\theta[t | u].$$ The Hessian is negative semi-definite, so the log-likelihood is concave.

Examples

data(valentesnsList)

mymodel <- new_defm(
  id    = valentesnsList$id,
  Y     = valentesnsList$Y,
  X     = valentesnsList$X,
  order = 1
)

td_logit_intercept(mymodel)
td_formula(mymodel, "{y1, 0y2} > {y1, y2}")
init_defm(mymodel)

par <- c(-1, -1, -1, 2)
loglike_defm_grad(mymodel, par)
#> [1]  184.759010 -186.869685 -133.869685    3.185653
loglike_defm_hess(mymodel, par)
#>               [,1]          [,2]          [,3]          [,4]
#> [1,] -2.282665e+02 -1.587716e-15 -3.340335e-17 -1.731634e-16
#> [2,] -1.587716e-15 -2.307223e+02 -5.314347e+00 -8.407174e+00
#> [3,] -3.340335e-17 -5.314347e+00 -2.307223e+02 -8.407174e+00
#> [4,] -1.731634e-16 -8.407174e+00 -8.407174e+00 -1.066821e+01