Skip to contents

Finds the constants (ASCs, delta) at which a fitted model's predicted market shares equal target shares, the share inversion of Berry (1994) used by Berry, Levinsohn, and Pakes (1995) doi:10.2307/2171802 , with the other parameters held at their estimates. The iteration, its stopping rule and its checks of the targets are described in blp_contraction().

Usage

blp(object, target_shares, ...)

Arguments

object

A fitted model object.

target_shares

Numeric vector of target market shares (length J).

...

Additional arguments passed to methods.

Value

The constants: an n x 1 matrix with one row per inside alternative, named by the alternatives' labels, and the attribute "convergence", a list with converged, status ("converged", "max_iter", "non_finite" or "non_finite_start"), iterations, evaluations (of the shares), residual (the largest absolute log-share residual at the point returned), tol and method. When the iteration does not converge, the evaluated point of smallest residual, with a warning of class choicer_blp_unconverged whose convergence element is the same list.

Details

The methods check the targets in R before the model's solver runs (and so before a mixed logit forms its draws): a numeric vector or one-column matrix of one share per alternative (the outside option's first, when the model has one), finite, positive, and summing to 1 within sqrt(.Machine$double.eps). They start from the fitted constants (zeros for a model fitted without them) unless delta_init is given. method = "anderson", the default, accelerates the two-level step of blp_contraction() by Anderson acceleration, its settings in control: memory, how many differences between successive iterates it keeps (default 10), and max_condition (default \(10^8\)). method = "simple" takes that step alone: a common shift of the constants, then the rest of each residual over the exact diagonal of its derivative. method = "original" is the contraction of Berry, Levinsohn and Pakes (1995, eq. 6.8), \(\delta \leftarrow \delta + \log S - \log s(\delta)\), normalized as the step is (the outside option's utility 0, or each group's reference held) and, for the nested logit, multiplied by damping. method = "squarem" extrapolates the step by SQUAREM, PyBLP's default (settings scheme, step_min, step_max and step_factor; PyBLP's defaults 3, 1, 1 and 4). Every method runs under the same checks, stopping rule, evaluation budget (max_iter), warning and returned point, and halves a step whose shares are not finite (the accelerated methods first fall back where an extrapolated point's are not), so their results and "convergence" records compare directly. The step's stalls belong to "simple"; its cap to the two-level step, which "anderson" and "squarem" extrapolate; and the contraction's joining to all three. Only the accelerated methods take settings in control.

Examples

# \donttest{
library(data.table)
#> 
#> Attaching package: ‘data.table’
#> The following object is masked from ‘package:base’:
#> 
#>     %notin%
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
blp(fit, target_shares = rep(1/J, J))
#>          [,1]
#> 1  0.00000000
#> 2 -0.03013133
#> 3  0.01204404
#> attr(,"convergence")
#> attr(,"convergence")$converged
#> [1] TRUE
#> 
#> attr(,"convergence")$status
#> [1] "converged"
#> 
#> attr(,"convergence")$iterations
#> [1] 4
#> 
#> attr(,"convergence")$evaluations
#> [1] 5
#> 
#> attr(,"convergence")$residual
#> [1] 2.220446e-16
#> 
#> attr(,"convergence")$tol
#> [1] 1e-12
#> 
#> attr(,"convergence")$method
#> [1] "anderson"
#> 
# }