Skip to contents

This document provides a detailed mathematical description of the mixed logit (random coefficients logit) model as implemented in src/mxlogit.cpp.

Notation

Symbol Description
i=1,…,Ni = 1, \ldots, N Index for choice situations
j=1,…,Jij = 1, \ldots, J_i Index for alternatives available to individual ii
s=1,…,Ss = 1, \ldots, S Index for simulation draws
jij_i The alternative chosen by individual ii
wiw_i Weight for individual ii
XijX_{ij} Row vector of covariates with fixed coefficients (1×Kx1 \times K_x)
WijW_{ij} Row vector of covariates with random coefficients (1×Kw1 \times K_w)
β\beta Fixed coefficient vector (Kx×1K_x \times 1)
μ\mu Mean parameters for random coefficients (Kw×1K_w \times 1)
LL Lower-triangular Cholesky factor (Kw×KwK_w \times K_w)
Σ=LL′\Sigma = LL' Covariance matrix of the latent Gaussian deviations (and of realized coefficients only in the all-normal case)
δj\delta_j Alternative-specific constant (ASC) for alternative jj
ηis\eta_i^s Standard-normal integration draw for choice situation ii and draw ss (Kw×1K_w \times 1)
γis=Lηis\gamma_i^s = L\eta_i^s Latent Gaussian random-coefficient deviation
n=1,…,Un = 1, \ldots, U Index for decision makers (respondents) when person_col groups situations into a panel; UU is the number of decision makers
t=1,…,Tnt = 1, \ldots, T_n Index for the choice situations belonging to decision maker nn; TnT_n is their count
ωns\omega_{ns} Posterior weight of draw ss given decision maker nn’s observed choices ($$2.6)

Sections 2–12 are written per choice situation ii, the default likelihood unit. §1.6 introduces the decision maker nn as an optional coarser unit; §2.6, §3.5 and §4.5 give the corresponding panel likelihood, score and Hessian. Without person_col, U=NU = N and Tn≡1T_n \equiv 1 for every nn, so ii and (n,t=1)(n, t{=}1) index the same choice situation and every panel formula collapses to its §2–4 counterpart.


1. Model Definition

1.1 Utility Specification

The utility that individual ii derives from alternative jj under simulation draw ss is:

Vijs=Xijβ+Wij(μ*+γis*)+δj V_{ij}^s = X_{ij}\beta + W_{ij}\left(\mu^* + \gamma_i^{s*}\right) + \delta_j

where: - XijβX_{ij}\beta captures the systematic utility from fixed coefficients - Wijμ*W_{ij}\mu^* captures the mean contribution of random coefficients - Wijγis*W_{ij}\gamma_i^{s*} captures the individual-specific random deviation - δj\delta_j is the alternative-specific constant (the first inside ASC is normalized without an outside option; with an outside option all inside ASCs are free)

1.2 Random Coefficient Structure

Random coefficients are generated through a Cholesky decomposition structure:

γis=Lηis,where ηis∼N(0,IKw) \gamma_i^s = L \eta_i^s, \quad \text{where } \eta_i^s \sim N(0, I_{K_w})

This implies:

γis∼N(0,Σ),where Σ=LL′ \gamma_i^s \sim N(0, \Sigma), \quad \text{where } \Sigma = LL'

For finite parameter values the exponentiated diagonal makes LL nonsingular, so Σ\Sigma is positive definite (positive semidefinite in the limiting sense as a diagonal element tends to zero). Here Σ\Sigma is the covariance of the latent Gaussian deviation γ\gamma. It is the covariance of the realized coefficient vector only when all entries are normal. After any componentwise log-normal transformation, the realized coefficient covariance is a nonlinear function of LL and is not LL′LL'.

1.3 Distribution Transformations

The implementation supports two distributions for each random coefficient kk:

Normal Distribution (rc_dist[k] = 0): μk*=μk,γiks*=γiks \mu_k^* = \mu_k, \quad \gamma_{ik}^{s*} = \gamma_{ik}^s

Log-Normal Distribution (rc_dist[k] = 1): μk*=exp⁡(μk),γiks*=exp⁡(γiks) \mu_k^* = \exp(\mu_k), \quad \gamma_{ik}^{s*} = \exp(\gamma_{ik}^s)

Note: This is a shifted log-normal parameterization: βk=exp⁡(μk)+exp⁡(Lkη)\beta_k = \exp(\mu_k) + \exp(L_k \eta), which differs from the textbook parameterization βk=exp⁡(μk+Lkη)\beta_k = \exp(\mu_k + L_k \eta).

When rc_mean = FALSE, μ*=0\mu^*=0 for every distribution. Thus a normal random coefficient is centered at zero, while a log-normal random coefficient is exp⁡((Lη)k)\exp((L\eta)_k) and has median one. Correlation is introduced on the latent-normal scale by the rows of LL; componentwise transformations then induce a generally non-Gaussian joint coefficient distribution.

1.4 Cholesky Parameterization

For a full covariance matrix (rc_correlation = TRUE), the lower-triangular matrix LL is parameterized as:

L=(exp⁡(ℓ11)00⋯ℓ21exp⁡(ℓ22)0⋯ℓ31ℓ32exp⁡(ℓ33)⋯⋮⋮⋮⋱) L = \begin{pmatrix} \exp(\ell_{11}) & 0 & 0 & \cdots \\ \ell_{21} & \exp(\ell_{22}) & 0 & \cdots \\ \ell_{31} & \ell_{32} & \exp(\ell_{33}) & \cdots \\ \vdots & \vdots & \vdots & \ddots \end{pmatrix}

where: - Diagonal elements: Lpp=exp⁡(ℓpp)L_{pp} = \exp(\ell_{pp}) (ensures positivity) - Off-diagonal elements: Lpq=ℓpqL_{pq} = \ell_{pq} for p>qp > q (unconstrained)

For diagonal-only covariance (rc_correlation = FALSE):

L=diag(exp(ℓ11),exp(ℓ22),…,exp(ℓKwKw)) L = \text{diag}\left(\exp(\ell_{11}), \exp(\ell_{22}), \ldots, \exp(\ell_{K_w K_w})\right)

The parameter vector for LL has length: - Full: Kw(Kw+1)/2K_w(K_w + 1)/2 - Diagonal: KwK_w

1.5 Scaling the Optimizer’s Coordinates

Motivation. scale_vars changes how the optimizer moves, never what it maximizes. L-BFGS starts each iteration from a single scalar multiple of the identity as its curvature, so it is slow, and can fail, when the log-likelihood’s curvature differs by orders of magnitude across parameters. Two things spread it. Units: a price in thousands next to a 0/1 dummy gives their coefficients curvatures far apart. The model: the constant of a small alternative (a school with few applicants, a rarely chosen hospital) has far less curvature than a large one’s, and the Cholesky entries of LL vary as well. Column scales reach the first; only the curvature itself reaches the second.

A change of coordinates. The kernels evaluate the model at θ\theta, on the data as supplied. The optimizer works on θ̃\tilde\theta, with

θ=m∘θ̃+c,m>0 elementwise.\theta = m \circ \tilde\theta + c, \qquad m > 0 \text{ elementwise}.

The objective in its coordinates is f̃(θ̃)=f(m∘θ̃+c)\tilde f(\tilde\theta) = f(m \circ \tilde\theta + c), with gradient ∇f̃=m∘∇f\nabla\tilde f = m \circ \nabla f: the map is affine and diagonal, J=diag(m)J = \mathrm{diag}(m), so the chain rule has no second-order term and the Hessian in θ̃\tilde\theta is JHJJ H J. The maximizer is the same: θ̂=m∘θ̃̂+c\hat\theta = m \circ \hat{\tilde\theta} + c. Start values and bounds are given in natural units and mapped by (θ−c)/m(\theta - c) / m, which keeps their order. scale_vars = "bhhh" is the default of run_mxlogit() and run_mnlogit(); "none" bypasses the map entirely.

Column scales ("sd", "mad", "iqr"). Let skXs_k^X and skWs_k^W be the columns’ scales. The map

Parameter mm cc
βk\beta_k 1/skX1 / s_k^X 0
μk\mu_k (normal RC) 1/skW1 / s_k^W 0
ℓpp\ell_{pp} (Cholesky diagonal, stored as log⁡Lpp\log L_{pp}) 1 −log⁡spW-\log s_p^W
ℓpq\ell_{pq} (Cholesky off-diagonal, p>qp > q) 1/spW1 / s_p^W 0
ASCj\text{ASC}_j 1 0

makes f̃\tilde f the log-likelihood of the design with every column divided by its scale: (x/s)(sβ)=xβ(x / s)(s \beta) = x \beta, and W̃p=Wp/spW\tilde W_p = W_p / s_p^W implies Lp⋅scaled=spWLp⋅L^{\mathrm{scaled}}_{p\cdot} = s_p^W L_{p\cdot}, row pp of LL (on the log diagonal, an additive shift; the scale is keyed on the row pp, not the column qq). The data are never divided: the optimizer sees the scaled problem through the map. "sd" uses the sample standard deviation; "mad" (stats::mad, 1.4826×median|X⋅k−median(X⋅k)|1.4826 \times \text{median}|X_{\cdot k} - \text{median}(X_{\cdot k})|) and "iqr" (stats::IQR(x) / 1.349) are SD-equivalent under normality and preferable when outliers dominate a column’s SD.

Why centering is omitted. Without an outside option, subtracting a column’s mean from every alternative shifts every utility in a situation by the same amount, which cancels from the softmax; the same argument applies draw by draw to WW. With an implicit outside option fixed at utility zero, an inside-only shift does not cancel against the outside: a fixed-XX shift could be offset only by shifting every inside ASC, and a random-WW shift cannot generally be absorbed by fixed ASCs. So the column-scale map corresponds to dividing the data by their scales, never to centering them.

Log-normal coefficients. For a log-normal coefficient (βk=exp⁡(μk)+exp⁡((Lη)k)\beta_k = \exp(\mu_k) + \exp((L\eta)_k), rc_dist[k] == 1), dividing the column by skWs_k^W has no closed form in (μk,Lk⋅)(\mu_k, L_{k\cdot}), because the two exponentials cannot share a scale factor; the column-scale map leaves those parameters at m=1m = 1, c=0c = 0.

BHHH scales ("bhhh"). With B0=∑uwususu⊤B_0 = \sum_u w_u s_u s_u^\top the BHHH (outer product of gradients) matrix at the start values θ0\theta_0, from the unit scores sus_u and the fit’s weights,

mk=2round(log⁡2(1/B0,kk)),c=0,m_k = 2^{\mathrm{round}(\log_2 (1 / \sqrt{B_{0,kk}}))}, \qquad c = 0,

for every parameter, the constants, the Cholesky entries and log-normal coefficients included. The diagonal of JB0JJ B_0 J is then within a factor of two of one, for entries neither floored nor clamped: insofar as B0B_0 tracks the curvature, the optimizer starts from curvatures near one in those coordinates. One extra gradient pass returns diag(B0)\mathrm{diag}(B_0), accumulated beside the gradient (the full matrix is never formed). A B0,kkB_{0,kk} that is not finite, or below 10−1210^{-12} times the median of the finite positive ones, carries no information at θ0\theta_0 and takes the median’s scale; scales are clamped to [2−400,2400][2^{-400}, 2^{400}]. Powers of two make the map exact: θ→θ̃→θ\theta \to \tilde\theta \to \theta round-trips without rounding, bounds included. The scales are fixed for the whole run, because L-BFGS’s line search and stored curvature pairs assume one fixed function of θ̃\tilde\theta. A start near the estimate (theta_init) gives B0B_0 closer to the information at the optimum. The scales fix the optimizer’s metric, not a start that is wrong in a column’s units; the default start is set in the columns’ units (below).

The default start. With theta_init = NULL the start is zero for β\beta, μ\mu, the off-diagonal ℓpq\ell_{pq} and the constants, and on the diagonal

ℓpp=−log⁡hp,Lpp=1/hp,\ell_{pp} = -\log h_p, \qquad L_{pp} = 1 / h_p,

for a normal random coefficient, where hph_p is the typical step of column pp: the distance between its values for a column with two (a dummy, a two-level attribute), the mean of |Wp−v||W_p - v| over the other rows when one value vv fills more than half of the column (a mostly-zero column), and its standard deviation otherwise. At the start, a step of hph_p in WpW_p moves the utility by a random amount with standard deviation one (the logit error’s is π/6≈1.28\pi / \sqrt{6} \approx 1.28). For a dummy with a share ϕ\phi of ones the standard deviation, ϕ(1−ϕ)\sqrt{\phi(1-\phi)}, would understate the step its coefficient multiplies, which is 1; with one of JJ alternatives marked per situation, ϕ≈1/J\phi \approx 1/J, it would start LppL_{pp} about J\sqrt{J} times higher. For a column far from zero with an implicit outside option, the contrast with the outside good is the level WpW_p itself, so a root mean square might seem the step; on a census-style design with a price of mean 100 and standard deviation 5 it put the start at the true heterogeneity and took 169 evaluations, against 104 from the standard deviation (the level errs high, below). A column with no variation (a step below 10−1210^{-12} of its level) or a step that is not finite starts at Lpp=1L_{pp} = 1, and steps are clamped to [2−500,2500][2^{-500}, 2^{500}]. A default start outside lower/upper is moved to the nearest bound, with a message.

Rescaling the column, Wp→aWpW_p \to a W_p, gives the same model with row pp of LL (and μp\mu_p, with rc_mean = TRUE) divided by aa, its ℓpp\ell_{pp} shifted by −log⁡a-\log a; hph_p is multiplied by aa, so this start transforms with it. So does the map of "sd" (and of "mad" and "iqr"), whose shift −log⁡spW-\log s_p^W puts ℓ̃pp\tilde\ell_{pp} at log⁡(spW/hp)\log(s_p^W / h_p), a constant that does not depend on the units (00 under "sd" for a column whose step is its standard deviation): in exact arithmetic the optimizer’s problem is then the same in any units. Under "bhhh", a parameter divided by aa has its score multiplied by aa, so B0,kkB_{0,kk} is multiplied by a2a^2 and mkm_k (before rounding, for entries neither floored nor clamped) divided by aa: θ̃k=θk/mk\tilde\theta_k = \theta_k / m_k is unchanged up to the power-of-two rounding. ℓpp\ell_{pp}, shifted by −log⁡a-\log a, keeps its score and its scale, so ℓ̃pp\tilde\ell_{pp} is translated by −log⁡a/mpp-\log a / m_{pp}, which L-BFGS’s steps do not see; only nloptr’s relative step test (xtol_rel, 10−810^{-8} by default) does.

From Lpp=0.5L_{pp} = 0.5 in each column’s own units (the 0.2.x default), no choice of scale_vars reached the maximum of a census-style design with columns rescaled by 10310^3 or 10−310^{-3} in 2,000 likelihood evaluations; from this start "bhhh" reaches it in 54, against 56 on the original units. The level, one, errs high on purpose. B0B_0 is fixed at the start, and while LppL_{pp} is small the simulated likelihood depends on it only at second order (the draws have mean zero, or nearly): the score in ℓpp\ell_{pp} is O(Lpp2)O(L_{pp}^2), so B0,ppB_{0,pp} is O(Lpp4)O(L_{pp}^4) and mpp∝Lpp−2m_{pp} \propto L_{pp}^{-2} at the start, while the curvature in ℓpp\ell_{pp} grows like Lpp2L_{pp}^2 as the optimizer raises it. A start with too little heterogeneity therefore yields a scale too generous for where the optimizer goes: from 0.25/sd(Wp)0.25 / \mathrm{sd}(W_p) one design reported convergence 554 log-likelihood units short.

A log-normal coefficient starts at ℓpp=log⁡0.5\ell_{pp} = \log 0.5: there LppL_{pp} is the standard deviation of the logarithm of the coefficient’s random part, which has no units. That random part, exp⁡((Lη)p)\exp((L\eta)_p), has median one in any units, so no start makes a log-normal column’s units irrelevant: its covariate belongs in units where the coefficient is of order one.

Variance. The variance is computed at θ̂\hat\theta, in natural units, whatever the coordinates the optimizer used: V=A−1V = A^{-1} with A=−H(θ̂)A = -H(\hat\theta) the information (the negated Hessian of the log-likelihood), the BHHH variance (∑uwususu⊤)−1\big(\sum_u w_u s_u s_u^\top\big)^{-1} at θ̂\hat\theta, or the sandwich A−1BA−1A^{-1} B A^{-1} of §9.3 (with BB the meat, its weights squared) or its cluster-robust extension (§9.5). The natural information can be ill-conditioned for the reasons a fit is scaled (columns in different units, the constants of rarely and often chosen alternatives), so every fit, scaled or not, inverts it equilibrated, as §5.6 describes.

The other models. run_mnlogit() and run_nestlogit() take the same map: column scales reach the coefficients only (m=1/skXm = 1/s_k^X on βk\beta_k; the constants and the nest parameters keep m=1m = 1), and "bhhh" reaches every parameter, the nest parameters λ\lambda included, whose lower bound is mapped with the start values. "bhhh" is the default of both; in the nested logit the BHHH scales fixed at θ0\theta_0 can slow the optimizer where nest parameters end far from their start, but "none" can stop at the start values where the covariates’ units make its first step too long. Their variances are inverted equilibrated in the same way, whatever scale_vars (§12.6 of the multinomial logit article, §5.8 of the nested logit article).

1.6 Likelihood Unit: Cross-Section or Panel

By default ii indexes a choice situation, and the likelihood integrates a separate coefficient distribution for each situation — the right model when tastes are redrawn every situation, or each decision maker is observed once. Passing person_col to run_mxlogit() / prepare_mxl_data() switches the likelihood unit from the choice situation to the decision maker nn (Revelt and Train 1998): the TnT_n choice situations belonging to nn then share one taste draw βn∼f(β∣θ)\beta_n \sim f(\beta \mid \theta), and the likelihood integrates their joint probability, Ln(θ)=∫∏t=1TnPnt(jnt∣β)f(β∣θ)dβ, L_n(\theta) = \int \prod_{t=1}^{T_n} P_{nt}(j_{nt} \mid \beta)\, f(\beta \mid \theta)\, d\beta, in place of the per-situation Li(θ)=∫Piji(β)f(β∣θ)dβL_i(\theta) = \int P_{ij_i}(\beta)\, f(\beta \mid \theta)\, d\beta used elsewhere in this document (§2.2–2.3). id_col must still identify choice situations uniquely across the data set — situation ids do not restart within person. person_col = NULL (the default) makes every situation its own decision maker, Tn≡1T_n \equiv 1, and recovers the cross-sectional likelihood exactly, so every formula in §2–§9 written per situation ii applies unchanged, read with i≡(n,t=1)i \equiv (n, t{=}1).

On panel data, imposing Tn≡1T_n \equiv 1 on genuinely repeated tasks (i.e., fitting cross-sectionally) maximizes a composite (pseudo-) likelihood: each situation’s marginal probability is still correctly specified under f(β∣θ)f(\beta \mid \theta), so the estimator stays consistent for θ\theta, provided the per-situation marginals identify θ\theta, but it is statistically inefficient — it never uses the fact that the same βn\beta_n generated a person’s repeated choices, which is exactly the variation that identifies Σ\Sigma most sharply (the marginals identify it too, but only through the shape of the mixture). Cluster-robust standard errors (cluster_col=, or vcov(fit, type = "cluster"); §9.5) repair the inference for the resulting score dependence but leave the likelihood unchanged; §2.6, §3.5 and §4.5 give the panel likelihood, score and Hessian that use the repetition directly. run_hmnlogit() is the Bayesian counterpart of the panel likelihood, adding partially-pooled alternative effects and posterior rather than asymptotic inference.


2. Log-Likelihood Function

2.1 Choice Probability

For a given draw ss, the probability that individual ii chooses alternative jj follows the logit formula:

Pijs=exp⁡(Vijs)∑k=1Jiexp⁡(Viks) P_{ij}^s = \frac{\exp(V_{ij}^s)}{\sum_{k=1}^{J_i} \exp(V_{ik}^s)}

If an outside option is included, it is normalized to Vi0s=0V_{i0}^s = 0.

2.2 Simulated Probability

Since the random coefficients γi\gamma_i are unobserved, we integrate over their distribution using simulation:

P‾iji=1S∑s=1SPijis \bar{P}_{ij_i} = \frac{1}{S} \sum_{s=1}^{S} P_{ij_i}^s

where jij_i is the alternative chosen by individual ii.

2.3 Log-Likelihood

The simulated log-likelihood is:

ℓ(θ)=∑i=1Nwilog⁡(P‾iji)=∑i=1Nwilog⁡(1S∑s=1SPijis) \ell(\theta) = \sum_{i=1}^{N} w_i \log\left(\bar{P}_{ij_i}\right) = \sum_{i=1}^{N} w_i \log\left(\frac{1}{S} \sum_{s=1}^{S} P_{ij_i}^s\right)

where θ=(β,μ,vec(L),δ)\theta = (\beta, \mu, \text{vec}(L), \delta) is the full parameter vector.

2.4 Log-Sum-Exp Trick for Numerical Stability

To avoid numerical overflow/underflow, the implementation uses:

  1. Probability computation: Before computing PijsP_{ij}^s, subtract max⁡kViks\max_k V_{ik}^s: Pijs=exp⁡(Vijs−maxkViks)∑kexp⁡(Viks−maxkViks) P_{ij}^s = \frac{\exp(V_{ij}^s - \max_k V_{ik}^s)}{\sum_k \exp(V_{ik}^s - \max_k V_{ik}^s)}

  2. Log-probability averaging: Use log-sum-exp to compute log⁡(∑sPijis)\log(\sum_s P_{ij_i}^s): logSumExp(a,b)=max⁡(a,b)+log⁡(exp(a−max(a,b))+exp(b−max(a,b))) \text{logSumExp}(a, b) = \max(a,b) + \log\left(\exp(a - \max(a,b)) + \exp(b - \max(a,b))\right)

    Applied iteratively across draws ss. With person_col (§2.6), each draw’s log-probability is first summed across a decision maker’s situations, λns=∑t=1Tnlog⁡Pntjnts\lambda_{ns} = \sum_{t=1}^{T_n} \log P_{ntj_{nt}}^s, and only then does log-sum-exp run over draws ss to obtain ℓn(θ)=LSEs(λn⋅)−log⁡S\ell_n(\theta) = \mathrm{LSE}_s(\lambda_{n\cdot}) - \log S.

  3. Log-probability of the chosen alternative: each term of λns\lambda_{ns} is formed directly in log space, as the chosen utility minus the log-sum-exp of the choice set, log⁡Pntjs=Vntjs−maxkVntks−log⁡∑kexp⁡(Vntks−maxkVntks), \log P_{ntj}^s = V_{ntj}^s - \max_k V_{ntk}^s - \log \sum_k \exp\left(V_{ntk}^s - \max_k V_{ntk}^s\right), never as the logarithm of PntjsP_{ntj}^s, which turns subnormal beyond a utility gap of about 708 and underflows to zero beyond about 745 (the multinomial logit kernels use the same form). Neither a single choice probability nor a product of many is ever formed outside log space, so λns\lambda_{ns} and ℓn(θ)\ell_n(\theta) are finite, and exact, whenever the utilities are finite, however poorly the parameter vector fits the observed choices.

2.5 Statistical Properties of the Finite-Draw Criterion

For independent pseudo-random draws, P‾iji\bar P_{ij_i} is an unbiased simulator of the exact mixed-logit probability. Its logarithm is not: Jensen’s inequality gives 𝔼[log⁡P‾]<log⁡𝔼[P‾]\mathbb E[\log\bar P]<\log\mathbb E[\bar P] when the simulator is nondegenerate, and a second-order expansion gives the familiar O(S−1)O(S^{-1}) criterion bias. Under the regularity conditions in Hajivassiliou and Ruud (1994), Lee (1995), Gouriéroux and Monfort (1996), and Train (2009, Ch. 10), the classical pseudo-random MSL benchmarks are:

  • Fixed SS: MSL is inconsistent — the simulation bias does not vanish as N→∞N \to \infty.
  • S→∞S \to \infty with NN (at any rate): MSL is consistent.
  • SS rising faster than N\sqrt{N}: MSL is asymptotically equivalent to exact maximum likelihood — simulation bias is o(N−1/2)o(N^{-1/2}) and simulation variance is negligible under the maintained draw scheme.

