structural econometric modeling in industrial … econometric modeling in industrial organization...

28
Structural Econometric Modeling in Industrial Organization Handout 3 Professor Matthijs Wildenbeest 18 May 2011 1

Upload: nguyendat

Post on 07-Apr-2018

225 views

Category:

Documents


3 download

TRANSCRIPT

Structural Econometric Modeling inIndustrial Organization

Handout 3

Professor Matthijs Wildenbeest

18 May 2011

1

Estimation of Random Coefficient Logit Models of Demand

• MATLAB basics

• MATLAB code

2

MATLAB basics

Short for “matrix laboratory.”

MATLAB is a numerical computer environment, created by theMathWorks.

Built around the MATLAB language (M-code). Can be typed atthe prompt, >>:

>> n = 1 + 1

n =

2

>>

3

MATLAB basics

MATLAB works with vectors and matrices.

>> v = [3 1]

v =

3 1

>> m = [3 1; 1 3]

m =

3 1

1 3

>> m(1,:)

ans =

3 1

>>

Many vector and matrix operations possible.

4

MATLAB basics

Important distinction: m^2 versus m.^2

>> m^2

ans =

10 6

6 10

>> m.^2

ans =

9 1

1 9

>>

The .^ operator produces element-by-element powers, while ^pmultiplies the matrix p-1 times by itself.

5

MATLAB basics

A MATLAB M-file is an executable file (extension “.m”) thatexecutes a set of commands when called.

Can be created in the MATLAB editor.

Simply type in the name of the M-file at the prompt to execute thefile, i.e., to start the M-file estcars.m:

>> estcars

OLS LOGIT DEMAND ESTIMATION

-------------------------------

...

MATLAB has many built in functions, some of them will bediscussed along the way.

6

MATLAB codeOverview

estcars.m reads the data and calls the other functions.gmmobj.m computes the objective function (demand).meanval.m computes the mean utility using the contraction.mufunc.m computes the non-linear part of the utility.ind_sh.m computes the ”individual” probabilities of choosing

each brand.

gmmobj2.m computes the objective function (demand and supply).markup.m computes the markups using the FOC.

cars.mat car data for the Netherlands (2003 and 2004).

7

MATLAB codeestcars.m

1 % Main file

2 % Written by Matthijs Wildenbeest, April 2010.

3

4 clear all;

5 warning off all

6 format long

7

8 global theta1 X iv W nsm vfull incfull pj sj sj0 nsm cdindex cdid owner X2

9

10 rand(’seed’,200) % reset uniform random number generator

11 randn(’seed’,200) % reset normal random number generator

12

13 load cars.mat; % sales 50 or less excluded, no exotic makes

14

15 T = 2; % number of markets

16 k = 6; % number of characteristics used (incl. constant)

17 nsm = 50; % number of simulated "indviduals" per market

18

19 % net ("besteedbaar") income distribution netherlands

20 % fitted parameters: location: 3.30; scale: 0.60 (gross income: 3.73 and 0.79)

21 inc = lognrnd(3.30,0.6,T,nsm);

22

23 v = normrnd(0,1,T,k*nsm); % multivariate normal draws, one for each characteristic

24

25 F=length(cdid2); % total number of brands

26 J=length(sales); % total number of models

27

8

MATLAB codeestcars.m

28 % define horsepower

29 hp=hpkw/.735;

30

31 % correct price for inflation

32 pj=price./(cpi(cdid)/100)/1000;

33

34 % define size

35 sizecar=(lengthcar/1000).*(width/1000);

36

37 % define kilometers per liter and per euro

38 kpl=100./gasavg;

39 kpe=kpl./(gasprice(cdid,:)/100);

40

41 % matrix of income

42 incfull=inc(cdid,:);

43

44 % create marketshares using sales data

45 sj=sales./(1000*numhh(cdid,:));

46

47 % compute the outside good market share by market

48 temp = cumsum(sj);

49 sum1 = temp(cdindex,:);

50 sum1(2:size(sum1,1),:) = diff(sum1);

51 sj0 = 1.0 - sum1(cdid,:);

52

53 y=log(sj)-log(sj0);

54 A=[ones(J,1) 10*hp./weight american+asian cruise kpe/10 sizecar];

