blp_contraction() for the nested logit: the constants at which
the nested logit's shares equal the targets, with \(\beta\) and the
dissimilarities held fixed.
Usage
nl_blp_contraction(
delta,
target_shares,
X,
beta,
lambda,
alt_idx,
nest_idx,
M,
weights,
include_outside_option = FALSE,
damping = 1,
tol = 1e-12,
max_iter = 1000L,
method = "anderson",
control = list()
)Arguments
- delta
Start: one constant per inside alternative (\(J\)). Without an outside option the first alternative's constant is the normalization: the start is shifted so that it is 0.
Target shares: \(J\) of them, or \(J + 1\) with the outside option's first. They must be positive and sum to 1 (to within about \(1.5 \times 10^{-8}\), the remainder rescaled away).
- X
sum(M) x K design matrix with covariates. M[i] x K matrix for individual i.
- beta
K x 1 vector with fixed coefficients.
- lambda
The dissimilarity of every nest of
nest_idx(max(nest_idx)entries, a singleton nest's 1), each positive and finite.- alt_idx
sum(M) x 1 vector with indices of alternatives; 1-based indexing.
- nest_idx
J x 1 vector with nest indices for each alternative; 1-based indexing.
- M
N x 1 vector with number of alternatives for each individual.
- weights
N x 1 vector with weights for each observation (finite and non-negative, with a finite sum).
- include_outside_option
Whether there is an outside option, with utility normalized to 0 and dissimilarity 1.
- damping
Multiplies each step, in (0, 1] (default 1). The steps are scaled by the share Jacobian's diagonal, whose within-nest term \((1 - P(j|g))/\lambda_g\) keeps them stable under strong nesting, so damping is seldom needed.
- tol
Convergence tolerance on the largest absolute log-share residual \(|\log S_j - \log s_j(\delta)|\) over the alternatives (the outside option's included; see Details). Default 1e-12. Rounding in the sums of the shares over choice situations sets a floor under the residual that grows with the situations each thread adds: at \(2^{22}\) (about 4.2 million) situations of four alternatives on one thread, about \(10^{-14}\), and \(7 \times 10^{-14}\) with an outside option. Above that the sums are compensated (Kahan's summation), which brings the floor to the precision of a double: \(2 \times 10^{-16}\) or less at \(10^8\) rows on one thread, where plain sums left \(2 \times 10^{-13}\). A
tolbelow the floor cannot be met. (The shares ofpredict()keep plain sums, so a residual recomputed from them keeps their floor.)- max_iter
The most evaluations of the shares (default 1000).
- method
The iteration:
"anderson"(the default), Anderson acceleration of the two-level step of Details;"simple", that step alone;"original", Berry, Levinsohn and Pakes's contraction; or"squarem", SQUAREM on the step.- control
A list of the method's settings.
"anderson"takesmemory, how many differences between successive iterates it keeps (a whole number from 1 to 100, cut to the number of free constants; default 10), andmax_condition, the largest condition estimate it accepts for their least squares (at least 1; default \(10^8\))."squarem"takesscheme, its step length's formula (1, 2 or 3; default 3),step_minandstep_max, the step length's bounds at the start (positive; default 1 and 1), andstep_factor, what the upper bound is multiplied by when the step length reaches it and divided by when such a step fails (at least 1; default 4): PyBLP's defaults. The other methods take none.
Details
The step's diagonal is built from the nested logit's derivative,
\(A_{jj} = \sum_i w_i P_{ij}((1 - P_i(j|g))/\lambda_g + P_i(j|g) -
P_{ij}) / \sum_i w_i\) for alternative \(j\) in nest \(g\), and,
without an outside option, its exact co-choice with the reference
\(b\), \(\sum_i w_i P_{ib}((1 - \lambda_g)/\lambda_g\, P_i(j|g)\,
1\{b \in g\} + P_{ij}) / \sum_i w_i\), which also give the exact rate at
which the residuals respond to the common shift (the nested logit's share
Jacobian is symmetric, with rows summing to zero). In a nest with
\(\lambda_g > 1\) nest-mates are complements where the nest's choice
probability is below \(1 - 1/\lambda_g\), and two safeguards apply. An alternative whose co-choice with the reference is
negative (the reference in its nest) has a diagonal of at least 1: its
exact one nets out the complementarity and can be near 0 or negative.
And the diagonal is at least \((1 + \lambda_g)/(2\lambda_g)\) times the
one the same probabilities give with \(\lambda_g = 1\): a common shift
of a nest's constants moves its shares as in a logit, while the exact
diagonal is smaller by the within-nest \(1/\lambda_g\), so its step
would overshoot that shift by up to \(\lambda_g\) and, for
\(\lambda_g\) above 2, diverge. See blp_contraction() for
the iteration, its stopping rule and its checks.
Examples
# \donttest{
library(data.table)
set.seed(42)
N <- 50; J <- 4
dt <- data.table(id = rep(1:N, each = J), alt = rep(1:J, N))
dt[, `:=`(x1 = rnorm(.N), x2 = rnorm(.N))]
#> id alt x1 x2
#> <int> <int> <num> <num>
#> 1: 1 1 1.3709584 -2.0009292
#> 2: 1 2 -0.5646982 0.3337772
#> 3: 1 3 0.3631284 1.1713251
#> 4: 1 4 0.6328626 2.0595392
#> 5: 2 1 0.4042683 -1.3768616
#> ---
#> 196: 49 4 1.0857749 1.0965134
#> 197: 50 1 0.4037749 0.4420131
#> 198: 50 2 0.5864875 0.2410163
#> 199: 50 3 1.8152284 -0.2556077
#> 200: 50 4 0.1288214 0.9310329
dt[, nest := ifelse(alt <= 2, "A", "B")]
#> id alt x1 x2 nest
#> <int> <int> <num> <num> <char>
#> 1: 1 1 1.3709584 -2.0009292 A
#> 2: 1 2 -0.5646982 0.3337772 A
#> 3: 1 3 0.3631284 1.1713251 B
#> 4: 1 4 0.6328626 2.0595392 B
#> 5: 2 1 0.4042683 -1.3768616 A
#> ---
#> 196: 49 4 1.0857749 1.0965134 B
#> 197: 50 1 0.4037749 0.4420131 A
#> 198: 50 2 0.5864875 0.2410163 A
#> 199: 50 3 1.8152284 -0.2556077 B
#> 200: 50 4 0.1288214 0.9310329 B
dt[, choice := 0L]
#> id alt x1 x2 nest choice
#> <int> <int> <num> <num> <char> <int>
#> 1: 1 1 1.3709584 -2.0009292 A 0
#> 2: 1 2 -0.5646982 0.3337772 A 0
#> 3: 1 3 0.3631284 1.1713251 B 0
#> 4: 1 4 0.6328626 2.0595392 B 0
#> 5: 2 1 0.4042683 -1.3768616 A 0
#> ---
#> 196: 49 4 1.0857749 1.0965134 B 0
#> 197: 50 1 0.4037749 0.4420131 A 0
#> 198: 50 2 0.5864875 0.2410163 A 0
#> 199: 50 3 1.8152284 -0.2556077 B 0
#> 200: 50 4 0.1288214 0.9310329 B 0
dt[, choice := sample(c(1L, rep(0L, J - 1))), by = id]
#> id alt x1 x2 nest choice
#> <int> <int> <num> <num> <char> <int>
#> 1: 1 1 1.3709584 -2.0009292 A 0
#> 2: 1 2 -0.5646982 0.3337772 A 0
#> 3: 1 3 0.3631284 1.1713251 B 0
#> 4: 1 4 0.6328626 2.0595392 B 1
#> 5: 2 1 0.4042683 -1.3768616 A 0
#> ---
#> 196: 49 4 1.0857749 1.0965134 B 1
#> 197: 50 1 0.4037749 0.4420131 A 0
#> 198: 50 2 0.5864875 0.2410163 A 0
#> 199: 50 3 1.8152284 -0.2556077 B 0
#> 200: 50 4 0.1288214 0.9310329 B 1
fit <- run_nestlogit(dt, "id", "alt", "choice", c("x1", "x2"), "nest")
#> Optimization run time 0h:0m:0.01s
beta <- coef(fit)[fit$param_map$beta]
lambda <- rep(1, length(unique(fit$data$nest_idx)))
lambda[as.integer(names(which(table(fit$data$nest_idx) > 1)))] <-
coef(fit)[fit$param_map$lambda]
target <- as.numeric(predict(fit, type = "shares"))
delta <- nl_blp_contraction(rep(0, J), rev(target), fit$data$X, beta,
lambda, fit$data$alt_idx, fit$data$nest_idx, fit$data$M, fit$data$weights)
delta
#> [,1]
#> [1,] 0.0000
#> [2,] 114.5576
#> [3,] 163.1524
#> [4,] 163.0163
# }