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::AbstractDataFrame
mid::Symbol
market identifier indf
s::Symbol
market sharesx::Vector{Symbol}
columns ofdf
of product characteristics.x[1]
must be pricew::Vector{Symbol}
cost shifterszd::Vector{Symbol}
demand instrumentszs::Vector{Symbol}
supply instrumentsν::Array{T, 3} where T
K × S × T
array 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σ
lengthK
vector 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
s
vectorJ
market sharesds
J × J
Jacobian matrix with ds[l,j] = ∂s[l]/∂p[j]Λ
J × J
diagonal matrix withds = Λ - Γ
Γ
J × J
matrix
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
mc
vector ofJ
marginal costsβ
vector ofK
taste coefficientsσ
vector ofK
taste standard deviationsξ
vector ofJ
demand shocksx
(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 tolerancemaxiter
maximum 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::BLPData
method=:MPEC
method for estimation. Available choices are :MPEC, :NFXP, or :GEL.verbose=true
whether to display information about optimization progressW=I
L × L
weighting matrix for moments.max_iter=200
number 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=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 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::BLPData
method=:MPEC
method for estimation. Available choices are :MPEC, :NFXP, or :GELverbose=true
whether to display information about optimization progressW=I
weighting matrixmax_iter
optimizer iteration limitoptimizer
seeestimateBLP
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.
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
xpred
x values to compute fitted yxdata
observed xydata
observed y, must havesize(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)
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 characteristicsx
K × J
matrix of product characteristicsν
K × S
matrix 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 characteristicsp
J
vector of pricesx
(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
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
J
numebr of productsT::Integer
number of marketsβ::AbstractVector
withlength(β)=K
, average tastes for characteristics. The first characteristic will be endogeneous (price)σ::AbstractVector
withlength(σ)=K
, standard deviation of tastes for characteristicsγ::AbstractVector
marginal cost coefficientsS
number of simulation draws to calculate market sharesvarξ=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 randjfirmid=1:J
firm identifiers. Must be length J.
Returns
dat
aBLPData
structξ
ω
BLPDemand.simulateIVRClogit
— Methodfunction simulateIVRClogit(T, β, σ, π, ρ, S)
Simulates a random coefficients logit model with endogeneity.
Arguments
T::Integer
number of marketsβ::AbstractVector
withlength(β)=K
, average tastes for characteristics.σ::AbstractVector
withlength(σ)=K
, standard deviation of tastes for characteristicsπ::AbstractMatrix
withsize(π) = (L, K, J)
, first stage coefficientsρ::Number
strength of endogeneityS
number 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σ
lengthK
vector of standard deviations of tastesξ
J × T
matrix 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.BLPData
BLPDemand.MarketData
BLPDemand.blpdata
BLPDemand.blpdata
BLPDemand.blpdata
BLPDemand.data_blp1999
BLPDemand.delta
BLPDemand.demandmoments
BLPDemand.dsharedp
BLPDemand.eqprices
BLPDemand.estimateBLP
BLPDemand.estimateRCIVlogit
BLPDemand.fracRCIVlogit
BLPDemand.makeivblp
BLPDemand.makeivblp
BLPDemand.optimalIV
BLPDemand.polyreg
BLPDemand.share
BLPDemand.sharep
BLPDemand.simulateBLP
BLPDemand.simulateIVRClogit
BLPDemand.supplymoments
BLPDemand.tran
BLPDemand.varianceBLP
BLPDemand.varianceRCIVlogit