Function Reference

BLPDemand.blpdataMethod

Constructs data for IV random coefficients logit model from arrays. Compared to MarketData, each argument should have one more dimension with length T=number of markets

source
BLPDemand.blpdataMethod

Constructs data for BLP demand model from arrays. Compared to MarketData, each argument should have one more dimension with length T=number of markets

source
BLPDemand.blpdataMethod
function blpdata(df::AbstractDataFrame,
             mid::Symbol,
             firmid::Symbol,
             s::Symbol,                 
             x::Vector{Symbol},
             w::Vector{Symbol},
             zd::Vector{Symbol},
             zs::Vector{Symbol},
             ν::AbstractArray{T,3} where T)

Construct BLPData from a DataFrame.

Arguments

  • df::AbstractDataFrame
  • mid::Symbol market identifier in df
  • s::Symbol market shares
  • x::Vector{Symbol} columns of df of product characteristics. x[1] must be price
  • w::Vector{Symbol} cost shifters
  • zd::Vector{Symbol} demand instruments
  • zs::Vector{Symbol} supply instruments
  • ν::Array{T, 3} where T K × S × T array of draws for Monte Carlo integration

See also MarketData

source
BLPDemand.deltaMethod

function delta(s, x, ν, σ; tol=sqrt(eps(eltype(s))), maxiter=1000)

Solves for δ in s = share(δ, ...) using contraction mapping iteration.

See also: share

source
BLPDemand.demandmomentsMethod
demandmoments(β::AbstractVector,σ::AbstractVector,
                       dat::BLPData)

Demand side moments in BLP model.

Arguments

  • β length K (=number characteristics) vector of average tastes for characteristics
  • σ length K vector of standard deviations of tastes
  • dat::BLPData

Returns (moments, ξ) where vector of moments is length L with moments[l] = 1/(JT) ∑ⱼ∑ₜ ξ[t][j]*dat[t].zd[l,j]

See also: share, delta, simulateRCIVlogit

source
BLPDemand.dsharedpMethod
function dsharedp(β::AbstractVector,
              σ::AbstractVector,
              p::AbstractVector,
              x::AbstractMatrix,
              ν::AbstractMatrix,
              ξ::AbstractVector)

Compute market shares and their derivatives in BLP random coefficients demand model.

See sharep for argument details.

Returns

  • s vector J market shares
  • ds J × J Jacobian matrix with ds[l,j] = ∂s[l]/∂p[j]
  • Λ J × J diagonal matrix with ds = Λ - Γ
  • Γ J × J matrix

See eqprices for usage of Λ and Γ.

source
BLPDemand.eqpricesMethod
function eqprices(mc::AbstractVector,
                  β::AbstractVector, σ::AbstractVector,
                  ξ::AbstractVector,
                  x, ν;
                  firmid= 1:length(mc),
                  tol=sqrt(eps(eltype(mc))),
                  maxiter=10000)

Compute equilibrium prices in BLP model using ζ contraction method of Morrow & Skerlos (2011).

Arguments

  • mc vector of J marginal costs
  • β vector of K taste coefficients
  • σ vector of K taste standard deviations
  • ξ vector of J demand shocks
  • x (K-1) × J exogenous product characteristics
  • ν K × S × T array of draws of ν
  • firmid= (1:J) identifier of firm producing each good. Default value assumes each good is produced by a different firm.
  • tol convergence tolerance
  • maxiter maximum number of iterations.
source
BLPDemand.estimateBLPMethod
estimateBLP(dat::BLPData; method=:MPEC, verbose=true, W=I, optimizer=optimizer_with_attributes(Ipopt.Optimizer, start_with_resto=>"no"), supply=true)

Estimates a random coefficients BLP demand model

Arguments

  • dat::BLPData
  • method=:MPEC method for estimation. Available choices are :MPEC, :NFXP, or :GEL.
  • verbose=true whether to display information about optimization progress
  • W=I L × L weighting matrix for moments.
  • max_iter=200 number of iterations of optimizer
  • optimizer=(method==:NFXP ? LBFGS(linesearch=LineSearches.HagerZhang()) : optimizer_with_attributes(Ipopt.Optimizer, "max_iter"=> 100, "start_with_resto" => "no", "print_level" => 5*verbose)) optimization method. See below for details.
  • supply=true whether to include supply side moments
  • β0=nothing initial value for β. If isnothing, then will set automatically.
  • σ0=nothing initial value for σ. If isnothing, then will set automatically.
  • γ0=nothing initial value of γ. If isnothing, then will set automatically.

Details

Uses L unconditional moments for estimation. The moments are moments[l] = 1/(T) ∑ₜ∑ⱼ (ξ[t][t]*dat[t].zd[l,j] + ω[t][j]*dat[t].zs[l,j])

