-
Notifications
You must be signed in to change notification settings - Fork 1
/
BLP_Orig_function.R
355 lines (293 loc) · 11.6 KB
/
BLP_Orig_function.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
BerryLevinsohnPakes <- function(dat,
mkt.id.fld= "mkt.id",
prod.id.fld= "prod.id",
prc.fld= "px",
share.fld= "share",
x.var.flds = c("x1", "x2", "x3"),
prc.iv.flds= "z",
n.sim= 100,
tol_inner = 1e-12,
tol_outer = 1e-6,
sigma.guess){
# all data should appear in the data.frame "dat"
# input variables ending in ".fld" are the names of the columns
# n.sim = number of simulated "indviduals" per market
# sigma guess may be missing
#
# Required packages (installed as the function runs):
# SQUAREM, BB, AER, cowsay
#
# Based on code written by B Chidmi, May 1998:
# Chidmi, B., & Murova, O. (2011). Measuring market power in the supermarket
# industry: the case of the Seattle–Tacoma fluid milk market. Agribusiness,
# 27 (4), 435–449.
#
# Based on code written by Aviv Nevo, May 1998:
# Nevo, A. (2000). A Practitioner's Guide to Estimation of Random ‐
# Coefficients Logit Models of Demand. Journal of Economics & Management
# Strategy, 9(4), # 513–548.
#
# Rasmusen, E., others. (2007). The BLP method of demand curve estimation in
# industrial organization. Paper, Department of Business Economics and Public # Policy, Kelley School of Business, Indiana University.(Disponible en Http
# ://Www. Rasmusen. Org/Papers/Blp-Rasmusen. Pdf).
blp_inner <- function(delta.in, mu.in) {
# Computes a single update of the BLP (1995) contraction mapping.
# of market level predicted shares.
# This single-update function is required by SQUAREM, see Varadhan and
# Roland (SJS, 2008), and Roland and Varadhan (ANM, 2005)
# INPUT
# delta.in : current value of delta vector
# mu.in: current mu matrix
# Requires global variables: s.jt
# OUTPUT
# delta.out : delta vector that equates observed with predicted market
# shares
#
# from page 533 of Nevo's "Guide"
pred.s <- rowMeans(ind_sh(delta.in, mu.in));
delta.out <- delta.in + log(s.jt) - log(pred.s)
return(delta.out)
}
ind_sh <- function(delta.in, mu.in){
# This function gives the individual probabilities of choosing each
# brand in a market.
# Requires global variables: mkt.id, X, v
numer <- exp(mu.in) * matrix(rep(exp(delta.in), n.sim), ncol= n.sim);
denom <- as.matrix(do.call("rbind", lapply(mkt.id, function(tt){
1 + colSums(numer[mkt.id %in% tt, ])
})))
return(numer/ denom);
}
gmm_obj <- function(theta2){
# This function computes the GMM objective function
# Requires global variable inputs: X, v, delta, a, W
# Outputs: theta1, xi.hat
cat('\n');
cat('\n');
cat('\n');
cat('\n');
print(paste0("GMM Loop number: ", Sys.time()))
print(a <<- a+ 1)
cat('\n');
print("Updated theta2 estimate:")
print(theta2)
print("Change in theta2 estimate:")
print(theta.chg <- as.numeric(theta2- theta2.prev));
if(sum(theta.chg != 0) <= 2){
delta <- dat[, "delta"];
} else {
delta <- Y;
}
theta2.prev <<- theta2;
mu <- X %*% diag(theta2) %*% v;
cat('\n');
print("Running SQUAREM contraction mapping")
print(system.time(
squarem.output <- squarem(par= delta,
fixptfn= blp_inner,
mu.in= mu,
control= list(K= 1,
method= 3,
square= TRUE,
step.min0= 1,
step.max0= 1,
mstep= 4,
kr= Inf,
objfn.inc= 1,
tol= tol_inner,
maxiter= 1500,
trace= FALSE,
intermed= FALSE))
));
cat('\n');
print("Number of contraction mapping (inner loop) iterations:")
print(squarem.output$iter)
print("Did contraction mapping (inner loop) converge:")
print(squarem.output$convergence)
cat('\n');
delta <- squarem.output$par
print(summary(dat[, "delta"]- delta));
dat[, "delta"] <<- delta;
mo.ivreg <- ivreg(fm.ivreg, data= dat, x= TRUE)
theta1 <<- coef(mo.ivreg);
xi.hat <<- as.vector(mo.ivreg$resid);
Z.hat <- Z * matrix(rep(xi.hat, ncol(Z)), ncol= ncol(Z))
W.inv <- try(solve(t(Z.hat) %*% Z.hat), silent= FALSE)
if("matrix" == class(W.inv)){
PZ <<- Z %*% W.inv %*% t(Z);
PX.inv <- solve(t(X) %*% PZ %*% X)
theta1 <<- PX.inv %*% t(X) %*% PZ %*% delta
xi.hat <<- delta- X %*% theta1
X.hat <- (PZ %*% X) * matrix(rep(xi.hat, K), ncol= K)
tsls.se <- sqrt(diag(PX.inv %*% t(X.hat) %*% X.hat %*% PX.inv))
cat('\n');
print("GMM step 2 updated theta1 estimate:")
print(beta.est <<- data.frame(beta.est= theta1, beta.se= tsls.se,
sigma.est= theta2))
}
dat[, "xi.hat"] <<- xi.hat
f <- t(xi.hat) %*% PZ %*% xi.hat;
cat('\n');
print("Updated GMM objective:")
print(f <- as.numeric(f));
return(f)
}
jacobian <- function(delta.in, theta.in){
print(paste0("Calculating Jacobian matrix, ", Sys.time()))
#Requires global variables X, v, mkt.id
mu1 <- X %*% diag(theta.in) %*% v;
ind.shares <- ind_sh(delta.in, mu1);
K <- ncol(X);
print(paste0("Calculating dsigma matrix, ", Sys.time()))
dsigma <- lapply(l.Xv, function(x){
temp2 <- x* ind.shares;
temp3 <- as.matrix(do.call("rbind", lapply(mkt.id, function(m){
colSums(temp2[mkt.id %in% m, ])
})));
dsigma.res <- rowMeans(temp2- ind.shares* temp3);
return(dsigma.res)
})
dsigma <- as.matrix(do.call("cbind", dsigma))
print(paste0("Calculating ddelta matrices, ", Sys.time()))
cat('\n');
ddelta <- list()
for(m in mkt.id){
if(m %in% names(ddelta)){next}
temp1 <- as.matrix(ind.shares[mkt.id %in% m, ]);
H1 <- temp1 %*% t(temp1);
H2 <- diag(rowSums(temp1));
H <- (H2 - H1)/ n.sim;
H.inv <- solve(H);
ddelta[[as.character(m)]] <- H.inv %*% dsigma[mkt.id %in% m, ];
rm(temp1, H1, H2, H, H.inv)
}
ddelta <- as.matrix(do.call("rbind", ddelta));
return(ddelta)
}
gradient_obj <- function(theta2){
# Requires global variables PZ, delta, xi.hat
cat('\n');
print(system.time(jacobian_res <<- jacobian(as.vector(dat[, "delta"]),
theta2)))
print(paste0("Updated gradient:", Sys.time()))
print(f <- -2 * as.numeric(t(jacobian_res) %*% PZ %*% xi.hat));
L <- ncol(Z)
covg <- matrix(0, nrow= L, ncol= L)
for(i in 1:JT){
covg <- covg + (Z[i, ] %*% t(Z[i, ]))* xi.hat[i]^2
}
d.delta <- jacobian_res;
Dg <- t(d.delta) %*% Z
p.Dg <- try(solve(Dg %*% W.inv %*% t(Dg)))
cov.mat <- p.Dg %*% (Dg %*% W.inv %*% covg %*% W.inv %*% t(Dg)) %*% p.Dg
beta.est$sigma.se <<- sqrt(diag(cov.mat));
cat('\n');
print(paste0("Updated coefficients table:", Sys.time()))
print(beta.est)
write.csv(beta.est, file= paste0("BLP_beta_est_", Sys.Date(), ".csv"))
return(as.numeric(f))
}
# Set up data
dat <- dat[dat[, share.fld] > 0, ]
dat <- dat[order(dat[, mkt.id.fld], dat[, prod.id.fld]), ]
JT <- nrow(dat)
#market identifier variable
mkt.id <- dat[, mkt.id.fld];
# Number of characteristics (including constant and price)
X <- as.matrix(cbind(ones= rep(1, JT), dat[, c(x.var.flds, prc.fld)]));
K <- ncol(X)
# Compute the outside good market share by market
s.jt <- as.vector(dat[, share.fld]);
temp <- aggregate(s.jt, by= list(mkt.id= mkt.id), sum);
sum1 <- temp$x[match(mkt.id, temp$mkt.id)];
s.j0 <- as.vector(1- sum1);
rm(temp, sum1);
dat[, "delta"] <- Y <- log(s.jt)- log(s.j0);
iv <- dat[, prc.iv.flds]
# while(!require(AER)){install.packages("AER")}
# Construct 2SLS regression specification
str.ivreg.y <- "delta ~ "
str.ivreg.x <- paste(x.var.flds, collapse= " + ")
str.ivreg.prc <- paste(prc.fld, collapse= " + ")
str.ivreg.iv <- paste(prc.iv.flds, collapse= " + ")
start_time <- Sys.time()
print("2SLS specification:")
print(fm.ivreg <- paste0(str.ivreg.y, str.ivreg.x, " + ", str.ivreg.prc,
" | ", str.ivreg.x, " + ", str.ivreg.iv))
rm(str.ivreg.y, str.ivreg.x, str.ivreg.prc, str.ivreg.iv)
print("2SLS beta estimate:")
print(summary(mo.ivreg <- ivreg(fm.ivreg, data= dat, x= TRUE)))
beta.est <- summary(mo.ivreg)$coef[, 1:2]
#Z = instrumental variable matrix include exogenous X's
Z <- as.matrix(mo.ivreg$x$instruments)
PZ <- Z %*% solve(t(Z) %*% Z) %*% t(Z);
theta1 <- coef(mo.ivreg);
xi.hat <- as.vector(mo.ivreg$resid);
Z.hat <- Z * matrix(rep(xi.hat, ncol(Z)), ncol= ncol(Z))
W.inv <- try(solve(t(Z.hat) %*% Z.hat), silent= FALSE)
if("matrix" == class(W.inv)){
PZ <- Z %*% W.inv %*% t(Z);
PX.inv <- solve(t(X) %*% PZ %*% X)
theta1 <- PX.inv %*% t(X) %*% PZ %*% Y
xi.hat <- Y- X %*% theta1
X.hat <- (PZ %*% X) * matrix(rep(xi.hat, K), ncol= K)
tsls.se <- sqrt(diag(PX.inv %*% t(X.hat) %*% X.hat %*% PX.inv))
print("GMM step 2 updated theta1 estimate:")
print(beta.est <- data.frame(beta.est= theta1, se.est= tsls.se))
}
dat[, "xi.hat"] <- xi.hat
# Starting point
cat('\n');
print("Sigma guess:")
if(missing(sigma.guess)){
tsls.se <- beta.est[, 2]
print(theta2 <- 0.5* tsls.se);
} else {
print(theta2 <- sigma.guess);
}
theta2.prev <- theta2;
# Matrix of individuals' characteristics
# Standard normal distribution draws, one for each characteristic
v <- matrix(rnorm(K* n.sim), nrow= K, ncol= n.sim)
# Break X and v matrices into list variables
# in attempt to expedite calculation of the Jacobian matrix
l.X <- lapply(1:K, function(k){
return(X[, k])
})
l.v <- lapply(1:K, function(k){
return(v[k, ])
})
l.Xv <- lapply(1:K, function(k){
l.X[[k]] %*% t(l.v[[k]]);
})
cat('\n');
print("Estimating random coefficients multinomial logit")
a <- 0;
beta.est <- NULL;
# while(!require(SQUAREM)){install.packages("SQUAREM")}
# while(!require(BB)){install.packages("BB")}
print(system.time(
theta.est <- multiStart(par= theta2,
fn= gmm_obj,
gr= gradient_obj,
lower= 0,
action= "optimize",
control= list(trace= TRUE,
checkGrad=FALSE,
ftol= tol_outer))
));
save(theta.est, file= paste0("theta_est_", Sys.time(), ".RData"))
cat('\n');
cat('\n');
print("Final coefficients estimate:")
theta2 <- theta.est$par
theta2 <- as.numeric(theta2)
gmm.res <- gmm_obj(theta2)
grad.res <- gradient_obj(theta2)
end_time <- Sys.time()
time_diff <- end_time-start_time
say(paste0("start time:\n", start_time, "\nend time:\n", end_time,
"\ntime difference (min):\n", time_diff), 'cat')
return(list(coef.mat= beta.est, gmm.obj.func= gmm.res, gmm_est= theta.est,
final.data= dat))
}