Finds the alternative-specific constants \(\delta\) at which the model's
market shares \(s_j(\delta) = \sum_i w_i P_{ij}(\delta) / \sum_i w_i\)
equal target shares \(S_j\), holding the other parameters fixed: the
inversion of Berry (1994) and Berry, Levinsohn and Pakes (1995)
doi:10.2307/2171802
behind calibration to aggregate shares and
counterfactual share matching. blp() runs it on a fitted
model; these functions take the model's inputs directly.
Usage
blp_contraction(
delta,
target_shares,
X,
beta,
alt_idx,
M,
weights,
include_outside_option = FALSE,
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 model parameters.
- alt_idx
sum(M) x 1 vector with indices of alternatives within each choice set; 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 (its rows are not in the data).
- 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.
Value
The constants, an n x 1 matrix (\(J\) rows, inside alternatives only); without an outside option the first is 0.
Details
With \(r = \log S - \log s(\delta)\), the iteration solves
\(\rho_j = r_j - r_b = 0\) for every free constant, the log-share
residual against a base: the outside option (Fukasawa's 2024
outside-share correction) or, without one, the reference of the
alternative's group. (The alternatives that share choice situations,
directly or through others, form a connected component, whose constants
the shares identify only up to a common level, so its smallest code's
constant is held at its start.) The default, method = "anderson",
extrapolates the step below, which method = "simple" takes alone.
Each iteration evaluates the shares once and takes a step in two parts.
First a common shift \(\tau\) of the group's free constants, to which
each \(\rho_j\) responds at the rate
\(-h_j\), \(h_j = C_{jb}/s_j + \sum_k C_{kb}/s_b\) exactly, where
\(C_{jb} = \sum_i w_i P_{ij} P_{ib} / \sum_i w_i\) is the share-weighted
co-choice of \(j\) and the base; \(\tau\) solves
\(\sum_j s_j (\rho_j - h_j \tau) = 0\), to first order the shift that
matches the base's share. Then the rest of each residual, divided by the
exact diagonal of its derivative, \(A_{jj}/s_j + C_{jb}/s_b\), where
\(A_{jj}\) is the diagonal of the share Jacobian
\(\partial s/\partial \delta\). The diagonal alone would misjudge the
common shift, and diverge where the base's share is small and each
alternative dominates choice situations of its own. Berry, Levinsohn and
Pakes's (1995) contraction (blp(method = "original")) moves each
constant by its own residual, \(r_j\) (without an outside option
\(r_j - r_b\), the reference held). A step is at most 5 log units long,
or \(|\rho_j|\) where that is longer; a step whose shares are not finite
is halved, up to ten times. Under "simple", an iteration whose
best residual has not improved for 30 iterations (a stall) halves its
later steps; it returns
to its best point only when its residual at the stall exceeds twice the
best (it diverges), once per best point, and otherwise goes on from where
it is, each such stall doubling the iterations the next one waits for,
so that constants climbing back from where their shares barely respond
are not pulled back. And once 100 iterations fail to halve the best
residual, or a trial point's shares stay non-finite after every halving
(the step then stops, and the contraction goes on alone), Berry,
Levinsohn and Pakes's contraction joins from the start: the two take
steps in turn, each with its halvings, and the first to meet tol
returns. The contraction stops if 100 of its own evaluations fail to
halve its best residual while the step's best is lower; the step then
has 200 iterations at a time to halve its own best residual, and
whenever it fails to (or its trial point's shares stay non-finite), the
contraction goes on from where it stopped. A divided step can amplify
the small residual of an alternative that nearly owns its choice
situations into a step that wrecks its neighbours' shares there, and
then oscillate, or carry the constants of alternatives that make up the
whole choice set of some choice situations so far down that the
residual barely responds to their common level; the contraction's step
is never amplified. So wherever the contraction converges in \(E\)
evaluations, the step converges within \(T + 4E\), and within
\(T + 2E\) where the contraction halves its best residual within every
100 of its evaluations, \(T\) being the evaluations before the
contraction joined; the bound leaves out the step's halvings after the
contraction joined and any 200 iterations in which the step alone
halved its best residual.
method = "anderson" accelerates the same step (Anderson
acceleration of type II; Walker and Ni 2011). Write \(f(\delta)\) for
the step at \(\delta\), and \(\Delta X\) and \(\Delta F\) for the
last memory differences between successive iterates and between
their steps. Each iteration finds the coefficients \(\gamma\) whose
combination of past changes in the step best cancels the current step,
\(\min_\gamma \| f - \Delta F \gamma \|_2\), and moves to
\(\delta - \Delta X \gamma + f - \Delta F \gamma\), with the step's part
multiplied by damping for the nested logit: the capped step
above, extrapolated. On a linear map, undamped and with unlimited
memory, this is essentially GMRES. The oldest differences are dropped
while the least squares' condition estimate exceeds
max_condition. When five iterations bring no residual below 0.9
times the best so far, or the shares at an extrapolated point are not
finite, the iteration clears its differences and takes the step from
where it is, or from its best point where its residual has grown past
twice the best (once per best point), halving that step while its
shares are not finite. Where a component's outside share underflows,
it takes the step above unextrapolated until the share returns. It
races Berry, Levinsohn and Pakes's
contraction as the step does, with the same windows; each of its
iterations takes one evaluation of the shares besides the trial points
whose shares are not finite, which the bound leaves out as it leaves out
the step's halvings, so the same bound holds.
method = "squarem" extrapolates the same step by SQUAREM
(Varadhan and Roland 2008), PyBLP's default (Conlon and Gortmaker 2020).
With \(G(\delta) = \delta + f(\delta)\) (\(f\) multiplied by
damping for the nested logit), each cycle takes
\(\delta_1 = G(\delta_0)\) and \(\delta_2 = G(\delta_1)\), sets
\(r = \delta_1 - \delta_0\) and \(v = \delta_2 - 2\delta_1 + \delta_0\),
moves to \(\delta_0 + 2\alpha r + \alpha^2 v\) with
\(\alpha = \|r\|/\|v\|\) (scheme 3) bounded below by
step_min and above by step_max, and takes one more step
\(G\) from there: three evaluations of the shares per cycle. With the
default bounds the first cycle's point is \(\delta_2\) itself. When
\(\alpha\) reaches step_max at a point whose residual is
finite (for \(\delta_2\), a step not halved), the bound is
multiplied by step_factor, so the extrapolation lengthens while
it succeeds; where the shares at an extrapolated point are not finite,
the cycle takes \(\delta_2\) instead, and a bound that \(\alpha\)
had reached is divided by step_factor, not below its start.
step_min stays. It races the contraction too, one evaluation per
turn besides the trial points that fail, so the same bound holds.
The iteration stops at the first evaluation whose largest absolute
log-share residual is at most tol (residuals relative to the
component's reference without an outside option; with one, the outside
option's own residual included), and returns that point. The error in
\(\delta\) is then of order tol times the conditioning of the
share equations, which segmented markets (choice sets with few
alternatives in common) can raise to hundreds or more. When it does not
converge within max_iter evaluations, or stops at shares that are
not finite, it warns (class choicer_blp_unconverged; the
condition's convergence element holds the status, iterations,
evaluations and residual) and returns the evaluated point of smallest
residual (the start when max_iter = 0).
After the first evaluation, the shares are read from exponentials kept
across evaluations wherever those allow it. The first keeps the
exponentials of every row's utility at its constants \(\delta^0\),
scaled within each choice situation (within each of its nests for the
nested logit, and for each draw for the mixed logit); a later one
multiplies them by \(\exp(\delta_j - \delta^0_j)\) (for the nested
logit by \(\exp((\delta_j - \delta^0_j)/\lambda_g)\), with one logarithm
and one exponential per offered nest), one exponential per alternative
in place of one per row: 0.4 to 0.65 of the time of an evaluation afresh
(about a third for the mixed logit, in blp()). Where a
constant has moved more than 50 log units from \(\delta^0\) (for the
nested logit, 50 times its nest's dissimilarity), or a share falls so low
(below about \(10^{-248}\) at the largest distance) that utilities
which underflowed when they were kept could matter, the evaluation
exponentiates every row afresh and keeps its exponentials in their
place. The shares are those of an evaluation afresh to within rounding,
so the iteration's path, and the returned \(\delta\), can move as two
solutions within tol differ (by about tol times the
conditioning, above). The multinomial and nested logits' kept values
take one double per row (the multinomial logit's also one per choice
situation with an outside option), within half the memory limit (see
set_num_threads) less what the call already holds; where
they do not fit, every evaluation is made afresh. Reading the limit, a
malformed options(choicer.max_memory = ) stops the call, as it
stops the estimation routines. mxl_blp_contraction() keeps nothing
across evaluations; blp() on a mixed logit keeps the
exponentials of as many choice situations as fit within its
keep_draws_bytes after the draws (see
blp.choicer_mxl).
Before any evaluation it checks that the targets can be met: every alternative is offered in a choice situation of positive weight; the targets of each component add up to the weight of its situations (to 1 with an outside option, and then leave a positive share to the outside option in each component); and each target lies strictly between the weight of the situations that offer the alternative alone and the weight of those that offer it. These conditions are necessary, not sufficient: a target that passes them may still not be attainable, and then the iteration does not converge.
References
Berry, S. T. (1994). Estimating discrete-choice models of product differentiation. RAND Journal of Economics, 25(2), 242–262.
Berry, S., Levinsohn, J., and Pakes, A. (1995). Automobile prices in market equilibrium. Econometrica, 63(4), 841–890.
Fukasawa, T. (2024). Fast and simple inner-loop algorithms of static / dynamic BLP estimations. arXiv:2404.04494.
Conlon, C., and Gortmaker, J. (2020). Best practices for differentiated products demand estimation with PyBLP. RAND Journal of Economics, 51(4), 1108–1161.
Varadhan, R., and Roland, C. (2008). Simple and globally convergent methods for accelerating the convergence of any EM algorithm. Scandinavian Journal of Statistics, 35(2), 335–353.
Walker, H. F., and Ni, P. (2011). Anderson acceleration for fixed-point iterations. SIAM Journal on Numerical Analysis, 49(4), 1715–1735.
Examples
# \donttest{
library(data.table)
set.seed(42)
N <- 50; J <- 3
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 -0.04069848
#> 2: 1 2 -0.5646982 -1.55154482
#> 3: 1 3 0.3631284 1.16716955
#> 4: 2 1 0.6328626 -0.27364570
#> 5: 2 2 0.4042683 -0.46784532
#> ---
#> 146: 49 2 1.1133860 -0.47733551
#> 147: 49 3 -0.4809928 -0.16626149
#> 148: 50 1 -0.4331690 0.86256338
#> 149: 50 2 0.6968626 0.09734049
#> 150: 50 3 -1.0563684 -1.62561674
dt[, choice := 0L]
#> id alt x1 x2 choice
#> <int> <int> <num> <num> <int>
#> 1: 1 1 1.3709584 -0.04069848 0
#> 2: 1 2 -0.5646982 -1.55154482 0
#> 3: 1 3 0.3631284 1.16716955 0
#> 4: 2 1 0.6328626 -0.27364570 0
#> 5: 2 2 0.4042683 -0.46784532 0
#> ---
#> 146: 49 2 1.1133860 -0.47733551 0
#> 147: 49 3 -0.4809928 -0.16626149 0
#> 148: 50 1 -0.4331690 0.86256338 0
#> 149: 50 2 0.6968626 0.09734049 0
#> 150: 50 3 -1.0563684 -1.62561674 0
dt[, choice := sample(c(1L, rep(0L, J - 1))), by = id]
#> id alt x1 x2 choice
#> <int> <int> <num> <num> <int>
#> 1: 1 1 1.3709584 -0.04069848 0
#> 2: 1 2 -0.5646982 -1.55154482 0
#> 3: 1 3 0.3631284 1.16716955 1
#> 4: 2 1 0.6328626 -0.27364570 0
#> 5: 2 2 0.4042683 -0.46784532 0
#> ---
#> 146: 49 2 1.1133860 -0.47733551 0
#> 147: 49 3 -0.4809928 -0.16626149 0
#> 148: 50 1 -0.4331690 0.86256338 1
#> 149: 50 2 0.6968626 0.09734049 0
#> 150: 50 3 -1.0563684 -1.62561674 0
fit <- run_mnlogit(dt, "id", "alt", "choice", c("x1", "x2"))
#> Optimization run time 0h:0m:0s
beta <- coef(fit)[fit$param_map$beta]
delta <- blp_contraction(rep(0, J), rep(1/J, J), fit$data$X,
beta, fit$data$alt_idx, fit$data$M, fit$data$weights)
delta
#> [,1]
#> [1,] 0.00000000
#> [2,] -0.03013133
#> [3,] 0.01204404
# }