| nl_loglik_hessian_parallel | R Documentation |
Computes the exact (analytical) Hessian of the negated log-likelihood for the Nested Logit model using OpenMP parallelisation with thread-local accumulators. Covers all parameter blocks: beta-beta, beta-lambda, beta-delta, lambda-lambda, lambda-delta, and delta-delta. Singleton nests (lambda fixed to 1, not estimated) contribute no rows or columns to the lambda blocks.
nl_loglik_hessian_parallel(
theta,
X,
alt_idx,
choice_idx,
nest_idx,
M,
weights,
use_asc = TRUE,
include_outside_option = FALSE
)
theta |
(K + n_non_singleton_nests + n_delta) parameter vector.
Order: |
X |
sum(M) x K design matrix of covariates. |
alt_idx |
sum(M)-length integer vector of 1-based alternative indices. |
choice_idx |
N-length integer vector of 1-based chosen alternative indices; 0 indicates the outside option was chosen. |
nest_idx |
J-length integer vector of 1-based nest indices for each inside alternative. |
M |
N-length integer vector of alternative-set sizes. |
weights |
N-length numeric vector of individual weights. |
use_asc |
Logical; whether alternative-specific constants are included. |
include_outside_option |
Logical; whether an outside option (V=0) is present. |
A symmetric (P x P) matrix: the Hessian of the negated
log-likelihood evaluated at theta. Structurally identical to the
output of nl_loglik_numeric_hessian; suitable for
invert_hessian().
library(data.table)
set.seed(42)
N <- 50; J <- 4
dt <- data.table(id = rep(1:N, each = J), alt = rep(1:J, N))
dt[, `:=`(x1 = rnorm(.N), x2 = rnorm(.N))]
dt[, nest := ifelse(alt <= 2, "A", "B")]
dt[, choice := 0L]
dt[, choice := sample(c(1L, rep(0L, J - 1))), by = id]
d <- prepare_nl_data(dt, "id", "alt", "choice", c("x1", "x2"), "nest")
K_x <- ncol(d$X)
K_l <- sum(table(d$nest_idx) > 1) # number of non-singleton nests (= 2)
theta <- c(rep(0, K_x), rep(0.8, K_l), rep(0, J - 1))
H <- choicer:::nl_loglik_hessian_parallel(theta, d$X, d$alt_idx, d$choice_idx,
d$nest_idx, d$M, d$weights)
dim(H)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.