Methods

  • :NFXP nested fixed point GMM. minimize_θ G(δ(θ),θ)'W G(δ(θ),θ)
  • :MPEC constrainted GMM. minimize_{θ,Δ} G(Δ, θ)' W G(Δ, θ) s.t. Δ = δ(θ)
  • :GEL constrained empiricla likelihood maximize_{p, θ, Δ} ∑ₜ log(p[t]) s.t. E_p[g(Δ, θ)] = 0 and Δ = δ(θ) For some models, there might be no feasible point for EL. This is especially likely if the number of moments is large.

supply=false should give the same results as estimateRCIVlogit. However, with method=:MPEC or :GEL, the formulation of the JuMP model differs, and so might the results. Generally, estimateRCIVlogit is faster for data with a small number of products, but scales very poorly as the number of products increases. See the developer notes for more information.

Optimizers

If method=:NFXP, optimizer should be an unconstrained optimizer from the Optim.jl package. The default of LBFGS() is usually a good choice. BFGS() instead of LBFGS() and/or changing linesearch to LineSearches.BackTracking() are also worth trying.

If method=:MPEC or :GEL, the optimizer must be comptible with JuMP and capable of solving nonliner problems. The default, Ipopt, will occassionally fail. If verbose is true and you repeatedly see warning messages from Ipopt, then it is likely that Ipopt will run for many iterations and eventually fail to converge. Changing Ipopt's start_with_reso option sometimes helps. For problems of the size seen in the docs or tests, when Ipopt succeeds, it generally does so in 100 or fewer iterations. Ipopt has many additional options, see the Ipopt documentation for a complete list.

See also: optimalIV, varianceBLP, simulateBLP

source
BLPDemand.estimateRCIVlogitMethod
estimateRCIVlogit(dat::BLPData; method=:MPEC, verbose=true, W=I)

Estimates a random coefficients IV logit model.

Arguments

  • dat::BLPData
  • method=:MPEC method for estimation. Available choices are :MPEC, :NFXP, or :GEL
  • verbose=true whether to display information about optimization progress
  • W=I weighting matrix
  • max_iter optimizer iteration limit
  • optimizer see estimateBLP for details
  • β0=nothing initial value for β. If isnothing, then will try to automatically set
  • σ0=nothing initial value for σ. If isnothing, then will try to automatically set

Note that methods :MPEC and :GEL use a large amount of memory when the number of products is large. Either method=:NFXP or estimateBLP with supply=false should be used if the number of products is large.

source
BLPDemand.fracRCIVlogitMethod
fracRCIVlogit(dat::BLPData)

Estimate random coefficients IV model using "Fast, Robust, Approximately Correct" method of Salanie & Wolak (2019)

source
BLPDemand.makeivblpMethod
makeivblp(x::AbstractMatrix, firmid=1:size(x,2))

Function for constructing demand instrumental variables following BLP (1995).

Instruments consist of exogenous product characteristics, sum of rivals exogenous characteristics, and, if there are any multi-product firms, sum of characteristics of other goods produced by the same firm.

If includeexp is true, then also use sum(exp(-([x[2:end,j,t] - x[l,j,t])^2) for l in 1:J)) as instruments.

source
BLPDemand.makeivblpMethod
makeivblp(dat::BLPData; includeexp=true)

Sets dat[:].zd and dat[:].zs to makeivblp([x[2:end,:] w])

source
BLPDemand.optimalIVMethod
optimalIV(β, σ, γ, dat::BLPData ; degree=2)

Computes optimal instruments for BLP model. Given initial estimates θ=(β, σ, γ), computes e(θ)ⱼₜ = (ξ(θ)ⱼₜ , ω(θ)ⱼₜ), and then approximates the optimal instruments with a polynomial regression of degree of ∂e/∂θ on z. Returns BLPData with instruments set to fitted values (zd, zs) = (E[∂ξ/∂θ|z], E[∂ω/∂θ|z])

Notes

  • This function has not be carefully tested and should be considered experimental.
  • This code assumes homoskedasticity to construct the optimal iv, E[ee'|z] = E[ee']
source
BLPDemand.polyregMethod
  polyreg(xpred::AbstractMatrix,
          xdata::AbstractMatrix,
          ydata::AbstractMatrix; degree=1)

Computes polynomial regression of ydata on xdata. Returns predicted y at x=xpred.

Arguments

  • xpred x values to compute fitted y
  • xdata observed x
  • ydata observed y, must have size(y)[1] == size(xdata)[1]
  • degree
  • deriv whether to also return df(xpred). Only implemented when xdata is one dimentional

Returns

  • Estimates of f(xpred)
source
BLPDemand.shareMethod
function share(δ::AbstractVector, σ::AbstractVector, x::AbstractMatrix, ν::AbstractMatrix)

Compute market shares in BLP random coefficients demand model.

Market shares are given by