choicer uses a Halton sequence rather than independent pseudo-random draws (§5.4), so the first sentence above is not a literal property of its deterministic store-mode simulator, and the pseudo-random rate conditions should not be quoted as an exact theorem for this implementation. Quasi-Monte Carlo often reduces integration error markedly at a given SS (Bhat 2003), but its error depends on integrand smoothness, dimension, sequence construction, and randomization. The operational implication is to treat SS and, for generated scrambled draws, the seed as part of the numerical specification; increase SS until estimates, the Cholesky block, the objective, and standard errors stabilize. mc_asymptotics() supplies Monte Carlo diagnostics rather than a proof that a particular finite-SS fit satisfies exact-ML asymptotics.

2.6 Panel Likelihood

With person_col, §1.6 replaces the per-situation likelihood by the per-decision-maker one. Writing λns=∑t=1Tnlog⁡Pntjnts\lambda_{ns} = \sum_{t=1}^{T_n} \log P_{nt j_{nt}}^s for the log-probability of decision maker nn’s choices at draw ss — a sum, over the situations sharing that draw, of the situation-level log-probabilities of §2.1 — the simulated log-likelihood is

ℓn(θ)=log⁡(1S∑s=1Sexp(λns)),ℓ(θ)=∑n=1Uwnℓn(θ), \ell_n(\theta) = \log\left(\frac1S\sum_{s=1}^S \exp(\lambda_{ns})\right), \qquad \ell(\theta) = \sum_{n=1}^U w_n\, \ell_n(\theta),

with wnw_n a decision-maker weight, constant across nn’s situations — a requirement of this panel objective, not of §9.1–§9.2’s WESML weights, which vary with the alternative a person chose and can therefore differ across their situations; WESML is accordingly not supported with person_col. Define the posterior draw weight

ωns=exp⁡(λns)∑s′=1Sexp⁡(λns′)=exp⁡(λns−LSEs(λn⋅)),∑s=1Sωns=1, \omega_{ns} = \frac{\exp(\lambda_{ns})}{\sum_{s'=1}^S \exp(\lambda_{ns'})} = \exp\bigl(\lambda_{ns} - \mathrm{LSE}_s(\lambda_{n\cdot})\bigr), \qquad \sum_{s=1}^S \omega_{ns} = 1,

the simulated posterior probability that draw ss is decision maker nn’s taste, given the choices they made — computed via the max-shifted log-sum-exp of §2.4, so it never forms the product of many situation probabilities outside log space. §3.5 and §4.5 write the panel score and Hessian in terms of ωns\omega_{ns}. Setting Tn≡1T_n \equiv 1 collapses λns\lambda_{ns} to log⁡Pijis\log P_{ij_i}^s and ωns\omega_{ns} to Pijis/∑s′Pijis′P_{ij_i}^s / \sum_{s'} P_{ij_i}^{s'}, the per-draw share already implicit in §2.3’s P‾iji=1S∑sPijis\bar P_{ij_i} = \frac1S\sum_s P_{ij_i}^s, so the panel and cross-sectional log-likelihoods agree termwise.


3. Gradient Computation

3.1 General Formula

Define for draw ss the log-probability of the chosen alternative:

log⁡Pijis=Vijis−log⁡(∑kexp(Viks)) \log P_{ij_i}^s = V_{ij_i}^s - \log\left(\sum_k \exp(V_{ik}^s)\right)

The gradient of this with respect to utility is:

∂log⁡Pijis∂Vias=𝟏a=ji−Pias \frac{\partial \log P_{ij_i}^s}{\partial V_{ia}^s} = \mathbf{1}_{a = j_i} - P_{ia}^s

3.2 Per-Draw Gradient

Define zijs=∂Vijs∂θz_{ij}^s = \frac{\partial V_{ij}^s}{\partial \theta} as the gradient of utility with respect to parameters. The per-draw gradient is:

gis=∂log⁡Pijis∂θ=∑j=1Ji(𝟏j=ji−Pijs)zijs g_i^s = \frac{\partial \log P_{ij_i}^s}{\partial \theta} = \sum_{j=1}^{J_i} \left(\mathbf{1}_{j = j_i} - P_{ij}^s\right) z_{ij}^s

Vectorized implementation. Define the logit residual vector 𝐝is∈ℝJi\mathbf{d}_i^s \in \mathbb{R}^{J_i} with elements dijs=𝟏j=ji−Pijsd_{ij}^s = \mathbf{1}_{j = j_i} - P_{ij}^s. The per-block gradient components collapse to matrix-vector products — computed once per draw rather than looped over alternatives:

Block Scalar sum Vectorized form
β\beta ∑jdijsXijT\sum_j d_{ij}^s X_{ij}^T XiT𝐝isX_i^T \mathbf{d}_i^s
μp\mu_p ∑jdijsWijp⋅∂μp*/∂μp\sum_j d_{ij}^s W_{ijp} \cdot \partial\mu_p^*/\partial\mu_p (w̃is)p⋅∂μp*/∂μp(\tilde{w}_i^s)_p \cdot \partial\mu_p^*/\partial\mu_p
ℓpq\ell_{pq} ∑jdijsWijp⋅∂γps*/∂γps⋅∂Lpq/∂ℓpq⋅ηqs\sum_j d_{ij}^s W_{ijp} \cdot \partial\gamma_p^{s*}/\partial\gamma_p^s \cdot \partial L_{pq}/\partial\ell_{pq} \cdot \eta_q^s (w̃is)p⋅∂γps*/∂γps⋅∂Lpq/∂ℓpq⋅ηqs(\tilde{w}_i^s)_p \cdot \partial\gamma_p^{s*}/\partial\gamma_p^s \cdot \partial L_{pq}/\partial\ell_{pq} \cdot \eta_q^s

where w̃is=WiT𝐝is∈ℝKw\tilde{w}_i^s = W_i^T \mathbf{d}_i^s \in \mathbb{R}^{K_w} (Wt_diff in the code) is computed once and shared across the μ\mu and LL blocks. The δ\delta block uses an irregular alt-index scatter and is kept as a loop.

Code reference: src/mxlogit.cpp

3.3 Gradient of Simulated Log-Likelihood

Using the identity for the derivative of a logarithm of a sum:

∂∂θlog⁡(1S∑sPijis)=∑sPijis⋅gis∑sPijis \frac{\partial}{\partial \theta} \log\left(\frac{1}{S}\sum_s P_{ij_i}^s\right) = \frac{\sum_s P_{ij_i}^s \cdot g_i^s}{\sum_s P_{ij_i}^s}

The full gradient is:

∇θℓ=∑i=1Nwi⋅∑sPijis⋅gis∑sPijis \nabla_\theta \ell = \sum_{i=1}^{N} w_i \cdot \frac{\sum_s P_{ij_i}^s \cdot g_i^s}{\sum_s P_{ij_i}^s}

Code reference: src/mxlogit.cpp

3.4 Utility Derivatives by Parameter Block

3.4.1 Fixed Coefficients (β\beta)

∂Vijs∂β=XijT \frac{\partial V_{ij}^s}{\partial \beta} = X_{ij}^T

Summing over alternatives: ∑jdijsXijT=XiT𝐝is\sum_j d_{ij}^s X_{ij}^T = X_i^T \mathbf{d}_i^s.

Code reference: src/mxlogit.cpp

3.4.2 Random Coefficient Means (μ\mu)

For Normal distribution: ∂Vijs∂μp=Wijp \frac{\partial V_{ij}^s}{\partial \mu_p} = W_{ijp}

For Log-Normal distribution: ∂Vijs∂μp=Wijp⋅exp⁡(μp) \frac{\partial V_{ij}^s}{\partial \mu_p} = W_{ijp} \cdot \exp(\mu_p)

Both cases are captured by dmu_final_dmu(p) (=1= 1 for normal, =exp⁡(μp)= \exp(\mu_p) for log-normal). Summing over alternatives: gμ=w̃is⊙dmu_final_dmug_\mu = \tilde{w}_i^s \odot \text{dmu\_final\_dmu}.

Code reference: src/mxlogit.cpp

3.4.3 Cholesky Parameters (LL)

Let ℓpq\ell_{pq} denote the parameter for LpqL_{pq} (where p≥qp \geq q).

Step 1: Derivative of LpqL_{pq} with respect to ℓpq\ell_{pq}: ∂Lpq∂ℓpq={Lpp=exp⁡(ℓpp)if p=q (diagonal)1if p>q (off-diagonal) \frac{\partial L_{pq}}{\partial \ell_{pq}} = \begin{cases} L_{pp} = \exp(\ell_{pp}) & \text{if } p = q \text{ (diagonal)} \\ 1 & \text{if } p > q \text{ (off-diagonal)} \end{cases}

Step 2: Derivative of γps\gamma_p^s with respect to ℓpq\ell_{pq}: ∂γps∂ℓpq=∂Lpq∂ℓpq⋅ηqs \frac{\partial \gamma_p^s}{\partial \ell_{pq}} = \frac{\partial L_{pq}}{\partial \ell_{pq}} \cdot \eta_q^s

Note: γps=∑r≤pLprηrs\gamma_p^s = \sum_{r \leq p} L_{pr} \eta_r^s, so only the term with r=qr = q contributes.

Step 3: Derivative of transformed γps*\gamma_p^{s*}:

For Normal distribution: ∂γps*∂γps=1 \frac{\partial \gamma_p^{s*}}{\partial \gamma_p^s} = 1

For Log-Normal distribution: ∂γps*∂γps=exp⁡(γps)=γps* \frac{\partial \gamma_p^{s*}}{\partial \gamma_p^s} = \exp(\gamma_p^s) = \gamma_p^{s*}

Step 4: Chain rule for utility derivative: ∂Vijs∂ℓpq=Wijp⋅∂γps*∂γps⋅∂γps∂ℓpq \frac{\partial V_{ij}^s}{\partial \ell_{pq}} = W_{ijp} \cdot \frac{\partial \gamma_p^{s*}}{\partial \gamma_p^s} \cdot \frac{\partial \gamma_p^s}{\partial \ell_{pq}}

Step 5: Summing over alternatives using w̃is=WiT𝐝is\tilde{w}_i^s = W_i^T \mathbf{d}_i^s: gℓpqs=(w̃is)p⋅∂γps*∂γps⋅∂Lpq∂ℓpq⋅ηqs g_{\ell_{pq}}^s = (\tilde{w}_i^s)_p \cdot \frac{\partial \gamma_p^{s*}}{\partial \gamma_p^s} \cdot \frac{\partial L_{pq}}{\partial \ell_{pq}} \cdot \eta_q^s

For the diagonal case, all KwK_w parameters are computed simultaneously as Wt_diff % dgamma_final_dgamma % L.diag() % eta_i_s. For the full correlated case, Wt_p = Wt_diff(p) * dgamma_final_dgamma(p) is factored out per row pp.

Code reference: src/mxlogit.cpp

3.4.4 Alternative-Specific Constants (δ\delta)

∂Vijs∂δa=𝟏j=a \frac{\partial V_{ij}^s}{\partial \delta_a} = \mathbf{1}_{j = a}

where δ1=0\delta_1 = 0 (normalized) if no outside option, or all δj\delta_j for j≥1j \geq 1 are free if outside option is included. The sum over alternatives is a simple scatter (one non-zero contribution per alternative), implemented as a loop.

Code reference: src/mxlogit.cpp

3.5 Panel Score and Posterior Draw Weights

With person_col, the per-draw score of §3.2 is first summed across a decision maker’s situations, gns=∑t=1Tngntsg_{ns} = \sum_{t=1}^{T_n} g_{nts} (each gntsg_{nts} the situation-level score of §3.2–§3.4), and then averaged over draws with the posterior weight of §2.6 in place of the per-draw probability share of §3.3:

∇θℓn=∑s=1Sωnsgns,∇θℓ=∑n=1Uwn∇θℓn. \nabla_\theta \ell_n = \sum_{s=1}^S \omega_{ns}\, g_{ns}, \qquad \nabla_\theta \ell = \sum_{n=1}^U w_n \nabla_\theta \ell_n.

This is §3.3’s ∑sPijisgis/∑sPijis\sum_s P_{ij_i}^s g_i^s / \sum_s P_{ij_i}^s with Pijis/∑s′Pijis′P_{ij_i}^s / \sum_{s'} P_{ij_i}^{s'} replaced by ωns\omega_{ns} and gisg_i^s replaced by the situation-summed gnsg_{ns}; Tn≡1T_n \equiv 1 recovers §3.3 termwise. The block decomposition of §3.4 (the β\beta/μ\mu/LL BLAS collapse via w̃s\tilde w^s, and the δ\delta scatter) is unchanged within a situation.

Implementation. The double sum over situations and draws needs no explicit loop over tt: mxl_unit_simulate() already stacks the logit residual dntsd_{nt}^s of every row of every situation belonging to unit nn into one Rn×SR_n \times S matrix DiffW (RnR_n = stacked alternative rows across all of nn’s TnT_n situations), because ωns\omega_{ns} is a single per-draw weight shared by every situation in the unit. d‾n=𝙳𝚒𝚏𝚏𝚆ωn\bar d_n = \texttt{DiffW}\,\omega_n then gives the β\beta, μ\mu and δ\delta blocks directly, exactly as §3.2’s vectorized table builds them from 𝐝is\mathbf d_i^s — the situation sum is absorbed into the row-stacking, and the draw sum into the matrix-vector product, in a single pass.

DiffW is formed BB draws at a time, Rn×BR_n \times B, with B=SB = S unless the two Rn×SR_n \times S matrices (DiffW and WnΓnW_n \Gamma_n) would exceed a small per-thread budget (4 MiB, sized to stay in cache): a unit is split into batches (of sizes differing by at most one draw) only when RnS>218R_n S > 2^{18}, i.e. for a long panel, a choice set of hundreds of alternatives or a very large SS, and a thread’s memory then grows with RnR_n rather than RnSR_n S. Each batch is folded in before the next one overwrites it. d‾n\bar d_n accumulates by a streaming max shift: the batch’s weights eλns−re^{\lambda_{ns} - r} are taken relative to the largest λns\lambda_{ns} seen so far. The running sum is rescaled by erold−re^{r_\text{old} - r} whenever rr rises, then divided by ∑seλns−r\sum_s e^{\lambda_{ns} - r} at the end, the same denominator used to normalize ωn\omega_n. Keeping the maximum separate from this denominator avoids losing normalization accuracy through subtraction of a large-magnitude log-sum-exp. Batching changes d‾n\bar d_n only by rounding (about one rounding per batch), and a unit in one batch computes exactly the unbatched values. The LL block cannot collapse through d‾n\bar d_n alone: its per-draw derivative couples the residuals with that draw’s ηns\eta_{ns} (§3.4.3), so the draw sum has to happen after that coupling; each batch writes its columns of Wn′𝙳𝚒𝚏𝚏𝚆W_n'\,\texttt{DiffW} (Kw×SK_w \times S, small whatever RnR_n is), and mxl_unit_score_cont() then forms A=((Wn′𝙳𝚒𝚏𝚏𝚆⊙Dγ1)diag(ωn))ηn′A = \bigl((W_n' \,\texttt{DiffW} \odot D\gamma_1)\,\mathrm{diag}(\omega_n)\bigr)\,\eta_n' (Kw×KwK_w \times K_w; a situation-stacked dgemm for the inner product, then a draw-block dgemm against ηn′\eta_n'), and reads ℓpq\ell_{pq}’s score off ∂Lpq/∂ℓpq⋅A(p,q)\partial L_{pq}/\partial \ell_{pq} \cdot A(p, q). (The Hessian, §4.5, needs an explicit per-situation loop because its outer-product terms cannot be pre-summed across situations before being squared.)

Code reference: mxl_unit_simulate(), mxl_unit_score_cont() (the continuous block, with the AA collapse), mxl_unit_score() and mxl_unit_score_local() (the delta block, over every parameter or the unit’s own, §4.4.6) in src/mxlogit.cpp, shared by mxl_loglik_gradient_parallel, mxl_scores_parallel, mxl_bhhh_parallel and mxl_cluster_meat_parallel.


4. Hessian Computation

4.1 General Formula

Define: - gi=∂log⁡P‾i∂θg_i = \frac{\partial \log \bar{P}_i}{\partial \theta} — the gradient for individual ii - gis=∂log⁡Pijis∂θg_i^s = \frac{\partial \log P_{ij_i}^s}{\partial \theta} — the per-draw gradient - His=∂2log⁡Pijis∂θ∂θ′H_i^s = \frac{\partial^2 \log P_{ij_i}^s}{\partial \theta \partial \theta'} — the per-draw Hessian

The Hessian of the simulated log-probability for individual ii is:

Hi=∂2log⁡P‾i∂θ∂θ′=∑sPijis(gis(gis)′+His)∑sPijis−gigi′ H_i = \frac{\partial^2 \log \bar{P}_i}{\partial \theta \partial \theta'} = \frac{\sum_s P_{ij_i}^s \left(g_i^s (g_i^s)' + H_i^s\right)}{\sum_s P_{ij_i}^s} - g_i g_i'

The full Hessian is:

∇θ2ℓ=∑i=1Nwi⋅Hi \nabla^2_\theta \ell = \sum_{i=1}^{N} w_i \cdot H_i

Code reference: src/mxlogit.cpp

4.2 Per-Draw Hessian

The per-draw Hessian HisH_i^s has the form:

His=−∑jPijszijs(zijs)′+(∑jPijszijs)(∑jPijszijs)′+∑j(𝟏j=ji−Pijs)HV,ijs H_i^s = -\sum_j P_{ij}^s z_{ij}^s (z_{ij}^s)' + \left(\sum_j P_{ij}^s z_{ij}^s\right)\left(\sum_j P_{ij}^s z_{ij}^s\right)' + \sum_j \left(\mathbf{1}_{j=j_i} - P_{ij}^s\right) H_{V,ij}^s

