-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimizeTaxesPrices.m
363 lines (276 loc) · 12.1 KB
/
optimizeTaxesPrices.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
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
356
357
358
359
360
361
362
363
function [t_1a_opt, t_2a_opt, p_1a_opt, p_2a_opt, p_2b_opt] = ...
optimizeTaxesPrices(parameters, p_guess, t_guess, M, N, ...
tol_f, tax_1a_type, tax_2a_type)
% ========================================================================
% OPTIMIZETAXESPRICES Taxes are optimized by setting the present taxes
% to a predefined tax type; this is the fixed point iteration method.
% Prices are optimized through the secant method. This requires two
% guesses: the first you can input in 'p_guess' and the latter is just
% randomly chosen to be close to your initial guess.
% Each iteration involves optimizing the taxes and then optimizing the
% prices, so it is garaunteed that the final results at least have the
% correct prices
% ========================================================================
% INPUT ARGUMENTS:
% parameters (struct) generated by loadParameters
% p_guess (vector) initial guess for prices
% t_guess (vector) initial guess for taxes
% M (int) number of optimization iterations
% N (int) number of secant method
% optimization iterations
% tol_f (func) tolerance as a function of M
% tax_1a_type (string) pick from 'none', 'myopic', 'dynamic',
% or 'optimal'
% tax_2a_type (string) pick from 'none' or 'pigouvian'
% ========================================================================
% OUTPUT:
% t_1a_opt (scalar) optimal period 1a tax
% t_1a_opt (scalar) optimal period 2a tax
% p_1a_opt (scalar) optimal period 1a price
% p_2a_opt (scalar) optimal period 2a price
% p_2b_opt (scalar) optimal period 2b price
% =========================================================================
%% Pre-lims
% debug structs
debug_off.taxes = 0;
debug_off.prices = 0;
debug_on.prices = 1;
debug_on.taxes = 1;
% set params to intial guesses
parameters.taxes_1a = t_guess(1);
parameters.taxes_2a = t_guess(2);
parameters.prices_1a = p_guess(1);
parameters.prices_2a = p_guess(2);
parameters.prices_2b = p_guess(3);
%% Main
% =============================Secant Method=============================
% Start with guesses x_0, x_1
% Estimate f'(x_1) using (f(x_1) - f(x_0))/(x_1 - x_0)
% Set x_2 = x_1 - f(x_1)/f'(x_1)
% Break if within tolerance or end of iterations
% Problems when f' close or equal to 0, in those cases, break early
% =======================================================================
fprintf(['\n\n' repmat('=', 1, 55) '\n' repmat('=', 1, 10) ...
' Running optimization procedure... ' repmat('=', 1, 10) '\n' ...
repmat('=', 1, 55) '\n']);
for j = 1:M
fprintf([repmat(' ', 1, 20) 'Iteration %i\n'], j);
% set tolerance
tol = tol_f(j);
fprintf('\nSetting tolerance to: %f\n', tol)
%% Optimize Period 1A tax
x_est_0 = t_guess(1);
x_est_1 = t_guess(1)*(1+randn()/100) + randn()/1000;
fprintf('\nOptimizing Period 1A tax...\n')
for i = 1:N
parameters.taxes_1a = x_est_0;
[~, tax_residual, ~, ~, pollution_aggregates, ~, ~, ~ ] = ...
runSimulationSingle( parameters, debug_off );
[t_1a_opt, ~] = getOptimalTaxes(parameters, ...
tax_residual, pollution_aggregates, tax_1a_type, tax_2a_type);
f_at_est_0 = parameters.taxes_1a - t_1a_opt;
parameters.taxes_1a = x_est_1;
[~, tax_residual, ~, ~, pollution_aggregates, ~, ~, ~ ] = ...
runSimulationSingle( parameters, debug_off );
[t_1a_opt, ~] = getOptimalTaxes(parameters, ...
tax_residual, pollution_aggregates, tax_1a_type, tax_2a_type);
f_at_est_1 = parameters.taxes_1a - t_1a_opt;
f_diff_hat = (f_at_est_1 - f_at_est_0)/(x_est_1 - x_est_0);
if abs(f_diff_hat) < 1e-100 || isnan(f_diff_hat)
x_est_new = x_est_1;
parameters.taxes_1a = x_est_new;
break
else
x_est_new = x_est_1 - (f_at_est_1)/(f_diff_hat);
end
x_est_0 = x_est_1;
x_est_1 = x_est_new;
parameters.taxes_1a = x_est_new;
[~, tax_residual, ~, ~, pollution_aggregates, ~, ~, ~ ] = ...
runSimulationSingle( parameters, debug_off );
[t_1a_opt, ~] = getOptimalTaxes(parameters, ...
tax_residual, pollution_aggregates, tax_1a_type, tax_2a_type);
f_at_est_new = parameters.taxes_1a - t_1a_opt;
fprintf(' Tax_Residual_1A: %f\n', f_at_est_new)
if abs(f_at_est_new) < tol
break
end
end
%% Optimize Period 2A tax
x_est_0 = t_guess(2);
x_est_1 = t_guess(2)*(1+randn()/100) + randn()/1000;
fprintf('\nOptimizing Period 2A tax...\n')
for i = 1:N
parameters.taxes_2a = x_est_0;
[~, tax_residual, ~, ~, pollution_aggregates, ~, ~, ~ ] = ...
runSimulationSingle( parameters, debug_off );
[~, t_2a_opt] = getOptimalTaxes(parameters, ...
tax_residual, pollution_aggregates, tax_1a_type, tax_2a_type);
f_at_est_0 = parameters.taxes_2a - t_2a_opt;
parameters.taxes_2a = x_est_1;
[~, tax_residual, ~, ~, pollution_aggregates, ~, ~, ~ ] = ...
runSimulationSingle( parameters, debug_off );
[~, t_2a_opt] = getOptimalTaxes(parameters, ...
tax_residual, pollution_aggregates, tax_1a_type, tax_2a_type);
f_at_est_1 = parameters.taxes_2a - t_2a_opt;
f_diff_hat = (f_at_est_1 - f_at_est_0)/(x_est_1 - x_est_0);
if abs(f_diff_hat) < 1e-100 || isnan(f_diff_hat)
x_est_new = x_est_1;
parameters.taxes_2a = x_est_new;
break
else
x_est_new = x_est_1 - (f_at_est_1)/(f_diff_hat);
end
x_est_0 = x_est_1;
x_est_1 = x_est_new;
parameters.taxes_2a = x_est_new;
[~, tax_residual, ~, ~, pollution_aggregates, ~, ~, ~ ] = ...
runSimulationSingle( parameters, debug_off );
[~, t_2a_opt] = getOptimalTaxes(parameters, ...
tax_residual, pollution_aggregates, tax_1a_type, tax_2a_type);
f_at_est_new = parameters.taxes_2a - t_2a_opt;
fprintf(' Tax_Residual_1A: %f\n', f_at_est_new)
if abs(f_at_est_new) < tol
break
end
end
%% Optimize Period 1A price
x_est_0 = p_guess(1);
x_est_1 = p_guess(1)*(1+randn()/100);
fprintf('\nOptimizing Period 1A price...\n')
for i = 1:N
parameters.prices_1a = x_est_0;
[~, ~, output_residual, ~, ~, ~, ~, ~] = ...
runSimulationSingle( parameters, debug_off );
f_at_est_0 = output_residual(1);
parameters.prices_1a = x_est_1;
[~, ~, output_residual, ~, ~, ~, ~, ~] = ...
runSimulationSingle( parameters, debug_off );
f_at_est_1 = output_residual(1);
f_diff_hat = (f_at_est_1 - f_at_est_0)/(x_est_1 - x_est_0);
x_est_new = x_est_1 - (f_at_est_1)/(f_diff_hat);
x_est_0 = x_est_1;
x_est_1 = x_est_new;
parameters.prices_1a = x_est_new;
[~, ~, output_residual, ~, ~, ~, ~, ~] = ...
runSimulationSingle( parameters, debug_off );
f_at_est_new = output_residual(1);
fprintf(' Output_Residual_1A: %f\n', f_at_est_new)
if abs(f_at_est_new) < tol
break
end
end
%% Optimize Period 2A price
x_est_0 = p_guess(2);
x_est_1 = p_guess(2)*(1+randn()/100);
fprintf('\nOptimizing Period 2A price...\n')
for i = 1:N
parameters.prices_2a = x_est_0;
[~, ~, output_residual, ~, ~, ~, ~, ~] = ...
runSimulationSingle( parameters, debug_off );
f_at_est_0 = output_residual(2);
parameters.prices_2a = x_est_1;
[~, ~, output_residual, ~, ~, ~, ~, ~] = ...
runSimulationSingle( parameters, debug_off );
f_at_est_1 = output_residual(2);
f_diff_hat = (f_at_est_1 - f_at_est_0)/(x_est_1 - x_est_0);
x_est_new = x_est_1 - (f_at_est_1)/(f_diff_hat);
x_est_0 = x_est_1;
x_est_1 = x_est_new;
parameters.prices_2a = x_est_new;
[~, ~, output_residual, ~, ~, ~, ~, ~] = ...
runSimulationSingle( parameters, debug_off );
f_at_est_new = output_residual(2);
fprintf(' Output_Residual_2A: %f\n', f_at_est_new)
if abs(f_at_est_new) < tol
break
end
end
%% Optimize Period 2B price
x_est_0 = p_guess(2);
x_est_1 = p_guess(2)*(1+randn()/100);
fprintf('\nOptimizing Period 2B price...\n')
for i = 1:N
parameters.prices_2b = x_est_0;
[~, ~, output_residual, ~, ~, ~, ~, ~] = ...
runSimulationSingle( parameters, debug_off );
f_at_est_0 = output_residual(3);
parameters.prices_2b = x_est_1;
[~, ~, output_residual, ~, ~, ~, ~, ~] = ...
runSimulationSingle( parameters, debug_off );
f_at_est_1 = output_residual(3);
f_diff_hat = (f_at_est_1 - f_at_est_0)/(x_est_1 - x_est_0);
x_est_new = x_est_1 - (f_at_est_1)/(f_diff_hat);
x_est_0 = x_est_1;
x_est_1 = x_est_new;
parameters.prices_2b = x_est_new;
[~, ~, output_residual, ~, ~, ~, ~, ~] = ...
runSimulationSingle( parameters, debug_off );
f_at_est_new = output_residual(3);
fprintf(' Output_Residual_2B: %f\n', f_at_est_new)
if abs(f_at_est_new) < tol
break
end
end
%% Results of Iteration
fprintf(['\nOptimization Results (Iteration %i)\n'], j);
[~, tax_residual, ~, ~, ~, ~, ~, ~] = ...
runSimulationSingle( parameters, debug_on );
fprintf([repmat('-', 1, 55) '\n']);
p_guess = [parameters.prices_1a, parameters.prices_2a, ...
parameters.prices_2b];
t_guess = [parameters.taxes_1a, parameters.taxes_2a];
end
%% Optimization Results
t_1a_opt = parameters.taxes_1a;
t_2a_opt = parameters.taxes_2a;
p_1a_opt = parameters.prices_1a;
p_2a_opt = parameters.prices_2a;
p_2b_opt = parameters.prices_2b;
% Results
fprintf([repmat(' ', 1, 16) 'Optimization Complete\n' ...
repmat('-', 1, 55) '\nOptimal Taxes:\n Period 1A: %f\n ' ...
'Period 2A: %f\nOptimal Prices:\n Period 1A: %f\n ' ...
'Period 2A: %f\n Period 2B: %f\n'], t_1a_opt, t_2a_opt, ...
p_1a_opt, p_2a_opt, p_2b_opt)
% Residual
[~, tax_residual, output_residual, ~, pollution_aggregates, ~, ~, ~ ] = ...
runSimulationSingle( parameters, debug_off );
[t_1a_opt_2, t_2a_opt_2] = getOptimalTaxes(parameters, ...
tax_residual, pollution_aggregates, tax_1a_type, tax_2a_type);
fprintf(['Tax Residual:\n Period 1A: %f\n ' ...
'Period 2A: %f\nOutput Residual:\n Period 1A: %f\n ' ...
'Period 2A: %f\n Period 2B: %f\n'], t_1a_opt - t_1a_opt_2, ...
t_2a_opt - t_2a_opt_2, output_residual(1), output_residual(2), ...
output_residual(3))
end
%% Auxillary functions
function [t_1a_opt, t_2a_opt] = getOptimalTaxes(parameters, ...
tax_residual, pollution_aggregates, tax_1a_type, tax_2a_type)
switch tax_1a_type
case 'none'
t_1a_opt = 0;
case 'myopic'
t_1a_opt = parameters.xi*2*pollution_aggregates(1);
case 'dynamic'
t_1a_opt = ...
2*parameters.xi*pollution_aggregates(1) + ...
parameters.delta*(1-parameters.psi)*(parameters.alpha*2 ...
*parameters.xi*pollution_aggregates(2) + (1- ...
parameters.alpha)*2*parameters.xi*pollution_aggregates(3));
case 'optimal'
t_1a_opt = parameters.taxes_1a - tax_residual(1);
otherwise
error('Unknown tax_1a_type')
end
switch tax_2a_type
case 'none'
t_2a_opt = 0;
case 'pigouvian'
t_2a_opt = parameters.taxes_2a - tax_residual(2);
case 'optimal'
t_2a_opt = parameters.taxes_2a - tax_residual(2);
otherwise
error('Unknown tax_2a_type')
end
end