Skip to contents

blp_contraction() for the mixed logit: the constants at which the simulated shares \(s_j(\delta) = \sum_i w_i S^{-1} \sum_r P_{ijr}(\delta) / \sum_i w_i\) equal the targets, over the draws given, with the other parameters fixed. The step's \(A_{jj}\) and \(C_{jb}\) average each draw's terms, \(A_{jj} = \sum_i w_i S^{-1} \sum_r P_{ijr}(1 - P_{ijr}) / \sum_i w_i\) and \(C_{jb} = \sum_i w_i S^{-1} \sum_r P_{ijr} P_{ibr} / \sum_i w_i\).

Usage

mxl_blp_contraction(
  delta,
  target_shares,
  X,
  W,
  beta,
  mu,
  L_params,
  alt_idx,
  M,
  weights,
  eta_draws,
  rc_dist,
  rc_correlation = TRUE,
  rc_mean = FALSE,
  include_outside_option = FALSE,
  tol = 1e-12,
  max_iter = 1000L,
  gen_seed = -1L,
  gen_scramble = 1L,
  gen_S = 0L,
  method = "anderson",
  control = list()
)

Arguments

delta

Start: one constant per inside alternative (\(J\), the largest code in alt_idx); without an outside option also the \(J - 1\) free ones, after the first's normalized 0.

target_shares

Target shares: \(J\), or \(J + 1\) with the outside option's first; positive, summing to 1.

X

sum(M) x K design matrix with covariates. M[i] x K matrix for individual i.

W

design matrix for random coefficients; sum(M_i) x K_w or J x K_w

beta

K_x vector with fixed coefficients

mu

K_w vector with mean parameters (raw, transformed if log-normal); used as given when rc_mean = FALSE

L_params

Cholesky parameters vector

alt_idx

sum(M) x 1 vector with indices of alternatives; 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)

eta_draws

Array with draws, K_w x S x N (an integer or logical array is converted to double first)

rc_dist

K_w vector indicating distribution (0=normal, 1=log-normal)

rc_correlation

whether random coefficients are correlated

rc_mean

whether mu parameters represent means (TRUE) or are zero (FALSE)

include_outside_option

whether outside option is included

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 tol below the floor cannot be met. (The shares of predict() keep plain sums, so a residual recomputed from them keeps their floor.)

max_iter

The most evaluations of the shares (default 1000).

gen_seed

Integer. < 0 (default) uses the materialized eta_draws cube; >= 0 forms the draws on the fly (see gen_scramble), with this master seed for the digit permutations of gen_scramble = 1.

gen_scramble

Integer mode of the draws formed on the fly: 0 = identity permutations (plain Halton) through the generator's inverse normal CDF, 1 = seeded position-wise digit permutations, 2 = identity permutations through R's qnorm(), the store-mode draws of get_halton_normals formed without its cube (bit for bit where randtoolbox and choicer are compiled with the same floating-point contraction). Other values are an error.

gen_S

Integer number of draws per individual, used only when gen_seed >= 0.

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" takes memory, how many differences between successive iterates it keeps (a whole number from 1 to 100, cut to the number of free constants; default 10), and max_condition, the largest condition estimate it accepts for their least squares (at least 1; default \(10^8\)). "squarem" takes scheme, its step length's formula (1, 2 or 3; default 3), step_min and step_max, the step length's bounds at the start (positive; default 1 and 1), and step_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); without an outside option the first is 0.

Details

Nothing is kept across evaluations of the shares: with gen_seed >= 0 the draws are formed again at every evaluation, and every evaluation exponentiates every row afresh. blp() on a fitted model runs the same inversion but keeps the draws, and then the exponentials of as many choice situations as fit, across evaluations within its keep_draws_bytes (see blp.choicer_mxl). See blp_contraction() for the iteration, its stopping rule and its checks.

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), w1 = rnorm(.N))]
#>         id   alt         x1          w1
#>      <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          w1 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          w1 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
d <- prepare_mxl_data(dt, "id", "alt", "choice", "x1", "w1")
eta <- get_halton_normals(50, d$N, ncol(d$W))
fit <- run_mxlogit(input_data = d, eta_draws = eta)
#> Optimization run time 0h:0m:0s
pm <- fit$param_map
delta <- mxl_blp_contraction(rep(0, J), rep(1/J, J), d$X, d$W,
  coef(fit)[pm$beta], rep(0, ncol(d$W)), coef(fit)[pm$sigma],
  d$alt_idx, d$M, d$weights, eta, rc_dist = rep(0L, ncol(d$W)),
  rc_correlation = FALSE, rc_mean = FALSE)
delta
#>             [,1]
#> [1,]  0.00000000
#> [2,] -0.01837654
#> [3,] -0.04093112
# }