-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.f90
726 lines (636 loc) · 28.7 KB
/
main.f90
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
! ******************************************************************************
! THIS CODE CONTROLS THE EXECUTION OF THE PROGRAM AND CONTAINS THE SUBROUTINES
! TO SOLVE THE STEADY STATE, THE DYNAMICS AND TO CALIBRATE THE MODEL.
! ******************************************************************************
! Compilation command:
! cd ..
! gfortran -fopenmp -O3 -ffixed-line-length-150 -fmax-stack-var-size=1000000 -J $(pwd)/compiledfiles toolkit.f90 parameters.f90 solution.f90 dynamics.f90 main.f90 -o lpw
PROGRAM main
USE omp_lib
USE parameters
IMPLICIT NONE
INTEGER :: i,j,solmet,ip,is,iw,iz,solmet2,cnoise(4),cinfl(5)
REAL(rp) :: time1,time0,timemp1,timemp0,kappa_pi0,kappa_w0
REAL(rp) :: PRICES1(3),ZEROS1(3),PARSMAX(7),PARSMIN(7)
CHARACTER(LEN=8) :: date
CHARACTER(LEN=10) :: time
CHARACTER(LEN=1) :: vers0,vers1
! ******************************************************************************
! INITIALIZE COMPUTATION
! Time and date of execution
CALL DATE_AND_TIME(DATE=date,TIME=time)
! Set working directory
j = LEN(TRIM(ADJUSTL(path)))
IF (path(j:j).EQ."/" .OR. path(j:j).EQ."\") THEN
CALL CHDIR(TRIM(ADJUSTL(path))//"textfiles/")
ELSE
CALL CHDIR(TRIM(ADJUSTL(path))//"/textfiles/")
END IF
! Start timing
CALL CPU_TIME(time0)
! Start timing (for OpenMp calculations)
timemp0 = omp_get_wtime()
! Set number of threads for parallel computation
numthreads = omp_get_max_threads( )
! ******************************************************************************
! PRINT HEADER
PRINT *, ' '
PRINT *, ' **************************************************************** '
PRINT *, ' MONETARY POLICY IMPLICATIONS OF STATE-DEPENDENT PRICES AND WAGES '
PRINT *, ' Anton Nakov, James Costain and Borja Petit '
PRINT *, ' 2020 '
PRINT *, ' **************************************************************** '
PRINT *, ' '
PRINT *, ' Date: ',date(7:8),'/',date(5:6),'/',date(1:4)
PRINT *, ' Time: ',time(1:2),':',time(3:4)
PRINT *, ' '
WRITE(*,'(A,I3)') ' Threads: ',numthreads
PRINT *, ' '
PRINT *, ' **************************************************************** '
! ******************************************************************************
! INITIALIZE PARAMETERS
! Read calibrated parameters
OPEN(unit=1,file="calibparams.txt",action='read')
READ(1,*) lbar
READ(1,*) rhobar
READ(1,*) kappa_pi
READ(1,*) kappa_lambda
READ(1,*) kappa_w
READ(1,*) kappa_rho
READ(1,*) rho_z
READ(1,*) stdMC_z
READ(1,*) rho_s
READ(1,*) stdMC_s
CLOSE (1)
! Baseline adjustment cost parameters (for experiments)
kappa_pi0 = kappa_pi
kappa_w0 = kappa_w
! Extra inflation rate (for experiements)
mu0 = zero
! Build grids and transition matrices
CALL SET_MATS( )
! Fill vector with empirical moments from the data
CALL SET_MOMENTS( )
! ******************************************************************************
! DEFINE WHAT TO DO AND INITIALIZE SOLUTION
vers0 = " " ! Model version - adjustment costs
vers1 = " " ! Model version - inflation rate
! **************************************
! Ask what to do
WRITE(*,'(A)') ' '
WRITE(*,'(A)') ' What do you want to do? '
WRITE(*,'(A)') ' '
WRITE(*,'(A)') ' (1) Solve steady state '
WRITE(*,'(A)') ' (2) Solve steady state and dynamics '
WRITE(*,'(A)') ' (3) Calibrate '
WRITE(*,'(A)') ' (4) Solve for diff. noise parameters '
WRITE(*,'(A)') ' (5) Solve for diff. inflation rates '
WRITE(*,'(A)') ' (6) Solve for all cases '
WRITE(*,'(A)') ' (7) Solve for pre and post inflation rates '
WRITE(*,'(A)') ' '
WRITE(*,'(A)',ADVANCE="NO") ' --> Your choice (1-7): ' ; READ (*,*) solmet
! **************************************
! Ask again if incorrect choice
IF (solmet.LT.1 .AND. solmet.GT.7) THEN
WRITE(*,'(A)',ADVANCE="NO") ' Incorrect choice: from 1 to 7. ' ; GOTO 9
END IF
! **************************************
! If solving steady state and/or dynamics, choose adjustment costs
IF (solmet.EQ.1 .OR. solmet.EQ.2) THEN ; vers0 = " "
WRITE(*,'(A)',ADVANCE="YES") ' '
WRITE(*,'(A)',ADVANCE="YES") ' Which version? '
WRITE(*,'(A)',ADVANCE="YES") ' '
WRITE(*,'(A)',ADVANCE="YES") ' (1) Sticky prices and sticky wages '
WRITE(*,'(A)',ADVANCE="YES") ' (2) Semi-flexible prices and sticky wages '
WRITE(*,'(A)',ADVANCE="YES") ' (3) Flexible prices and sticky wages '
WRITE(*,'(A)',ADVANCE="YES") ' (4) Sticky prices and sticky wages '
WRITE(*,'(A)',ADVANCE="YES") ' (5) Sticky prices and semi-lexible wages '
WRITE(*,'(A)',ADVANCE="YES") ' (6) Flexible prices and flexible wages '
WRITE(*,'(A)',ADVANCE="YES") ' '
WRITE(*,'(A)',ADVANCE="NO") ' --> Your choice (1-6): ' ; READ (*,*) j
IF (j.LT.1 .AND. j.GT.6) THEN
WRITE (*,'(A)',ADVANCE="NO") ' Incorrect version: from 1 to 6. '
GOTO 9
END IF
WRITE(vers0,'(I1)') j
CALL SETKAPPAS(vers0,kappa_pi0,kappa_w0)
END IF
! **************************************
! If solving steady state and/or dynamics, choose inflation
IF (solmet.EQ.1 .OR. solmet.EQ.2) THEN ; vers1 = " "
WRITE(*,'(A)',ADVANCE="YES") ' '
WRITE(*,'(A)',ADVANCE="YES") ' Which inflation rate? '
WRITE(*,'(A)',ADVANCE="YES") ' '
WRITE(*,'(A)',ADVANCE="YES") ' (0) Inflation = 2% (baseline) '
WRITE(*,'(A)',ADVANCE="YES") ' (1) Inflation = -1% '
WRITE(*,'(A)',ADVANCE="YES") ' (2) Inflation = 0% '
WRITE(*,'(A)',ADVANCE="YES") ' (3) Inflation = 4% '
WRITE(*,'(A)',ADVANCE="YES") ' (4) Inflation = 8% '
WRITE(*,'(A)',ADVANCE="YES") ' (5) Inflation = -2% '
WRITE(*,'(A)',ADVANCE="YES") ' (6) Inflation = 1% '
WRITE(*,'(A)',ADVANCE="YES") ' '
WRITE(*,'(A)',ADVANCE="NO" ) ' --> Your choice (0-6): ' ; READ (*,*) j
IF (j.LT.0 .OR. j.GT.6) THEN
WRITE (*,'(A)',ADVANCE="NO") ' Incorrect version: from 0 to 6. ' ; GOTO 9
END IF
WRITE(vers1,'(I1)') j
CALL SETINFLATION(vers1)
END IF
! **************************************
! If solving more than one version, choose whether to solve only steady-state or both steady-state and dynamics
IF (solmet.EQ.4 .OR. solmet.EQ.5 .OR. solmet.EQ.6) THEN
WRITE(*,'(A)',ADVANCE="YES") ' What to solve? '
WRITE(*,'(A)',ADVANCE="YES") ' '
WRITE(*,'(A)',ADVANCE="YES") ' (1) Steady-state '
WRITE(*,'(A)',ADVANCE="YES") ' (2) Steady-state and dynamics '
WRITE(*,'(A)',ADVANCE="YES") ' '
WRITE(*,'(A)',ADVANCE="NO") ' --> Your choice (1-7): ' ; READ (*,*) solmet2
IF (j.LT.1 .AND. j.GT.2) THEN
WRITE (*,'(A)',ADVANCE="NO") ' Incorrect choice: either 1 or 2. '
GOTO 9
END IF
END IF
PRINT * , ' '
PRINT * , ' **************************************************************** '
PRINT * , ' '
! ******************************************************************************
! IMPLEMENT DESIRED SOLUTION
! **************************************
! OPTION 1: Solve steady state for given version
IF (solmet.eq.1) THEN
CALL PRINTVERSION("V"//vers0//vers1)
CALL SOLVESTEADY("V"//vers0//vers1,2)
! **************************************
! OPTION 2: Solve steady state and dynamics for given version
ELSE IF (solmet.eq.2) THEN
CALL PRINTVERSION("V"//vers0//vers1)
CALL SOLVEDYN("V"//vers0//vers1)
! **************************************
! OPTION 3: Calibrate the parameters
ELSE IF (solmet.eq.3) THEN
CALL PRINTVERSION("VC0")
CALL CALIBRATE( )
! **************************************
! OPTION 4: Compute the steady-state and dynamics for the different values of the noise parameters
ELSEIF (solmet.EQ.4) THEN ; WRITE(vers1,'(I1)') 0
DO j=1,6 ; WRITE(vers0,'(I1)') j
CALL SETKAPPAS(vers0,kappa_pi0,kappa_w0)
CALL SETINFLATION(vers1)
CALL PRINTVERSION("V"//vers0//vers1)
IF (solmet2.eq.1) CALL SOLVESTEADY("V"//vers0//vers1)
IF (solmet2.eq.2) CALL SOLVEDYN("V"//vers0//vers1)
END DO
GOTO 9
! **************************************
! OPTION 5: Compute the steady-state and dynamics for different values of the inflation rate
ELSEIF (solmet.EQ.5) THEN ; WRITE(vers0,'(I1)') 1
DO j = 0,6 ; WRITE(vers1,'(I1)') j
CALL SETKAPPAS(vers0,kappa_pi0,kappa_w0)
CALL SETINFLATION(vers1)
CALL PRINTVERSION("V"//vers0//vers1)
IF (solmet2.eq.1) CALL SOLVESTEADY("V"//vers0//vers1)
IF (solmet2.eq.2) CALL SOLVEDYN("V"//vers0//vers1)
END DO
GOTO 9
! **************************************
! OPTION 6: Compute the steady state for all cases
ELSEIF (solmet.EQ.6) THEN ;
cnoise = (/ 1, 3, 5, 6 /)
cinfl = (/ 5, 2, 0, 3, 4 /)
DO i = 1,4 ; WRITE(vers0,'(I1)') cnoise(i)
DO j = 1,5 ; WRITE(vers1,'(I1)') cinfl(j)
!DO i = 1,6 ; WRITE(vers0,'(I1)') i
!DO j = 0,6 ; WRITE(vers1,'(I1)') j
CALL SETKAPPAS(vers0,kappa_pi0,kappa_w0)
CALL SETINFLATION(vers1)
CALL PRINTVERSION("V"//vers0//vers1)
IF (solmet2.eq.1) CALL SOLVESTEADY("V"//vers0//vers1)
IF (solmet2.eq.2) CALL SOLVEDYN("V"//vers0//vers1)
END DO
END DO
GOTO 9
! **************************************
! OPTION 7: Compute the steady state and the jacobian for cases 7 and 8 (pre and post 2000)
ELSEIF (solmet.EQ.7) THEN
vers0 = "1" ! vers0 = 1: baseline cost parameters
CALL SETKAPPAS(vers0,kappa_pi0,kappa_w0)
DO j=7,8 ; WRITE(vers1,'(I1)') j
CALL SETINFLATION(vers1)
CALL PRINTVERSION("V"//vers0//vers1)
CALL SOLVEDYN("V"//vers0//vers1)
END DO
GOTO 9
END IF
! ******************************************************************************
! PRINTING STATISTICS
PRINT * , ' '
PRINT *, ' **************************************************************** '
PRINT * , ' '
PRINT ('(A2,A7,F12.10)'), ' ' , 'cbar ' , cbar
PRINT ('(A2,A7,F12.10)'), ' ' , 'nbar ' , nbar
PRINT ('(A2,A7,F12.10)'), ' ' , 'wbar ' , wbar
PRINT * , ' '
PRINT ('(A2,60(A1))' ) , ' ', ('-',j=1,46)
PRINT ('(A2,A15,A14,A3,A14)' ) , ' ', ' ', ' Prices ' , ' |', ' Wages '
PRINT ('(A2,A15,2(A7,A7,A3))') , ' ', ' ', ' Data',' Model', ' |', ' Data',' Model'
PRINT ('(A2,60(A1))' ) , ' ', ('-',j=1,46)
DO j=1,9
PRINT ('(A2,A15,2(F7.2,F7.2,A3))') , ' ' ,&
MOMPRINTNAME(j),MOMPRINTDATA(j),MOMPRINTMODEL(j),' |' ,MOMPRINTDATA(j+19),MOMPRINTMODEL(j+19)
END DO
PRINT ('(A2,60(A1))' ) , ' ', ('-',j=1,46)
DO j=10,13
PRINT ('(A2,A15,2(A7,F7.2,A3))') , ' ' ,&
MOMPRINTNAME(j),' -- ',MOMPRINTMODEL(j), ' |' ,' -- ' ,MOMPRINTMODEL(j+19)
END DO
PRINT ('(A2,60(A1))' ) , ' ', ('-',j=1,46)
PRINT ('(A2,A15,2(A7,F7.2,A3))') , ' ' ,&
MOMPRINTNAME(14),' -- ',MOMPRINTMODEL(14),' |'
PRINT ('(A2,60(A1))' ) , ' ', ('-',j=1,32)
PRINT * , ' '
PRINT *, ' **************************************************************** '
PRINT * , ' '
! ******************************************************************************
! PRINTING TIMING:
9 CALL CPU_TIME(time1)
timemp1 = omp_get_wtime()
PRINT * , ' '
PRINT * , ' Completed! '
PRINT * , ' '
IF (time1 - time0 .lt. DBLE(60.0)) PRINT * , ' Time:' , time1-time0 ,'segs'
IF (time1 - time0 .ge. DBLE(60.0)) PRINT * , ' Time:' , (time1-time0)/DBLE(60.0) ,'mins'
IF (timemp1 - timemp0 .lt. DBLE(60.0)) PRINT * , ' Time:' , timemp1-timemp0 ,'segs'
IF (timemp1 - timemp0 .ge. DBLE(60.0)) PRINT * , ' Time:' , (timemp1-timemp0)/DBLE(60.0) ,'mins'
PRINT * , ' '
PRINT * , ' '
PRINT * , ' '
CONTAINS
! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
! SE PARAMETER VALUES FOR EXPERIMENTS
! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
! SET DIFFERENT VALUES FOR THE COST OF DECISION-MAKING
SUBROUTINE SETKAPPAS(vers,kp0,kw0)
IMPLICIT NONE
REAL(rp) , INTENT(IN) :: kp0,kw0
CHARACTER(LEN=1) , INTENT(IN) :: vers
IF (vers.EQ."1") THEN
kappa_pi = kp0
kappa_w = kw0
ELSE IF (vers.EQ."2") THEN
kappa_pi = kp0/diez
kappa_w = kw0
ELSE IF (vers.EQ."3") THEN
kappa_pi = kp0/cien
kappa_w = kw0
ELSE IF (vers.EQ."4") THEN
kappa_pi = kp0
kappa_w = kw0/diez
ELSE IF (vers.EQ."5") THEN
kappa_pi = kp0
kappa_w = kw0/cien
ELSE IF (vers.EQ."6") THEN
kappa_pi = kp0/cien
kappa_w = kw0/cien
END IF
kappa_lambda = kappa_pi
kappa_rho = kappa_w
RETURN
END SUBROUTINE SETKAPPAS
! SET DIFFERENT VALUES FOR THE STEADY-STATE INFLATION RATE
SUBROUTINE SETINFLATION(mucase)
IMPLICIT NONE
CHARACTER(LEN=1) , INTENT(IN) :: mucase
IF (mucase.EQ." ") mu0 = zero ! Inflation rate ~ 2% ---> 0% annual more
IF (mucase.EQ."0") mu0 = zero ! Inflation rate ~ 2% ---> 0% annual more
IF (mucase.EQ."1") mu0 = (((mu**DBLE(12.0))-DBLE(0.03))**(one/DBLE(12.0))) - mu ! Inflation rate ~ -1% ---> 3% annual less
IF (mucase.EQ."2") mu0 = (((mu**DBLE(12.0))-DBLE(0.02))**(one/DBLE(12.0))) - mu ! Inflation rate ~ +0% ---> 2% annual less
IF (mucase.EQ."3") mu0 = (((mu**DBLE(12.0))+DBLE(0.02))**(one/DBLE(12.0))) - mu ! Inflation rate ~ +4% ---> 2% annual more
IF (mucase.EQ."4") mu0 = (((mu**DBLE(12.0))+DBLE(0.06))**(one/DBLE(12.0))) - mu ! Inflation rate ~ +8% ---> 6% annual more
IF (mucase.EQ."5") mu0 = (((mu**DBLE(12.0))-DBLE(0.04))**(one/DBLE(12.0))) - mu ! Inflation rate ~ -2% ---> 4% annual less
IF (mucase.EQ."6") mu0 = (((mu**DBLE(12.0))-DBLE(0.01))**(one/DBLE(12.0))) - mu ! Inflation rate ~ +1% ---> 1% annual less
IF (mucase.EQ."7") mu0 = ( DBLE(1.046342)**(one/DBLE(12.0))) - mu ! Inflation rate ~ 4.63% ---> average 1980-2000
IF (mucase.EQ."8") mu0 = ( DBLE(1.020054)**(one/DBLE(12.0))) - mu ! Inflation rate ~ 2.01% ---> average 2000-2020
CALL SET_MATS( ) ! Re-fill the transition matrix to account for the chosen inflation rate
RETURN
END SUBROUTINE SETINFLATION
! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
! EQUILIBRIUM
! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
! SUBROUTINE TO SOLVE THE STEADY STATE
SUBROUTINE SOLVESTEADY(name,IPP)
IMPLICIT NONE
CHARACTER(LEN=3) , INTENT(IN) :: name
INTEGER , INTENT(IN) , OPTIONAL :: IPP
INTEGER :: IPR
IPR = 0 ; IF (PRESENT(IPP)) IPR = IPP
CALL SET_MATS( )
CALL READSTEADY(name)
CALL COMPUTEGE(ZEROS1,PRICES1,IPR)
CALL CALCSTATS( )
CALL WRITESTEADY(name)
RETURN
END SUBROUTINE SOLVESTEADY
! SUBROUTINE TO COMPUTE THE EQUAILIBRIUM
SUBROUTINE COMPUTEGE(Y1,X1,IPP)
USE toolkit , ONLY : LMMIN
IMPLICIT NONE
REAL(rp) , INTENT(OUT) :: Y1(3),X1(3)
INTEGER , INTENT(IN) , OPTIONAL :: IPP
REAL(rp) :: X0(3),YY
INTEGER :: ITER,IND,IPR
IPR = 0
IF (PRESENT(IPP) .AND. IPP.EQ.1) IPR = 1
IF (PRESENT(IPP) .AND. IPP.EQ.2) IPR = 2
IF (IPR.GT.1) WRITE(*,'(A)',ADVANCE="NO") ' Solving the steady state with initial guess... '
Y1 = FIRSTORDERCONDITIONS(cbar,nbar,wbar)
IF (IPR.GT.1) WRITE(*,'(A,F10.7)',ADVANCE="YES") ' Error = ', SQRT(SUM(Y1(:)*Y1(:)))
IF (SQRT(SUM(Y1(:)*Y1(:))).GT.DBLE(0.000001)) THEN
IF (IPR.GT.1) WRITE(*,'(A)') ' Error is too large. Computing the steady state '
X0 = (/ cbar , nbar , wbar /)
CALL LMMIN(EQUILIBRIUM,X1,Y1,ITER,IND,X0,ITERMAX=500,DAMP=one,SHCK=DBLE(0.05),IPRINT=IPR,USEBRO=0)
Y1 = FIRSTORDERCONDITIONS(X1(1),X1(2),X1(3))
END IF
RETURN
END SUBROUTINE COMPUTEGE
! FUNCTION THAT FINDS THE STEADY STATE CONSUMPTION, WAGE AND LABOR SUPPLY
FUNCTION EQUILIBRIUM(EQPRICE) RESULT(FOCS)
IMPLICIT NONE
DOUBLE PRECISION :: EQPRICE(:)
DOUBLE PRECISION, ALLOCATABLE :: FOCS(:)
ALLOCATE(FOCS(3))
FOCS = FIRSTORDERCONDITIONS(EQPRICE(1),EQPRICE(2),EQPRICE(3))
RETURN
END FUNCTION EQUILIBRIUM
! RESIDUALS FROM STEADY-STATE SYSTEM
FUNCTION FIRSTORDERCONDITIONS(cbar0,nbar0,wbar0) RESULT(FOCNDS)
USE parameters , ONLY : nump,nums,numw,numz,Wdist,Pdist,epsilon,epsilonN,&
Pi,w_grid,z_grid,p_grid,s_grid,lambda
USE solution , ONLY : SOLVEFIRMS,SOLVEWORKERS
IMPLICIT NONE
DOUBLE PRECISION :: cbar0,nbar0,wbar0
DOUBLE PRECISION :: FOCNDS(3)
REAL(rp) :: test_c,test_n,test_w,DDelta,Kpi,Klambda,KL_pi
INTEGER :: is,ip,iw,iz,ips
cbar = cbar0
nbar = nbar0
wbar = wbar0
CALL SOLVEFIRMS( )
CALL SOLVEWORKERS( )
test_c = zero ; DDelta = zero
test_n = zero ; Kpi = zero
test_w = zero ; Klambda = zero
! FOC for aggregate consumption
DO ip = 1,nump ; DO is = 1,nums
test_c = test_c + Pdist(ip,is)*exp(p_grid(ip)*(one-epsilon))
END DO ; END DO
! FOC for aggregate wgae
DO iw = 1,numw ; DO iz = 1,numz
test_w = test_w + Wdist(iw,iz)*exp((one-epsilonN)*(w_grid(iw)-z_grid(iz)))
END DO ; END DO
! FOC for aggregate labor supply
DO ip = 1,nump ; DO is = 1,nums
KL_pi = zero
DO ips = 1,nump
KL_pi = KL_pi + Pi(ips,is)*log(max(Pi(ips,is)*DBLE(nump),tol))
END DO
Kpi = Kpi + lambda(ip,is)*Pdist(ip,is)*KL_pi
DDelta = DDelta + Pdist(ip,is)*exp(s_grid(is)-epsilon*p_grid(ip))
Klambda = Klambda + Pdist(ip,is)*(lambda(ip,is)*log(max(tol,lambda(ip,is)/lbar)) + &
(one-lambda(ip,is))*log(max(tol,(one-lambda(ip,is))/(one-lbar))))
END DO ; END DO
test_n = DDelta*cbar + kappa_pi*Kpi + kappa_lambda*Klambda
! Residual in aggregate price (normalized to 1 - model in real terms)
FOCNDS(1) = one - test_c**(one/(one-epsilon))
! Residual in aggregate labor demand
FOCNDS(2) = nbar - test_n
! Residual in aggregate (real) wage
FOCNDS(3) = wbar - test_w**(one/(one-epsilonN))
RETURN
END FUNCTION FIRSTORDERCONDITIONS
! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
! CALIBRATION
! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
! SUBROUTINE TO CALIBRATE THE MODEL
SUBROUTINE CALIBRATE( )
USE toolkit , ONLY : SIMPLEX,LMMIN,NORMALIZE
IMPLICIT NONE
REAL(rp) :: PARS0(7),PARS1(7),PPR(3),PPY(3)
REAL(rp) :: TEST,TOLERANCE,SHOCK,DAMP,MOMS(144)
INTEGER :: ITER,ITERMAX,IPRINT,METHOD,IND
! Set optimization parameters
ITERMAX = 300 ! Maximum number of function evaluations
TOLERANCE = DBLE(0.00000001) ! Convergence criterium for sum of square errors.
IPRINT = 2 ! 2 to print every iteration, 1 to print only important workings, 0 to not print anything
DAMP = 1.0000 ! Initial damping factor for Levenberg–Marquardt
SHOCK = DBLE(0.01) ! Shock to compute numerical Jacobian (for Levenberg–Marquardt)
! Ask which method to use
1 WRITE(*,'(A)',ADVANCE="NO") ' Calibration method: (1) Simplex, (2) LMIN : '
READ (*,*) METHOD
IF ( METHOD.LT.1 .or. METHOD.GT.2) THEN
WRITE(*,'(A)',ADVANCE="NO") ' Invalid entry...'
GOTO 1
END IF
CALL READSTEADY("V10")
! Define moments weights in calibration
WEIGHT = zero
DO J=1,142
WEIGHT(J) = one
END DO
WEIGHT(143) = sqrt(142.0)
WEIGHT(144) = sqrt(142.0)
! Define max and min of parameters to calibrate
PARSMAX(1) = DBLE(0.30) ; PARSMIN(1) = DBLE(0.05) ! lbar
PARSMAX(2) = DBLE(0.30) ; PARSMIN(2) = DBLE(0.05) ! rhobar
PARSMAX(3) = DBLE(0.05) ; PARSMIN(3) = DBLE(0.005) ! kappa_w
PARSMAX(4) = DBLE(0.97) ; PARSMIN(4) = DBLE(0.50) ! rho_z
PARSMAX(5) = DBLE(0.08) ; PARSMIN(5) = DBLE(0.02) ! stdMC_z
PARSMAX(6) = DBLE(0.97) ; PARSMIN(6) = DBLE(0.50) ! rho_s
PARSMAX(7) = DBLE(0.12) ; PARSMIN(7) = DBLE(0.02) ! stdMC_s
! Redefine parameters to be unbounded (NORMALIZE transform bounded variables into unbounded ones, see toolkit.f90)
CALL NORMALIZE(PARS0(1), lbar, PARSMAX(1),PARSMIN(1),0)
CALL NORMALIZE(PARS0(2), rhobar, PARSMAX(2),PARSMIN(2),0)
CALL NORMALIZE(PARS0(3), kappa_w, PARSMAX(3),PARSMIN(3),0)
CALL NORMALIZE(PARS0(4), rho_z, PARSMAX(4),PARSMIN(4),0)
CALL NORMALIZE(PARS0(5), stdMC_z, PARSMAX(5),PARSMIN(5),0)
CALL NORMALIZE(PARS0(6), rho_s, PARSMAX(6),PARSMIN(6),0)
CALL NORMALIZE(PARS0(7), stdMC_s, PARSMAX(7),PARSMIN(7),0)
! Call chosen optimization routine
IF ( METHOD.eq.1 ) CALL SIMPLEX(SUMERRORS,PARS1,TEST,ITER,IND,PARS0,ITERMAX=1000,IPRINT=2)
IF ( METHOD.EQ.2 ) CALL LMMIN(ERRORS,PARS1,MOMS,ITER,IND,PARS0,ITERMAX=1000,IPRINT=2)
! Redefine parameter values (revert normalization with NORMALIZE)
CALL NORMALIZE(PARS0(1), lbar, PARSMAX(1),PARSMIN(1),1)
CALL NORMALIZE(PARS0(2), rhobar, PARSMAX(2),PARSMIN(2),1)
CALL NORMALIZE(PARS0(3), kappa_w, PARSMAX(3),PARSMIN(3),1)
CALL NORMALIZE(PARS0(4), rho_z, PARSMAX(4),PARSMIN(4),1)
CALL NORMALIZE(PARS0(5), stdMC_z, PARSMAX(5),PARSMIN(5),1)
CALL NORMALIZE(PARS0(6), rho_s, PARSMAX(6),PARSMIN(6),1)
CALL NORMALIZE(PARS0(7), stdMC_s, PARSMAX(7),PARSMIN(7),1)
kappa_lambda = kappa_pi
kappa_rho = kappa_w
CALL COMPUTEGE(PPY,PPR,2)
CALL CALCSTATS( )
CALL WRITESTEADY("VC0")
RETURN
END SUBROUTINE CALIBRATE
! COMPUTE VECTOR OF CALIBRATION RESIDUALS
FUNCTION ERRORS(PARS) RESULT(MOMS0)
USE toolkit , ONLY : NORMALIZE
USE parameters , ONLY : nump,numw
IMPLICIT NONE
REAL(rp) :: PARS(:),RR,PP1(3),PR1(3),vect(11)
REAL(rp) , ALLOCATABLE :: MOMS0(:)
INTEGER :: j
! Define parameter values (revert normalization with NORMALIZE)
CALL NORMALIZE(PARS(1), lbar, PARSMAX(1),PARSMIN(1),1)
CALL NORMALIZE(PARS(2), rhobar, PARSMAX(2),PARSMIN(2),1)
CALL NORMALIZE(PARS(3), kappa_w, PARSMAX(3),PARSMIN(3),1)
CALL NORMALIZE(PARS(4), rho_z, PARSMAX(4),PARSMIN(4),1)
CALL NORMALIZE(PARS(5), stdMC_z, PARSMAX(5),PARSMIN(5),1)
CALL NORMALIZE(PARS(6), rho_s, PARSMAX(6),PARSMIN(6),1)
CALL NORMALIZE(PARS(7), stdMC_s, PARSMAX(7),PARSMIN(7),1)
kappa_lambda = kappa_pi
kappa_rho = kappa_w
! Solve for the steady-state
CALL SET_MATS( )
CALL COMPUTEGE(PR1,PP1,0)
CALL CALCSTATS( )
! Define model residuals
ALLOCATE(MOMS0(2*nump+2*numw))
MOMS0(:) = DMOMS(:)
! Sum of squared errors
RR = SUM(DMOMS(:)*DMOMS(:))
! Read last iteration's sum of squared errors
OPEN(unit=1,file="calibparams.txt",action='read')
DO j=1,11
READ(1,*) vect(j)
END DO
CLOSE(1)
! If RR is lower than the lowest found so far, write parameters
IF (RR.lt.vect(11)) THEN
OPEN(unit=1,file="calibparams.txt",action='write')
WRITE(1,'(F20.15)') lbar
WRITE(1,'(F20.15)') rhobar
WRITE(1,'(F20.15)') kappa_pi
WRITE(1,'(F20.15)') kappa_lambda
WRITE(1,'(F20.15)') kappa_w
WRITE(1,'(F20.15)') kappa_rho
WRITE(1,'(F20.15)') rho_z
WRITE(1,'(F20.15)') stdMC_z
WRITE(1,'(F20.15)') rho_s
WRITE(1,'(F20.15)') stdMC_s
WRITE(1,'(F20.15)') RR
CLOSE (1)
CALL CALCSTATS( )
CALL WRITESTEADY("VC0")
END IF
RETURN
END FUNCTION ERRORS
! COMPUTE SUM OF SQUARED RESIDUALS FROM CALIBRATION
FUNCTION SUMERRORS(PARS) RESULT(RR)
IMPLICIT NONE
REAL(rp) :: PARS(:),RR,MOMENTS(144)
MOMENTS = ERRORS(PARS)
RR = SUM(MOMENTS(:)*MOMENTS(:))
RETURN
END FUNCTION SUMERRORS
! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
! DYNAMICS
! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
! SUBROUTINE TO COMPUTE THE JACOBIAN OF THE DYNAMIC SUSTEM
SUBROUTINE SOLVEDYN(name)
USE parameters , ONLY : Pdist,Wdist,V,L,nu,gamma,delta,mu,mu0,nump,nums,numz,numw
USE dynamics , ONLY : DYNSYS
IMPLICIT NONE
CHARACTER(LEN=3) , INTENT(IN) :: name
INTEGER :: i,j,ip,is,j2
INTEGER , PARAMETER :: NUPV = nump*nums
INTEGER , PARAMETER :: NUWL = numw*numz
INTEGER , PARAMETER :: NUWZ = 5
INTEGER , PARAMETER :: NUMB = 3
INTEGER , PARAMETER :: NUMA = 2*NUPV + 2*NUWL + NUWZ
INTEGER , PARAMETER :: NUMV = NUMA + NUMB
INTEGER , PARAMETER :: NUMR = NUMA
REAL(rp) :: XVEC(NUMV*2),XVEC0(NUMV*2),PRICESD(3),ZEROSD(3),jacstep
REAL(rp) :: RESIDJ(NUMR),RESID1(NUMR),RESID0(NUMR)
REAL(rp) :: JACP(NUMR,NUMV),JACN(NUMR,NUMV),mbar
jacstep = tol
! ----------------------------------------------------------------------------
! SOLVING STEADY STATE
WRITE(*,'(A)',ADVANCE="YES") ' Solving the steady state equilibrium... '
WRITE(*,'(A)',ADVANCE="YES") ' '
CALL SOLVESTEADY(name,2)
! ----------------------------------------------------------------------------
! CONSTRUCT VARIABLES OF DYNAMIC SYSTEM
! [ Pdist , Wdist , mlag , w , Inf , V , L , C , N , Pdist', Wdist', mlag', w', Inf', V', L', C', N', Shocks', Shocks ]
mbar = nu*(mu+mu0)*(cbar**gamma)/((mu+mu0)-one+delta)
j = 0
DO ip=1,nump ; DO is=1,nums ; j = j + 1
XVEC0(j) = Pdist(ip,is) ; XVEC0(NUMA+j) = XVEC0(j)
END DO ; END DO
DO iw=1,numw ; DO iz=1,numz ; j = j + 1
XVEC0(j) = Wdist(iw,iz) ; XVEC0(NUMA+j) = XVEC0(j)
END DO ; END DO
j = j + 1 ; XVEC0(j) = mbar ; XVEC0(NUMA+j) = XVEC0(j)
j = j + 1 ; XVEC0(j) = wbar ; XVEC0(NUMA+j) = XVEC0(j)
j = j + 1 ; XVEC0(j) = (mu+mu0) ; XVEC0(NUMA+j) = XVEC0(j)
DO ip=1,nump ; DO is=1,nums ; j = j + 1
XVEC0(j) = V(ip,is) ; XVEC0(NUMA+j) = XVEC0(j)
END DO ; END DO
DO iw=1,numw ; DO iz=1,numz ; j = j + 1
XVEC0(j) = L(iw,iz) ; XVEC0(NUMA+j) = XVEC0(j)
END DO ; END DO
j = j + 1 ; XVEC0(j) = cbar ; XVEC0(NUMA+j) = XVEC0(j)
j = j + 1 ; XVEC0(j) = nbar ; XVEC0(NUMA+j) = XVEC0(j)
DO j=NUMA*2+1,NUMA*2+NUMB*2
XVEC0(j) = zero
END DO
! ----------------------------------------------------------------------------
! CHECKING RESIDUALS FROM STEADY STATE (SHOULD BE CLOSE TO 0)
WRITE(*,'(A)',ADVANCE="YES") ' '
WRITE(*,'(A)',ADVANCE="NO") ' Checking residuals in steady state... ' ; RESID0 = DYNSYS(XVEC0)
PRINT * , MAXVAL(ABS(RESID0(:)))
WRITE(*,'(A)',ADVANCE="YES") ' '
WRITE(*,'(A)',ADVANCE="NO") ' Residuals in steady state: Pdist '
PRINT * , MAXVAL(ABS(RESID0(1:NUPV)))
WRITE(*,'(A)',ADVANCE="NO") ' Residuals in steady state: Wdist '
PRINT * , MAXVAL(ABS(RESID0(NUPV+1:NUPV+NUWL)))
WRITE(*,'(A)',ADVANCE="NO") ' Residuals in steady state: V '
PRINT * , MAXVAL(ABS(RESID0(NUPV+NUWL+2:NUPV+NUWL+NUPV+1)))
WRITE(*,'(A)',ADVANCE="NO") ' Residuals in steady state: L '
PRINT * , MAXVAL(ABS(RESID0(NUPV+NUWL+NUPV+2:NUPV+NUWL+NUPV+NUWL+1)))
WRITE(*,'(A)',ADVANCE="NO") ' Residuals in steady state: money '
PRINT * , ABS(RESID0(NUPV+NUWL+1))
WRITE(*,'(A)',ADVANCE="NO") ' Residuals in steady state: euler '
PRINT * , ABS(RESID0(NUPV+NUWL+NUPV+NUWL+2))
WRITE(*,'(A)',ADVANCE="NO") ' Residuals in steady state: labor '
PRINT * , ABS(RESID0(NUPV+NUWL+NUPV+NUWL+3))
WRITE(*,'(A)',ADVANCE="NO") ' Residuals in steady state: wage '
PRINT * , ABS(RESID0(NUPV+NUWL+NUPV+NUWL+4))
WRITE(*,'(A)',ADVANCE="NO") ' Residuals in steady state: price '
PRINT * , ABS(RESID0(NUPV+NUWL+NUPV+NUWL+5))
WRITE(*,'(A)',ADVANCE="YES") ' '
! ----------------------------------------------------------------------------
! COMPUTING JACOBIAN
WRITE(*,'(A)',ADVANCE="YES") ' Computing the jacobian... '
WRITE(*,'(A)',ADVANCE="YES") ' '
WRITE(*,'(A)',ADVANCE="NO" ) ' Shocking today´s variables... '
OPEN(unit=1,file="_dyn/"//TRIM(ADJUSTL(name))//"_dyn.txt",action='write')
DO j=1,NUMV*2
XVEC = XVEC0
XVEC(j) = XVEC0(j) + jacstep*MAX(one,ABS(XVEC(j)))
RESID1 = DYNSYS(XVEC)
RESIDJ = (RESID1 - RESID0)/(XVEC(j)-XVEC0(j))
DO j2=1,NUMR
WRITE(1,*) RESIDJ(j2)
END DO
IF (j.eq.NUMV) THEN
WRITE(*,'(A)',ADVANCE="YES") ' Done '
WRITE(*,'(A)',ADVANCE="NO") ' Shocking tomorrow´s variables... '
END IF
END DO
WRITE(*,'(A)',ADVANCE="YES") ' Done '
CLOSE(1)
RETURN
END SUBROUTINE SOLVEDYN
! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
END PROGRAM main