-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathBenPorathContTimeLH.m
293 lines (239 loc) · 9.71 KB
/
BenPorathContTimeLH.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
%{
Continuous time Ben-Porath model. Based on Manuelli-Seshadri (AER 2014)
max present value of
w * h(a) * (1 - n(a)) - px * x(a)
subject to
h(0) given
\dot(h) = A (h * n) ^ alpha * x ^ beta - deltaH * h
Code must be efficient
%}
classdef BenPorathContTimeLH < handle
properties
A double
deltaH double
gamma1 double
gamma2 double
px double
% Annual interest rate
r double
wage double
% length of work life
T double
h0 double
dbg uint8 = 0
end
properties (Dependent)
% gamma1 + gamma2
gamma
% Optimal x / nh when n is interior
% From static FOC
x2nh
end
methods
%% Constructor
function bpS = BenPorathContTimeLH(A, deltaH, gamma1, gamma2, T, h0, px, r, wage)
bpS.A = A;
bpS.deltaH = deltaH;
bpS.gamma1 = gamma1;
bpS.gamma2 = gamma2;
bpS.px = px;
bpS.r = r;
bpS.wage = wage;
bpS.T = T;
bpS.h0 = h0;
bpS.validate;
end
%% Validation
function validate(bpS)
validateattributes(bpS.A, {'double'}, {'finite', 'nonnan', 'nonempty', 'real', 'scalar', 'positive'})
validateattributes(bpS.deltaH, {'double'}, {'finite', 'nonnan', 'nonempty', 'real', 'scalar', ...
'>=', 0, '<', 1})
validateattributes(bpS.gamma1, {'double'}, {'finite', 'nonnan', 'nonempty', 'real', 'scalar', 'positive', '<', 1})
validateattributes(bpS.gamma2, {'double'}, {'finite', 'nonnan', 'nonempty', 'real', 'scalar', 'positive', '<', 1})
assert(bpS.gamma1 + bpS.gamma2 < 0.99);
validateattributes(bpS.px, {'double'}, {'finite', 'nonnan', 'nonempty', 'real', 'scalar', 'positive'})
validateattributes(bpS.r, {'double'}, {'finite', 'nonnan', 'nonempty', 'real', 'scalar', '>=', 0, '<', 1})
validateattributes(bpS.T, {'double'}, {'finite', 'nonnan', 'nonempty', 'real', 'scalar', 'positive'})
end
function gma = get.gamma(bpS)
gma = bpS.gamma1 + bpS.gamma2;
end
function xOverNh = get.x2nh(bpS)
xOverNh = bpS.wage / bpS.px * bpS.gamma2 / bpS.gamma1;
end
% Term in brackets in many equations (Q)
function out1 = bracket_term(bpS)
out1 = bpS.A * bpS.gamma1 / (bpS.r + bpS.deltaH) * ((bpS.gamma2 / bpS.gamma1 * bpS.wage ./ bpS.px) ^ bpS.gamma2);
end
% Marginal value of h. Only depends on horizon remaining, not on current h
% (20) in MS 2014
function q_aV = marginal_value_h(bpS, ageV)
q_aV = bpS.wage .* bpS.m_age(ageV) ./ (bpS.r + bpS.deltaH);
end
%% Marginal value of increasing starting age
% -r V + dV/d(age0)
%{
This can be computed without integration (easier than computing the parts separately)
Useful for optimal stopping rule that sets starting age (e.g. MS2014 school problem)
Must be efficient
%}
function mValue = marginal_value_age0(bpS)
OneMinusGamma = 1 - bpS.gamma;
m0 = bpS.m_age(0);
C1 = OneMinusGamma ./ bpS.gamma1 .* (bpS.bracket_term .^ (1 ./ OneMinusGamma));
mValue = bpS.wage .* bpS.h0 ./ (bpS.r + bpS.deltaH) .* (bpS.mprime_age(0) - bpS.r .* m0) ...
- bpS.wage .* C1 .* (m0 .^ (1 ./ OneMinusGamma));
validateattributes(mValue, {'double'}, {'finite', 'nonnan', 'nonempty', 'real', 'scalar'})
end
% dV/d(age0)
% C1 = bpS.h0 .* bpS.mprime_age(0) ./ (bpS.r + bpS.deltaH);
% C2 = -(bpS.m_age(0) .^ (1 ./ (1 - bpS.gamma)));
% C3 = integral(@mva_nested, 0, bpS.T);
%
% mValue = bpS.wage .* (C1 + (1 - bpS.gamma) ./ bpS.gamma1 .* (bpS.bracket_term .^ (1 ./ (1 - bpS.gamma))) .* ...
% (C2 + bpS.r .* C3));
% validateattributes(mValue, {'double'}, {'finite', 'nonnan', 'nonempty', 'real', 'scalar'})
%
% % Nested: integrand
% function outV = mva_nested(tV)
% outV = exp(- bpS.r .* tV) .* (bpS.m_age(tV) .^ (1 ./ (1 - bpS.gamma)));
% end
% end
%% Marginal value of T
function mValue = marginal_value_T(bpS)
mValue = bpS.wage .* bpS.h0 ./ (bpS.r + bpS.deltaH) .* bpS.dm_dT(0) + ...
bpS.wage .* (1 - bpS.gamma) ./ bpS.gamma1 .* (bpS.bracket_term .^ (1 ./ (1-bpS.gamma))) .* ...
integral(@integ_mv, 0, bpS.T);
validateattributes(mValue, {'double'}, {'finite', 'nonnan', 'nonempty', 'real', 'scalar'})
% Nested: integrand
function outV = integ_mv(tV)
outV = exp(-bpS.r .* tV) .* (bpS.m_age(tV) .^ (bpS.gamma ./ (1 - bpS.gamma))) .* ...
bpS.dm_dT(tV) ./ (1 - bpS.gamma);
end
end
%% Technology: hDot as a function of h,n,x
function hDotV = htech(bpS, hV, nV, xV)
hDotV = bpS.A .* ((hV .* nV) .^ bpS.gamma1) .* (xV .^ bpS.gamma2) - bpS.deltaH .* hV;
end
%% Age earnings profile
function [earnV, haV, naV, xwV] = age_earnings_profile(bpS, ageV)
xwV = bpS.x_age(ageV);
[haV, naV] = bpS.h_age(ageV);
earnV = bpS.wage .* haV .* (1 - naV) - bpS.px .* xwV;
validateattributes(earnV, {'double'}, {'finite', 'nonnan', 'nonempty', 'real', 'size', size(ageV)})
end
%% Present value of lifetime earnings
% Value function V(h,T)
% MS 2014 have a closed form solution
function pvEarn = pv_earnings(bpS)
gma = bpS.gamma;
gmaInv = 1 / (1 - gma);
rr = bpS.r;
c1 = bpS.m_age(0) ./ (bpS.r + bpS.deltaH) .* bpS.h0;
c2 = (1 - gma) / bpS.gamma1 .* (bpS.bracket_term .^ gmaInv);
int1 = integral(@int1_nested, 0, bpS.T);
pvEarn = bpS.wage .* (c1 + c2 .* int1);
validateattributes(pvEarn, {'double'}, {'finite', 'nonnan', 'nonempty', 'real', 'scalar'})
function out1 = int1_nested(t)
out1 = exp(-rr .* t) .* (bpS.m_age(t) .^ gmaInv);
end
end
%% n(a) h(a) from (17)
%{
This agrees with MS
Can be used to determine whether hh chooses n<1 at age 0
%}
function [nhV, xV] = nh(bpS, ageV)
%c1 = bpS.A .* (bpS.gamma1 .^ (1 - bpS.gamma2)) .* (bpS.gamma2 .^ bpS.gamma2) ./ (bpS.r + bpS.deltaH);
%c2 = (bpS.wage ./ bpS.px) .^ bpS.gamma2;
maV = bpS.m_age(ageV);
nhV = (bpS.bracket_term .* maV) .^ (1 / (1-bpS.gamma));
validateattributes(nhV, {'double'}, {'finite', 'nonnan', 'nonempty', 'real', ...
'>=', 0, 'size', size(ageV)})
% Static condition recovers x
xV = nhV .* bpS.x2nh;
validateattributes(xV, {'double'}, {'finite', 'nonnan', 'nonempty', 'real', '>=', 0})
end
%% x(a) from (18)
% this is where MS2014 have a mistake
% Easier to get this from nh()
function xwV = x_age(bpS, ageV)
c1 = bpS.gamma2 / bpS.gamma1 * bpS.wage ./ bpS.px;
%c2 = bpS.A * bpS.gamma1 / (bpS.r + bpS.deltaH) * ((bpS.gamma2 / bpS.gamma1 * bpS.wage ./ bpS.px) ^ bpS.gamma2);
maV = bpS.m_age(ageV);
xwV = c1 .* ((bpS.bracket_term .* maV) .^ (1 / (1-bpS.gamma)));
validateattributes(xwV, {'double'}, {'finite', 'nonnan', 'nonempty', 'real', ...
'>=', 0, 'size', size(ageV)})
end
%% h(a) from (19)
%{
It would be nice to speed this up. The loop over ages is expensive, but hard to avoid
A faster version of intergral would help. Most of its time is spent on parsing arguments +++
%}
function [haV, naV] = h_age(bpS, ageV)
c2 = bpS.bracket_term;
c3V = zeros(size(ageV));
for i1 = 1 : length(ageV)
age = ageV(i1);
c3V(i1) = integral(@c3_integrand, 0, ageV(i1));
end
validateattributes(c3V, {'double'}, {'finite', 'nonnan', 'nonempty', 'real', ...
'>=', 0, 'size', size(ageV)})
haV = exp(-bpS.deltaH .* ageV) .* bpS.h0 + ...
(bpS.r + bpS.deltaH) ./ bpS.gamma1 .* (c2 .^ (1 / (1 - bpS.gamma))) .* c3V;
validateattributes(haV, {'double'}, {'finite', 'nonnan', 'nonempty', 'real', ...
'>', 0, 'size', size(ageV)})
naV = bpS.nh(ageV) ./ haV;
validateattributes(naV, {'double'}, {'finite', 'nonnan', 'nonempty', 'real', ...
'>=', 0, 'size', size(ageV)})
% Nested: integrand for c3
function x = c3_integrand(tV)
mtV = bpS.m_age(tV);
x = exp(-bpS.deltaH .* (age - tV)) .* (mtV .^ (bpS.gamma / (1 - bpS.gamma)));
validateattributes(x, {'double'}, {'finite', 'nonnan', 'nonempty', 'real', ...
'>=', 0})
end
end
%% m(a)
% Efficiency important here
function maV = m_age(bpS, ageV)
maV = 1 - exp((bpS.r + bpS.deltaH) .* (ageV - bpS.T));
%validateattributes(bpS.T - ageV, {'double'}, {'finite', 'nonnan', 'nonempty', 'real', '>=', 0})
%validateattributes(maV, {'double'}, {'finite', 'nonnan', 'nonempty', 'real', 'size', size(ageV)})
end
% m'(a)
function mPrimeV = mprime_age(bpS, ageV)
mPrimeV = -(bpS.r + bpS.deltaH) .* exp((bpS.r + bpS.deltaH) .* (ageV - bpS.T));
end
% dm(a)/dT
function dmdT = dm_dT(bpS, ageV)
dmdT = (bpS.r + bpS.deltaH) .* exp((bpS.r + bpS.deltaH) .* (ageV - bpS.T));
end
%% Path h(a) for given inputs n(a), x(a); and h0
%{
The result is not super precise (see test function)
IN
ageV, naV, xaV
on an age grid: inputs (will be interpolated)
ageOut
solve for h at this age
OUT
tOutV, hOutV
values of h(t) between 0 and ageOut
%}
function [tOutV, hOutV] = hpath(bpS, ageOut, ageV, naV, xaV)
if ageOut < 1e-6
tOutV = ageOut;
hOutV = bpS.h0;
return;
end
% Make a smooth approximation to n(a) and x(a)
nFct = griddedInterpolant(ageV, naV, 'linear');
xFct = griddedInterpolant(ageV, xaV, 'linear');
% Solve the ode
% Integral of hDot between 0 and ageOut
[tOutV, hOutV] = ode45(@(t,h) bpS.htech(h, nFct(t), xFct(t)), [0, ageOut], bpS.h0);
validateattributes(hOutV, {'double'}, {'finite', 'nonnan', 'nonempty', 'real', 'positive'})
end
end
end