Function Reference
BLPDemand.BLPData — TypeData set for BLP demand model.
BLPDemand.MarketData — TypeData from a single market for a BLP demand model
BLPDemand.blpdata — MethodConstructs 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
BLPDemand.blpdata — MethodConstructs data for BLP demand model from arrays. Compared to MarketData, each argument should have one more dimension with length T=number of markets
BLPDemand.blpdata — Methodfunction 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::AbstractDataFramemid::Symbolmarket identifier indfs::Symbolmarket sharesx::Vector{Symbol}columns ofdfof product characteristics.x[1]must be pricew::Vector{Symbol}cost shifterszd::Vector{Symbol}demand instrumentszs::Vector{Symbol}supply instrumentsν::Array{T, 3} where TK × S × Tarray of draws for Monte Carlo integration
See also MarketData
BLPDemand.data_blp1999 — Methodfunction loadblpdata()Loads data from Berry, Levinsohn, and Pakes (1999).
Returns a DataFrame.
BLPDemand.delta — Methodfunction delta(s, x, ν, σ; tol=sqrt(eps(eltype(s))), maxiter=1000)
Solves for δ in s = share(δ, ...) using contraction mapping iteration.
See also: share
BLPDemand.demandmoments — Methoddemandmoments(β::AbstractVector,σ::AbstractVector,
dat::BLPData)Demand side moments in BLP model.
Arguments
βlengthK(=number characteristics) vector of average tastes for characteristicsσlengthKvector of standard deviations of tastesdat::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
BLPDemand.dsharedp — Methodfunction 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
svectorJmarket sharesdsJ × JJacobian matrix with ds[l,j] = ∂s[l]/∂p[j]ΛJ × Jdiagonal matrix withds = Λ - ΓΓJ × Jmatrix
See eqprices for usage of Λ and Γ.
BLPDemand.eqprices — Methodfunction 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
mcvector ofJmarginal costsβvector ofKtaste coefficientsσvector ofKtaste standard deviationsξvector ofJdemand shocksx(K-1) × Jexogenous product characteristicsνK × S × Tarray of draws ofνfirmid= (1:J)identifier of firm producing each good. Default value assumes each good is produced by a different firm.tolconvergence tolerancemaxitermaximum number of iterations.
BLPDemand.estimateBLP — MethodestimateBLP(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::BLPDatamethod=:MPECmethod for estimation. Available choices are :MPEC, :NFXP, or :GEL.verbose=truewhether to display information about optimization progressW=IL × Lweighting matrix for moments.max_iter=200number of iterations of optimizeroptimizer=(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=truewhether to include supply side momentsβ0=nothinginitial value for β. If isnothing, then will set automatically.σ0=nothinginitial value for σ. If isnothing, then will set automatically.γ0=nothinginitial 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
:NFXPnested fixed point GMM.minimize_θ G(δ(θ),θ)'W G(δ(θ),θ):MPECconstrainted GMM.minimize_{θ,Δ} G(Δ, θ)' W G(Δ, θ) s.t. Δ = δ(θ):GELconstrained empiricla likelihoodmaximize_{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
BLPDemand.estimateRCIVlogit — MethodestimateRCIVlogit(dat::BLPData; method=:MPEC, verbose=true, W=I)Estimates a random coefficients IV logit model.
Arguments
dat::BLPDatamethod=:MPECmethod for estimation. Available choices are :MPEC, :NFXP, or :GELverbose=truewhether to display information about optimization progressW=Iweighting matrixmax_iteroptimizer iteration limitoptimizerseeestimateBLPfor detailsβ0=nothinginitial value for β. If isnothing, then will try to automatically setσ0=nothinginitial 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.
BLPDemand.fracRCIVlogit — MethodfracRCIVlogit(dat::BLPData)Estimate random coefficients IV model using "Fast, Robust, Approximately Correct" method of Salanie & Wolak (2019)
BLPDemand.makeivblp — Methodmakeivblp(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.
BLPDemand.makeivblp — Methodmakeivblp(dat::BLPData; includeexp=true)Sets dat[:].zd and dat[:].zs to makeivblp([x[2:end,:] w])
BLPDemand.optimalIV — MethodoptimalIV(β, σ, γ, 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']
BLPDemand.polyreg — Method polyreg(xpred::AbstractMatrix,
xdata::AbstractMatrix,
ydata::AbstractMatrix; degree=1)Computes polynomial regression of ydata on xdata. Returns predicted y at x=xpred.
Arguments
xpredx values to compute fitted yxdataobserved xydataobserved y, must havesize(y)[1] == size(xdata)[1]degreederivwhether to also return df(xpred). Only implemented when xdata is one dimentional
Returns
- Estimates of
f(xpred)
BLPDemand.share — Methodfunction 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 lengthJ=number of productsσvector of lengthK=number of characteristicsxK × Jmatrix of product characteristicsνK × Smatrix of simulation draws for integration
Returns vector of length J market shares.
See also: delta
BLPDemand.sharep — Methodfunction 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 lengthK=number of characteristicsσvector of lengthK=number of characteristicspJvector of pricesx(K-1) × Jmatrix of exogenous product characteristicsνK × Smatrix of simulation draws for integrationξJvector of demand shocks
Returns vector of length J market shares.
See also: share, dsharedp
BLPDemand.simulateBLP — Methodfunction simulateBLP(J, T, β, σ, γ, S; varξ=1, varω=1, randj=1.0, firmid=1:J, costf=:log)Simulates a BLP demand and supply model.
Arguments
Jnumebr of productsT::Integernumber of marketsβ::AbstractVectorwithlength(β)=K, average tastes for characteristics. The first characteristic will be endogeneous (price)σ::AbstractVectorwithlength(σ)=K, standard deviation of tastes for characteristicsγ::AbstractVectormarginal cost coefficientsSnumber of simulation draws to calculate market sharesvarξ=1variance of ξvarω=1variance of ωrandj=1.0if less than 1, then J is the maximum number of products per market. Each product is included in each market with probability randjfirmid=1:Jfirm identifiers. Must be length J.
Returns
dataBLPDatastructξω
BLPDemand.simulateIVRClogit — Methodfunction simulateIVRClogit(T, β, σ, π, ρ, S)Simulates a random coefficients logit model with endogeneity.
Arguments
T::Integernumber of marketsβ::AbstractVectorwithlength(β)=K, average tastes for characteristics.σ::AbstractVectorwithlength(σ)=K, standard deviation of tastes for characteristicsπ::AbstractMatrixwithsize(π) = (L, K, J), first stage coefficientsρ::Numberstrength of endogeneitySnumber of simulation draws to calculate market shares
Returns BLPData struct
BLPDemand.supplymoments — Methodsupplymoments(γ::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βlengthK(=number characteristics) vector of average tastes for characteristicsσlengthKvector of standard deviations of tastesξJ × Tmatrix of market demand shocksdat::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.
BLPDemand.tran — Methodpack(β::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(θ)) = θBLPDemand.varianceBLP — MethodvarianceBLP(β, σ, γ, dat::BLPData ; W=I)
Computes variance of BLP estimates. Computes moment variance clustering on t.
Returns Σ = covariance of [β, σ, γ] and varm = (clustered) covariance of moments.
BLPDemand.varianceRCIVlogit — MethodvarianceRCIVlogit(β, σ, dat::BLPData ; W=I)
Computes variance of RCIVlogit estimates. Computes moment variance clustering on t.
Returns Σ = covariance of [β, σ, γ] and varm = (clustered) covariance of moments.
Index
BLPDemand.BLPDataBLPDemand.MarketDataBLPDemand.blpdataBLPDemand.blpdataBLPDemand.blpdataBLPDemand.data_blp1999BLPDemand.deltaBLPDemand.demandmomentsBLPDemand.dsharedpBLPDemand.eqpricesBLPDemand.estimateBLPBLPDemand.estimateRCIVlogitBLPDemand.fracRCIVlogitBLPDemand.makeivblpBLPDemand.makeivblpBLPDemand.optimalIVBLPDemand.polyregBLPDemand.shareBLPDemand.sharepBLPDemand.simulateBLPBLPDemand.simulateIVRClogitBLPDemand.supplymomentsBLPDemand.tranBLPDemand.varianceBLPBLPDemand.varianceRCIVlogit