\[ s_{j} = \int \frac{e^{\delta_j + x_j σ ν}}{1+\sum_{i=1}^J e^{\delta_i + x_i σ ν}} dF_ν(ν)\]

Arguments:

  • δ vector of length J= number of products
  • σ vector of length K= number of characteristics
  • x K × J matrix of product characteristics
  • ν K × S matrix of simulation draws for integration

Returns vector of length J market shares.

See also: delta

source
BLPDemand.sharepMethod

function sharep(β::AbstractVector, σ::AbstractVector, p::AbstractVector, x::AbstractMatrix, ν::AbstractMatrix, ξ::AbstractVector)

Compute market shares in BLP random coefficients demand model.

Market shares are given by

\[ s_{j} = \int \frac{e^{\delta_j + x_j σ ν}}{1+\sum_{i=1}^J e^{\delta_i + x_i σ ν}} dF_ν(ν)\]

where

\[\delta_j = β[1]*p[j] + x[:,j]' *β[2:end] + ξ[j]\]

Arguments:

  • β vector of length K= number of characteristics
  • σ vector of length K= number of characteristics
  • p J vector of prices
  • x (K-1) × J matrix of exogenous product characteristics
  • ν K × S matrix of simulation draws for integration
  • ξ J vector of demand shocks

Returns vector of length J market shares.

See also: share, dsharedp

source
BLPDemand.simulateBLPMethod
function simulateBLP(J, T, β, σ, γ, S; varξ=1, varω=1, randj=1.0, firmid=1:J, costf=:log)

Simulates a BLP demand and supply model.

Arguments

  • J numebr of products
  • T::Integer number of markets
  • β::AbstractVector with length(β)=K, average tastes for characteristics. The first characteristic will be endogeneous (price)
  • σ::AbstractVector with length(σ)=K, standard deviation of tastes for characteristics
  • γ::AbstractVector marginal cost coefficients
  • S number of simulation draws to calculate market shares
  • varξ=1 variance of ξ
  • varω=1 variance of ω
  • randj=1.0 if less than 1, then J is the maximum number of products per market. Each product is included in each market with probability randj
  • firmid=1:J firm identifiers. Must be length J.

Returns

source
BLPDemand.simulateIVRClogitMethod
function simulateIVRClogit(T, β, σ, π, ρ, S)

Simulates a random coefficients logit model with endogeneity.

Arguments

  • T::Integer number of markets
  • β::AbstractVector with length(β)=K, average tastes for characteristics.
  • σ::AbstractVector with length(σ)=K, standard deviation of tastes for characteristics
  • π::AbstractMatrix with size(π) = (L, K, J), first stage coefficients
  • ρ::Number strength of endogeneity
  • S number of simulation draws to calculate market shares

Returns BLPData struct

source
BLPDemand.supplymomentsMethod
supplymoments(γ::AbstractVector, β::AbstractVector, σ::AbstractVector,
                   ξ::AbstractVector, dat::BLPData)

Supply side moments in a BLP model. Assumes that marginal cost is costf linear,

\[costf(c_{jt}) = w_{jt}'γ + ω_{jt}\]

and prices are Bertrand-Nash

\[c_{t} = p_{t} + (∂s/∂p)^{-1} s\]

where ∂s/∂p is the Jacobian of shares with respect to prices, but with the j,l entry set to zero unless goods j and l are produced by the same firm.

Arguments

  • γ marginal cost coefficients
  • β length K (=number characteristics) vector of average tastes for characteristics
  • σ length K vector of standard deviations of tastes
  • ξ J × T matrix of market demand shocks
  • dat::BLPData

Returns (moments, ω) an L vector of moments with moments[l] = 1/(T) ∑ₜ∑ⱼ ω[t][j]*dat[t].zs[l,j]

Notes:

May encounter numeric problems if costf has a limited domain. In particular, for some values of parameters, we may have

\[p_{t} + (∂s/∂p)^{-1} s < 0\]

in which case the implied marginal cost is negative.

source
BLPDemand.tranMethod

pack(β::AbstractVector, σ::AbstractVector, ...)

Packs parameters into a single vector, θ.

Returns tuple with the packed parameters, θ, and a function to unpack them, i.e.

unpack(θ) = (β, σ, ...)

and

pack(unpack(θ)) = θ
source
BLPDemand.varianceBLPMethod

varianceBLP(β, σ, γ, dat::BLPData ; W=I)

Computes variance of BLP estimates. Computes moment variance clustering on t.

Returns Σ = covariance of [β, σ, γ] and varm = (clustered) covariance of moments.

source
BLPDemand.varianceRCIVlogitMethod

varianceRCIVlogit(β, σ, dat::BLPData ; W=I)

Computes variance of RCIVlogit estimates. Computes moment variance clustering on t.

Returns Σ = covariance of [β, σ, γ] and varm = (clustered) covariance of moments.

source

Index