where HV,ijs=∂2Vijs∂θ∂θ′H_{V,ij}^s = \frac{\partial^2 V_{ij}^s}{\partial \theta \partial \theta'} is the Hessian of utility.

In the code, this is computed as: - sum_Pzz =∑jPijszijs(zijs)′= \sum_j P_{ij}^s z_{ij}^s (z_{ij}^s)' - sum_Pz =∑jPijszijs= \sum_j P_{ij}^s z_{ij}^s - sum_diff_H_V =∑j(𝟏j=ji−Pijs)HV,ijs= \sum_j (\mathbf{1}_{j=j_i} - P_{ij}^s) H_{V,ij}^s

Then: His=−sum_Pzz+sum_Pz⋅sum_Pz′+sum_diff_H_VH_i^s = -\text{sum\_Pzz} + \text{sum\_Pz} \cdot \text{sum\_Pz}' + \text{sum\_diff\_H\_V}

Code reference: src/mxlogit.cpp

4.3 Second Derivatives of Utility

Most second derivatives are zero since utility is linear in most parameters. Non-zero second derivatives arise from:

4.3.1 Mean Parameters (μ\mu) — Log-Normal Only

For log-normal distribution: ∂2Vijs∂μp2=Wijp⋅exp⁡(μp) \frac{\partial^2 V_{ij}^s}{\partial \mu_p^2} = W_{ijp} \cdot \exp(\mu_p)

Code reference: src/mxlogit.cpp

4.3.2 Cholesky Diagonal Parameters (ℓpp\ell_{pp})

Since Lpp=exp⁡(ℓpp)L_{pp} = \exp(\ell_{pp}): ∂2Lpp∂ℓpp2=exp⁡(ℓpp)=Lpp \frac{\partial^2 L_{pp}}{\partial \ell_{pp}^2} = \exp(\ell_{pp}) = L_{pp}

For normal distribution: ∂2Vijs∂ℓpp2=Wijp⋅Lpp⋅ηps \frac{\partial^2 V_{ij}^s}{\partial \ell_{pp}^2} = W_{ijp} \cdot L_{pp} \cdot \eta_p^s

For log-normal distribution (combining chain rule): ∂2Vijs∂ℓpp2=Wijp[γps*(∂γps∂ℓpp)2+∂γps*∂γps⋅∂2γps∂ℓpp2] \frac{\partial^2 V_{ij}^s}{\partial \ell_{pp}^2} = W_{ijp} \left[ \gamma_p^{s*} \left(\frac{\partial \gamma_p^s}{\partial \ell_{pp}}\right)^2 + \frac{\partial \gamma_p^{s*}}{\partial \gamma_p^s} \cdot \frac{\partial^2 \gamma_p^s}{\partial \ell_{pp}^2} \right]

where ∂2γps∂ℓpp2=Lpp⋅ηps\frac{\partial^2 \gamma_p^s}{\partial \ell_{pp}^2} = L_{pp} \cdot \eta_p^s.

Code reference: src/mxlogit.cpp

4.3.3 Cholesky Cross-Derivatives Within a Row

Let q≠rq\neq r with q,r≤pq,r\leq p and define

apqs=∂γps∂ℓpq=dpqηqs,dpq={Lppq=p,1q<p. a_{pq}^s=\frac{\partial\gamma_p^s}{\partial\ell_{pq}} =d_{pq}\eta_q^s, \qquad d_{pq}=\begin{cases}L_{pp}&q=p,\\1&q<p.\end{cases}

Distinct Cholesky parameters affect the same transformed coefficient only when they belong to the same row pp. Their cross derivative is

∂2Vijs∂ℓpq∂ℓpr=Wijpfp″(γps)apqsaprs, \frac{\partial^2 V_{ij}^s}{\partial \ell_{pq} \partial \ell_{pr}} =W_{ijp}\,f_p''(\gamma_p^s)\,a_{pq}^s a_{pr}^s,

where fp(γ)=γf_p(\gamma)=\gamma for a normal coefficient and fp(γ)=exp⁡(γ)f_p(\gamma)=\exp(\gamma) for a log-normal coefficient. Hence the cross derivative is zero for normal coefficients and equals Wijpexp⁡(γps)apqsaprsW_{ijp}\exp(\gamma_p^s)a_{pq}^sa_{pr}^s for log-normal coefficients. This expression also covers a diagonal/off-diagonal pair (say q=p,r<pq=p,r<p); restricting both indices to be off-diagonal would omit terms that the C++ Hessian includes. Cross derivatives across different rows of LL are zero. The separate shifted term exp⁡(μp)\exp(\mu_p) implies no μ\mu–LL cross derivative.

Code reference: src/mxlogit.cpp


4.4 Implementation Tricks in mxl_hessian_parallel

The subsections below describe the computational restructuring used by mxl_hessian_parallel. They cover (i) a block decomposition of the score vector and Hessian terms, (ii) buffer accumulation that eliminates the per-draw dense matrix allocation, (iii) BLAS-3 batching of two outer-product sums, (iv) the separation of the draws from the alternatives, which turns every sum over both into matrix products of the situation’s probabilities with small matrices, (v) the symmetry strategy used for final assembly, and (vi) the compact block of the parameters a unit’s likelihood involves, in which (i)–(v) run, and how the blocks are accumulated into the result.

A reader who has never seen the C++ can understand what the function does and why the result is numerically equivalent to the per-draw naive form (§4.1–4.2) from these sections alone.

4.4.1 Parameter Block Layout and Score Vector Decomposition

The parameter vector θ\theta is partitioned into a continuous block of length KcK_c and a delta block (ASCs) of length JdJ_d:

θ=(β,μ,vec(L)⏟continuous, length Kc,δ⏟delta, length Jd),nparams=Kc+Jd. \theta = \bigl(\underbrace{\beta,\; \mu,\; \mathrm{vec}(L)}_{\text{continuous, length } K_c},\; \underbrace{\delta}_{\text{delta, length } J_d}\bigr), \qquad n_\text{params} = K_c + J_d.

The boundary index Kc=Kx+(𝟏rc_mean⋅Kw)+L_sizeK_c = K_x + (\mathbf{1}_{\mathrm{rc\_mean}} \cdot K_w) + L\_\text{size} is stored as idx_delta_start in the code.

The per-draw score vector gis∈ℝnparamsg_{is} \in \mathbb{R}^{n_\text{params}} inherits this partition: gis=[gis,c;gis,d]g_{is} = [g_{is,c};\; g_{is,d}]. The continuous sub-vector is

gis,c=∑adiaszc,ias,dias=𝟏a=ji−Pias, g_{is,c} = \sum_{a} d_{ia}^s\, z_{c,ia}^s, \qquad d_{ia}^s = \mathbf{1}_{a = j_i} - P_{ia}^s,

where zc,ias∈ℝKcz_{c,ia}^s \in \mathbb{R}^{K_c} is the continuous-block utility gradient for alternative aa at draw ss. The delta sub-vector scatters logit residuals diasd_{ia}^s into the ASC index for each alternative.

Define also: - sum_Pzis=∑aPiaszc,ias∈ℝKc\mathrm{sum\_Pz}_{is} = \sum_a P_{ia}^s z_{c,ia}^s \in \mathbb{R}^{K_c} (the cc weighted-score sum, plus a JdJ_d-length delta counterpart sum_Pz_dis\mathrm{sum\_Pz\_d}_{is}), - sum_Pzzcc,is=∑aPiaszc,ias(zc,ias)⊤\mathrm{sum\_Pzz}_{cc,is} = \sum_a P_{ia}^s z_{c,ia}^s (z_{c,ia}^s)^\top (Kc×KcK_c \times K_c, symmetric), and its cd (Kc×JdK_c \times J_d) and dd (diagonal JdJ_d-vector) sub-blocks.

4.4.2 Block Buffer Accumulation — Identity C

Two of the three terms in HisH_{is} (§4.2) are linear in the draw weight PisP_{is}:

−sum_Pzzisand+sum_diff_H_Vis. -\mathrm{sum\_Pzz}_{is} \quad \text{and} \quad +\mathrm{sum\_diff\_H\_V}_{is}.

Their draw-weighted sum can therefore be accumulated directly into per-individual buffer matrices without constructing the full nparams×nparamsn_\text{params} \times n_\text{params} dense matrix HisH_{is} at each draw:

buf_Pzz=∑s=1SPis⋅sum_Pzzis,buf_diff_HV=∑s=1SPis⋅sum_diff_HVis. \mathrm{buf\_Pzz} = \sum_{s=1}^{S} P_{is} \cdot \mathrm{sum\_Pzz}_{is}, \qquad \mathrm{buf\_diff\_HV} = \sum_{s=1}^{S} P_{is} \cdot \mathrm{sum\_diff\_HV}_{is}.

This replaces the original path that assembled the full HisH_{is} at every draw and then computed hess_term1_numerator += P_s * (g_is * g_isᵀ + H_is). The per-draw blocks sum_Pzzis\mathrm{sum\_Pzz}_{is} and sum_diff_HVis\mathrm{sum\_diff\_HV}_{is} are not formed either: §4.4.4 reads every buffer off two matrix products per situation, except for a likelihood unit outside that section’s range, whose blocks are formed as here.

The block structure of buf_Pzz minimizes memory by storing only the three structurally distinct sub-blocks:

Buffer Shape Content Notes
buf_Pzz_cc Kc×KcK_c \times K_c ∑sPissum_Pzzcc,s\sum_s P_{is}\,\mathrm{sum\_Pzz}_{cc,s} Symmetric
buf_Pzz_cd Kc×JdK_c \times J_d ∑sPissum_Pzzcd,s\sum_s P_{is}\,\mathrm{sum\_Pzz}_{cd,s} Rectangular; not symmetric
buf_Pzz_dd JdJ_d-vector ∑sPissum_Pzzdd,s\sum_s P_{is}\,\mathrm{sum\_Pzz}_{dd,s} Diagonal only
buf_diff_HV_cc Kc×KcK_c \times K_c ∑sPissum_diff_HVs\sum_s P_{is}\,\mathrm{sum\_diff\_HV}_{s} Symmetric; cc block only (HVH_V has no delta rows)

All four buffers are thread-private scratch, zeroed at the start of each individual ii. In the code the cd and dd buffers hold only the constants that ii’s likelihood involves (§4.4.6), so they grow only to the largest such block their thread meets.

4.4.3 BLAS-3 Batching of Outer-Product Sums — Identities A and B

The third term in HisH_{is} — +sum_Pzissum_Pzis⊤+\mathrm{sum\_Pz}_{is}\,\mathrm{sum\_Pz}_{is}^\top — is not linear in PisP_{is}, so it cannot go into a linear buffer. Instead, together with the OPG term ∑sPisgisgis⊤\sum_s P_{is} g_{is} g_{is}^\top, it is handled by reformulating both weighted outer-product sums as a single BLAS level-3 matrix multiply.

Identity A (OPG batching). With the normalized draw weights ωis=Pis/P̂i\omega_{is} = P_{is} / \hat P_i, P̂i=∑sPis\hat P_i = \sum_s P_{is}, and the weighted mean score gi=∑sωisgisg_i = \sum_s \omega_{is} g_{is}, define the nparams×Sn_\text{params} \times S matrix GG with columns

G[:,s]=ωis(gis−gi). G_{[:,s]} = \sqrt{\omega_{is}}\; (g_{is} - g_i).

Then

∑s=1Sωisgisgis⊤−gigi⊤=GG⊤. \sum_{s=1}^{S} \omega_{is}\, g_{is} g_{is}^\top - g_i g_i^\top = G G^\top.

Proof. GG⊤=∑sωis(gis−gi)(gis−gi)⊤=∑sωisgisgis⊤−gigi⊤G G^\top = \sum_s \omega_{is} (g_{is} - g_i)(g_{is} - g_i)^\top = \sum_s \omega_{is} g_{is} g_{is}^\top - g_i g_i^\top, because ∑sωis=1\sum_s \omega_{is} = 1. The identity requires only ωis≥0\omega_{is} \geq 0, which holds because PisP_{is} is a softmax probability (guaranteed by stable_softmax). It folds the OPG term and the final −gigi⊤- g_i g_i^\top of the Hessian (§4.4.5) into one product, and the centered form is also the numerically stable one. The implementation normalizes max-shifted exponentials directly, then subtracts the highest-weight draw’s score before computing the weighted mean of the deviations. This prevents a large common score from amplifying rounding in the weights into spurious covariance.

Identity B (sum-Pz outer-product batching). Define the nparams×Sn_\text{params} \times S matrix FF with columns

F[:,s]=ωis[sum_Pz_cis;sum_Pz_dis]. F_{[:,s]} = \sqrt{\omega_{is}}\;\bigl[\mathrm{sum\_Pz\_c}_{is};\;\mathrm{sum\_Pz\_d}_{is}\bigr].

Then

∑s=1Sωissum_Pzissum_Pzis⊤=FF⊤, \sum_{s=1}^{S} \omega_{is}\;\mathrm{sum\_Pz}_{is}\,\mathrm{sum\_Pz}_{is}^\top = F F^\top,

by the same expansion without the centering. The linear buffers of §4.4.2 are likewise accumulated with ωis\omega_{is} in place of PisP_{is}, which divides them by P̂i\hat P_i once and for all.

Combined BLAS-3 form. Concatenating GG and FF into [G∣F]∈ℝnparams×2S[G \mid F] \in \mathbb{R}^{n_\text{params} \times 2S} and forming one matrix product gives

[G∣F][G∣F]⊤=GG⊤+FF⊤, [G \mid F]\;[G \mid F]^\top = G G^\top + F F^\top,

because [G∣F]⊤=[G⊤;F⊤][G \mid F]^\top = [G^\top;\; F^\top] and the block multiply sums over 2S2S columns with no GG-FF cross term. This is the form implemented in mxl_hessian_parallel: G_stash and F_stash are filled column by column from the products of §4.4.4, and opg_pz accumulates FF⊤F F^\top and GG⊤G G^\top as two separate rank-SS BLAS level-3 updates rather than one join_rows product (§4.5 explains why: with person_col, FF is squared and added once per situation while GG accumulates across situations and is squared once per unit; the two coincide here because §4.4 is the single-situation, Tn≡1T_n \equiv 1 case). The result is ∑sPis(gisgis⊤+sum_Pzissum_Pzis⊤)\sum_s P_{is}(g_{is} g_{is}^\top + \mathrm{sum\_Pz}_{is}\,\mathrm{sum\_Pz}_{is}^\top) in cache-friendly level-3 kernels.

Floating-point precision. Changing the summation order (per-draw increments vs. one level-3 multiply) is a reassociation of additions that is exact in real arithmetic. In floating-point the results differ only by rounding. Across 10 equivalence scenarios sweeping all combinations of rc_dist, rc_correlation, and rc_mean, the worst-case element-wise deviation from the per-draw form was 4.588×10−134.588 \times 10^{-13}, several orders of magnitude below the 10−610^{-6} tolerance enforced by the package’s equivalence tests.

4.4.4 Separating the Draws from the Alternatives — Identity D

Identities A–C still sum over every alternative aa and draw ss of the situation. Formed term by term, buf_Pzz_cc alone takes a Kc×KcK_c \times K_c rank-one update per alternative and draw, O(JiSKc2)O(J_i S K_c^2) operations per situation, and these updates dominated the kernel: on a school-census design, removing them halved its time with ten fixed coefficients (Kc=19K_c = 19) and cut three quarters of it with thirty. The derivatives of utility have a structure that turns every such double sum into matrix products of the situation’s Ji×SJ_i \times S probability matrix with small matrices, so that what remains per alternative and draw is the softmax that forms the probabilities.

The factorization. Split the continuous part of the utility gradient (§3.4) into the coefficients whose derivatives do not depend on the draw and the Cholesky parameters:

zc,ias=(zia0,ζias),zia0=(Xia,Wia⊙μ*′)∈ℝK0,ζia,rs=Wiapeirs,eirs=fp′(γips)airs, z^s_{c,ia} = \bigl(z^0_{ia},\; \zeta^s_{ia}\bigr), \qquad z^0_{ia} = \bigl(X_{ia},\; W_{ia} \odot \mu^{*\prime}\bigr) \in \mathbb{R}^{K_0}, \qquad \zeta^s_{ia,r} = W_{iap}\; e^s_{ir}, \qquad e^s_{ir} = f_p'(\gamma^s_{ip})\, a^s_{ir},

where K0=Kx+𝟏rc_meanKwK_0 = K_x + \mathbf{1}_{\mathrm{rc\_mean}} K_w, μp*′=∂μp*/∂μp\mu^{*\prime}_p = \partial\mu^*_p/\partial\mu_p (§3.4.2), r=(p,q)r = (p, q) indexes the Cholesky parameter ℓpq\ell_{pq}, and airs=(∂Lpq/∂ℓpq)ηiqsa^s_{ir} = (\partial L_{pq}/\partial\ell_{pq})\,\eta^s_{iq} as in §4.3.3; eirse^s_{ir} is the derivative of the transformed deviation γips*\gamma^{s*}_{ip} in ℓpq\ell_{pq} (§3.4.3, Steps 1–3). The constants’ part of zz is an indicator. So z0z^0 and the constants do not depend on the draw, and each Cholesky entry of zz is a covariate of the alternative, WiapW_{iap}, times a factor of the draw, eirse^s_{ir}. The second derivatives of utility (§4.3) factor the same way: they are nonzero only at (μp,μp)(\mu_p, \mu_p), where they equal Wiapμp*′′W_{iap}\,\mu^{*\prime\prime}_p, and between two Cholesky parameters r=(p,q)r = (p, q) and r′=(p,q′)r' = (p, q') of one row, where they equal Wiaphi,rr′sW_{iap}\, h^s_{i,rr'} with

hi,rr′s=fp″(γips)airsair′s+𝟏q=q′fp′(γips)∂2Lpq∂ℓpq2ηiqs. h^s_{i,rr'} = f_p''(\gamma^s_{ip})\, a^s_{ir}\, a^s_{ir'} + \mathbf{1}_{q = q'}\, f_p'(\gamma^s_{ip})\, \frac{\partial^2 L_{pq}}{\partial\ell_{pq}^2}\, \eta^s_{iq}.

Two products per situation. Every term of Identity C has the form ∑sωs∑aPiasϕaψs\sum_s \omega_s \sum_a P^s_{ia}\, \phi_a\, \psi_s: a covariate ϕ\phi of the alternative times a factor ψ\psi of the draw (with the residual diasd^s_{ia} in place of PiasP^s_{ia} for HVH_V), and every entry of the columns of FF and GG that Identities A and B multiply out is such a sum at one draw, ∑aPiasϕaψs\sum_a P^s_{ia}\, \phi_a\, \psi_s (with the residuals for GG). A sum over both can be taken over the alternatives first or over the draws first,

∑sωs∑aPiasϕaψs=∑aϕa(Pi(ω⊙ψ))a=∑sωsψs(ϕ⊤Pi)s, \sum_s \omega_s \sum_a P^s_{ia}\, \phi_a\, \psi_s = \sum_a \phi_a \bigl(P_i\, (\omega \odot \psi)\bigr)_a = \sum_s \omega_s\, \psi_s\, \bigl(\phi^\top P_i\bigr)_s,

where PiP_i is the matrix of the probabilities PiasP^s_{ia}, a row per alternative, the outside option’s included when there is one (its covariates are zero, so it drops out of every sum until they are centered below). Collect the covariates in Zi=[Xi∣Wi∣Wi(2)]Z_i = [X_i \mid W_i \mid W^{(2)}_i], whose last block holds the products WiapWiap′W_{iap}W_{iap'} for p≤p′p \le p', and the draw factors in Eω=[ω∣ω⊙er]E_\omega = [\omega \mid \omega \odot e_r], an S×(1+nL)S \times (1 + n_L) matrix for nLn_L Cholesky parameters. Two products per situation then give every sum:

Qi=Zi⊤Pi,Ri=PiEω. Q_i = Z_i^\top P_i, \qquad R_i = P_i\, E_\omega.

The columns of QiQ_i are the sums over the alternatives at each draw: ∑aPiasXia\sum_a P^s_{ia} X_{ia}, ∑aPiasWia\sum_a P^s_{ia} W_{ia} and Yi,pp′s=∑aPiasWiapWiap′Y^s_{i,pp'} = \sum_a P^s_{ia} W_{iap} W_{iap'}. The rows of RiR_i are the sums over the draws for each alternative: its posterior probability πia=∑sωsPias\pi_{ia} = \sum_s \omega_s P^s_{ia} and Aia,r=∑sωsPiaseirsA_{ia,r} = \sum_s \omega_s P^s_{ia}\, e^s_{ir}. The residuals need no product of their own: ∑adiasXia=Xiji−∑aPiasXia\sum_a d^s_{ia} X_{ia} = X_{ij_i} - \sum_a P^s_{ia} X_{ia} and

bips=∑adiasWiap=Wijip−∑aPiasWiap b^s_{ip} = \sum_a d^s_{ia}\, W_{iap} = W_{ij_ip} - \sum_a P^s_{ia} W_{iap}

are rows of QiQ_i less the chosen alternative’s covariates (nothing is subtracted from when the outside option is chosen); bib_i is the Kw×SK_w \times S matrix Wi⊤𝙳𝚒𝚏𝚏𝚆W_i^\top\texttt{DiffW} of §3.5.

Identity C, block by block, is then

buf_Pzz00=∑aπiazia0zia0⊤,(buf_Pzz0ζ)⋅r=∑aWiapAia,rzia0,(buf_Pzzζζ)rr′=∑sωseirseir′sYi,p(r)p(r′)s, \mathrm{buf\_Pzz}_{00} = \sum_a \pi_{ia}\, z^0_{ia} z^{0\top}_{ia}, \qquad \bigl(\mathrm{buf\_Pzz}_{0\zeta}\bigr)_{\cdot r} = \sum_a W_{iap}\, A_{ia,r}\; z^0_{ia}, \qquad \bigl(\mathrm{buf\_Pzz}_{\zeta\zeta}\bigr)_{rr'} = \sum_s \omega_s\, e^s_{ir}\, e^s_{ir'}\; Y^s_{i,p(r)p(r')},

with pp the row of rr in the second, the column of a constant kk in the cd block, ∑a→k(πiazia0,(WiapAia,r)r)\sum_{a \to k} \bigl(\pi_{ia}\, z^0_{ia},\; (W_{iap}\, A_{ia,r})_r\bigr), and its dd entry ∑a→kπia\sum_{a \to k} \pi_{ia} (sums over the rows aa that list alternative kk); and

(buf_diff_HV)μpμp=μp*′′∑sωsbips,(buf_diff_HV)rr′=∑sωshi,rr′sbips(r, r′ in row p). \bigl(\mathrm{buf\_diff\_HV}\bigr)_{\mu_p\mu_p} = \mu^{*\prime\prime}_p \sum_s \omega_s\, b^s_{ip}, \qquad \bigl(\mathrm{buf\_diff\_HV}\bigr)_{rr'} = \sum_s \omega_s\, h^s_{i,rr'}\, b^s_{ip} \quad \text{($r$, $r'$ in row $p$)}.

The blocks with at most one Cholesky index sum over the draws first (through RiR_i); the (ζ,ζ)(\zeta, \zeta) block sums over the alternatives first, since its covariate products number Kw(Kw+1)/2K_w(K_w + 1)/2 where its draw products erer′e_r e_{r'} number nL(nL+1)/2n_L(n_L + 1)/2; and HVH_V, whose sum over the alternatives is bb, does too.

Identities A and B read their columns off QiQ_i as well. Column ss of FF (§4.4.3) is

ωs(∑aPiasXia,μ*′⊙∑aPiasWia,(eirs∑aPiasWiap)r,(∑a→kPias)k), \sqrt{\omega_s}\,\Bigl(\textstyle\sum_a P^s_{ia} X_{ia},\;\; \mu^{*\prime} \odot \sum_a P^s_{ia} W_{ia},\;\; \bigl(e^s_{ir} \sum_a P^s_{ia} W_{iap}\bigr)_r,\;\; \bigl(\sum_{a \to k} P^s_{ia}\bigr)_k\Bigr),

and the score gisg_{is} in column ss of GG is the same with the residuals for the probabilities, (Xiji−∑aPiasXia,μ*′⊙bis,(eirsbips)r,(𝟏ji→k−∑a→kPias)k)\bigl(X_{ij_i} - \sum_a P^s_{ia}X_{ia},\; \mu^{*\prime} \odot b^s_i,\; (e^s_{ir}\, b^s_{ip})_r,\; (\mathbf{1}_{j_i \to k} - \sum_{a \to k} P^s_{ia})_k\bigr). FF⊤F F^\top and GG⊤G G^\top are formed as in §4.4.3.

Cost. Per situation the two products take O(JiS(Kx+Kw+Kw(Kw+1)/2+1+nL))O\bigl(J_i S\,(K_x + K_w + K_w(K_w + 1)/2 + 1 + n_L)\bigr) operations, against O(JiSKc2)O(J_i S K_c^2) for the rank-one updates they replace, and the per-draw vectors of §4.4.1 are never formed. The blocks read off the products cost O(JiK0(K0+nL))O(J_i K_0 (K_0 + n_L)) per situation and O(SnL2)O(S\, n_L^2) per likelihood unit, and Identities A and B keep their products (§4.4.6).

Centering. Identities B and C nearly cancel: at each draw, −∑aPiasziaszias⊤+(∑aPiaszias)(∑aPiaszias)⊤-\sum_a P^s_{ia} z^s_{ia} z^{s\top}_{ia} + \bigl(\sum_a P^s_{ia} z^s_{ia}\bigr) \bigl(\sum_a P^s_{ia} z^s_{ia}\bigr)^\top is minus the covariance of zz across the situation’s alternatives, which is small next to either term when the covariates share a large common level or the probabilities are nearly degenerate; and the two products of this subsection associate their sums differently, so that, formed from the levels, their rounding errors would not cancel with them. The covariance, and the scores, are unchanged when every alternative’s covariates are shifted by one vector cic_i, the outside option’s included (its covariates are zero, so it takes −ci-c_i): the probabilities sum to one and the residuals to zero,

∑aPias(za−c)(za−c)⊤−(∑aPias(za−c))(∑aPias(za−c))⊤=∑aPiaszaza⊤−(∑aPiasza)(∑aPiasza)⊤,∑adias(za−c)=∑adiasza, \sum_a P^s_{ia}(z_a - c)(z_a - c)^\top - \Bigl(\sum_a P^s_{ia}(z_a - c)\Bigr) \Bigl(\sum_a P^s_{ia}(z_a - c)\Bigr)^\top = \sum_a P^s_{ia} z_a z_a^\top - \Bigl(\sum_a P^s_{ia} z_a\Bigr)\Bigl(\sum_a P^s_{ia} z_a\Bigr)^\top, \qquad \sum_a d^s_{ia}(z_a - c) = \sum_a d^s_{ia} z_a,

with the sums over every alternative. A shift of WiW_i by cwc_w shifts each Cholesky entry ζia,rs\zeta^s_{ia,r} by cw,peirsc_{w,p} e^s_{ir}, the same for every alternative at a draw, so the identities hold for it as well. The kernel shifts XiX_i and WiW_i by the covariates of the alternative with the largest posterior probability πia\pi_{ia} (by zero when that is the outside option) before forming ZiZ_i, so that both products work on the covariates’ deviations rather than their levels.

Floating point. The identities hold in real arithmetic and change only the order of the additions. Judged against a 60-digit evaluation of the per-draw formulas (§4.1–§4.3) on 137 Hessians of the package’s reference battery, the largest normwise error, max⁡|H−H*|/max⁡(1,max⁡|H*|)\max|H - H^*| / \max(1, \max|H^*|), was 4.7×10−154.7 \times 10^{-15} on designs with columns of ordinary scale, 4.0×10−134.0 \times 10^{-13} on designs with columns rescaled by factors between 10−410^{-4} and 350350, and 3.4×10−163.4 \times 10^{-16} with columns rescaled by 10±710^{\pm 7} (nearly degenerate probabilities); the per-alternative accumulation it replaces erred by 3.7×10−123.7 \times 10^{-12}, 5.9×10−115.9 \times 10^{-11} and 2.3×10−52.3 \times 10^{-5} on the same Hessians, the last in entries whose exact value is below 10−10010^{-100}.

Range. Identity D takes the utility’s derivatives apart. A Cholesky entry ζia,rs=Wiapeirs\zeta^s_{ia,r} = W_{iap}\, e^s_{ir} and a mean’s derivative Wiapμp*′W_{iap}\, \mu^{*\prime}_p are products, and the blocks above multiply their factors separately (WiapWiap′W_{iap} W_{iap'} in ZiZ_i; eirseir′se^s_{ir} e^s_{ir'} and μp*′μp′*′\mu^{*\prime}_p \mu^{*\prime}_{p'} after the sums), where the per-alternative formula of §4.4.1–§4.4.2 squares the derivatives themselves. A factor can then pass the double range (about 1.3×101541.3 \times 10^{154} before it is squared) where the derivative does not: a log-normal draw factor of 1017610^{176} against covariates that are zero, whose exact terms are zero rather than ∞⋅0\infty \cdot 0; covariates of 1016010^{160} against Cholesky entries of 10−16010^{-160}; a mean derivative of 1016010^{160} against covariates of 10−16010^{-160}; covariates of −10308-10^{308} and 1030810^{308}, whose difference, the centering, overflows by itself; or covariates of 2−5402^{-540} against draw factors of 2402^{40} (about 1.1×10121.1 \times 10^{12}), whose products WiapWiap′W_{iap} W_{iap'} underflow to zero where the derivatives’ squares, near 10−30110^{-301}, do not. Information that small still matters: it can be strongly correlated with that of a parameter of ordinary size, so that dropping it, or reversing its sign, moves that parameter’s standard error. Scaling a unit’s covariates by one power of two does not mend this: in a panel, one task’s large covariates would push another task’s ordinary ones below the double range.

The kernel therefore checks each likelihood unit before forming it. It uses Identity D when every nonzero covariate, draw factor eirse^s_{ir} and mean derivative μp*′\mu^{*\prime}_p is at least 2−1282^{-128} (about 2.9×10−392.9 \times 10^{-39}) in magnitude, every |eirs||e^s_{ir}| and |μp*′||\mu^{*\prime}_p| at most 2642^{64} (about 1.8×10191.8 \times 10^{19}), and every covariate’s spread over a situation’s alternatives (its maximum less its minimum, the outside option’s zeros included) at most 2642^{64}, all of them finite. A covariate’s common level, however large, spreads nothing. The centered covariates are then zero or between 2−1802^{-180} and 2642^{64}, so no product overflows, and one can fall below the normal range (2−10222^{-1022}) where the derivatives’ products would not only at an alternative and draw whose probability times draw weight is below 2−5342^{-534}. A term lost there is below 2−8302^{-830}, and every term formed there below 2−2782^{-278}: a parameter whose information comes from such alternatives alone has a standard error beyond 103010^{30}, and its correlation with any parameter of ordinary information is negligible.

A unit outside that range is formed by the per-alternative formula instead, which forms the utility’s derivatives before any product and so keeps their range. Its arithmetic is that formula’s, in its order, with three kinds of term left out where exact arithmetic leaves them out and the formula’s would not be finite, or not zero. A draw of weight zero adds nothing to Identity C or HVH_V, as in Identity D. Where fp″f''_p is zero (a normal coefficient, or a log-normal draw whose eγe^{\gamma} underflowed), the diagonal factor hi,rrs=fp″(apps)2+fp′Lppηpsh^s_{i,rr} = f''_p\,(a^s_{pp})^2 + f'_p\, L_{pp}\, \eta^s_p (§4.3.2) has no first term, which would be 0⋅∞0 \cdot \infty once appsa^s_{pp} passes about 1015410^{154} (Identity D’s hh leaves it out too). And a covariate with one value on all of a situation’s alternatives adds nothing to that situation: it moves no probability there, so its terms cancel exactly. HVH_V forms none of its curvature factors there, which can overflow against a covariate that is zero (with or without an outside option), giving 0⋅∞0 \cdot \infty where the exact term is zero. Without an outside option, a finite, nonzero value also enters as zero, as Identity D’s centering makes it: summed from its level, its terms leave rounding, enough to give a covariate that varies within no situation a nonzero row of the Hessian and a finite standard error. Elsewhere it repeats that formula bit for bit: with every unit sent to it, 1,683 of the 1,700 Hessians that main’s tests compute equal main’s, and the other 17, each with a situation where a covariate has one value (in 16, a situation with one alternative), differ from them by at most 6×10−166 \times 10^{-16} in the measure above.

Scores that cancel. Both formulas, like the kernel before them, weight HVH_V by the residuals diasd^s_{ia} of rounded probabilities: through the sums bipsb^s_{ip} above, or one alternative at a time. Where a unit’s situations mirror one another, the residuals’ sums over its situations cancel at leading order, leaving a term of first order in the random coefficients’ contributions to utility. Two binary situations with covariates 00 and ww on their two alternatives, chosen differently, where the rest of the utility ties the alternatives, give b1ps+b2ps=−tanh⁡(vs/2)wb^s_{1p} + b^s_{2p} = -\tanh(v_s/2)\, w, with vsv_s the random coefficients’ utility difference at draw ss. What is left of HVH_V, tanh⁡(vs/2)∂2vs/∂ℓ2\tanh(v_s/2)\, \partial^2 v_s / \partial\ell^2 in the information, is of the same order in vsv_s as the rest of that parameter’s information, 2P(1−P)(∂vs/∂ℓ)22 P (1 - P) (\partial v_s / \partial\ell)^2, and can be as large, and through that parameter’s correlation with a fixed coefficient it moves the fixed coefficient’s standard error. Formed from probabilities rounded near 1/21/2, it carries a relative error of about 10−16/|vs|10^{-16} / |v_s|, and once |vs||v_s| is below about 10−1610^{-16} it is lost: Identity D drops it, and the per-alternative formula leaves in its place a rounding residue of either sign, far larger than the term. The problem is ill-conditioned there. In such a panel with vsv_s near 3×10−1473 \times 10^{-147} (covariates of 2−5402^{-540} against a log-normal coefficient near e37e^{37}), the fixed coefficient’s standard error is 1.81, and moving one situation’s covariate by one unit in the last place makes it 1.41, or leaves the information indefinite (exact arithmetic, at 1,300 digits). At these draws the kernel gives 3.09, the standard error without the term; at nearby draws the per-alternative formula also gives 1.41, or an information that is not positive definite. Each is what exact arithmetic gives for a covariate less than one unit in the last place from the given one. With |vs||v_s| near 10−810^{-8} the kernel gives 1.81 to eight digits.

4.4.5 Final Assembly and Symmetry

After the products of §4.4.3–§4.4.4 for individual ii, let opg_pz=[G∣F][G∣F]⊤\mathrm{opg\_pz} = [G \mid F][G \mid F]^\top, with the weights of §4.4.3. The Hessian ∑sωis(His+(gis−gi)(gis−gi)⊤)\sum_s \omega_{is}(H_{is} + (g_{is} - g_i)(g_{is} - g_i)^\top) is assembled block-by-block into hess_t1 (the code builds it in place in opg_pz, reading each block before overwriting it):

hess_t1|cc=opg_pzcc−buf_Pzz_cc+buf_diff_HV_cc, \mathrm{hess\_t1}\big|_{\mathrm{cc}} = \mathrm{opg\_pz}_{\mathrm{cc}} - \mathrm{buf\_Pzz\_cc} + \mathrm{buf\_diff\_HV\_cc},

hess_t1|cd=opg_pzcd−buf_Pzz_cd,hess_t1|dc=(hess_t1|cd)⊤, \mathrm{hess\_t1}\big|_{\mathrm{cd}} = \mathrm{opg\_pz}_{\mathrm{cd}} - \mathrm{buf\_Pzz\_cd}, \qquad \mathrm{hess\_t1}\big|_{\mathrm{dc}} = \bigl(\mathrm{hess\_t1}|_{\mathrm{cd}}\bigr)^\top,

hess_t1|dd=opg_pzdd−diag(buf_Pzz_dd). \mathrm{hess\_t1}\big|_{\mathrm{dd}} = \mathrm{opg\_pz}_{\mathrm{dd}} - \mathrm{diag}(\mathrm{buf\_Pzz\_dd}).

The cd block is rectangular (Kc×JdK_c \times J_d) and not symmetric, so both the cd and dc sub-matrices are written explicitly into hess_t1. The normalized draw weight ωis≡Pis/P̂i\omega_{is} \equiv P_{is}/\hat P_i replaces a separate division by P̂i\hat P_i, and the centered GG of Identity A already carries −gigi⊤- g_i g_i^\top, matching §4.5’s Louis-identity assembly:

Hi=hess_t1,result+=wiHi(upper triangle, §4.4.6). H_i = \mathrm{hess\_t1}, \qquad \text{result} \mathrel{+}= w_i H_i \quad \text{(upper triangle, §4.4.6)}.

Symmetry strategy. HiH_i is exactly symmetric in floating point, not only in exact arithmetic: GG⊤G G^\top and FtFt⊤F_t F_t^\top are symmetric products, each block of buf_Pzz_cc and buf_diff_HV_cc is formed on its upper triangle and mirrored, and the dc block is written as the transpose of the cd block. So only the upper triangle of wiHiw_i H_i is added into the result, which is mirrored once after the parallel region (§4.4.6); the mirror is the sum the lower triangle would have accumulated.

4.4.6 Compact Blocks and Accumulation

A likelihood unit’s score and Hessian are zero outside the continuous block and the constants of the alternatives the unit faced (unit uu: a decision maker, or a choice situation in the cross-section, §1.6). Let Jd,uJ_{d,u} be the number of free ASCs among the alternatives in uu’s choice situations (the normalized reference has none without an outside option, and the outside option has none), and Jd,t≤Jd,uJ_{d,t} \le J_{d,u} the number in situation tt. Everything in §4.4.1–§4.4.5 runs in uu’s compact block of Kc+Jd,uK_c + J_{d,u} local parameters, the continuous parameters first and then uu’s free ASCs in increasing global order, so that each JdJ_d-sized piece there is restricted to the constants uu’s likelihood involves. Each situation has its own slots as well: the per-situation pieces (the probability sums ∑a→kPtas\sum_{a \to k} P^s_{ta} and FtF_t) are formed on its Kc+Jd,tK_c + J_{d,t} parameters and added into uu’s slots, and an alternative listed twice in a situation adds into one slot, as the dense scatter did. The product GG⊤G G^\top then costs O(S(Kc+Jd,u)2)O(S\,(K_c+J_{d,u})^2) per unit and each FtFt⊤F_t F_t^\top costs O(S(Kc+Jd,t)2)O(S\,(K_c+J_{d,t})^2) per situation, instead of O(Snparams2)O(S\,n_\text{params}^2): on a school-census design with 2,620 parameters, a student’s likelihood involves 59 of them on average.

wuHuw_u H_u is added into the nparams×nparamsn_\text{params} \times n_\text{params} result at its global indices, upper triangle only (§4.4.5). With one thread the blocks are added into the result itself. With several, each thread adds into its own packed upper triangle while the threads’ triangles, nparams(nparams+1)/2n_\text{params}(n_\text{params}+1)/2 doubles each, fit in 2 GiB together (up to about 7,000 parameters at eleven threads), and the triangles are added up in thread order. Above that the threads share the result: each keeps private only the rows of the KcK_c continuous parameters, which every unit touches, and adds the ASC block atomically. The two differ only in the order of the additions. Per-thread triangles are kept where they fit because they need no atomic additions: at census scale with eleven threads, the clustered meat of 200,000 students in 2,000 clusters (§9.5) took 1.37 s with them and 1.82 s with a shared result, and the BHHH matrix of 200,000 students 1.23 s and 1.32 s; the Hessian took the same time either way, and only with two draws, where accumulation dominates, was the shared result faster (the BHHH matrix in 0.47 s against 0.52 s). The BHHH kernel’s rank-one updates wugugu⊤w_u g_u g_u^\top (§4.1’s score) use the same blocks and accumulators, and the cluster meat’s update with each cluster’s summed weighted scores (§9.5) the union of its units’ blocks.

Non-finite terms spread as the dense products spread them: a non-finite row of FtF_t or of the centered GG makes the rows and columns of the parameters concerned NaN in the result (0⋅NaN0 \cdot \mathrm{NaN}), and a non-finite weight makes every entry outside the unit’s block NaN (w⋅0w \cdot 0; NA for a missing weight), or, with several such units, every entry outside the intersection of their blocks. A draw whose weight ωis\omega_{is} is zero (its likelihood underflows next to the unit’s largest) is left out of the per-unit sums of §4.4.4’s (ζ,ζ)(\zeta, \zeta) and HVH_V blocks, as in exact arithmetic, because their draw factors eirseir′se^s_{ir} e^s_{ir'} and hi,rr′sh^s_{i,rr'} may overflow at such a draw; it still enters FtF_t and GG, so a draw factor eirse^s_{ir} that is itself non-finite makes its parameters’ rows and columns NaN by the rule above.

4.5 Panel Hessian: the Louis (1982) Identity and Its Two-Pass Assembly

With person_col, §4.1’s per-situation Hessian generalizes to decision maker nn by the Louis (1982) missing-information identity, treating the draw ss as missing data and nn’s choices as the observed data:

Hn=∇θ2ℓn=∑s=1Sωns(Hns+gnsgns⊤)−g‾ng‾n⊤,g‾n=∑s=1Sωnsgns,∇θ2ℓ=∑n=1UwnHn, H_n = \nabla^2_\theta \ell_n = \sum_{s=1}^S \omega_{ns}\bigl(H_{ns} + g_{ns} g_{ns}^\top\bigr) - \bar g_n \bar g_n^\top, \qquad \bar g_n = \sum_{s=1}^S \omega_{ns}\, g_{ns}, \qquad \nabla^2_\theta \ell = \sum_{n=1}^U w_n H_n,

with gns=∑tgntsg_{ns} = \sum_t g_{nts} as in §3.5 and Hns=∑tHntsH_{ns} = \sum_t H_{nts} the situation-summed per-draw Hessian of §4.2. Because ωns\omega_{ns} is already normalized (∑sωns=1\sum_s \omega_{ns} = 1, §2.6), this is HnH_n directly, with no separate division step — the same normalized form as §4.4.5’s ωis≡Pis/P̂i\omega_{is} \equiv P_{is}/\hat P_i (defined there). Setting Tn≡1T_n \equiv 1 makes §4.4.5’s formula the special case of the one above, term by term.

Two passes per decision maker. Unlike the score (§3.5), the Hessian cannot be read off the situation-stacked residual matrix alone: its outer-product terms must be squared within a situation before being summed across situations, since summing residuals across situations first and squaring once would cross-multiply different situations’ sum_Pz\mathrm{sum\_Pz} vectors — an identity the algebra does not license. mxl_hessian_parallel therefore runs two passes per unit, sharing the same draw loop that computes ωns\omega_{ns} (§2.6):

  • Pass 1 is the draw loop of mxl_unit_simulate() without residuals, the same operations in the same order: it yields λns\lambda_{ns} (the per-draw log-likelihoods of the unit’s choices), their log-sum-exp over draws, and ωns\omega_{ns} — exactly as in §3.5 — and keeps every situation’s probabilities at every draw for pass 2.
  • Pass 2 loops over the situations tt. For each it takes the probability matrix PtP_t (its mtm_t inside alternatives and the outside option, at every draw) from pass 1, or, for a unit whose rows-by-draws matrices exceed the batch budget of §3.5 (RnSR_n S above 2182^{18}, 219/32^{19}/3 with an outside option) and which pass 1 therefore splits into batches of draws without keeping them, forms it from that situation’s mt×Sm_t \times S rows of WnΓnW_n \Gamma_n, so that a thread’s memory grows with the largest choice set rather than with the unit; then it forms the two products of §4.4.4, Qt=Zt⊤PtQ_t = Z_t^\top P_t and Rt=PtEωR_t = P_t E_\omega. The draw weights ωns\omega_{ns} and the draw factors enrse^s_{nr} are the unit’s, shared by all its situations, since its draws are:
    • Identity C (linear pieces): the blocks read off RtR_t (buf_Pzz_cc’s (z0,z0)(z^0, z^0) and (z0,ζ)(z^0, \zeta) blocks, buf_Pzz_cd, buf_Pzz_dd) are summed over the situations; the (ζ,ζ)(\zeta, \zeta) block and buf_diff_HV_cc depend on the situation only through YntsY^s_{nt} and bntsb^s_{nt}, so these are summed across the situations draw by draw, Yns=∑tYntsY^s_n = \sum_t Y^s_{nt} and bns=∑tbntsb^s_n = \sum_t b^s_{nt} (the latter is §3.5’s Wn⊤𝙳𝚒𝚏𝚏𝚆W_n^\top\texttt{DiffW}), and the two blocks are formed once per unit — linear accumulation does not care whether it is summed within or across situations.
    • Identity B (F_stash, the sum_Pzsum_Pz⊤\mathrm{sum\_Pz}\,\mathrm{sum\_Pz}^\top term): column ss is overwritten each situation with ωnssum_Pznts\sqrt{\omega_{ns}}\,\mathrm{sum\_Pz}_{nts}, read off QtQ_t, and FtFt⊤F_t F_t^\top, formed on the situation’s slots (§4.4.6), is added into opg_pz once per situation, immediately after it is formed — situations’ outer products are added, never combined before squaring: ∑tFtFt⊤=∑t∑sωnssum_Pzntssum_Pznts⊤\sum_t F_t F_t^\top = \sum_t \sum_s \omega_{ns}\,\mathrm{sum\_Pz}_{nts}\, \mathrm{sum\_Pz}_{nts}^\top.
    • Identity A (G_stash, the score outer product): column ss instead accumulates gntsg_{nts} across every situation in the unit (its μ\mu and Cholesky rows once per unit, from bnb_n), and only after the situation loop ends is it centered at g‾n\bar g_n, scaled by ωns\sqrt{\omega_{ns}} and squared once: GG⊤=∑sωns(gns−g‾n)(gns−g‾n)⊤G G^\top = \sum_s \omega_{ns} (g_{ns} - \bar g_n)(g_{ns} - \bar g_n)^\top with gns=∑tgntsg_{ns} = \sum_t g_{nts} — the situation sum must happen before squaring here, the opposite order from Identity B, because it is the person’s total score that enters the outer product, not each situation’s on its own. Since the ωns\omega_{ns} sum to one, this is the Louis identity’s ∑sωnsgnsgns⊤−g‾ng‾n⊤\sum_s \omega_{ns} g_{ns} g_{ns}^\top - \bar g_n \bar g_n^\top, computed without its cancellation. In code, g_bar first holds a highest-weight draw’s score, which is subtracted from every column; it then holds the weighted mean of those deviations, subtracted in turn. This is equivalent to centering at g‾n\bar g_n while avoiding amplification of weight-rounding error by a large common score.

Final assembly mirrors §4.4.5’s block-by-block combination (hess_t1 = opg_pz - buf_Pzz + buf_diff_HV, cc/cd/dc/dd), whose centered Identity A already carries the Louis identity’s −g‾ng‾n⊤-\bar g_n \bar g_n^\top, and closes by adding the upper triangle of wnw_n\,hess_t1 into the result (§4.4.6) — no /P̂n/\hat P_n step, because ωns\omega_{ns} is already a normalized weight (§2.6), as in §4.4.5. Setting Tn≡1T_n \equiv 1 collapses pass 2 to a single situation per unit: F_stash (holding ωnssum_Pznts\sqrt{\omega_{ns}}\,\mathrm{sum\_Pz}_{nts}) and G_stash (holding ωnsgnts\sqrt{\omega_{ns}}\, g_{nts}) remain different vectors, but with only one situation to loop over, F’s per-situation overwrite and G’s cross-situation accumulate produce the same result, and the two-pass structure reduces exactly to §4.4’s single-situation accumulation.

The non-finite-likelihood guard. Pass 1 skips a unit only when its log-sum-exp is non-finite, !std::isfinite(lse). Because every log⁡Pntjs\log P_{ntj}^s is formed in log space (§2.4), lse is finite whenever the unit’s utilities are, however poorly the unit is fit; the guard fires only for utilities that overflow, where the unit’s log-likelihood is undefined. It replaces the former P̂isum≤10−12\hat P_i^{\mathrm{sum}} \le 10^{-12} threshold of §5.4, which could also discard a unit that was merely very poorly fit under the current parameter vector. See §5.4 and NEWS.md.

Code reference: mxl_hessian_parallel() in src/mxlogit.cpp.


5. Implementation Details

5.1 Parameter Vector Structure

The full parameter vector θ\theta is organized as:

Block Indices Length Description
β\beta [0,Kx)[0, K_x) KxK_x Fixed coefficients
μ\mu [Kx,Kx+Kw)[K_x, K_x + K_w) KwK_w (if rc_mean=TRUE) Random coefficient means
LL next block Kw(Kw+1)/2K_w(K_w+1)/2 or KwK_w Cholesky parameters
δ\delta remaining 00, J−1J-1, or JJ Free ASCs

The ASC length is zero when use_asc = FALSE, J−1J-1 without an outside option, and JJ with an outside option. The low-level parser requires at least one fixed-coefficient column (Kx>0K_x>0), even when the substantive focus is entirely on random coefficients.

5.2 Delta Method for Variance Parameters

When reporting results, the Cholesky parameters ℓ\ell are transformed to variance matrix elements Σ=LL′\Sigma = LL'.

The Jacobian J=∂vech(Σ)∂ℓJ = \frac{\partial \text{vech}(\Sigma)}{\partial \ell} is computed analytically in jacobian_vech_Sigma().

For element Σij=∑kLikLjk\Sigma_{ij} = \sum_k L_{ik} L_{jk}: ∂Σij∂ℓpq=∂Lpq∂ℓpq(Ljq𝟏i=p+Liq𝟏j=p) \frac{\partial \Sigma_{ij}}{\partial \ell_{pq}} = \frac{\partial L_{pq}}{\partial \ell_{pq}} \left( L_{jq} \mathbf{1}_{i=p} + L_{iq} \mathbf{1}_{j=p} \right)

This follows from the product rule applied to Σ=LLT\Sigma = LL^T: ∂Σ/∂ℓpq=E⋅LT+L⋅ET\partial \Sigma / \partial \ell_{pq} = E \cdot L^T + L \cdot E^T, where EE is the matrix with a single non-zero entry Epq=∂Lpq/∂ℓpqE_{pq} = \partial L_{pq} / \partial \ell_{pq}.

The variance-covariance matrix of vech(Σ)\text{vech}(\Sigma) is then: VΣ=J⋅Vℓ⋅J′ V_\Sigma = J \cdot V_\ell \cdot J'

Code reference: src/mxlogit.cpp

5.3 OpenMP Parallelization

The implementation parallelizes over likelihood units (§1.6) using OpenMP, with dynamic scheduling for load balancing (#pragma omp for schedule(dynamic)): - The log-likelihood and gradient keep thread-local accumulators, combined in a #pragma omp critical section. - The Hessian and BHHH kernels add each unit’s compact block, and the cluster meat each cluster’s, into per-thread packed upper triangles, added up in thread order after the parallel region, or into a shared result when those would exceed 2 GiB (§4.4.6). - Before allocating, each estimation kernel compares an upper estimate of its working memory, per thread and shared, with the machine’s physical memory (or options(choicer.max_memory)), and stops with an error naming the number of threads that would fit instead of running out of memory inside the parallel region. - The elasticity and diversion-ratio kernels (§6, §7) add each choice situation’s terms into a J×JJ \times J sum: one thread into the result itself, TT threads into copies of their own while those fit in 2 GiB (added up in thread order), and beyond that into the shared result atomically, each thread keeping the outside option’s row and column to itself. They check the memory of that sum and of their working arrays the same way first.

5.4 Practical Notes

  1. Negated Objectives: mxl_loglik_gradient_parallel returns −ℓ(θ)-\ell(\theta) and −∇ℓ(θ)-\nabla\ell(\theta); mxl_hessian_parallel returns −∇2ℓ(θ)-\nabla^2\ell(\theta). The BHHH kernel instead returns the positive-semidefinite information approximation ∑iwisisiT\sum_iw_is_is_i^T.

  2. ASC Identification: When include_outside_option = FALSE, the first inside alternative’s ASC (δ1\delta_1) is fixed to zero for identification. When include_outside_option = TRUE, all inside ASCs are free parameters (the outside option with V=0V=0 serves as the reference).

  3. Gradient vectorization: The per-draw gradient is computed without a per-alternative loop. After forming 𝐝is=𝐞ji−Pis\mathbf{d}_i^s = \mathbf{e}_{j_i} - P_i^s, the β\beta and μ\mu/LL blocks are evaluated with two BLAS dgemv calls (XiT𝐝isX_i^T \mathbf{d}_i^s and WiT𝐝isW_i^T \mathbf{d}_i^s). The delta block uses an irregular scatter and remains a loop.

  4. Exact draw layout and source: In both modes, likelihood unit uu (§1.6: a decision maker under person_col, a choice situation otherwise) receives the contiguous global indices g=(u−1)S+sg=(u-1)S+s for s=1,…,Ss=1,\ldots,S; dimension kk uses the kkth prime base and the resulting uniform is mapped through the standard-normal inverse CDF. In draws = "store", randtoolbox::halton(n = U*S, dim = K_w, normal = TRUE) is materialized as a Kw×S×UK_w\times S\times U cube during estimation, one Kw×SK_w \times S block per unit (UU decision makers under person_col, or NN choice situations otherwise). It is filled a block of units at a time with halton(start = ), which returns exactly those points. Store mode needs US≤231−1US \le 2^{31} - 1, the largest starting index halton() accepts. The prediction family (predict(), elasticities(), diversion_ratios(), blp(), logsum(), consumer_surplus(), and gof() through predict()) integrates over one block per choice situation instead (NN blocks for a panel fit, not UU), which it forms on the fly from the same points of the sequence (§5.5), with no limit on NSNS for up to 128 random coefficients. The fitted object retains only draws_info metadata, not the cube; the estimation-type functions (vcov(type = ), wesml_vcov(), conditional_tastes()) rebuild it. This mode is deterministic and has no seed or scramble. In draws = "generate", HaltonGen computes the radical inverse on demand. scramble = "none" uses identity permutations and is tested against the randtoolbox sequence. scramble = "permuted" (the default) constructs one independently seeded Fisher–Yates permutation for every (dimension, digit-position) pair and shares that table across sequence indices. Evaluation stops after the last nonzero base-bb digit, so trailing zero digits remain unchanged. This is not Owen nested-uniform scrambling: the digit permutation does not depend on the preceding digit prefix, and the construction does not make each point marginally uniform. Consequently it carries no standard RQMC unbiasedness, variance-rate, or replicate-based error-estimation guarantee. The historical API value "owen" is a deprecated alias for "permuted"; old fitted objects carrying that serialized label remain readable. The result is deterministic conditional on the seed and independent of OpenMP scheduling. Numerical sensitivity should be assessed by increasing SS and, for permuted draws, varying the seed. Generate mode is available only through the convenience data workflow; the advanced input_data workflow requires an explicit eta_draws cube.

  5. Finite-value guards: The likelihood/gradient kernel replaces a non-finite global negated objective by 101010^{10} and a zero gradient, allowing the optimizer’s line search to backtrack; isolated non-finite gradient entries are zeroed. The Hessian contributes a likelihood unit — decision maker nn under person_col, choice situation ii otherwise (§1.6, §4.5) — only when its log-sum-exp lse\mathrm{lse} is finite. Since every log⁡Pntjs\log P_{ntj}^s is the chosen utility minus a log-sum-exp (§2.4), λns\lambda_{ns}, lse\mathrm{lse} and the objective are finite whenever the utilities are, however poorly the parameter vector fits the observed choices: the 101010^{10} sentinel, the Hessian’s skip and the NA conditional tastes (§13) all fire only when the utilities themselves overflow (for instance an exploding Cholesky diagonal during a line search), where the log-likelihood is undefined. (Earlier releases skipped a situation whenever P̂isum=∑sPijis≤10−12\widehat P_i^{\mathrm{sum}} = \sum_s P_{ij_i}^s \le 10^{-12}, which could also discard a situation that was merely very poorly fit rather than genuinely zero-probability, biasing the reported Hessian and its standard errors; and until the log-space form of §2.4, a single choice probability that underflowed to zero, at a utility gap beyond about 745, made the whole objective the sentinel with a zero gradient, while subnormal probabilities, at gaps beyond about 708, lost precision. See NEWS.md.) Relatedly, the posterior draw weights ωns\omega_{ns} (§2.6) that both the score (§3.5) and the Hessian (§4.5) are built from are computed in log space via log-sum-exp rather than as a ratio of raw probabilities, so they stay finite and accurate even when the product over situations falls far below the smallest double — the earlier raw-probability construction could return an identically zero gradient at a genuinely poorly-fit point, a false stationary point for the optimizer. These are numerical failure guards, not changes to the mathematical criterion in its regular finite region.

  6. Constants without a finite estimate: By §3.5 the score of alternative jj’s constant is ∂ℓ/∂δj=∑nwn∑sωns∑t(𝟏j=jnt−Pntjs)\partial \ell / \partial \delta_j = \sum_n w_n \sum_s \omega_{ns} \sum_t (\mathbf{1}_{j = j_{nt}} - P_{ntj}^s), with jntj_{nt} the alternative chosen in situation tt of unit nn and the inner sum over the situations that offer jj: a positively weighted sum of terms with 0<Pntjs<10 < P_{ntj}^s < 1. If jj is never chosen in the data every term is negative, and if it is chosen in every situation that offers it every term is positive, at every value of θ\theta. The likelihood then keeps rising as δj→−∞\delta_j \to -\infty (or +∞+\infty), toward a supremum it never attains: δj\delta_j has no finite maximum-likelihood estimate, the optimizer stops it wherever its tolerance does, and its standard error is not meaningful. At that supremum jj’s probabilities are 0 (or 1), so the other parameters approach the maximum of the likelihood without alternative jj (or without the situations that offer it). The same holds for the reference whose constant is normalized to 0, the outside option or the base alternative: the other constants then diverge together, and with the outside option never chosen the supremum is the model without it. run_mxlogit(), like run_mnlogit() and run_nestlogit(), warns before the optimizer runs, from the counts in alt_mapping (class choicer_unbounded_constants); a finite bound in the direction a constant diverges (lower for a never-chosen alternative’s, upper for an always-chosen one’s) holds it at the bound, and that alternative is not named. The Newton phase (§5.7) holds the named constants of inside alternatives at the optimizer’s values.

5.5 Data Layout

  • XX has ∑iMi\sum_iM_i rows; situation ii occupies rows ∑i′<iMi′+1,…,∑i′≤iMi′\sum_{i'<i}M_{i'}+1,\ldots,\sum_{i'\le i}M_{i'} (64-bit offsets in the kernels).
  • WW may be row-aligned with XX or have one row per global inside alternative; the kernels either read the situation’s block of rows or gather rows by the global 1-based alt_idx. The convenience R workflow constructs the row-aligned form.
  • choice_idx is a 1-based local row position within each situation block, or 0 for an implicit outside choice. alt_idx is instead the global alternative ID used to gather ASCs and alternative-level WW rows.
  • Stored draws have dimensions Kw×S×UK_w\times S\times U, one block per likelihood unit (UU decision makers under person_col, choice situations otherwise). Generate-mode kernels receive an empty placeholder cube plus gen_seed, gen_scramble, and gen_S.
  • Prediction kernels read one block per choice situation: situation ii gets points (i−1)S+1,…,iS(i-1)S+1,\ldots,iS of the Halton sequence. With stored draws and up to 128 random coefficients (the on-the-fly generator’s limit; beyond it they build the cube), the prediction-type functions (predict(), logsum(), elasticities(), diversion_ratios(), blp(), and through them consumer_surplus() and gof()) never build that Kw×S×NK_w\times S\times N cube (though blp() may keep the draws across its iterations, §8.4): they pass an empty placeholder and gen_scramble = 2, and each thread forms its situation’s block on the fly from HaltonGen’s identity-permutation uniforms, which are randtoolbox::halton()’s points, mapped to normals by R’s qnorm() (R::qnorm, libR’s own qnorm5), as get_halton_normals() maps them. The draws, and so the results, are the cube’s bit for bit (per situation at any thread count, and sums over situations at one thread; at more threads their rounding depends on the order in which threads take the situations, as it always did) where randtoolbox and choicer are compiled with the same floating-point contraction: both add each digit’s term djb−(j+1)d_j b^{-(j+1)} to the radical inverse in the same order, and agree bit for bit when both fuse those multiply-adds or neither does, as on CRAN’s platforms. Otherwise under 1% of the uniforms, all of bases 5 and up, differ in their last bit.

5.6 Inverting the Information

Every reported variance of θ̂\hat\theta is the inverse of an estimate of the information matrix, or a sandwich built on one: V=ℐ−1V = \mathcal{I}^{-1} with ℐ\mathcal{I} the negated Hessian −∇θ2ℓ(θ̂)-\nabla^2_\theta \ell(\hat\theta) of §4 (se_method = "hessian") or the BHHH matrix ∑uwususu⊤\sum_u w_u\, s_u s_u^{\top} of §9.5 ("bhhh"), or V=A−1BA−1V = A^{-1} B A^{-1} with the bread AA and meat BB of §9.3 ("sandwich") or §9.5 ("cluster"). In the data’s units the diagonal of ℐ\mathcal{I} can span many orders of magnitude: a covariate measured in thousands next to one measured in thousandths, or the constant of an alternative a handful of individuals chose next to that of one most chose. Inverted as it is, such a matrix can be refused as computationally singular although only the units make it so (solve() refuses a matrix whose estimated reciprocal condition number falls below the machine epsilon, and the units alone can push it there), and a sandwich built on it can lose digits. Every fit, whatever the coordinates its optimizer used (scale_vars, §1.5), therefore inverts the equilibrated matrix:

V=D(DℐD)−1D,D=diag(d),d=2round(−12log⁡2|diag(ℐ)|), V = D\,(D \mathcal{I} D)^{-1} D, \qquad D = \mathrm{diag}(d), \quad d = 2^{\,\mathrm{round}(-\frac{1}{2}\log_2 |\mathrm{diag}(\mathcal{I})|)},

with diag(ℐ)\mathrm{diag}(\mathcal{I}) the vector of the diagonal entries of ℐ\mathcal{I} and the functions applied elementwise: dd holds the powers of two nearest, on a log scale, to the inverse square roots of the diagonal’s magnitudes, with an entry of 1 where the diagonal entry is zero or not finite and every entry clamped to [2−500,2500][2^{-500}, 2^{500}]. For the sandwich, V=DVDDV = D\,V_D\,D with VD=(DAD)−1(DBD)(DAD)−1V_D = (DAD)^{-1} (DBD) (DAD)^{-1} and DD from AA. Any DD leaves VV unchanged in exact arithmetic, and powers of two make the products with DD exact too, barring overflow or underflow: only the LULU factorization and the solves round (for the sandwich, also the symmetrization below), on a matrix whose diagonal is within a factor of two of one in magnitude except where a diagonal entry is zero or not finite, or an entry of dd is clamped. The magnitude of the diagonal is used so that an information that is not positive definite (away from a maximum) is equilibrated too, and its negative variances reported: their standard errors are NA, with a message.

DℐDD \mathcal{I} D is formed in one working copy, which LAPACK factors in place (LULU with partial pivoting) with solve()’s checks: a pivot that is exactly zero is an error, and so, when every factor is finite, is a reciprocal condition number (estimated in the 1-norm) below the machine epsilon; either error leaves the fit without a variance, its standard errors NA, with a message. The identity is then solved against the factors into the result, as reference LAPACK’s dgesv does for solve(), with two nparams×nparamsn_\text{params} \times n_\text{params} matrices besides ℐ\mathcal{I} (nparamsn_\text{params} the number of parameters) where solve() holds three. The sandwich is solved twice against the factors of DADDAD, first (DAD)Y=DBD(DAD)\,Y = DBD and then the transposed system (DAD)⊤VD⊤=Y⊤(DAD)^{\top} V_D^{\top} = Y^{\top}, without forming an inverse: two nparams×nparamsn_\text{params} \times n_\text{params} matrices besides AA and BB where an explicit inverse and two products hold four, and 423nparams34\tfrac{2}{3} n_\text{params}^3 operations against 623nparams36\tfrac{2}{3} n_\text{params}^3. VDV_D is symmetrized, (VD+VD⊤)/2(V_D + V_D^{\top})/2, before DD is applied. At nparams=17,000n_\text{params} = 17{,}000 each such matrix takes 2.3 GB. The working copy is freed as the call returns, and the package’s own passes over whole matrices (the scaling, the scan of the factors for non-finite entries, the transposition and the symmetrization) run in parallel over columns, without sums across threads, so they do not depend on the number of threads. The fit and post-hoc vcov(fit, type = ) and wesml_vcov() take this same route.

On one test design, a mixed logit’s robust variance, the bread had condition number 1.2×10121.2 \times 10^{12}, and 2.62.6 equilibrated: its inverse was accurate either way, with standard errors within 6×10−166 \times 10^{-16} relative of an exact computation, but the sandwich’s standard errors, computed raw from an explicit inverse and two products, were off by 2.1×10−72.1 \times 10^{-7} relative, and equilibrated by 9×10−169 \times 10^{-16}. Against an exact computation on the package’s reference battery and tests (963 inversions of 477 distinct matrices, of 2 to 13 parameters), a raw inversion refused 52 as singular; equilibrated, 44 of them, with raw condition numbers from 4.5×10184.5 \times 10^{18} to 1.2×10281.2 \times 10^{28}, had condition numbers below 20 and came out with standard errors within about 10−1510^{-15} relative of exact, while the other eight stayed ill-conditioned or singular. Equilibration removes the ill-conditioning that the parameters’ units cause, not the likelihood’s own: a matrix that stays ill-conditioned after it (a likelihood nearly flat along a combination of parameters, which no rescaling of the parameters one by one removes) keeps only the digits its condition number allows, by any inversion, and one singular to working precision is refused either way.

Code reference: invert_hessian(), .sandwich_combine() and .jacobi_scale() in R/classes.R; equilibrated_inverse() and equilibrated_sandwich() in src/vcov.cpp.

5.7 Polishing the Optimizer’s Solution

run_mxlogit(polish = ) follows the optimizer with a Newton phase. It does not change the estimator: it finishes the climb to the maximum of the same simulated log-likelihood that the optimizer approached, and certifies it. L-BFGS converges slowly at the end where the curvature spreads over orders of magnitude, as it does across thousands of alternative-specific constants (§1.5); near a regular maximum Newton’s method converges quadratically.

The certificate. Write f(θ)=−ℓ(θ)f(\theta) = -\ell(\theta), g=∇fg = \nabla f at the current θ\theta, and CC for the curvature: the information ℐ=−∇θ2ℓ\mathcal{I} = -\nabla^2_\theta \ell of §4 and §5.6 (polish = "newton") or the BHHH matrix ℬ=∑uwususu⊤\mathcal{B} = \sum_u w_u\, s_u s_u^{\top} of §9.5, with the fit’s weights ("bhhh"). The Newton step is Δ=−C−1g\Delta = -C^{-1} g, and the Newton decrement

ν=12g⊤C−1g=−12g⊤Δ \nu = \tfrac{1}{2}\, g^{\top} C^{-1} g = -\tfrac{1}{2}\, g^{\top} \Delta

is the gain in log-likelihood that the quadratic model predicts for that step. On a positive definite information, Δ\Delta is the step to the maximum of the quadratic model, and since Δ⊤ℐΔ=2ν\Delta^{\top} \mathcal{I} \Delta = 2\nu, Cauchy–Schwarz in the metric of ℐ\mathcal{I} gives, for every linear combination a⊤θa^{\top}\theta,

|a⊤Δ|≤2νa⊤ℐ−1a: |a^{\top} \Delta| \le \sqrt{2\nu}\, \sqrt{a^{\top} \mathcal{I}^{-1} a}:

the remaining step moves each linear combination of the estimates by at most 2ν\sqrt{2\nu} of its Hessian standard error. The polish has converged when ν/w‾\nu / \bar w, computed with the undamped curvature and w‾\bar w the mean weight, falls below tol_decrement (10−810^{-8} by default: 1.4×10−41.4 \times 10^{-4} standard errors with weights of mean one); dividing by w‾\bar w makes the rule independent of the weights’ scale, as the estimates are, where in raw units weights summing to one would loosen it by the sample size. The polish then stops without taking the step, so the certificate is for the point returned. With parameters held or on a bound (below), ν\nu is computed over the others, and the bound holds in their standard errors conditional on the rest, which are no larger than the reported ones.

BHHH needs no second derivatives and is positive semidefinite everywhere; it can be positive definite only with at least as many likelihood units as the parameters it moves, which "bhhh" requires. Where ℐ\mathcal{I} is positive definite, the step Δℐ=−ℐ−1g\Delta_{\mathcal{I}} = -\mathcal{I}^{-1} g to the quadratic model’s maximum satisfies

2νℬ=g⊤ℬ−1g=Δℐ⊤ℐℬ−1ℐΔℐ, 2\nu_{\mathcal{B}} = g^{\top} \mathcal{B}^{-1} g = \Delta_{\mathcal{I}}^{\top}\, \mathcal{I} \mathcal{B}^{-1} \mathcal{I}\, \Delta_{\mathcal{I}},

its squared length in the metric of the sandwich ℐ−1ℬℐ−1\mathcal{I}^{-1} \mathcal{B} \mathcal{I}^{-1}, the robust variance of §9.3 for an unweighted fit: 2νℬ\sqrt{2\nu_{\mathcal{B}}} bounds the remaining step in robust standard errors. But a small νℬ\nu_{\mathcal{B}} on its own shows only that gg is small, a stationary point, which BHHH’s positive definiteness cannot tell from a saddle. BHHH’s steps converge linearly, fast only where the information-matrix equality ℬ≈ℐ\mathcal{B} \approx \mathcal{I} holds, which finitely many draws and misspecification break.

The step. The step solves, by Cholesky, the system equilibrated by the Jacobi scale of §5.6 and damped,

(DCFFD+λI)y=−DgF,ΔF=Dy, (D\, C_{FF}\, D + \lambda I)\, y = -D\, g_F, \qquad \Delta_F = D\, y,

on the free parameters FF (below). DD consists of powers of two, so the scaled block is formed exactly, and λI\lambda I on it is the damping λ|diagC|\lambda\,|\mathrm{diag}\, C| of Levenberg (1944) and Marquardt (1963) to within a factor of two: the step does not depend on the parameters’ units. λ=0\lambda = 0 when the block’s diagonal is positive and its factorization succeeds with a reciprocal condition number (estimated in the 1-norm, as solve() does) above the machine epsilon ε\varepsilon. Otherwise (away from the maximum the simulated likelihood need not be concave), and also when the step is not finite or its line search fails, λ\lambda starts at max⁡(10−6,λprev/100)\max(10^{-6}, \lambda_\text{prev}/100) and grows tenfold, up to 101010^{10}. A step longer than 10 units of the optimizer’s coordinates in some free parameter (|Δk|/mk|\Delta_k| / m_k, with mm the map of §1.5) is shortened to that length, its direction kept: a maximum step, as in Dennis and Schnabel’s (1983) methods and nlm()‘s stepmax. A free parameter whose step exceeds ten maximum steps, 100mk100\, m_k, on a curvature that has all but vanished (|Ckk|mk2|C_{kk}|\, m_k^2 below 10−410^{-4} times the free parameters’ median; see the limits below) is instead held for that iteration and the step solved again without it: shortening its step to the maximum would shorten every other parameter’s by more than ten. A step that is long because the start is far from the maximum is shortened. Under scale_vars = "none", whose coordinates are the parameters’ natural units, the scales mkm_k are instead the BHHH map’s of §1.5 taken where the polish starts: one gradient pass at the optimizer’s solution (clipped to the bounds), powers of two, ones where the pass overflows. In natural units a badly scaled column far from its maximum would be held as if its curvature had vanished: in the multinomial and nested logits’ census- and claims-style designs with half of the covariates rescaled by 10310^3 and the other half by 10−310^{-3}, 20 steps at unit scales left a decrement of 1,054 to 2,060, where 8 or 9 at these scales certified the maximum.

Bounds. With lower/upper the polish projects (Bertsekas 1982). A parameter within ϵk\epsilon_k of a bound across which its descent direction −gk-g_k points is ϵ\epsilon-active and moves to the projection of its scaled-gradient step, P(θk−Dk2gk)P(\theta_k - D_k^2\, g_k), which reaches the bound when that step covers the distance (the line search may shorten it); ϵk=mkmin⁡(10−3,max⁡i|wi/mi|)\epsilon_k = m_k \min(10^{-3}, \max_i |w_i / m_i|), the maximum over the parameters not held, with w=θ−P(θ−m2∘g)w = \theta - P(\theta - m^2 \circ g) the projected gradient step, shrinks to zero at a stationary point. The other parameters are free. Convergence then requires the ϵ\epsilon-active parameters on their bounds and the decrement over the free ones below the tolerance: the Karush–Kuhn–Tucker conditions of the bounded problem.

The line search. On the projected path P(θ+αΔ)P(\theta + \alpha \Delta), with displacement Δα=P(θ+αΔ)−θ\Delta_\alpha = P(\theta + \alpha \Delta) - \theta, α\alpha is halved, up to 40 times, until Armijo’s condition f(θ+Δα)≤f(θ)+10−4g⊤Δαf(\theta + \Delta_\alpha) \le f(\theta) + 10^{-4}\, g^{\top} \Delta_\alpha holds. Near the maximum of a likelihood summed over UU units the rounding in ff is about Uε|f|\sqrt{U}\, \varepsilon\, |f|, which at population scale reaches the gain of a correct step, so Armijo’s test can refuse it. Where the predicted gain −g⊤Δα-g^{\top} \Delta_\alpha is within εf=10−10max⁡(|f|,1)\varepsilon_f = 10^{-10} \max(|f|, 1), a step is also accepted on the approximate Armijo condition of Hager and Zhang (2005), the upper half of their approximate Wolfe conditions: f(θ+Δα)≤f(θ)+εff(\theta + \Delta_\alpha) \le f(\theta) + \varepsilon_f and ∇f(θ+Δα)⊤Δα≤−0.8g⊤Δα\nabla f(\theta + \Delta_\alpha)^{\top} \Delta_\alpha \le -0.8\, g^{\top} \Delta_\alpha, which reads the gradient, accurate where ff is not. A trial point whose utilities overflow (§5.4), or whose objective or gradient is not finite, fails. Should accepted steps add up to a point worse than the start by more than εf\varepsilon_f, the start is returned.

What is held. The constants of §5.4, item 6, of inside alternatives (never chosen, or chosen wherever offered) are held at the optimizer’s values: they have no finite estimate, and the certificate concerns the other parameters given the held values. Those parameters approach the maximum without the alternative (or without the situations that offer it) only as the held constants approach their limits, so an optimizer stopped early leaves them further from it. When the reference itself, the outside option or the base alternative, is never chosen or chosen wherever offered, the other constants diverge together, and the polish does not report convergence, whatever its decrement, unless the reference is never chosen and the constants held or bounded above anchor all the others, which stops their rise. An alternative is anchored if its constant is held or bounded above, or if it is offered in a choice situation whose chosen alternative is anchored. With every constant that moves anchored, any direction that raises a set of them meets the situation that anchored the first of them; there the chosen alternative, outside the set, has a probability that falls to zero, and with it the log-likelihood. A held alternative chosen only where it is offered alone anchors nothing. A parameter whose curvature column and gradient are exactly zero carries no information at the current point (as where a log-Cholesky diagonal has gone so far toward −∞-\infty that its derivatives underflow), and one held by the step rule above has all but none: either is left out of the steps, and the polish does not report convergence.

Limits. A random coefficient whose variance’s maximum is at zero drives its log-Cholesky diagonal ℓpp\ell_{pp} (§1.4) toward −∞-\infty. There ff moves with ℓpp\ell_{pp} like ce2ℓppc\, e^{2 \ell_{pp}}, so the Newton step in ℓpp\ell_{pp} is about −1/2-1/2 and the decrement shrinks by a factor of only about ee per step: the polish may stop at max_iter unconverged, or certify a point where the variance is negligible but ℓpp\ell_{pp}, and its standard error, are set by tol_decrement. With finitely many draws the simulated likelihood can also carry a term linear in the standard deviation, ceℓppc\, e^{\ell_{pp}}. Of one sign it sends the supremum to −∞-\infty as before (Newton steps of about −1-1); of the other it puts a maximum at a negligible standard deviation, which the Newton polish certifies. BHHH’s curvature in that direction falls faster than the Hessian’s (like e4ℓppe^{4 \ell_{pp}} against e2ℓppe^{2 \ell_{pp}} where the quadratic term leads, e2ℓppe^{2 \ell_{pp}} against eℓppe^{\ell_{pp}} where the linear one does), and its steps grow accordingly until the rule above holds ℓpp\ell_{pp}, before they can carry it to where its information underflows: the other parameters then reach their maximum given it, and the polish stops unconverged. On the shipped mode_choice data, with a travel-time coefficient of mean zero and free variance (rc_mean = FALSE), fixed cost and waiting-time coefficients and 50 draws, the simulated likelihood has its maximum at ℓ11≈−11.05\ell_{11} \approx -11.05, a standard deviation of about 1.6×10−51.6 \times 10^{-5} per minute (0.001 to 0.023 in utility over the data’s 63 to 1,440 minutes of travel): the Newton polish of a loose fit (maxeval = 10, ℓ11=−7.44\ell_{11} = -7.44) certifies it in 11 steps, while the BHHH polish holds ℓ11\ell_{11} at −9.94-9.94 and stops after 18 steps, 6.5×10−46.5 \times 10^{-4} log-likelihood units short.

The restart. A point where the linear term puts the supremum at ℓpp→−∞\ell_{pp} \to -\infty is a local maximum of the simulated likelihood on the boundary of the parameter space, but often a false one. The linear term comes from the draws’ finite-sample asymmetry and has mean zero over the draws, while the quadratic term is what identifies the variance. If the log-likelihood moves with the standard deviation σ\sigma like bσ+12qσ2b\, \sigma + \tfrac{1}{2} q\, \sigma^2, with b<0<qb < 0 < q, it falls to a minimum at σ=|b|/q\sigma = |b|/q and passes its value at zero at σ=2|b|/q\sigma = 2|b|/q, so the decrement certifies a point below a higher maximum. So a polish whose estimates end with σp\sigma_p below 10−410^{-4} of the default start’s (its typical effect on a utility below 10−410^{-4}, for a normal coefficient) restarts once: the optimizer and the polish again from the same point with those standard deviations raised, in coordinates taken afresh, and the higher likelihood stands. The level is the default start’s 1/hp1/h_p for a normal coefficient; a log-normal’s σ\sigma has no units, and 1 is used, since from the default start’s 0.5, below the design’s truth of 0.70 here, more fits came back. A probe along the axis alone cannot do this: with the other parameters at the plateau’s values the likelihood rose in only 3 of 57 composite fits so stopped, because they must adjust, where the restart raised it in 45. On a simulated stated-preference panel with 200 persons (S=500S = 500), 4 of 40 fits from the default start stopped so, 4.1 to 10.3 log-likelihood units below the maximum, and the restart reached it in all 4. On composite fits of those data (S=200S = 200), 57 of 100 stopped so, and 54 of them then stood at or above the fit from the true parameters; certified fits more than 0.01 below that fit fell from 47 to 6. At 800 persons, all 6 that stopped so reached it. Where the variance is zero, the restart comes back: on logit data with two random coefficients (N=2,000N = 2{,}000, S=200S = 200), 23 of 40 fits stopped so and all 23 ended at zero again, as did all 4 of 24 with a random coefficient of zero variance added to the panel. A variance that ends at zero warns, with or without a polish: the diagonal’s standard error says nothing (a clustered one is of order one, the Hessian’s about 10410^4), and a test that a variance is zero has a null on the boundary, where Wald and likelihood ratio statistics do not have their usual distributions (Self and Liang 1987). The restart took 0.5 to 1 times the first fit’s time at the median and 2.4 at most, on the fits that stop there, and where the first point stands the variance evaluates its matrix once more; polish_control = list(restart = FALSE) turns it off. Under the profile likelihood, whose constants have their own solver, the fit warns without restarting.

The variance is computed afterwards at the polished estimates, by se_method, through §5.6. Since every stop is decided at the point whose curvature the polish has just evaluated, that matrix is the variance’s own at the estimates returned (unless the start is returned, as above, or the curvature itself fails) wherever se_method needs it, and is not evaluated again: after a Newton polish, ℐ\mathcal{I} for "hessian" and as the bread of "sandwich" and "cluster"; after a BHHH polish, ℬ\mathcal{B} for "bhhh" (the sandwich’s meat squares the weights, so it is computed). There, a polish that certifies the optimizer’s solution at once costs one evaluation of the log-likelihood and its gradient and one Cholesky factorization of the curvature beyond the fit without it. fit$optimizer$polish records the phase. A profile fit (§5.8) takes its variance the same way, from the full matrices at its estimates.

Code reference: run_polish(), .polish_control(), and the wiring the wrappers share (the held constants and the BHHH rank check, .polish_held(); the decrement over the mean weight, the divergence rule and the record, .polish_fit()) in R/classes.R; newton_step() in src/newton.cpp; the curvature and its reuse in run_mxlogit(), R/mxlogit_utils.R.

5.8 Concentrating Out the Constants

With many alternatives the constants are most of the parameters: 2,511 of 2,530 in a census-style design of school choice. run_mxlogit(concentrate_out_asc = "profile_likelihood") splits θ=(θc,δ)\theta = (\theta_c, \delta), with θc=(β,μ,L)\theta_c = (\beta, \mu, L), and maximizes the profile log-likelihood

ℓ*(θc)=maxδℓ(θc,δ) \ell^*(\theta_c) = \max_\delta\, \ell(\theta_c, \delta)

over θc\theta_c alone. Its maximizer is the θc\theta_c part of the joint maximizer, and δ̂(θ̂c)\hat\delta(\hat\theta_c) the δ\delta part: the estimator does not change.

The constants’ first-order condition. By §3.4.4 and §3.5, with ωns\omega_{ns} the posterior draw weights of §2.6,

∂ℓ∂δj=∑n=1Uwn∑t=1Tn(𝟏{ynt=j}−∑s=1SωnsPntjs)=0: \frac{\partial \ell}{\partial \delta_j} = \sum_{n=1}^U w_n \sum_{t=1}^{T_n} \Bigl(\mathbf 1\{y_{nt} = j\} - \sum_{s=1}^S \omega_{ns}\, P_{ntj}^s\Bigr) = 0:

each alternative’s weighted chosen count equals its predicted count, each decision maker’s probabilities averaged over the draws with the weight of the draw given their choices. In the multinomial logit, without draws, this is the familiar property that the fitted shares match the observed ones. With random coefficients it is not BLP’s share matching (§8), which equates the unconditional shares, with ωns\omega_{ns} replaced by 1/S1/S, to the observed ones: a different estimator, concentrate_out_asc = "blp" (§5.9). jj runs over the free constants (the outside option’s or the base alternative’s is normalized).

The envelope gradient. At an interior maximum δ̂(θc)\hat\delta(\theta_c) the constants’ score vanishes, so

∇ℓ*(θc)=∇θcℓ(θc,δ̂(θc)): \nabla \ell^*(\theta_c) = \nabla_{\theta_c}\, \ell\bigl(\theta_c, \hat\delta(\theta_c)\bigr):

the joint gradient’s θc\theta_c part at the solved constants, with no derivative of δ̂\hat\delta.

The inner solve. At each θc\theta_c the optimizer evaluates, the constants are solved by the Newton phase of §5.7 on δ\delta alone: θc\theta_c held, no bounds, unit scales (the maximum step and the stall rule in units of utility), and the decrement over the mean weight (§5.7). Its curvature is the information’s constants block ℐδδ\mathcal I_{\delta\delta}, from mxl_delta_hessian_parallel() in the same pass as the objective and the gradient: minus the δ\delta rows and columns of §4.5’s assembly,

ℐδδ=∑n=1Uwn[diag(∑t,sωnsPnts)−∑tFtFt⊤−GG⊤], \mathcal I_{\delta\delta} = \sum_{n=1}^U w_n \Bigl[\mathrm{diag}\Bigl(\sum_{t,s} \omega_{ns} P_{nt}^s\Bigr) - \sum_t F_t F_t^\top - G G^\top\Bigr],

with FtF_t the situation’s slot probabilities scaled by ωns\sqrt{\omega_{ns}} and GG the decision maker’s per-draw scores of the constants, centered at their posterior mean and scaled likewise: the complete information less the missing one (Louis 1982, §4.5). It needs no continuous block and costs about three gradient passes, against three to four for the full Hessian, whose continuous blocks Identity D forms (§4.4.4). A trial point the line search accepts gives the next step’s curvature from the same pass. The solve stops when the Newton decrement ν=12gδ⊤ℐδδ−1gδ\nu = \tfrac12\, g_\delta^\top \mathcal I_{\delta\delta}^{-1} g_\delta over the mean weight falls below tol_decrement (10−1210^{-12}), or when every constant’s score, and the reference’s (minus their sum: the scores of all the alternatives’ constants sum to zero), is below tol_residual (10−1010^{-10}) relative to its alternative’s weighted chosen count plus the mean weight, a test that needs no factorization; neither rule depends on the weights’ scale. It fails after max_iter (25) steps or 33\,max_iter+1\,+ 1 passes, or when it stops otherwise (a failed line search, negligible steps). The first solve, at the start values, starts from δj=log⁡(cj/c0)\delta_j = \log(c_j / c_0), with cjc_j the weighted chosen counts and c0c_0 the reference’s (the estimate when the model has constants only and every situation offers every alternative), or from a supplied theta_init’s constants, and may take four times as many steps; where it fails, the fit stops (class choicer_profile_unconverged; an error inside it is reported as itself).

How exact the inner solve must be. Write ℐ\mathcal I for the information at (θc,δ̂)(\theta_c, \hat\delta), gδg_\delta for the gradient of −ℓ-\ell in δ\delta at a solve’s constants δ̃\tilde\delta, and ν\nu for its decrement. To first order δ̃−δ̂=ℐδδ−1gδ\tilde\delta - \hat\delta = \mathcal I_{\delta\delta}^{-1} g_\delta, and the envelope gradient errs by e=ℐcδℐδδ−1gδe = \mathcal I_{c\delta}\, \mathcal I_{\delta\delta}^{-1} g_\delta. At the outer maximum this moves θc\theta_c by −𝒮−1e-\mathcal S^{-1} e, where

𝒮=ℐcc−ℐcδℐδδ−1ℐδc \mathcal S = \mathcal I_{cc} - \mathcal I_{c\delta}\, \mathcal I_{\delta\delta}^{-1}\, \mathcal I_{\delta c}

is the profile’s curvature (below). A linear combination a⊤θca^\top \theta_c has standard error (a⊤𝒮−1a)1/2(a^\top \mathcal S^{-1} a)^{1/2}, and by Cauchy–Schwarz it moves by at most (e⊤𝒮−1e)1/2(e^\top \mathcal S^{-1} e)^{1/2} of it. By the inverse of the partitioned ℐ\mathcal I,

e⊤𝒮−1e=gδ⊤((ℐ−1)δδ−ℐδδ−1)gδ≤(κ−1)gδ⊤ℐδδ−1gδ=2(κ−1)ν, e^\top \mathcal S^{-1} e = g_\delta^\top \bigl((\mathcal I^{-1})_{\delta\delta} - \mathcal I_{\delta\delta}^{-1}\bigr) g_\delta \le (\kappa - 1)\, g_\delta^\top \mathcal I_{\delta\delta}^{-1} g_\delta = 2 (\kappa - 1)\, \nu,

with κ≥1\kappa \ge 1 the largest eigenvalue of ℐδδ1/2(ℐ−1)δδℐδδ1/2\mathcal I_{\delta\delta}^{1/2} (\mathcal I^{-1})_{\delta\delta}\, \mathcal I_{\delta\delta}^{1/2}, the most by which estimating θc\theta_c inflates the variance of a combination of the constants. The outer solution therefore moves by at most 2(κ−1)ν\sqrt{2(\kappa - 1)\nu} standard errors: 1.4×10−51.4 \times 10^{-5} at ν=10−12\nu = 10^{-12}, even for κ=100\kappa = 100. The bound is in standard errors of the likelihood with weights of mean one, the unit of the decrement over the mean weight. Usually the decrement rule stops the solve first.

Warm starts and failures. A line search tries points it then rejects, and constants solved at a rejected point are a poor start for the next. Every solve starts from the constants of the best point so far (the lowest objective among converged solves), which under a monotone line search is the current iterate or a better trial on its line. That point’s value, constants and gradient are kept, so an evaluation at exactly its θc\theta_c, as the optimizer’s last one often is, needs no solve. A solve that does not converge reaches the optimizer as the overflow sentinel of §5.4, so that its line search backtracks: at unconverged constants −ℓ-\ell only bounds −ℓ*-\ell^* from above, and the θc\theta_c part of the gradient is not the profile’s. Such failures are counted; the fit warns of those after the best point last improved, which may have stopped the optimizer. The constants returned are solved at the optimizer’s solution.

The outer problem. The optimizer works in the coordinates of §1.5 restricted to θc\theta_c; under "bhhh" the BHHH diagonal is taken at (θc,0,δ̂(θc,0))(\theta_{c,0}, \hat\delta(\theta_{c,0})), after the first solve. Bounds on θc\theta_c go to the optimizer. The constants may not be bounded, and each must have a finite estimate (§5.4, item 6): either case is an error, before the draws.

The variance. By the same partitioned inverse, (ℐ−1)cc=𝒮−1(\mathcal I^{-1})_{cc} = \mathcal S^{-1}: inverting the full information at (θ̂c,δ̂)(\hat\theta_c, \hat\delta), as §5.6 does for a joint fit, gives θc\theta_c the inverse of the profile’s curvature, which accounts for the constants being estimated. Inverting ℐcc\mathcal I_{cc} alone, as if δ\delta were known, gives ℐcc−1≼𝒮−1\mathcal I_{cc}^{-1} \preceq \mathcal S^{-1}: standard errors too small. The profile fit’s variance is therefore the joint fit’s, by se_method, from the full matrices; after a polish, from its last curvature where that is the matrix se_method needs (§5.7).

Cost. A solve from a warm start takes about two Newton steps, each a pass of the constants’ kernel (about three gradient passes) and a Cholesky factorization of the Jδ×JδJ_\delta \times J_\delta block (Jδ3/3J_\delta^3/3 operations: 0.17 s at Jδ=2,511J_\delta = 2{,}511 on 11 threads, 4 to 5 s at 10,000). In the validation study of NEWS (census- and claims-style designs of 518 to 2,620 parameters, two sizes each) the profile took 21 to 49 evaluations of the optimizer and reached the joint maximum, ending closer to it (the joint Newton decrement at its stop 3×10−133 \times 10^{-13} to 3×10−103 \times 10^{-10}, against 10−1010^{-10} to 2×10−82 \times 10^{-8} at the default joint fit’s stop), but at 2 to 4 times the joint fit’s time (modeled from the kernels’ calls; 1.8 to 4.9 times on the clock): L-BFGS in the coordinates of §1.5 needed only 46 to 224 evaluations of the joint problem, each about a third of a pass of the constants’ kernel. Profiling would pay where the joint optimizer needs many more evaluations than that, which the study did not find.

Code reference: .profile_objective() and .concentrate_control() in R/classes.R; .mxl_profile_solver(), .check_profile_inputs(), .chosen_counts() and the wiring in run_mxlogit(), R/mxlogit_utils.R; mxl_delta_hessian_parallel() in src/mxlogit.cpp.

5.9 Matching Aggregate Shares

run_mxlogit(concentrate_out_asc = "blp") is a different estimator of the same model: the constants are held at the values at which the model’s market shares equal target shares, and the other parameters maximize the likelihood given them. With P‾ntj=1S∑sPntjs\bar P_{ntj} = \frac1S \sum_s P^s_{ntj} the probability averaged over the decision maker’s own estimation draws (each draw weighted 1/S1/S, not by the posterior weight ωns\omega_{ns} of §2.6 that §5.8’s condition uses), the predicted share of inside alternative jj in market mm is

smj(θc,δ)=1w(m)∑(n,t)∈mwnP‾ntj(θc,δ),w(m)=∑(n,t)∈mwn, s_{mj}(\theta_c, \delta) = \frac{1}{w_{(m)}} \sum_{(n,t) \in m} w_n\, \bar P_{ntj}(\theta_c, \delta), \qquad w_{(m)} = \sum_{(n,t) \in m} w_n,

a market being a group of alternatives that share choice situations, directly or through others (the components of the share inversion, §8; one in most data), and (n,t)∈m(n,t) \in m its choice situations. The constants δ(θc)\delta(\theta_c) solve smj(θc,δ)=Smj*s_{mj}(\theta_c, \delta) = S^*_{mj} for every free constant: for each θc\theta_c the solution, where one exists, is unique (Berry 1994), and smooth in θc\theta_c. The estimator is

θ̂c=arg⁡maxθcℓ(θc,δ(θc)),δ̂=δ(θ̂c). \hat\theta_c = \arg\max_{\theta_c}\, \ell\bigl(\theta_c, \delta(\theta_c)\bigr), \qquad \hat\delta = \delta(\hat\theta_c).

Train and Winston (2007, §3) estimated the mixed logit of new-vehicle choice this way, with the constants recomputed by BLP’s contraction at every trial value of the other parameters so that the shares averaged over their sample equal national market shares; Berry, Levinsohn and Pakes (2004) and Petrin (2002) instead combine moments of the micro data with the share constraint by GMM. Imbens and Lancaster (1994) give the theory of aggregate information in micro estimation.

The targets. target_shares gives external shares Smj*S^*_{mj}, each inside alternative’s share within its market (BLP’s sjts_{jt}, tt their market); with an outside option a market’s outside share is one minus its inside targets. By default the targets are the sample’s own weighted shares, Smj*=∑(n,t)∈mwn𝟏{ynt=j}/w(m)S^*_{mj} = \sum_{(n,t) \in m} w_n \mathbf 1\{y_{nt} = j\} / w_{(m)}: the fitted model then reproduces them exactly, a calibration of the constants. A choice-based sample’s population shares QQ (§9), when its provenance carries them, are the default targets of a WESML fit, taken as exact: under choice-based sampling the weighted sample shares equal QQ only by construction of the weights, and the chosen counts are fixed by the strata. The predicted shares are the weighted sample’s, so the weights must make each market’s sample represent the population its targets describe. With an outside option that population includes the outside choices, so samples of buyers only (as in Berry, Levinsohn and Pakes 2004) are outside this option’s scope; without one, the model is the choice among buyers, fitted to buyers’ shares, as in Train and Winston (2007, §2.1).

Where it is maximum likelihood. Two cases make the estimators coincide, and the tests pin both.

  • The logit with the sample’s shares. Without random coefficients P‾=P=Ps\bar P = P = P^s and ωns=1/S\omega_{ns} = 1/S, so the share equations are the constants’ first-order conditions (§5.8), and θ̂=θ̂ML\hat\theta = \hat\theta_{ML}.
  • The ML fit’s own shares as targets. Let ℓ*(θc)=max⁡δℓ(θc,δ)\ell^*(\theta_c) = \max_\delta \ell(\theta_c, \delta) be the profile of §5.8. For any θc\theta_c, ℓ(θc,δ(θc))≤ℓ*(θc)≤ℓ*(θ̂c,ML)=ℓ(θ̂ML)\ell(\theta_c, \delta(\theta_c)) \le \ell^*(\theta_c) \le \ell^*(\hat\theta_{c,ML}) = \ell(\hat\theta_{ML}), and with S*S^* the ML fit’s predicted shares on its estimation draws δ(θ̂c,ML)=δ̂ML\delta(\hat\theta_{c,ML}) = \hat\delta_{ML}, so the last bound is attained: θ̂ML\hat\theta_{ML} is the global constrained maximum, for any heterogeneity, panel or not.

Otherwise the estimators differ. With the sample’s own shares the share-constrained estimator is asymptotically at least as variable as ML, which is efficient among estimators that use the sample alone. With external shares known exactly it can be more precise, since the targets carry information the sample does not: in the Monte Carlo below its constants’ standard deviations are 0.51 to 0.60 of ML’s. It is not the efficient use of external shares: it gives the share equations infinite weight while they carry the sample’s covariate noise. The efficient combination stacks the two sources’ likelihoods or moments (Imbens and Lancaster 1994; Grieco, Murry, Pinkse and Sagl 2025).

The inner solve. At each θc\theta_c the optimizer tries, the share inversion of §8 runs on the estimation draws: each situation integrates over its decision maker’s draws (the draws of the panel likelihood, cube slice nn or the Halton block the kernels form for nn), Anderson acceleration, exponentials kept across evaluations and compensated sums at scale, to a largest log-share residual of tol_shares (10−1210^{-12}; Dubé, Fox and Su 2012). It starts from the best point’s constants advanced along the tangent δbest+Dbest(θc−θc,best)\delta_{best} + D_{best}(\theta_c - \theta_{c,best}), D=∂δ/∂θc⊤D = \partial\delta / \partial\theta_c^\top (below): 4 to 6 evaluations of the shares per solve in the Monte Carlo below (5.0 to 5.6 on three datasets of its cross-section design, against 6.8 to 7.1 from the best point’s constants alone), and 17 and 9.5 on the census- and claims-style data of the last paragraph. A solve that does not converge within max_iter (1000) evaluations is the overflow sentinel to the optimizer (§5.4), as in §5.8; the first, at the start values, from the targets’ log ratios log⁡(Sj*/S0*)\log(S^*_j/S^*_0) (or a supplied theta_init’s constants) with four times the budget, stops the fit where it fails (class choicer_blp_unconverged).

The objective and its gradient. The envelope theorem does not apply: at δ(θc)\delta(\theta_c) the gradient of −ℓ-\ell in the constants, gδg_\delta (§5.8), is not zero. With gcg_c its gradient in θc\theta_c and C=∂(∑n,twnP‾nt)/∂(θc,δ)⊤C = \partial(\sum_{n,t} w_n \bar P_{nt}) / \partial(\theta_c, \delta)^\top the Jacobian of the predicted counts (its blocks CcC_c and CδC_\delta on the free constants), the implicit function theorem gives D=−Cδ−1CcD = -C_\delta^{-1} C_c, and

∇θc[−ℓ(θc,δ(θc))]=gc+D⊤gδ=gc−Cc⊤λ,Cδλ=gδ: \nabla_{\theta_c} \bigl[-\ell(\theta_c, \delta(\theta_c))\bigr] = g_c + D^\top g_\delta = g_c - C_c^\top \lambda, \qquad C_\delta \lambda = g_\delta:

one solve with CδC_\delta (symmetric positive definite on the free constants). Per draw, ∂Pnts/∂ψ⊤\partial P^s_{nt} / \partial\psi^\top is the constants’ rows of the logit’s information ∑aPazaza⊤−z‾z‾⊤\sum_a P_a z_a z_a^\top - \bar z \bar z^\top, za=∂Va/∂ψz_a = \partial V_a / \partial\psi: PjP_j on the diagonal, −PjPk-P_j P_k off it and Pj(zcj−z‾c)P_j (z_{cj} - \bar z_c) against θc\theta_c. So CC is the constants’ rows of the complete-information part of §4.5’s assembly (Identities B and C, without Identity A’s missing information or second derivatives of utility), every draw weighted 1/S1/S. mxl_blp_jacobian_parallel() forms it in the gradient’s pass, at the cost of 2.2 to 3.5 gradient passes (one thread, 20,000 situations, S=100S = 100). An inner error e=δ̃−δ(θc)e = \tilde\delta - \delta(\theta_c) moves −ℓ-\ell by gδ⊤eg_\delta^\top e, a first-order error the optimizer would see as noise; the objective it is given is instead

f(θc)=−ℓ(θc,δ̃)−λ⊤(∑n,twnP‾nt(δ̃)−c*), f(\theta_c) = -\ell(\theta_c, \tilde\delta) - \lambda^\top \bigl(\textstyle\sum_{n,t} w_n \bar P_{nt}(\tilde\delta) - c^*\bigr),

with c*c^* the target counts: the residual’s term cancels gδ⊤eg_\delta^\top e to first order, so the error is O(∥e∥2)O(\|e\|^2). At tol_shares = 10−1210^{-12} both errors are at the objective’s rounding; at 10−810^{-8} the correction took the largest error over ten warm starts from 3×10−83 \times 10^{-8} to 10−1110^{-11}, on one dataset of the Monte Carlo’s cross-section design.

The variance. With g‾n=(g‾c,n,g‾δ,n)\bar g_n = (\bar g_{c,n}, \bar g_{\delta,n}) decision maker nn’s score of ℓ\ell (§4.5), the estimate solves the stacked equations ∑nwnmn=0\sum_n w_n m_n = 0, mn=g‾c,n+D⊤g‾δ,nm_n = \bar g_{c,n} + D^\top \bar g_{\delta,n} (the score projected along the constraint; the gradients of −ℓ-\ell above are g=−∑nwng‾ng = -\sum_n w_n \bar g_n), and ∑nwncn=0\sum_n w_n c_n = 0, cn=∑t(P‾nt−ynt)c_n = \sum_t (\bar P_{nt} - y_{nt}) for the sample’s shares or ∑t(P‾nt−Sm(n)*)\sum_t (\bar P_{nt} - S^*_{m(n)}) for supplied targets. With ℐ\mathcal I the information, ℐ̃=[ID⊤]ℐ[I;D]\tilde{\mathcal I} = [I\ D^\top]\, \mathcal I\, [I;\ D] (the curvature along the constraint) and R=(ℐcδ+D⊤ℐδδ)Cδ−1R = (\mathcal I_{c\delta} + D^\top \mathcal I_{\delta\delta}) C_\delta^{-1},

θ̂c−θc0≈ℐ̃−1∑nwn(mn+Rcn),δ̂−δ0≈D(θ̂c−θc0)−Cδ−1∑nwncn, \hat\theta_c - \theta_c^0 \approx \tilde{\mathcal I}^{-1} \sum_n w_n (m_n + R c_n), \qquad \hat\delta - \delta^0 \approx D(\hat\theta_c - \theta_c^0) - C_\delta^{-1} \sum_n w_n c_n,

and with Ω\Omega the variance of the stacked sums,

Vc=ℐ̃−1(Ω11+RΩ21+Ω12R⊤+RΩccR⊤)ℐ̃−1. V_c = \tilde{\mathcal I}^{-1} \bigl(\Omega_{11} + R\,\Omega_{21} + \Omega_{12} R^\top + R\, \Omega_{cc} R^\top\bigr) \tilde{\mathcal I}^{-1}.

DD’s own sampling error does not enter to first order: it multiplies the constants’ score, whose mean is zero at the truth.

se_method ℐ\mathcal I Ω11\Omega_{11}, Ω12\Omega_{12} Ωcc\Omega_{cc}, sample shares Ωcc\Omega_{cc}, supplied
"hessian" negated Hessian ℐ̃\tilde{\mathcal I}, 0 Cδ+KC_\delta + K ∑nwncncn⊤\sum_n w_n c_n c_n^\top
"bhhh" BHHH ℐ̃\tilde{\mathcal I}, 0 Cδ+KC_\delta + K ∑nwncncn⊤\sum_n w_n c_n c_n^\top
"sandwich" negated Hessian blocks of ∑nwn2vnvn⊤\sum_n w_n^2 v_n v_n^\top, vn=(mn,cn)v_n = (m_n, c_n) its cccc block its cccc block
"cluster" negated Hessian blocks of ∑gvgvg⊤\sum_g v_g v_g^\top, vg=∑n∈gwnvnv_g = \sum_{n \in g} w_n v_n its cccc block its cccc block

Under the model, Ω12=0\Omega_{12} = 0 (E[g‾ncn⊤]=−Cn⊤E[\bar g_n c_n^\top] = -C_n^\top, and [ID⊤]C⊤=0[I\ D^\top] C^\top = 0) and a decision maker’s chosen counts vary by Cδ,n+KnC_{\delta,n} + K_n, Kn=Covs(∑tPnts)K_n = \mathrm{Cov}_s(\sum_t P^s_{nt}) (the law of total variance over the equally weighted draws); supplied targets’ cnc_n vary only through the covariates. Targets estimated from an independent sample add diag(w(m(j)))VSdiag(w(m(j)))\mathrm{diag}(w_{(m(j))}) V_S \mathrm{diag}(w_{(m(j))}) to Ωcc\Omega_{cc}, with VSV_S = target_vcov and w(m(j))w_{(m(j))} free constant jj’s market weight (for shares from Nm*N^*_m independent choices in market mm, VS=(diag(Sm*)−Sm*Sm*⊤)/Nm*V_S = (\mathrm{diag}(S^*_m) - S^*_m S^{*\top}_m)/N^*_m on its block: a diagonal VSV_S drops the covariances the shares’ adding up imposes, and overstates the variance of the market’s total inside share, so of the constants’ common level). In the logit with the sample’s shares, cn=−g‾δ,nc_n = -\bar g_{\delta,n} and R=0R = 0, and ℐ̃\tilde{\mathcal I} is §5.8’s Schur complement 𝒮\mathcal S: "hessian" (where Ωcc=Cδ+K=ℐδδ\Omega_{cc} = C_\delta + K = \mathcal I_{\delta\delta}), "sandwich" and "cluster" return ML’s variances. All four types assume correctly specified probabilities: "sandwich" and "cluster" relax the information equality (sampling weights, dependence), not the model. Under misspecification the constants’ expected score is not zero, and two terms omitted here become first order: ∑jE[g‾δj]∂2δj/∂θc∂θc⊤\sum_j E[\bar g_{\delta j}]\, \partial^2 \delta_j / \partial\theta_c \partial\theta_c^\top in the bread and DD’s sampling error in the meat. Simulation noise in the share equations is omitted too, as maximum simulated likelihood omits its own.

What the fit records. fit$share_targets holds the targets and their source; fit$optimizer$concentration the outer and inner work, the residuals, λ̂=Cδ−1gδ\hat\lambda = C_\delta^{-1} g_\delta at the estimate (the derivative of −ℓ-\ell with respect to each free constant’s target count: positive where the micro data would put that alternative’s share below its target) and the outer certificate 12∇⊤ℐ̃−1∇\frac12 \nabla^\top \tilde{\mathcal I}^{-1} \nabla over the mean weight. ℐ̃\tilde{\mathcal I} is not the curvature of ℓ(θc,δ(θc))\ell(\theta_c, \delta(\theta_c)), which adds ∑jg‾δj∂2δj/∂θc∂θc⊤\sum_j \bar g_{\delta j}\, \partial^2 \delta_j / \partial\theta_c \partial\theta_c^\top, g‾δ\bar g_\delta the constants’ score at the solution. That score is not zero even with the sample’s own shares (§5.8: the share equations average each situation’s probabilities over the draws, the score weights each draw by its posterior given the choices). The term is Op(N)O_p(\sqrt N) against the O(N)O(N) of ℐ̃\tilde{\mathcal I}, which is why the variance can omit it, but it can turn a minimum’s curvature positive. So where the optimizer reports a failure or stops at its cap and the certificate decides whether the point is a maximum, the decrement is taken in the objective’s own Hessian, from central differences of its gradient (two solves of the constants per continuous parameter), whatever the standard errors. logLik(), AIC and BIC are the likelihood at the constrained estimate, and a likelihood-ratio test between such fits is not χ2\chi^2. conditional_tastes()’s identities rest on the scores of μ\mu and Σ\Sigma being zero, which they are not here. predict(type = "shares") and blp(fit, targets) reproduce the targets, as shares of the whole sample (fit$share_targets$engine), and δ̂\hat\delta only in a cross-section: a panel’s predictions integrate per situation, the estimator per decision maker.

A Monte Carlo. Family F10 of the validation suite (Rscript _validation/mc.R --family=F10) simulates four designs of 2,000 choice situations among ten alternatives, each situation offering a random subset of at least half of them (of its market’s): a cross-section with an outside option; 500 decision makers with four situations each, with an outside option; a cross-section without one; and two markets of five alternatives, with an outside option and targets within each. Utility is x1+(−1+e1n)w1+(0.5+0.7e2n)w2+δj+εntjx_1 + (-1 + e_{1n}) w_1 + (0.5 + 0.7 e_{2n}) w_2 + \delta_j + \varepsilon_{ntj}, with x1∼N(0,1)x_1 \sim N(0, 1), wk∼N(0,1.52)w_k \sim N(0, 1.5^2), ekn∼N(0,1)e_{kn} \sim N(0, 1) and δ\delta evenly spaced from −1-1 to 11, and every fit uses S=100S = 100 draws. Each of 1,000 replications per design fits ML (certified by Newton steps) and this estimator with the sample’s shares, with the population’s exact shares, and with shares estimated from 10N10N and NN choices per market (NN = 2,000 divided by the number of markets), their multinomial covariance as target_vcov. Over the four designs:

targets constants’ SD / ML’s θc\theta_c’s SD / ML’s 95% coverage largest bias / SD, θc\theta_c
(ML itself) 1 1 0.946–0.954 0.05–0.10
the sample’s shares 1.02–1.05 1.00 0.944–0.956 0.06–0.10
exact shares 0.51–0.60 0.99–1.00 0.945–0.951 0.07–0.13
shares from 10N10N choices 0.60–0.70 0.99–1.00 0.945–0.953 0.08–0.15
shares from NN choices 1.15–1.31 0.99–1.00 0.941–0.954 0.16–0.29

Coverage is the Hessian form’s. The SD ratios and coverage average over θc\theta_c’s parameters or over the constants; the bias column is the largest over θc\theta_c’s, each with a Monte Carlo standard error of about 0.03. Every inner solve converged, and every fit’s outer decrement was below 3×10−123 \times 10^{-12} (one optimizer stop carried nloptr’s generic failure code, at a decrement of 2.5×10−142.5 \times 10^{-14}). Averaged over each group’s parameters, the mean standard error was within 2.3% of the estimates’ standard deviation for every estimator, ML’s included, and the sandwich covered as the Hessian form did. Exact shares roughly halve the constants’ standard deviations and shares from ten times the sample’s choices cut them by a third, while θc\theta_c stays as precise as under ML; shares from as many choices as the sample’s make the constants noisier than ML’s. Noisy targets bias θc\theta_c toward zero, coefficients and standard deviations alike, by an amount that grows with the targets’ variance: in the panel, beyond exact targets’ bias, by 0.16 standard deviations for w1w_1’s mean with shares from NN choices and by 0.02 with 10N10N. That costs the intervals at most a point of coverage.

Cost. On the census- and claims-style designs of _benchmarks/scale/ (fit_study.R, cell blp_lbfgs: the sample’s shares, no outside option, correlated coefficients, S=50S = 50, 11 threads), a fit took about as long as the profile likelihood of §5.8: 16 s against 16 s on 20,000 students in 24,560 situations with 2,511 constants (the joint fit 4.4 s), and 26 s against 23 s on 1,500 patients in 17,339 situations with 499 constants (the joint fit 14 s). It took 21 and 46 outer evaluations, each one share inversion and one pass of the share Jacobian, and the passes took about half the time on census and two-thirds on claims.

Code reference: .mxl_blp_solver(), .mxl_blp_setup(), .blp_resolve_targets(), .blp_vcov() and .blp_fit_vcov() in R/mxlogit_blp.R; mxl_blp_jacobian_parallel(), mxl_blp_meat_parallel(), blp_check_targets() and mxl_blp_solve() in src/mxlogit.cpp; spd_solve() in src/newton.cpp.


6. Elasticity Computation

6.1 Elasticity Definitions

The elasticity measures the percentage change in choice probability with respect to a percentage change in an attribute. For the mixed logit model, we compute elasticities using simulated probabilities.

Simulated Probability:

P‾ij=1S∑s=1SPijs \bar{P}_{ij} = \frac{1}{S} \sum_{s=1}^{S} P_{ij}^s

6.2 Elasticity for Fixed Coefficients (X Variables)

For a variable kk in the design matrix XX with fixed coefficient βk\beta_k:

Own-Elasticity (elasticity of PijP_{ij} with respect to xijkx_{ijk}):

Ejjk=∂P‾ij∂xijk⋅xijkP‾ij E_{jj}^k = \frac{\partial \bar{P}_{ij}}{\partial x_{ijk}} \cdot \frac{x_{ijk}}{\bar{P}_{ij}}

Using the chain rule and averaging over draws:

Ejjk=1P‾ij⋅1S∑s=1Sβk⋅xijk⋅Pijs⋅(1−Pijs) E_{jj}^k = \frac{1}{\bar{P}_{ij}} \cdot \frac{1}{S} \sum_{s=1}^{S} \beta_k \cdot x_{ijk} \cdot P_{ij}^s \cdot (1 - P_{ij}^s)

Cross-Elasticity (elasticity of PijP_{ij} with respect to ximkx_{imk} where m≠jm \neq j):

Ejmk=1P‾ij⋅1S∑s=1S(−βk⋅ximk⋅Pijs⋅Pims) E_{jm}^k = \frac{1}{\bar{P}_{ij}} \cdot \frac{1}{S} \sum_{s=1}^{S} \left( -\beta_k \cdot x_{imk} \cdot P_{ij}^s \cdot P_{im}^s \right)

6.3 Elasticity for Random Coefficients (W Variables)

For a variable kk in the design matrix WW with random coefficient, the effective coefficient for individual ii and draw ss is:

βiks=μk*+γiks* \beta_{ik}^s = \mu_k^* + \gamma_{ik}^{s*}

where: - For normal distribution: μk*=μk\mu_k^* = \mu_k, γiks*=γiks\gamma_{ik}^{s*} = \gamma_{ik}^s - For log-normal distribution: μk*=exp⁡(μk)\mu_k^* = \exp(\mu_k), γiks*=exp⁡(γiks)\gamma_{ik}^{s*} = \exp(\gamma_{ik}^s)

Own-Elasticity:

Ejjk=1P‾ij⋅1S∑s=1Sβiks⋅wijk⋅Pijs⋅(1−Pijs) E_{jj}^k = \frac{1}{\bar{P}_{ij}} \cdot \frac{1}{S} \sum_{s=1}^{S} \beta_{ik}^s \cdot w_{ijk} \cdot P_{ij}^s \cdot (1 - P_{ij}^s)

Cross-Elasticity:

Ejmk=1P‾ij⋅1S∑s=1S(−βiks⋅wimk⋅Pijs⋅Pims) E_{jm}^k = \frac{1}{\bar{P}_{ij}} \cdot \frac{1}{S} \sum_{s=1}^{S} \left( -\beta_{ik}^s \cdot w_{imk} \cdot P_{ij}^s \cdot P_{im}^s \right)

6.4 Weighted Average of Individual Elasticities

The implementation computes weighted average individual probability elasticities:

E‾jmk=∑i=1Nwi⋅Eijmk∑i=1Nwi \bar{E}_{jm}^k = \frac{\sum_{i=1}^{N} w_i \cdot E_{ijm}^k}{\sum_{i=1}^{N} w_i}

This is not generally the elasticity of an aggregate share under a common market-level perturbation. Rows index responding alternatives and columns perturbed alternatives. The kernel sets a contribution to zero when P‾ij≤10−12\bar P_{ij}\leq10^{-12} and divides the accumulated matrix by total weight over all situations; with ordinary finite utilities, the threshold is only a numerical guard.

Code reference: mxlogit.cpp:mxl_elasticities_parallel


7. Diversion Ratios

7.1 Definition

The attribute-based diversion ratio from alternative jj to alternative mm with respect to attribute kk is

DRj→m(xk)=−∂sm/∂xj,k∂sj/∂xj,k, \mathrm{DR}_{j \to m}(x_k) \;=\; -\,\frac{\partial s_m / \partial x_{j,k}}{\partial s_j / \partial x_{j,k}},

where sj=1∑iwi∑iwiP‾ijs_j = \frac{1}{\sum_i w_i}\sum_i w_i \bar{P}_{ij} is the aggregate share. It is the fraction of demand lost by jj that is captured by mm when a marginal change in jj’s attribute kk reduces sjs_j.

7.2 MNL Special Case (β cancels)

For multinomial logit, the per-individual derivatives of the choice probability are

∂Pij∂xj,k=βkPij(1−Pij),∂Pim∂xj,k=−βkPijPim(m≠j). \frac{\partial P_{ij}}{\partial x_{j,k}} = \beta_k\, P_{ij}(1 - P_{ij}), \qquad \frac{\partial P_{im}}{\partial x_{j,k}} = -\beta_k\, P_{ij}P_{im} \quad (m \neq j).

Letting w‾=∑iwi\bar{w} = \sum_i w_i, the aggregate share derivatives are

∂sj∂xj,k=1w‾∑iwi∂Pij∂xj,k=βk⋅1w‾∑iwiPij(1−Pij), \frac{\partial s_j}{\partial x_{j,k}} \;=\; \frac{1}{\bar{w}}\sum_i w_i \frac{\partial P_{ij}}{\partial x_{j,k}} \;=\; \beta_k \cdot \frac{1}{\bar{w}}\sum_i w_i\, P_{ij}(1 - P_{ij}),

∂sm∂xj,k=−βk⋅1w‾∑iwiPijPim. \frac{\partial s_m}{\partial x_{j,k}} \;=\; -\,\beta_k \cdot \frac{1}{\bar{w}}\sum_i w_i\, P_{ij} P_{im}.

Because βk\beta_k is a scalar constant, it pulls out of both weighted sums. Forming the ratio,

DRj→mMNL=−∂sm/∂xj,k∂sj/∂xj,k=βk⋅1w‾∑iwiPijPimβk⋅1w‾∑iwiPij(1−Pij)=∑iwiPijPim∑iwiPij(1−Pij), \mathrm{DR}_{j \to m}^{\text{MNL}} \;=\; -\,\frac{\partial s_m / \partial x_{j,k}}{\partial s_j / \partial x_{j,k}} \;=\; \frac{\beta_k \cdot \tfrac{1}{\bar{w}}\sum_i w_i\, P_{ij} P_{im}}{\beta_k \cdot \tfrac{1}{\bar{w}}\sum_i w_i\, P_{ij}(1 - P_{ij})} \;=\; \frac{\sum_i w_i\, P_{ij} P_{im}}{\sum_i w_i\, P_{ij}(1 - P_{ij})},

with βk\beta_k and 1/w‾1/\bar{w} cancelling. The result is independent of which attribute is perturbed — this is a special property of MNL.

7.3 MXL: β Does Not Cancel

For mixed logit the realized coefficient on a random variable for individual ii at draw ss is

βiks=μk*+γiks*, \beta_{ik}^s = \mu_k^* + \gamma_{ik}^{s*},

with the same distribution transforms as in §6.3. The derivatives evaluated at draw ss are

∂Pijs∂xj,k=βiksPijs(1−Pijs),∂Pims∂xj,k=−βiksPijsPims. \frac{\partial P_{ij}^s}{\partial x_{j,k}} = \beta_{ik}^s\, P_{ij}^s\,(1 - P_{ij}^s), \qquad \frac{\partial P_{im}^s}{\partial x_{j,k}} = -\beta_{ik}^s\, P_{ij}^s P_{im}^s.

Integrating over draws gives ∂P‾ij/∂xj,k=1S∑sβiksPijs(1−Pijs)\partial \bar{P}_{ij}/\partial x_{j,k} = \frac{1}{S}\sum_s \beta_{ik}^s P_{ij}^s(1-P_{ij}^s) and similarly for the cross term. Aggregating over individuals, the diversion ratio is

DRj→mMXL(xk)=∑iwi⋅1S∑sβiksPijsPims∑iwi⋅1S∑sβiksPijs(1−Pijs). \mathrm{DR}_{j \to m}^{\text{MXL}}(x_k) \;=\; \frac{\sum_i w_i \cdot \tfrac{1}{S}\sum_s \beta_{ik}^s\, P_{ij}^s P_{im}^s}{\sum_i w_i \cdot \tfrac{1}{S}\sum_s \beta_{ik}^s\, P_{ij}^s (1 - P_{ij}^s)}.

Because βiks\beta_{ik}^s varies across (i,s)(i, s), it cannot be pulled out of the sums and does not cancel. The MXL diversion ratio therefore depends on the perturbed variable. For a variable with a fixed coefficient the dependence again vanishes (βiks\beta_{ik}^s is a constant scalar); for a random-coefficient variable it does not.

7.4 Properties

Column-sum identity. For each jj, ∑m≠jDRj→m=1\sum_{m \neq j} \mathrm{DR}_{j \to m} = 1. This follows from ∑m≠jPijsPims=Pijs(1−Pijs)\sum_{m \neq j} P_{ij}^s P_{im}^s = P_{ij}^s(1 - P_{ij}^s), which holds inside the draw sum and is preserved through the same βiks\beta_{ik}^s weight on both sides.

Sign of βiks\beta_{ik}^s. If βiks\beta_{ik}^s is constant negative (e.g. a fixed price coefficient), both numerator and denominator change sign together and the ratio is unchanged — DR is non-negative. With a random coefficient of mixed sign across (i,s)(i, s), terms can partially cancel; the aggregate ratio is still well-defined but can be numerically sensitive. The implementation guards against this with an abs(denominator) > 1e-15 check.

Implementation. Cross-products PijsPimsP_{ij}^s P_{im}^s and Pijs(1−Pijs)P_{ij}^s(1 - P_{ij}^s) must be accumulated inside the per-draw loop, multiplied by βiks\beta_{ik}^s at that point, and only then averaged across draws and weighted by wiw_i. Computing (P¯ij)(P¯im)(\overline{P}_{ij})(\overline{P}_{im}) or βiks\beta_{ik}^s-weighted averages computed outside the draw loop is biased.

Code reference: mxlogit.cpp:mxl_diversion_ratios_parallel


8. BLP Share Inversion

8.1 Problem Statement

Given observed market shares sjs_j, find the ASC parameters δj\delta_j such that the model-predicted shares match the observed shares:

ŝj(δ)=sj∀j \hat{s}_j(\delta) = s_j \quad \forall j

where ŝj(δ)\hat{s}_j(\delta) is the predicted market share for alternative jj computed using simulated probabilities.

8.2 Simulated Market Shares

The predicted market share for alternative jj is:

ŝj(δ)=1∑iwi∑i=1Nwi⋅P‾ij \hat{s}_j(\delta) = \frac{1}{\sum_i w_i} \sum_{i=1}^{N} w_i \cdot \bar{P}_{ij}

where:

P‾ij=1S∑s=1SPijs(δ) \bar{P}_{ij} = \frac{1}{S} \sum_{s=1}^{S} P_{ij}^s(\delta)

and Pijs(δ)P_{ij}^s(\delta) is the probability for individual ii, alternative jj, and draw ss, which depends on δ\delta through the utility specification.

8.3 The Iteration

The inversion is the multinomial logit’s (§7 of the multinomial logit article), with the simulated probabilities: its identification, its checks of the targets, its step and its stopping rule are the same. With r=log⁡s−log⁡ŝ(δ)r = \log s - \log \hat{s}(\delta) (ss the targets), bb the outside option or, without one, the reference of jj’s component, and ρj=rj−rb\rho_j = r_j - r_b, each iteration shifts the component’s free constants together by τ\tau, then takes the exact diagonal on the rest:

δj←δj+τ+ρj−hjτAjj/ŝj+Cjb/ŝb,Ajj=∑iwi1S∑sPijs(1−Pijs)∑iwi,Cjb=∑iwi1S∑sPijsPibs∑iwi, \delta_j \leftarrow \delta_j + \tau + \frac{\rho_j - h_j \tau}{A_{jj}/\hat{s}_j + C_{jb}/\hat{s}_b}, \qquad A_{jj} = \frac{\sum_i w_i \frac{1}{S}\sum_{s} P_{ij}^s(1 - P_{ij}^s)}{\sum_i w_i}, \qquad C_{jb} = \frac{\sum_i w_i \frac{1}{S}\sum_{s} P_{ij}^s P_{ib}^s}{\sum_i w_i},

where −hj-h_j, hj=Cjb/ŝj+∑kCkb/ŝbh_j = C_{jb}/\hat{s}_j + \sum_k C_{kb}/\hat{s}_b, is the exact rate at which ρj\rho_j responds to the common shift and τ\tau solves ∑jŝj(ρj−hjτ)=0\sum_j \hat{s}_j(\rho_j - h_j \tau) = 0. The denominator is the exact diagonal of the derivative of ρj\rho_j: the logit’s terms, draw by draw, then averaged. Within a draw the shares are a logit’s, so the simulated share Jacobian is symmetric with rows summing to zero, which makes hjh_j exact, and every alternative is a substitute for every other, so Cjb≥0C_{jb} \ge 0. Berry, Levinsohn and Pakes’s (1995) contraction, method = "original", takes rjr_j itself (rj−rbr_j - r_b without an outside option, the reference held). The safeguards are the logit’s: a step is at most max⁡(|ρj|,5)\max(|\rho_j|, 5) in magnitude; where a component’s outside share underflows to zero the step drops the outside correction and the common shift for that iteration; a step whose shares are not finite is halved; under method = "simple", an iteration whose best residual has not improved for 30 iterations (a stall) halves its later steps, returning to its best point first only where its residual at the stall exceeds twice the best (once per best point), and otherwise doubling the iterations the next stall waits for; and once 100 iterations fail to halve the best residual, the contraction races the step from the start (stopping when it is behind and slow, and going on whenever the step alone fails to halve its best residual within 200 iterations), the first to reach the tolerance returning. method = "anderson", the default, and method = "squarem" accelerate the step as in the logit (Anderson acceleration and SQUAREM; §7.3 of the multinomial logit article).

The iteration stops at the first evaluation whose largest absolute residual (relative to each component’s reference without an outside option; with one, each component’s outside share’s included) is at most tol, and returns that point; max_iter counts evaluations of the shares. When it stops otherwise it returns the evaluated point of smallest residual and warns with class choicer_blp_unconverged. A start at which some predicted share underflows to zero is such a stop (status "non_finite_start"), not an error.

8.4 Implementation Details

  1. No outside option. delta may be the full length-JJ vector or the free length-(J−1)(J-1) block; the latter is padded with a leading zero. The start is normalized by subtracting its first element, each component’s reference is held there, and the returned vector has length JJ with its first entry zero.
  2. Outside option. delta has length JJ and contains inside ASCs only; target_shares has J+1J + 1 entries, the outside option’s first. Every target share, the outside option’s included, must be positive, which is checked before any evaluation.
  3. The draws. JJ is the largest alternative code. An integer or logical cube of draws is converted to double first.

The public blp.choicer_mxl() method resolves the fitted draw metadata through .mxl_pred_draws(): store-mode fits have the kernel form the plain-Halton points on the fly (§5.5), while generate-mode fits pass the original seed, permutation mode, and SS to the inversion. Share inversion therefore draws from the same deterministic Halton sequence as the fit, one block per choice situation (for a cross-sectional fit, the estimation draws themselves; a panel fit’s estimation draws are per decision maker instead, §5.5). Neither the draws nor LL change across iterations, so blp() keeps each situation’s Γi=Lηi\Gamma_i = L\eta_i, with its log-normal rows exponentiated, from the first evaluation of the shares on, when the draws are formed on the fly (stored draws with up to 128 random coefficients, or generated ones), the NN blocks take at most keep_draws_bytes (8KwSN8K_wSN bytes; 4 GiB by default) and the allocation succeeds; otherwise every evaluation forms them again. The kept blocks are the values each evaluation would compute, so by themselves they leave the shares, and the returned δ\delta, as they are. What they leave of keep_draws_bytes (all of it where the draws are read from a cube) keeps, as in the logit (§7.6 of the multinomial logit article), each draw’s exponentials of the utilities at an evaluation’s δ0\delta^0, shifted by the draw’s largest, for as many choice situations as fit (8S8S bytes per row, and per situation with an outside option) within half the memory limit less what the call holds. A later evaluation at δ\delta forms those situations’ PijsP_{ij}^s as the kept values times aj=exp⁡(δj−δj0)a_j = \exp(\delta_j - \delta^0_j) over their sum, without the draws, Γi\Gamma_i, WiΓiW_i\Gamma_i or any exponential; with every situation kept, an evaluation takes about a third of the time. These probabilities agree with those formed afresh to within rounding, so the budget can move the returned δ\delta as two solutions within tol differ (by about tol times the conditioning). The exported mxl_blp_contraction() keeps nothing.

On the estimation draws. blp() and the exports draw one block per choice situation, the prediction’s draws. Given each decision maker’s number of situations (Ti, internal), each situation instead reads its decision maker’s block, cube slice nn or the Halton block the estimation kernels form for nn, so that the shares integrate over the panel likelihood’s own draws: concentrate_out_asc = "blp" solves its share equations so (§5.9). The Gamma cache stays per situation, a decision maker’s held once for each of its situations within the budget.

Code reference: MxlBlpModel and mxl_blp_solve() in src/mxlogit.cpp, over src/blp_engine.h.


9. Choice-Based Sampling and WESML Weighting

9.1 Endogenous Stratified (Choice-Based) Sampling

In random (exogenous) sampling each choice situation is drawn independently of its outcome. Under choice-based (endogenous stratified) sampling the strata are defined by the chosen alternative jij_i, and situations are drawn with stratum-specific frequencies – for example, deliberately oversampling the few individuals who chose a rare alternative. Let

Q(j)=population share of alternative j,H(j)=sample share of choosers of j. Q(j) = \text{population share of alternative } j, \qquad H(j) = \text{sample share of choosers of } j .

Maximum simulated likelihood applied naively to such a sample is generally inconsistent for the population parameters. (A known special case: in a pure multinomial logit with a full set of alternative-specific constants, choice-based sampling biases only the constants; in general – and for the mixed logit – both the slopes and the constants are affected.)

9.2 The WESML Weighted Log-Likelihood

Manski and Lerman (1977) restore consistency by weighting each situation’s contribution by the ratio of population to sample share of its chosen alternative,

wi=Q(ji)H(ji), w_i = \frac{Q(j_i)}{H(j_i)},

which yields the weighted simulated log-likelihood already defined in $$2.3,

ℓW(θ)=∑i=1Nwilog⁡P‾iji(θ). \ell^{W}(\theta) = \sum_{i=1}^{N} w_i \log \bar{P}_{i j_i}(\theta).

The maximizer θ̂\hat{\theta} is invariant to multiplying every wiw_i by a common positive constant (the objective is merely rescaled), so the weights may be normalized to mean 1 without changing the estimates.

9.3 Robust (Sandwich) Variance: the w2w^2 Meat

WESML is a weighted M-estimator solving ∑iwisi(θ̂)=0\sum_i w_i\, s_i(\hat{\theta}) = 0, where si=∂log⁡P‾iji/∂θs_i = \partial \log \bar{P}_{i j_i} / \partial\theta is the per-situation score ($$3) and Hi=∂2log⁡P‾iji/∂θ∂θ⊤H_i = \partial^2 \log\bar{P}_{i j_i}/\partial\theta\,\partial\theta^\top its Hessian ($$4). Its robust (Huber–White) asymptotic variance is the sandwich

V=A−1BA−1,A=∑iwi(−Hi),B=∑iwi2sisi⊤. V = A^{-1} B A^{-1}, \qquad A = \sum_{i} w_i\,(-H_i), \qquad B = \sum_{i} w_i^{2}\, s_i s_i^{\top}.

The weight enters the bread AA linearly, but the meat BB carries the weight squared: the contribution of situation ii to the estimating equation is wisiw_i s_i, so the variance of that contribution involves (wisi)(wisi)⊤=wi2sisi⊤(w_i s_i)(w_i s_i)^{\top} = w_i^{2}\, s_i s_i^{\top}.

This is exactly why the two naive variances are wrong under weighting:

  • the inverse-Hessian A−1A^{-1} assumes the information-matrix equality A=BA = B, which fails once the wiw_i are non-degenerate;
  • the ordinary BHHH/OPG (∑iwisisi⊤)−1\big(\sum_i w_i\, s_i s_i^{\top}\big)^{-1} uses the weight to the first power, not the second.

A consistency check: rescaling all weights by a constant cc sends A→cAA \to cA and B→c2BB \to c^{2} B, so V→(cA)−1(c2B)(cA)−1=A−1BA−1V \to (cA)^{-1}(c^{2}B)(cA)^{-1} = A^{-1}BA^{-1} – the sandwich is invariant to the weight scale, whereas A−1→c−1A−1A^{-1} \to c^{-1} A^{-1} is not.

9.4 Scope: Robust vs. Design-Based Variance

The estimator implemented here is the robust weighted-M-estimator (Huber–White) variance, whose meat B=∑iwi2sisi⊤B = \sum_i w_i^{2}\, s_i s_i^{\top} is uncentered. This is the asymptotic variance under variable-probability sampling (each population unit sampled independently with a stratum-dependent probability). Under a fixed-quota design – a prescribed number drawn from each choice stratum, i.e. standard stratified sampling – the design-based variance centers the scores within strata and may be smaller; that stratum-centered (design-based) estimator is out of scope here. See Manski & McFadden (1981) for the design-based treatment and Cosslett (1981) for the asymptotically efficient conditional-maximum-likelihood alternative.

9.5 Implementation and Cluster-Robust Extension

The variance routes never form the U×nparamsU\times n_\text{params} matrix SS of the units’ weight-free scores: two million decision makers by 2,620 parameters would take 42 GB. With the bread A=𝚖𝚡𝚕_𝚑𝚎𝚜𝚜𝚒𝚊𝚗_𝚙𝚊𝚛𝚊𝚕𝚕𝚎𝚕(θ̂;w)A=\texttt{mxl\_hessian\_parallel}(\widehat\theta;w), the robust meat is the BHHH kernel with squared weights, summed over each unit’s compact block (§4.4.6),

B=∑iwi2sisiT=𝚖𝚡𝚕_𝚋𝚑𝚑𝚑_𝚙𝚊𝚛𝚊𝚕𝚕𝚎𝚕(θ̂;w2), B=\sum_i w_i^2 s_i s_i^T=\texttt{mxl\_bhhh\_parallel}(\widehat\theta;w^2),

and vcov(type = "bhhh") is the same kernel with weights ww. One set of draws, stored or generated from the fit’s draw metadata, serves both matrices, which are combined as V=A−1BA−1V = A^{-1} B A^{-1}, computed equilibrated by two solves against the LULU factors of the equilibrated bread, without forming an inverse (§5.6). For clusters g(i)g(i), the meat

Bcluster=∑g(∑i:g(i)=gwisi)(∑i:g(i)=gwisi)T B_{\mathrm{cluster}}=\sum_g \left(\sum_{i:g(i)=g}w_is_i\right) \left(\sum_{i:g(i)=g}w_is_i\right)^T

is mxl_cluster_meat_parallel: the units are sorted by cluster and cut into chunks of similar numbers of rows, about four per thread; each cluster’s weighted scores are summed over the parameters they touch, and a cluster that a chunk boundary cuts is summed in parts, added after the parallel region, so a few large clusters still keep every thread busy. There is no finite-cluster correction. mxl_scores_parallel (internal; no variance route of the mixed logit calls it) still returns SS for small problems and refuses a matrix above 2 GiB. Clustering changes inference, not the estimand of whichever likelihood was fit (§1.6): cross-sectional by default, or the panel likelihood of §2.6 under person_col, where cluster labels must nest decision makers. It is only §9.1–§9.2’s WESML weighting that is defined solely for the cross-sectional likelihood: wiw_i is a weight that varies with the alternative chosen in situation ii, whereas the panel objective of §2.6 requires one weight wnw_n per decision maker, constant across their situations, so a choice-based weight cannot enter it — run_mxlogit(..., person_col = ) therefore rejects choice-based-sampling provenance. The sandwich variance of §9.3 and its cluster-robust extension here in §9.5 carry no such restriction: both are built from whichever likelihood unit’s score was actually fit (a decision maker under person_col, a choice situation otherwise), so they apply to panel fits too, with cluster labels then nesting decision makers exactly as above. The variance is computed in natural units and inverted equilibrated (§5.6); the Cholesky-to-Σ\Sigma delta method (§5.2) applies on top, exactly as for any stored vcov. User-facing entry points: wesml_weights() and sample_by_choice() build the weights; run_mxlogit(..., se_method = "sandwich") estimates with the robust variance; wesml_vcov() returns it post hoc.

Code reference: mxl_bhhh_parallel(), mxl_cluster_meat_parallel() and mxl_scores_parallel() in src/mxlogit.cpp; .assemble_score_vcov(), .mxl_score_meat() (the multinomial and nested logits’ .logit_meat(); .score_meat() forms the same meats from the score matrix, as the tests’ reference) and .sandwich_combine() in R/classes.R; equilibrated_sandwich() in src/vcov.cpp; wesml_weights() / sample_by_choice() in R/sampling.R.


10. Willingness to Pay

10.1 Fixed Coefficients

When the price variable has a fixed coefficient α=βp\alpha = \beta_p (a column of XX), the WTP for any fixed attribute kk is the usual marginal rate of substitution, with the same analytic delta-method SE as in the MNL case (§8 of the MNL document):

WTPk=−βkα,∇g=(−1/αβk/α2),SÊ=∇gTV̂(k,p)∇g \mathrm{WTP}_k = -\frac{\beta_k}{\alpha}, \qquad \nabla g = \begin{pmatrix} -1/\alpha \\ \beta_k/\alpha^2 \end{pmatrix}, \qquad \widehat{\mathrm{SE}} = \sqrt{\nabla g^T \hat{V}_{(k,p)} \nabla g}

10.2 Random Coefficients

For a random attribute coefficient βk(η)\beta_k(\eta) the WTP is itself a random variable −βk(η)/α-\beta_k(\eta)/\alpha, and a scalar summary must be chosen. The summaries reported follow the package’s parameterization (§1.3):

Normal RC with estimated mean (rc_mean = TRUE, rc_dist[k] = 0): βk=μk+(Lη)k\beta_k = \mu_k + (L\eta)_k has mean μk\mu_k, so the mean WTP is

WTPk=−μkα \mathrm{WTP}_k = -\frac{\mu_k}{\alpha}

with the linear-ratio gradient of §10.1 applied to (μk,α)(\mu_k, \alpha).

Shifted log-normal RC with estimated location (rc_mean = TRUE, rc_dist[k] = 1): βk=exp⁡(μk)+exp⁡((Lη)k)\beta_k = \exp(\mu_k) + \exp((L\eta)_k). Since βk\beta_k is monotone in the normal draw (Lη)k(L\eta)_k and the median of exp⁡((Lη)k)\exp((L\eta)_k) is exp⁡(0)=1\exp(0) = 1, the population median is exp⁡(μk)+1\exp(\mu_k) + 1 and the median WTP is

WTPkmed=−exp⁡(μk)+1α,∂g∂μk=−exp⁡(μk)α,∂g∂α=exp⁡(μk)+1α2 \mathrm{WTP}_k^{\text{med}} = -\frac{\exp(\mu_k) + 1}{\alpha}, \qquad \frac{\partial g}{\partial \mu_k} = -\frac{\exp(\mu_k)}{\alpha}, \qquad \frac{\partial g}{\partial \alpha} = \frac{\exp(\mu_k) + 1}{\alpha^2}

The median is preferred to the mean, exp⁡(μk)+exp⁡(σk2/2)\exp(\mu_k) + \exp(\sigma_k^2/2) with σk2=(LLT)kk\sigma_k^2 = (LL^T)_{kk}, because the mean is highly sensitive to the estimated variance (a thin right tail dominates it).

Shifted log-normal RC without location (rc_mean = FALSE, rc_dist[k] = 1): βk=exp⁡((Lη)k)\beta_k = \exp((L\eta)_k) has median exactly 1, so the median WTP is −1/α-1/\alpha with ∂g/∂α=1/α2\partial g / \partial \alpha = 1/\alpha^2 — uncertainty comes solely from α̂\hat\alpha.

Normal RC without mean (rc_mean = FALSE, rc_dist[k] = 0): the mean is 0 by construction; no WTP row is reported.

10.3 Random Price Coefficients Are Rejected

The implementation requires price_var to be a fixed-coefficient column of XX and rejects every random price coefficient. For a normally distributed denominator this is substantively important: positive density arbitrarily close to zero generally makes E|βk/α|E|\beta_k/\alpha| diverge. That no-moment argument does not cover every possible mixing distribution—for example, inverse moments of a sign-constrained log-normal can be finite—but wtp() still does not simulate the joint ratio distribution, account for its correlation, or report its quantiles. Thus the rejection is both a specification guard for normal price mixing and an implementation boundary for other distributions. WTP-space estimation (Train and Weeks 2005) is not implemented in choicer v0.2.0.

Code reference: R/wtp.R


11. Consumer Surplus and the Simulated Logsum

11.1 Expected Logsum

Conditional on a coefficient realization β(η)\beta(\eta), expected maximum utility over the iid type-I extreme-value errors is log⁡∑jexp⁡(Vij(η))+γE\log \sum_j \exp(V_{ij}(\eta))+\gamma_E, where γE\gamma_E is Euler’s constant. As in the other package methods, logsum() omits this scenario-invariant additive constant. The mixed-logit logsum integrates over the mixing distribution:

logsumi=𝔼η[log∑j∈Ciexp(Vij(η))]≈1S∑s=1Slog⁡∑j∈Ciexp⁡(Vijs) \mathrm{logsum}_i = \mathbb{E}_\eta\left[\log \sum_{j \in C_i} \exp\big(V_{ij}(\eta)\big)\right] \;\approx\; \frac{1}{S} \sum_{s=1}^{S} \log \sum_{j \in C_i} \exp\big(V_{ij}^s\big)

simulated over the fit’s Halton sequence, one block per choice situation (regenerated from the stored draw metadata; for a cross-sectional fit, the estimation draws themselves; a panel fit’s estimation draws are per decision maker instead), with the outside option contributing exp⁡(0)\exp(0) in each draw’s sum when present, and the max-subtraction trick applied per draw.

Order of operations matters. The log-sum-exp function is convex, so by Jensen’s inequality

1S∑slse(Vs)≥lse(1S∑sVs), \frac{1}{S}\sum_s \mathrm{lse}(V^s) \;\geq\; \mathrm{lse}\left(\frac{1}{S}\sum_s V^s\right),

with strict inequality when the draws induce non-common variation in utilities. A nondegenerate Σ\Sigma alone is not sufficient for strictness: if the random contribution is the same additive shift for every available alternative, log-sum-exp is affine in that direction and equality holds. A dedicated C++ kernel (mxl_logsum) computes the per-draw logsum and then averages across draws.

11.2 Expected Consumer Surplus

With a fixed price coefficient α\alpha (the marginal utility of income −α-\alpha is then nonrandom):

𝔼[CSi]=logsumi−α \mathbb{E}[CS_i] = \frac{\mathrm{logsum}_i}{-\alpha}

A random price coefficient is rejected for the capability and specification reasons in §10.3; the divergence argument applies directly to normal price mixing but not to every sign-constrained distribution. As in MNL, the formula assumes no income effects. The routine does not enforce α<0\alpha<0; the economic interpretation as positive marginal utility of income requires the analyst to verify that sign. CS levels inherit the ASC normalization, so only differences across counterfactual scenarios are meaningful. The implementation reports point estimates for MXL (the delta-method SE of mean CS is currently provided for MNL only); Krinsky–Robb resampling is a practical interval method.

Code reference: R/surplus.R; kernel in src/mxlogit.cpp (mxl_logsum)


12. Goodness of Fit

The measures are model-agnostic and identical to §10 of the MNL document, computed from the simulated log-likelihood and simulated choice probabilities:

  • McFadden pseudo R²: R2=1−ℓ(θ̂)/ℓ0R^2 = 1 - \ell(\hat\theta)/\ell_0, adjusted Radj2=1−(ℓ(θ̂)−K)/ℓ0R^2_{\text{adj}} = 1 - (\ell(\hat\theta) - K)/\ell_0, with KK counting all estimated parameters (β\beta, μ\mu, Cholesky block, ASCs).
  • Equal-shares null (default; exact for unbalanced choice sets and weights): ℓ0=−∑iwilog⁡(Mi+𝟏outside)\ell_0 = -\sum_i w_i \log(M_i + \mathbf{1}_{\text{outside}}).
  • Market-shares null (constants-only closed form): ℓ0=c∑jNjlog⁡(sj)\ell_0 = c\sum_j N_j \log(s_j), where cc is the common weight, valid only for identical choice-set composition and uniform weights. The constants-only model contains no random coefficients, so the MNL closed form applies unchanged.
  • Hit rate: weighted share of choice situations whose observed choice has the highest simulated probability P‾ij=1S∑sPijs\bar{P}_{ij} = \frac{1}{S}\sum_s P_{ij}^s; with an outside option, the kernel returns the simulated outside probability directly and the outside good competes for the maximum.

Code reference: R/gof.R


13. Conditional (Individual-Level) Tastes

conditional_tastes() recovers, for each likelihood unit, the mean and standard deviation of the random coefficients conditional on the choices observed — the same posterior draw weights ωns\omega_{ns} that build the panel score and Hessian (§2.6, §3.5, §4.5), now applied to the realized taste draws themselves rather than to the score:

E[βn∣yn,xn;θ̂]≈∑s=1Sωnsβns,SD(βn∣yn,xn;θ̂)≈∑s=1Sωns(βns−β‾n)2, E[\beta_n \mid y_n, x_n; \hat\theta] \approx \sum_{s=1}^S \omega_{ns}\, \beta_{ns}, \qquad \mathrm{SD}(\beta_n \mid y_n, x_n; \hat\theta) \approx \sqrt{\sum_{s=1}^S \omega_{ns}\,(\beta_{ns} - \bar\beta_n)^2},

with βns=μ⋅*+γns*\beta_{ns} = \mu^*_\cdot + \gamma_{ns}^{*} the taste at draw ss on the scale it enters utility (§1.1–1.3), β‾n\bar\beta_n the conditional mean above, and yny_n the sequence of choices observed across nn’s TnT_n situations (Revelt and Train 2000; Train 2009, ch. 11). Tn≡1T_n \equiv 1 defines the same object for the default cross-sectional fit, one choice situation at a time; a single choice reveals comparatively little, so its conditional mean sits closer to the population mean than a panel decision maker’s typically does.

Not βn\beta_n itself. E[βn∣yn,xn;θ̂]E[\beta_n \mid y_n, x_n; \hat\theta] is the average taste among decision makers who would make nn’s choices, not nn’s own realized βn\beta_n — it shrinks toward the population mean, more so the fewer situations nn contributes. Consequently the variance across decision makers of the conditional means understates Σ\Sigma: by the law of total variance, Var(β)=Varn(E[βn∣yn])⏟𝚛𝚎𝚟𝚎𝚊𝚕𝚎𝚍+En[Var(βn∣yn)]⏟𝚛𝚎𝚜𝚒𝚍𝚞𝚊𝚕, \mathrm{Var}(\beta) = \underbrace{\mathrm{Var}_n\bigl(E[\beta_n \mid y_n]\bigr)}_{\texttt{revealed}} \;+\; \underbrace{E_n\bigl[\mathrm{Var}(\beta_n \mid y_n)\bigr]}_{\texttt{residual}}, so the revealed share (wnw_n-weighted variance of the conditional means) and the residual share (wnw_n-weighted average of the conditional variances) should sum to about one. These are numerical checks, not specification tests: for normal random coefficients with free means, both this identity and 𝚖𝚎𝚊𝚗_𝚌𝚘𝚗𝚍≈𝚖𝚘𝚍𝚎𝚕_𝚖𝚎𝚊𝚗\texttt{mean\_cond} \approx \texttt{model\_mean} are the likelihood’s first-order conditions for μ\mu and Σ\Sigma — the population mean and variance are exactly the ω\omega-weighted mean and variance the estimating equations set to zero at θ̂\hat\theta — so they hold at a converged maximum whatever the true taste distribution. A departure points to too few draws SS or an unconverged θ̂\hat\theta; when a coefficient’s mean is fixed at zero (rc_mean = FALSE and no fixed coefficient on the same variable), a mean_cond far from zero can also flag that restriction. For log-normal coefficients the variance identity holds on the underlying normal scale γ\gamma, not the utility scale β\beta reported here, so expect departures of a few percent in either direction at moderate SS. print.choicer_tastes() reports these alongside model_mean/model_sd (the moments of f(β∣θ̂)f(\beta \mid \hat\theta) itself, §1.2–1.3) and mean_cond/sd_cond (the weighted mean and SD of the conditional means across units).

Implementation. mxl_conditional_tastes_parallel() reuses mxl_unit_load() and mxl_unit_simulate() (§3.5) to obtain ωns\omega_{ns} per unit, then forms γ‾n=Γnωn\bar\gamma_n = \Gamma_n\,\omega_n over the unit’s Kw×SK_w \times S realized-deviation block Γn\Gamma_n (the same γns*\gamma_{ns}^{*} values already used to build VijsV_{ij}^s, §1.1) and adds back the mean block, β‾n=μ*+γ‾n\bar\beta_n = \mu^* + \bar\gamma_n; the SD uses the analogous weighted second moment of Γn\Gamma_n around γ‾n\bar\gamma_n. Both are matrix-vector products — no second pass is needed, since neither quantity is an outer product. Units whose log-likelihood is non-finite — possible only when their utilities overflow (§4.5, §5.4) — return NA and are dropped from the population summary.

Code reference: mxl_conditional_tastes_parallel() in src/mxlogit.cpp; R/conditional_tastes.R.


References

  • Train, K. E. (2009). Discrete Choice Methods with Simulation. Cambridge University Press.
  • Revelt, D., & Train, K. (1998). Mixed logit with repeated choices: households’ choices of appliance efficiency level. Review of Economics and Statistics, 80(4), 647-657.
  • Revelt, D., & Train, K. (2000). Customer-specific taste parameters and mixed logit: households’ choice of electricity supplier. Working Paper E00-274, Department of Economics, University of California, Berkeley.
  • Louis, T. A. (1982). Finding the observed information matrix when using the EM algorithm. Journal of the Royal Statistical Society: Series B, 44(2), 226-233.
  • Bertsekas, D. P. (1982). Projected Newton methods for optimization problems with simple constraints. SIAM Journal on Control and Optimization, 20(2), 221-246.
  • Dennis, J. E., & Schnabel, R. B. (1983). Numerical Methods for Unconstrained Optimization and Nonlinear Equations. Prentice-Hall.
  • Hager, W. W., & Zhang, H. (2005). A new conjugate gradient method with guaranteed descent and an efficient line search. SIAM Journal on Optimization, 16(1), 170-192.
  • Levenberg, K. (1944). A method for the solution of certain non-linear problems in least squares. Quarterly of Applied Mathematics, 2(2), 164-168.
  • Marquardt, D. W. (1963). An algorithm for least-squares estimation of nonlinear parameters. Journal of the Society for Industrial and Applied Mathematics, 11(2), 431-441.
  • Train, K., & Weeks, M. (2005). Discrete choice models in preference space and willingness-to-pay space. In R. Scarpa & A. Alberini (Eds.), Applications of Simulation Methods in Environmental and Resource Economics (pp. 1-16). Springer.
  • McFadden, D., & Train, K. (2000). Mixed MNL models for discrete response. Journal of Applied Econometrics, 15(5), 447-470.
  • Berry, S., Levinsohn, J., & Pakes, A. (1995). Automobile prices in market equilibrium. Econometrica, 63(4), 841-890.
  • Berry, S. (1994). Estimating discrete-choice models of product differentiation. RAND Journal of Economics, 25(2), 242-262.
  • Berry, S., Levinsohn, J., & Pakes, A. (2004). Differentiated products demand systems from a combination of micro and macro data: the new car market. Journal of Political Economy, 112(1), 68-105.
  • Petrin, A. (2002). Quantifying the benefits of new products: the case of the minivan. Journal of Political Economy, 110(4), 705-729.
  • Train, K. E., & Winston, C. (2007). Vehicle choice behavior and the declining market share of U.S. automakers. International Economic Review, 48(4), 1469-1496.
  • Imbens, G. W., & Lancaster, T. (1994). Combining micro and macro data in microeconometric models. Review of Economic Studies, 61(4), 655-680.
  • Dubé, J.-P., Fox, J. T., & Su, C.-L. (2012). Improving the numerical performance of static and dynamic aggregate discrete choice random coefficients demand estimation. Econometrica, 80(5), 2231-2267.
  • Grieco, P. L. E., Murry, C., Pinkse, J., & Sagl, S. (2025). Optimal estimation of discrete choice demand models with consumer and product data. NBER Working Paper 33397.
  • Bhat, C. R. (2003). Simulation estimation of mixed discrete choice models using randomized and scrambled Halton sequences. Transportation Research Part B, 37(9), 837-855.
  • Gouriéroux, C., & Monfort, A. (1996). Simulation-Based Econometric Methods. Oxford University Press.
  • Hajivassiliou, V. A., & Ruud, P. A. (1994). Classical estimation methods for LDV models using simulation. In R. F. Engle & D. L. McFadden (Eds.), Handbook of Econometrics, Vol. 4 (pp. 2383-2441). North-Holland.
  • Lee, L.-F. (1995). Asymptotic bias in simulated maximum likelihood estimation of discrete choice models. Econometric Theory, 11(3), 437-483.
  • Manski, C. F., & Lerman, S. R. (1977). The estimation of choice probabilities from choice based samples. Econometrica, 45(8), 1977-1988.
  • Manski, C. F., & McFadden, D. (1981). Alternative estimators and sample designs for discrete choice analysis. In Structural Analysis of Discrete Data with Econometric Applications. MIT Press.
  • Cosslett, S. R. (1981). Maximum likelihood estimator for choice-based samples. Econometrica, 49(5), 1289-1316.
  • Wooldridge, J. M. (2010). Econometric Analysis of Cross Section and Panel Data (2nd ed.), Section 13.8. MIT Press.