-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgpuIFP.m
More file actions
168 lines (142 loc) · 3.68 KB
/
gpuIFP.m
File metadata and controls
168 lines (142 loc) · 3.68 KB
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
clear; clc;
reset(gpuDevice);
%
% Parameters
%
R = 1.03;
beta = 0.95;
bMin = 0.0;
bMax = 2.0;
ySz = 51;
bSz = 900;
%
% Endowment Process
%
[ y, Pi ] = makeMarkovChain(0.0, 0.95, 0.05, ySz);
% Grids
y = gpuArray(single(exp(y)));
b = gpuArray(single(linspace(bMin, bMax, bSz)));
gpuPi = gpuArray(single(Pi));
% Memory on GPU
V0 = gpuArray.zeros([ ySz, bSz ], 'single');
V1 = gpuArray.zeros([ ySz, bSz ], 'single');
bPrIx = gpuArray.ones([ ySz, bSz ], 'uint16');
% Pre-compute consumption & utils for all (y, b, b')
[gpuB, gpuY, gpuA] = meshgrid(b', y', b');
cc = gpuY - gpuB + R * gpuA;
uu = (1-beta) * (1.0 - 1.0 / cc);
uu(cc < 0.0) = -5000.0;
%
% Iterate
%
err = 1.0;
errTol = 1.0e-8;
iter = 1;
while err > errTol
tic;
contVal = repmat(gpuPi * V0, [1, 1, bSz ]);
[ V1, bPrIx ] = max( uu + beta * contVal, [], 2 );
V1 = squeeze(V1);
bPrIx = squeeze(bPrIx);
err = max(abs(V1(:)-V0(:)));
V0 = V1;
elapsed = toc;
if mod(iter, 20) == 0
fprintf('%d ~ %8.6f ~ %8.6fs \n', iter, err, elapsed);
end
iter = iter + 1;
end
% Done. Bring from GPU
b = double(gather(b));
V1 = double(gather(V1));
bPrIx = gather(bPrIx);
%
% Values and Policies
%
figure(1)
plot(b, b(bPrIx) - repmat(b, [ySz, 1]), '-'); hold on; plot(b, zeros(size(b)), '--k');
xlabel('Assets (b)');
ylabel('Change in Assets (b'' - b)');
title('Policy');
figure(2)
plot(b, V1, '-');
xlabel('Assets (b)');
title('Value Function');
%
% Stationary distribution with eigenvector of sparse matrix
%
% Collapse 2D to 1D
mapping = zeros([ ySz*bSz, 2 ]);
revMapping = zeros([ ySz bSz ]);
ix = 1;
for yIx = 1:ySz
for bIx = 1:bSz
mapping(ix, 1) = yIx;
mapping(ix, 2) = bIx;
revMapping(yIx, bIx) = ix;
ix = ix + 1;
end
end
ii = zeros([ySz * bSz * ySz, 1]);
jj = zeros([ySz * bSz * ySz, 1]);
vv = zeros([ySz * bSz * ySz, 1]);
ix = 1;
for bIx = 1:bSz
for yIx = 1:ySz
myFrom = revMapping(yIx, bIx);
dest = bPrIx(yIx, bIx);
for yPrIx = 1:ySz
ii(ix) = myFrom;
jj(ix) = revMapping(yPrIx, dest);
vv(ix) = Pi(yIx, yPrIx);
ix = ix + 1;
end
end
end
S = sparse(ii, jj, vv);
if size(S, 2) < size(S, 1)
S(:, ySz * bSz) = 0.0;
end
% Eigenvector of Markov transition of (y, b) => (y', b')
[VV, DD] = eigs(S.', 1);
VV = VV ./ sum(VV);
stat = ones([ySz, bSz]);
for yIx = 1:ySz
for bIx = 1:bSz
stat(yIx, bIx) = VV(revMapping(yIx, bIx));
end
end
% stat constains the stationary distribution
% figure(3);
% contourf(b(2:end), y, stat(:, 2:end), 15); colorbar;
fprintf('Agg savings %6.4f at r = %6.4f \n', dot( sum(stat, 1), b ), R-1);
function [ X, Pi ] = makeMarkovChain(yBar, rho, sgma, n)
%
% Construct Markov Chain with states X and transition probabilities Pi
% using AR(1) with mean yBar, autocorrelation rho, and s.d. of innovation
% sgma, with n points of support.
%
% X = equally spaced over an interval
% [ yBar - 2 s.d, yBar + 2 s.d. ]
X = linspace( yBar - 2 * sgma / sqrt(1 - rho^2), ...
yBar + 2 * sgma / sqrt(1 - rho^2), n);
% Pi - transition matrix
Pi = zeros([ n, n ]);
for stYesterday = 1:n
% work at state stYesterday
% Left tail...
Pi(stYesterday, 1) = normcdf((X(1) + X(2))/2, ...
(1-rho) * yBar + rho * X(stYesterday), sgma);
% Right tail...
Pi(stYesterday, n) = 1 - normcdf((X(n-1) + X(n))/2, ...
(1-rho) * yBar + rho * X(stYesterday), sgma);
% Interior states...
for stToday = 2:n-1
Pi(stYesterday, stToday) = ...
normcdf((X(stToday) + X(stToday+1))/2, ...
(1-rho) * yBar + rho * X(stYesterday), sgma) ...
- normcdf((X(stToday-1) + X(stToday))/2, ...
(1-rho) * yBar + rho * X(stYesterday), sgma);
end
end
end