55 X=[A pj];

56

9

MATLAB codeestcars.m

57 %%%

58 % OLS logit estimation

59 %%%

60

61 e=y-X*inv(X’*X)*X’*y;

62 n=size(X,1);

63 k=size(X,2);

64 N=eye(n)-ones(n,1)*ones(n,1)’/n;

65

66 betaols = (X’*X)\X’*y;

67 covbetaols=(e’*e/(n-k))*inv(X’*X);

68 sebetaols=sqrt(diag(covbetaols));

69

70 alphaols=betaols(size(A,2)+1);

71 eols=alphaols*pj.*(1-sj);

72

73 R2ols=(betaols’*X’*N*X*betaols)/(y’*N*y);

74 tstatols=betaols./sebetaols;

75 pvalueols=2*(1-tcdf(abs(betaols./sebetaols),n-k));

76

10

MATLAB codeestcars.m

77 % print estimation results on screen

78 varnames=[’constant ’; ’hp/weight’; ’foreign ’; ’cruise ’; ’kmpeuro ’; ’size ’; ’price ’];

79 fprintf(1,’OLS LOGIT DEMAND ESTIMATION\n’)

80 fprintf(1,’-------------------------------\n’)

81 fprintf(1,’variable beta se p\n’)

82 fprintf(1,’%s’, varnames(1,:))

83 fprintf(1,’ % 1.3f %1.3f %1.3f\n’,[betaols(1)’;sebetaols(1)’; pvalueols(1)’])

84 for i=2:k;

85 fprintf(1,’%s’, varnames(i,:))

86 fprintf(1,’ % 1.3f %1.3f %1.3f\n’,[betaols(i)’;sebetaols(i)’; pvalueols(i)’])

87 end

88 fprintf(1,’R-squared %1.3f\n’,R2ols)

