-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.m
238 lines (192 loc) · 6.52 KB
/
test.m
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
% test script
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
J = 10; % number of products
N = 2; % number of attributes
M = 1; % number of markets
Ms = [1,J+1]; % market blocks
I = 1000; % number of "individuals" for sample-variance draws
% attributes (random)
Y = randn(J,N);
% "true" betas (in the case where we use outside good)
btaT = rand(N,1); btaT = [ btaT ; - 0.9 * max(btaT) ];
% outside good
og = 'y';
% fmincon or ktrlink?
sol = 'f';
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% initial condition
bta0 = [];
% options and solver
switch( sol ),
case 'k',
opt = optimset('ktrlink');
opt.Display = 'final';
opt.GradObj = 'on';
opt.GradConstr = 'on';
opt.Algorithm = 'interior-point';
% opt.DerivativeCheck = 'on';
otherwise,
opt = optimset('fmincon');
opt.Display = 'final';
opt.GradObj = 'on';
opt.GradConstr = 'on';
opt.Algorithm = 'interior-point';
% opt.Algorithm = 'sqp';
% opt.DerivativeCheck = 'on';
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% PPV/NPV TESTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% draw shares
s = drawshares( Y , btaT , J , M , Ms , 100 , og );
% compute model estimates
[bta,flag,code] = MLELogit(J,N,M,Ms,Y,s,og,bta0,opt,sol);
% now, approximate PPV / NPV; i.e., probability that a product is chosen
% given that the model says it is chosen. This is a function of x... so
% first define x
x = randn(N,1);
% now, draw choices with each model (do we equate errors?)
for i = 1:1000,
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SOLUTION TESTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Is = [10,100,1000,10000,100000];
figure(1), clf,
for n = 1:N,
subplot(1,N,n),
semilogx( [min(Is),max(Is)] , [0,0] , '--k' ),
end
for t = 1:size(Is,2),
for T = 1:10,
s = drawshares( Y , btaT , J , M , Ms , Is(t) , og );
[bta,flag,code] = MLELogit(J,N,M,Ms,Y,s,og,bta0,opt,sol);
for n = 1:N,
subplot(1,N,n), hold on,
semilogx( Is(t) , 100*(bta(n)-btaT(n))/abs(btaT(n)) , ...
'.k' , 'MarkerSize' , 20 ),
end
end
s = drawshares( Y , btaT , J , M , Ms , Is(t) , og );
[bta,flag,code] = MLELogit(J,N,M,Ms,Y,s,og,bta0,opt,sol);
% assume this last model is accurate
for T = 1:10,
s1 = drawshares( Y , bta , J , M , Ms , I , og );
[bta1,flag,code] = MLELogit(J,N,M,Ms,Y,s1,og,bta,opt,sol);
for n = 1:N,
subplot(1,N,n), hold on,
semilogx( Is(t) , 100*(bta1(n)-bta(n))/abs(bta(n)) , ...
'.r' , 'MarkerSize' , 20 ),
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MLE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% solve with MLE
[bta,flag,code] = MLELogit(J,N,M,Ms,Y,s,og,bta0,opt,sol);
flag, if( flag < 0 ), code, end
bta, btaT,
% take coefficients at face value, and resample to determine a coefficient
% range
btaR = [ bta , bta ];
for t = 1:10,
% draw shares assuming the model made is accurate
s1 = drawshares( Y , bta , J , M , Ms , I , og );
% solve for MLE estimates
[btaS(:,t),flag,code] = MLELogit(J,N,M,Ms,Y,s1,og,bta,opt,sol);
btaR(:,1) = min( btaR(:,1) , btaS(:,t) );
btaR(:,2) = max( btaR(:,2) , btaS(:,t) );
end
btaS, btaR, btaT
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% GMM %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % no instruments,
%
% % instruments
% K = 0; Z = [];
%
% % no weighting
% w = 0; W = [];
%
% % solving
% [bta,flag,code] = GMMLogit(J,N,K,M,Ms,Y,s,og,Z,w,W,bta0,opt,sol);
%
% flag, if( flag < 0 ), code, end
% bta, btaT,
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % generic weighting
%
% % instruments
% K = 0; Z = [];
%
% % generic weighting
% w = 2; R = triu(randn(J,J)); W = R' * R;
%
% % solving
% [bta,flag,code] = GMMLogit(J,N,K,M,Ms,Y,s,og,Z,w,W,bta0,opt,sol);
%
% flag, if( flag < 0 ), code, end
% bta, btaT,
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % instrumented, not weighted
%
% % instruments
% K = 1; Z = randn(J,K);
%
% % no weighting
% w = 0; W = [];
%
% % solving
% [bta,flag,code] = GMMLogit(J,N,K,M,Ms,Y,s,og,Z,w,W,bta0,opt,sol);
%
% flag, if( flag < 0 ), code, end
% bta, btaT,
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % instrumented, Z weighting
%
% % instruments
% K = 1; Z = randn(J,K);
%
% % Z weighting
% w = 1; W = [];
%
% % solving
% [bta,flag,code] = GMMLogit(J,N,K,M,Ms,Y,s,og,Z,w,W,bta0,opt,sol);
%
% flag, if( flag < 0 ), code, end
% bta, btaT,
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % instrumented, generic weighting
%
% % instruments
% K = 2; Z = randn(J,K);
%
% % generic weighting
% w = 2; R = triu(randn(K,K)); W = R'*R;
%
% % solving
% [bta,flag,code] = GMMLogit(J,N,K,M,Ms,Y,s,og,Z,w,W,bta0,opt,sol);
%
% flag, if( flag < 0 ), code, end
% bta, btaT,
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% OLS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% [bta,flag,code] = OLSLogit(J,N,K,M,Ms,Y,s,og,[],bta0,opt,sol);
%
% flag, if( flag < 0 ), code, end
% bta, btaT,
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%