-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathps4.f95
291 lines (264 loc) · 8.83 KB
/
ps4.f95
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
MODULE PS4PARA
IMPLICIT NONE
INTEGER:: I ! FOR ITER
REAL(KIND=8), PARAMETER:: ALPHA=1.5, BETA=0.9932
REAL(KIND=8), DIMENSION(2), PARAMETER:: STATES=(/1.0, 0.5/)
INTEGER, PARAMETER:: NZ=SIZE(STATES), NA=700
REAL(KIND=8), DIMENSION(NZ, NZ), PARAMETER:: PI=TRANSPOSE(RESHAPE((/0.97, 0.03,0.5,0.5/),(/2,2/)))
REAL(KIND=8), PARAMETER:: A_MIN=-2., A_MAX=5., STEPS=(A_MAX-A_MIN)/(FLOAT(NA)-1.)
REAL(KIND=8), DIMENSION(NA), PARAMETER:: A_GRID=(/(I*STEPS, I=1, NA)/) + A_MIN - STEPS
LOGICAL:: COMPLETE=.FALSE. ! COMPLETE MARKET IF 1 OR INCOMPLETE MARKET IF 0
END MODULE
MODULE PS4RES
USE PS4PARA
IMPLICIT NONE
REAL(KIND=8):: HIGH_Q=1., LOW_Q=0.9932, Q
REAL(KIND=8), DIMENSION(NA,NZ):: VFUNC=0., VFUNC_NEW=0., CONSUM_ARR, LAMBDA
REAL(KIND=8), DIMENSION(NA*NZ):: STAT_DIST
REAL(KIND=8), DIMENSION(NA*NZ,NA*NZ):: A_CHANGE_MAT
REAL(KIND=8), DIMENSION(700):: LORENZ
INTEGER, DIMENSION(NA,NZ):: PFUNC=0
END MODULE
PROGRAM PS4
USE PS4PARA
USE PS4RES
IMPLICIT NONE
INTEGER:: SROWIDX
REAL(KIND=8):: ERROR_Q=100., ERROR_VFI, CRIT=1e-3
REAL(KIND=8), DIMENSION(NA,NZ):: VFUNC_INCOMPLETE=0.
DO WHILE (ERROR_Q> CRIT)
PRINT*, "Q", Q
Q = (HIGH_Q+LOW_Q)/2.
ERROR_VFI=100.
IF (COMPLETE) THEN
! MUST RUN THE INCOMPLETE VERSION PRIOR TO
OPEN(UNIT=13, FILE='VFUNC', STATUS='OLD')
DO SROWIDX=1,NA
READ(UNIT=13, FMT=*) VFUNC_INCOMPLETE(SROWIDX,:)
ENDDO
CLOSE(UNIT=13)
DO WHILE (ERROR_VFI> CRIT)
CALL BELLMAN_COMPLETE(ERROR_VFI, VFUNC_INCOMPLETE)
ENDDO
ELSE
DO WHILE (ERROR_VFI> CRIT)
CALL BELLMAN(ERROR_VFI)
ENDDO
ENDIF
CALL POP_A_CHANGE_MAT()
CALL FIND_STAT_DIST()
CALL COMPUTE_ERROR(ERROR_Q)
ENDDO
PRINT*, "Q", Q
CALL FIND_LORENZ()
IF (COMPLETE) THEN
CALL WELFARE(VFUNC_INCOMPLETE)
ENDIF
CALL WRITE_ALL()
END PROGRAM PS4
SUBROUTINE BELLMAN(ERROR_VFI)
USE PS4PARA
USE PS4RES
IMPLICIT NONE
REAL(KIND=8), INTENT(OUT):: ERROR_VFI
REAL(KIND=8):: CONSUM, UTIL, COND_UTIL, CU, NU
REAL(KIND=8), DIMENSION(NA,NZ):: ABS_DIFF
INTEGER:: RIDX, CIDX, CHOICE
DO RIDX=1, NA
DO CIDX=1, NZ
COND_UTIL=-1e10
DO CHOICE=1, NA
CONSUM = STATES(CIDX) + A_GRID(RIDX) - Q*A_GRID(CHOICE)
IF (CONSUM>0.) THEN
CU = CONSUM**(1-ALPHA)/(1-ALPHA) ! CURRENT UTIL
NU = SUM(PI(CIDX,:)*VFUNC(CHOICE,:))! NEXT EXPECTED UTIL
UTIL = CU + BETA*NU
IF (UTIL>COND_UTIL) THEN
VFUNC_NEW(RIDX, CIDX)=UTIL
PFUNC(RIDX, CIDX)=CHOICE
COND_UTIL=UTIL
CONSUM_ARR(RIDX, CIDX) = CONSUM
ENDIF
ENDIF
ENDDO
ENDDO
ENDDO
ABS_DIFF = ABS(VFUNC_NEW-VFUNC)
ERROR_VFI = MAXVAL(ABS_DIFF)
VFUNC = VFUNC_NEW
END SUBROUTINE
SUBROUTINE BELLMAN_COMPLETE(ERROR_VFI, VFUNC_INCOMPLETE)
USE PS4PARA
USE PS4RES
IMPLICIT NONE
REAL(KIND=8), INTENT(OUT):: ERROR_VFI
REAL(KIND=8):: CONSUM, UTIL, COND_UTIL, CU, NOM, DENOM
REAL(KIND=8), DIMENSION(NA,NZ):: ABS_DIFF
REAL(KIND=8), DIMENSION(NA,NZ), INTENT(IN):: VFUNC_INCOMPLETE
INTEGER:: RIDX, CIDX, CHOICE
DO RIDX=1, NA
DO CIDX=1, NZ
COND_UTIL=-1e10
DO CHOICE=1, NA
CONSUM = STATES(CIDX) + A_GRID(RIDX) - Q*A_GRID(CHOICE)
IF (CONSUM>0.) THEN
CU = CONSUM**(1-ALPHA)/(1-ALPHA) ! CURRENT UTIL
UTIL = CU ! COMPLETE MARKET NEXT UTIL= CURRENT UTIL
IF (UTIL>COND_UTIL) THEN
VFUNC_NEW(RIDX, CIDX)=UTIL
PFUNC(RIDX, CIDX)=CHOICE
COND_UTIL=UTIL
CONSUM_ARR(RIDX, CIDX) = CONSUM
ENDIF
ENDIF
ENDDO
NOM = VFUNC_NEW(RIDX, CIDX) + 1/((1-ALPHA)*(1-BETA))
DENOM = VFUNC_INCOMPLETE(RIDX, CIDX) + 1/((1-ALPHA)*(1-BETA))
LAMBDA(RIDX, CIDX) = (NOM/DENOM)**(1/(1-ALPHA))-1
ENDDO
ENDDO
ABS_DIFF = ABS(VFUNC_NEW-VFUNC)
ERROR_VFI = MAXVAL(ABS_DIFF)
VFUNC = VFUNC_NEW
END SUBROUTINE
SUBROUTINE POP_A_CHANGE_MAT()
USE PS4PARA
USE PS4RES
IMPLICIT NONE
INTEGER:: RIDX, CIDX, CUR_IDX, NEXT_IDX, CHOICE, NEXT_Z
A_CHANGE_MAT=0.
DO RIDX=1, NA
DO CIDX=1, NZ
CUR_IDX = 2*RIDX -2 + CIDX
CHOICE=PFUNC(RIDX, CIDX)
DO NEXT_Z=1, NZ
NEXT_IDX= 2*CHOICE -2 + NEXT_Z
A_CHANGE_MAT(CUR_IDX, NEXT_IDX) = A_CHANGE_MAT(CUR_IDX, NEXT_IDX)+ PI(CIDX, NEXT_Z)
ENDDO
ENDDO
ENDDO
END SUBROUTINE
SUBROUTINE FIND_STAT_DIST()
USE PS4PARA
USE PS4RES
IMPLICIT NONE
REAL(KIND=8):: ERROR_STAT, CRIT_STAT=1e-3
REAL(KIND=8), DIMENSION(NA*NZ) :: STAT_DIST_NEW, ABS_DIFF
STAT_DIST= 1/(FLOAT(NA)*2.)
ERROR_STAT=100.
DO WHILE (ERROR_STAT>CRIT_STAT)
STAT_DIST_NEW = MATMUL(STAT_DIST, A_CHANGE_MAT)
ABS_DIFF = ABS(STAT_DIST_NEW-STAT_DIST)
ERROR_STAT = MAXVAL(ABS_DIFF)
STAT_DIST = STAT_DIST_NEW
ENDDO
END SUBROUTINE
SUBROUTINE COMPUTE_ERROR(ERROR_Q)
USE PS4PARA
USE PS4RES
IMPLICIT NONE
REAL(KIND=8), INTENT(OUT):: ERROR_Q
INTEGER:: RIDX, CIDX
REAL(KIND=8):: NET_ASSET! MARKET CLEARING
NET_ASSET = 0.
DO RIDX=1, NA
DO CIDX=1, NZ
NET_ASSET = NET_ASSET + A_GRID(PFUNC(RIDX, CIDX))*STAT_DIST(2*RIDX -2 + CIDX)
ENDDO
ENDDO
IF (NET_ASSET>0.) THEN
HIGH_Q=Q
ELSE
LOW_Q = Q
ENDIF
ERROR_Q = ABS(HIGH_Q-LOW_Q)
END SUBROUTINE
SUBROUTINE FIND_LORENZ()
USE PS4PARA
USE PS4RES
IMPLICIT NONE
INTEGER:: RIDX
REAL(KIND=8):: GINI
REAL(KIND=8), DIMENSION(NA):: DEGREE
DEGREE=(/(I*(1./FLOAT(NA)), I=1,NA)/)
GINI= 0.
LORENZ(1) = STAT_DIST(1)
DO RIDX = 2, 700
LORENZ(RIDX)= LORENZ(RIDX-1) + STAT_DIST(RIDX*2-2+1) + STAT_DIST(RIDX*2-2+2)
GINI = GINI + ABS(DEGREE(RIDX)-LORENZ(RIDX))
ENDDO
PRINT*, "degree", degree(na), degree(na-1)
PRINT*,"GINI", GINI/FLOAT(NA)
END SUBROUTINE
SUBROUTINE WRITE_ALL()
USE PS4PARA
USE PS4RES
IMPLICIT NONE
INTEGER:: SROWIDX
INTEGER, DIMENSION(2):: SIDX
CHARACTER(LEN=130):: PATH="./"
CHARACTER(LEN=150):: FILE_NAME
FILE_NAME = TRIM(PATH)//"VFUNC"
OPEN(UNIT=1, FILE=FILE_NAME, STATUS='REPLACE') ! START WITH THE TWO VALUE FUNCTIONS
DO SROWIDX=1, NA
WRITE(UNIT=1,FMT=*) VFUNC(SROWIDX,:)
ENDDO
CLOSE(UNIT=1)
FILE_NAME = TRIM(PATH)//"PFUNC"
OPEN(UNIT=2, FILE=FILE_NAME, STATUS='REPLACE') ! ALSO SAVE POLICY FUNCTIONS
DO SROWIDX=1, NA
WRITE(UNIT=2,FMT=*) PFUNC(SROWIDX,:)
ENDDO
CLOSE(UNIT=2)
FILE_NAME = TRIM(PATH)//"STATDIST"
OPEN(UNIT=3, FILE=FILE_NAME, STATUS='REPLACE') ! ALSO SAVE POLICY FUNCTIONS
DO SROWIDX=1, NA
SIDX = (/SROWIDX*2-1, SROWIDX*2/)
WRITE(UNIT=3,FMT=*) STAT_DIST(SIDX)
ENDDO
CLOSE(UNIT=3)
FILE_NAME = TRIM(PATH)//"AGRID"
OPEN(UNIT=4, FILE=FILE_NAME, STATUS='REPLACE') ! FOR HAVING THE X-AXIS OF PLOT
DO SROWIDX=1, NA
WRITE(UNIT=4,FMT=*) A_GRID(SROWIDX)
ENDDO
CLOSE(UNIT=4)
FILE_NAME = TRIM(PATH)//"LORENZ"
OPEN(UNIT=5, FILE=FILE_NAME, STATUS='REPLACE') ! FOR HAVING THE X-AXIS OF PLOT
DO SROWIDX=1, 100
WRITE(UNIT=5,FMT=*) LORENZ(SROWIDX)
ENDDO
CLOSE(UNIT=5)
IF (COMPLETE) THEN
FILE_NAME = TRIM(PATH)//"LAMBDA"
OPEN(UNIT=6, FILE=FILE_NAME, STATUS='REPLACE') ! FOR HAVING THE X-AXIS OF PLOT
DO SROWIDX=1, NA
WRITE(UNIT=6,FMT=*) LAMBDA(SROWIDX,:)
ENDDO
CLOSE(UNIT=6)
ENDIF
END SUBROUTINE
SUBROUTINE WELFARE(VFUNC_INCOMPLETE)
USE PS4PARA
USE PS4RES
IMPLICIT NONE
REAL(KIND=8):: WG=0., WINC=0., WFB=0., VOTE=0.
INTEGER:: RIDX, CIDX, DIS_LOC
REAL(KIND=8), DIMENSION(NA,NZ), INTENT(IN):: VFUNC_INCOMPLETE
PRINT*, SUM(STAT_DIST), "STAT"
DO RIDX=1,NA
DO CIDX=1, NZ
DIS_LOC = RIDX*2-2 + CIDX
WG = WG+ LAMBDA(RIDX, CIDX)* STAT_DIST(DIS_LOC)
WINC = WINC +VFUNC_INCOMPLETE(RIDX, CIDX)*STAT_DIST(DIS_LOC)
WFB = WFB + VFUNC(RIDX, CIDX)*STAT_DIST(DIS_LOC)
IF (LAMBDA(RIDX, CIDX)>0.) THEN
VOTE = VOTE + STAT_DIST(DIS_LOC)
ENDIF
ENDDO
ENDDO
PRINT*, "FRIST BEST", WFB
PRINT*, "WINC", WINC
PRINT*, "WG", WG
PRINT*, "VOTING FOR", VOTE
END SUBROUTINE