89 fprintf(1,’# inel.dem. %1.0f\n’,length(find(abs(eols)<1)))

90 fprintf(1,’Avg. elas. % 1.3f\n’,mean(eols))

91 fprintf(1,’# products %1.0f\n’,length(X))

92 fprintf(1,’-------------------------------\n’)

93

11

MATLAB codeResults OLS

OLS LOGIT DEMAND ESTIMATION

-------------------------------

variable beta se p

constant -13.545 1.225 0.000

hp/weight -0.285 0.353 0.420

foreign -0.577 0.129 0.000

cruise 0.288 0.153 0.060

kmpeuro 2.963 0.459 0.000

size 0.335 0.099 0.001

price -0.028 0.006 0.000

R-squared 0.353

# inel.dem. 349

Avg. elas. -0.770

# products 443

-------------------------------

12

MATLAB codeestcars.m

94 %%%

95 % IV logit estimation

96 %%%

97

98 % create a matrix containing the instruments; similar to BLP

99 makeindex=[find(diff(makecode)~=0); J];

100 groupindex=[find(diff(groupcode)~=0); size(X,1)];

101 nmdl=[makeindex(1); diff(makeindex)];% number of models per brand

102

103 % create ownership matrix

104 owner=[];

105 ngroup=[groupindex(1); diff(groupindex)];

106 n=1;

107 groupcode2=zeros(length(groupcode),1);

108 for i=1:length(ngroup)

109 owner=blkdiag(owner,ones(ngroup(i)));

110 groupcode2(n:groupindex(i),:) = i*ones(ngroup(i),1);

111 n=groupindex(i)+1;

112 end

113

114 temp=cumsum(A);

115 sum1 = temp(groupindex,:);

116 sum1(2:size(sum1,1),:) = diff(sum1);

117 z1=A;

118 z2=sum1(groupcode2,:)-A;

119 sum2=temp(cdindex,:);

120 sum2(2:size(sum2,1),:) = diff(sum2);

121 z3=sum2(cdid,:)-sum1(groupcode2,:);

122 z2nd=[z2(:,2) z2(:,5) z2(:,6)]; % non-dummy attributes

123 z3nd=[z3(:,2) z3(:,5) z3(:,6)]; % non-dummy attributes

124 iv=[z1 z2(:,1) sum(z2nd,2) z3(:,1) sum(z3nd,2) sum(z1,2).*z2(:,1)...

... sum(z1,2).*sum(z2nd,2) sum(z1,2).*z3(:,1) sum(z1,2).*sum(z3nd,2)]; % instruments 13

MATLAB codeestcars.m

125

126 xhat = iv/(iv’*iv)*iv’*X;

127 PX2sls = (xhat’*xhat)\xhat’; % project. matrix on weighted x-space for 2SLS

128 beta2sls = PX2sls*y;

129 covbeta2sls=(1/(n-k))*((y-X*beta2sls)’*(y-X*beta2sls))*inv(xhat’*xhat);

130 sebeta2sls=sqrt(diag(covbeta2sls));

131

132 alpha2sls=beta2sls(size(A,2)+1);

133 e2sls=alpha2sls*pj.*(1-sj); %own-elasticity

134

135 ecp2sls=zeros(J,J);

136 for jj=1:J

137 for kk=1:J

138 ecp2sls(jj,kk)=-alpha2sls*pj(kk).*sj(kk);

139 end

140 end

141

142 tstat2sls=beta2sls./sebeta2sls;

143 pvalue2sls=2*(1-tcdf(abs(beta2sls./sebeta2sls),n-k));

144

14

MATLAB codeestcars.m

145 % print estimation results on screen

146 fprintf(1,’IV LOGIT DEMAND ESTIMATION\n’)

147 fprintf(1,’-------------------------------\n’)

148 fprintf(1,’variable beta se p\n’)

149 fprintf(1,’%s’, varnames(1,:))

150 fprintf(1,’ % 1.3f %1.3f %1.3f\n’,[beta2sls(1)’;sebeta2sls(1)’; pvalue2sls(1)’])

151 for i=2:k;

152 fprintf(1,’%s’, varnames(i,:))

153 fprintf(1,’ % 1.3f %1.3f %1.3f\n’,[beta2sls(i)’;sebeta2sls(i)’; pvalue2sls(i)’])

154 end

155 fprintf(1,’R-squared n.a.\n’)

156 fprintf(1,’# inel.dem. %1.0f\n’,length(find(abs(e2sls)<1)))

157 fprintf(1,’Avg. elas. % 1.3f\n’,mean(e2sls))

158 fprintf(1,’# products %1.0f\n’,length(X))

159 fprintf(1,’-------------------------------\n’)

160

15

MATLAB codeResults IV

IV LOGIT DEMAND ESTIMATION

-------------------------------

variable beta se p

constant -14.119 1.289 0.000

hp/weight 0.576 0.591 0.330

foreign -0.733 0.156 0.000

cruise 0.361 0.161 0.025

kmpeuro 2.620 0.504 0.000

size 0.455 0.120 0.000

price -0.052 0.014 0.000

R-squared n.a.

# inel.dem. 164

Avg. elas. -1.451

# products 443

-------------------------------

16

MATLAB codeestcars.m

161 %%%%

162 % ESTIMATE DEMAND SIDE BLP

163 %%%

164

165 W=iv’*iv; % weight matrix

166 X=A;

167 vfull=v(cdid,:);

168

169 options = optimset(’Display’,’iter’,’MaxIter’,1000,’MaxFunEvals’,10000,’TolFun’,1e-4,...

...’TolX’,1e-4,’GradObj’,’off’,’LargeScale’,’on’);

170

171 tic

172 [theta2hat3,fval3] = fminsearch(’gmmobj’,[5 1 1 1 1 1 1],options);

173 t=toc;

174

175 fprintf(1,’It took %1.0f minutes and %1.0f seconds to estimate the demand side of BLP.\n’,...

...floor(t/60),round(t-floor(t/60)*60))

176

177 theta1hat3 = theta1;

178

179 delta=meanval(theta2hat3);

180 sigma=theta2hat3(1:end);

181 mu=mufunc(X,sigma);

182

17

MATLAB codegmmobj.m

1 function f = gmmobj(theta2)

2 % This function computes the GMM objective function

3 % Based on code written by Aviv Nevo, May 1998

4 % Adapted by Matthijs Wildenbeest, April 2010

5

6 global theta1 X iv W

7

8 delta = meanval(theta2);

9

10 temp1 = X’*iv;

11 temp2 = iv’*delta;

12 theta1 = (temp1/W*temp1’)\temp1/W*temp2;

13 clear temp1 temp2

14 gmmresid = delta - X*theta1;

15 temp1 = gmmresid’*iv;

16 f = temp1/W*temp1’;

17 clear temp1

18

MATLAB codemeanval.m

1 function f = meanval(theta2)

2 % This function computes the mean utility level

3 % Based on code written by Aviv Nevo, May 1998

4 % Adapted by Matthijs Wildenbeest, April 2010

5

6 global sj sj0 nsm X

7

8 tol = 1e-6;

9

10 norm = 10;

11 avgnorm = 10;

12

13 mvalold=(log(sj)-log(sj0));

14

15 i = 0;

16 while norm > tol && avgnorm > tol

17

18 mu=mufunc(X,theta2);

19

20 mval = mvalold + log(sj) - log(sum(ind_sh(mvalold,mu),2)/nsm);

21

22 t = abs(mval-mvalold);

23 norm = max(t);

24 avgnorm = mean(t);

25 mvalold = mval;

26 i = i + 1;

27 end

28 %disp([’# of iterations for delta convergence: ’ num2str(i)]);

29

30 f = mval;

19

MATLAB codemufunc.m

1 function f = mufunc(X,sigma)

2 % This function computes the non-linear part of the utility

3 % Based on code written by Aviv Nevo, May 1998

4 % Adapted by Matthijs Wildenbeest, April 2010

5

6 global nsm vfull incfull pj

7 [n k] = size(X);

8

9 for i = 1:nsm

10 v_i = vfull(:,i:nsm:k*nsm);

11 inc_i = incfull(:,i);

12 mu(:,i) = (-sigma(1).*pj./inc_i+(X.*v_i*(sigma(2:end))’));

13 end

14 f = mu;

20

MATLAB codeind sh.m

1 function f = ind_sh(delta,mu)

2 % This function computes the "individual" probabilities of choosing each brand

3 % Based on code written by Aviv Nevo, May 1998.

4 % Adapted by Matthijs Wildenbeest, April 2010

5

6 global nsm cdindex cdid

7 eg = exp(mu).*kron(ones(1,nsm),exp(delta));

8 temp = cumsum(eg);

9 sum1 = temp(cdindex,:);

10 sum1(2:size(sum1,1),:) = diff(sum1);

11

12 denom1 = 1./(1+sum1);

13 denom = denom1(cdid,:);

14

15 sij=eg.*denom;

16

17 f = sij;

21

MATLAB codeestcars.m

183 % print estimation results on screen

184 fprintf(1,’GMM DEMAND ESTIMATION\n’)

185 fprintf(1,’--------------------------\n’)

186 fprintf(1,’variable beta stdev\n’)

187 fprintf(1,’%s’, varnames(1,:))

188 fprintf(1,’ % 1.3f % 1.3f\n’,[theta1hat3(1)’;theta2hat3(2)’])

189 for i=2:size(X,2);

190 fprintf(1,’%s’, varnames(i,:))

191 fprintf(1,’ % 1.3f % 1.3f\n’,[theta1hat3(i)’;theta2hat3(i+1)’])

192 end

193 fprintf(1,’%s’, varnames(size(X,2)+1,:))

194 fprintf(1,’ % 1.3f ---\n’,theta2hat3(1)’)

195 fprintf(1,’Obj. func. %1.3f\n’,fval3)

196 fprintf(1,’# products %1.0f\n’,length(X))

197 fprintf(1,’--------------------------\n’)

198

22

MATLAB codeResults GMM Demand

GMM DEMAND ESTIMATION

--------------------------

variable beta stdev

constant -10.365 -0.377

hp/weight 1.199 -0.358

foreign -2.432 3.717

cruise -0.387 1.306

kmpeuro 0.811 0.709

size -0.568 0.783

price 3.047 ---

Obj. func. 0.734

# products 443

--------------------------

23

MATLAB codegmmobj2.m

1 function f = gmmobj2(theta2)

2 % This function computes the GMM objective function

3 % Based on code written by Aviv Nevo, May 1998

4 % Adapted by Matthijs Wildenbeest, April 2010

5

6 global theta1 X iv W pj X2

7

8 delta = meanval(theta2);

9

10 %%% SUPPLY SIDE

11 mu=mufunc(X,theta2); % non-linear part utility function

12 sij=ind_sh(delta,mu); % individual buying probabilities

13 alpha=theta2(1); % price coefficient

14 b=markup(alpha,sij); % markup

15 mc=log(pj-b); % marginal cost specification

16 delta = [delta; mc]; % delta now includes mc

17

18 X1 = blkdiag(X,X2);

19 iv2= blkdiag(iv,iv);

20 W=iv2’*iv2;

21 %%%

22

23 temp1 = X1’*iv2;

24 temp2 = iv2’*delta;

25 theta1 = (temp1/W*temp1’)\temp1/W*temp2;

26 clear temp1 temp2

27 gmmresid = delta - X1*theta1;

28 temp1 = gmmresid’*iv2;

29 f = temp1/W*temp1’;

30 clear temp1

24

MATLAB codemarkup.m

1 function b = markup(alpha,sij)

2 % This function computes the markups

3

4 % Written by Matthijs Wildenbeest, April 2010.

5

6 global D1 D nsm incfull cdindex owner

7

8 n = 1;

9 D=zeros(size(sij,1),size(sij,1));

10 s=sum(sij,2)/nsm;

11 ss=sum((alpha./incfull).*sij,2)/nsm;

12 for i = 1:size(cdindex,1)

13 D1 = (((alpha./incfull(n:cdindex(i),:)).*sij(n:cdindex(i),:))*sij(n:cdindex(i),:)’)/nsm;

14 D(n:cdindex(i),n:cdindex(i)) = (diag(ss(n:cdindex(i))) - D1);

15 n = cdindex(i) + 1;

16 end

17

18 b=(D.*owner)\s;

25

MATLAB codeestcars.m

199 %%%%

200 % ESTIMATE DEMAND AND SUPPLY SIDE BLP

201 %%%

202

203 X2=[A(:,1) log(A(:,2)) A(:,3) A(:,4) log(kpl/10) log(A(:,6))]; % contains the cost side explanatory variables

204

205 tic

206 [theta2hat4,fval4] = fminsearch(’gmmobj2’,[5 1 1 1 1 1 1],options);

207 t=toc;

208

209 fprintf(1,’It took %1.0f minutes and %1.0f seconds to estimate the demand and supply side of BLP.\n’,floor(t/60),round(t-floor(t/60)*60))

210

211 theta1hat4 = theta1;

212

213 delta=meanval(theta2hat4);

214 sigma=theta2hat4(1:end);

215 mu=mufunc(X,sigma);

216

26

MATLAB codeestcars.m

217 % print estimation results on screen

218 fprintf(1,’GMM DEMAND AND SUPPLY ESTIMATION\n’)

219 fprintf(1,’---------------------------------\n’)

220 fprintf(1,’variable beta stdev cost\n’)

221 fprintf(1,’%s’, varnames(1,:))

222 fprintf(1,’ % 1.3f % 1.3f % 1.3f\n’,[theta1hat4(1)’;theta2hat4(2)’;theta1hat4(7)])

223 for i=2:size(X,2);

224 fprintf(1,’%s’, varnames(i,:))

225 fprintf(1,’ % 1.3f % 1.3f % 1.3f\n’,[theta1hat4(i)’;theta2hat4(i+1)’;theta1hat4(i+6)])

226 end

227 fprintf(1,’%s’, varnames(size(X,2)+1,:))

228 fprintf(1,’ % 1.3f --- ---\n’,theta2hat4(1)’)

229 fprintf(1,’Obj. func. %1.3f\n’,fval4)

230 fprintf(1,’# products %1.0f\n’,length(X))

231 fprintf(1,’---------------------------------\n’)

27

MATLAB codeResults GMM Demand and Supply

GMM DEMAND AND SUPPLY ESTIMATION

---------------------------------

variable beta stdev cost

constant -12.039 0.183 -3.909

hp/weight 1.188 0.397 1.197

foreign -4.031 3.765 -0.137

cruise -0.088 1.064 -0.162

kmpeuro 0.991 -0.081 -1.123

size 0.351 0.354 3.478

price 4.125 --- ---

Obj. func. 4.575

# products 443

---------------------------------

28