-
Notifications
You must be signed in to change notification settings - Fork 131
/
RBC_F90.f90
145 lines (103 loc) · 5.4 KB
/
RBC_F90.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
!============================================================================
! Name : RBC_F90.f90
! Description : Basic RBC model with full depreciation
! Date : July 21, 2013
!============================================================================
program RBC_F90
!----------------------------------------------------------------
! 0. variables to be defined
!----------------------------------------------------------------
implicit none
integer, parameter :: nGridCapital = 17820
integer, parameter :: nGridProductivity = 5
real(8), parameter :: tolerance = 0.0000001
real :: time_begin, time_end
integer :: nCapital, nCapitalNextPeriod, gridCapitalNextPeriod, nProductivity, nProductivityNextPeriod
integer :: iteration
real(8) :: aalpha, bbeta, capitalSteadyState, outputSteadyState, consumptionSteadyState
real(8) :: valueHighSoFar, valueProvisional, consumption, capitalChoice
real(8) :: maxDifference,diff,diffHighSoFar
real(8), dimension(nGridProductivity) :: vProductivity
real(8), dimension(nGridProductivity,nGridProductivity) :: mTransition
real(8), dimension(nGridCapital) :: vGridCapital
real(8), dimension(nGridCapital,nGridProductivity) :: mOutput, mValueFunction, mValueFunctionNew, mPolicyFunction
real(8), dimension(nGridCapital,nGridProductivity) :: expectedValueFunction
call CPU_TIME(time_begin)
!----------------------------------------------------------------
! 1. Calibration
!----------------------------------------------------------------
aalpha = 0.33333333333; ! Elasticity of output w.r.t
bbeta = 0.95 ! Discount factor
! Productivity value
vProductivity = (/0.9792, 0.9896, 1.0000, 1.0106, 1.0212/)
! Transition matrix
mTransition = reshape( (/0.9727, 0.0273, 0., 0., 0., &
0.0041, 0.9806, 0.0153, 0.0, 0.0, &
0.0, 0.0082, 0.9837, 0.0082, 0.0, &
0.0, 0.0, 0.0153, 0.9806, 0.0041, &
0.0, 0.0, 0.0, 0.0273, 0.9727 /), (/5,5/))
mTransition = transpose(mTransition)
!----------------------------------------------------------------
! 2. Steady state
!----------------------------------------------------------------
capitalSteadyState = (aalpha*bbeta)**(1.0/(1.0-aalpha))
outputSteadyState = capitalSteadyState**(aalpha)
consumptionSteadyState = outputSteadyState-capitalSteadyState
print *, 'Steady State values'
print *, 'Output: ', outputSteadyState, 'Capital: ', capitalSteadyState, 'Consumption: ', consumptionSteadyState
! Grid for capital
do nCapital = 1, nGridCapital
vGridCapital(nCapital) = 0.5*capitalSteadyState+0.00001*(nCapital-1)
end do
!----------------------------------------------------------------
! 3. Pre-build Output for each point in the grid
!----------------------------------------------------------------
do nProductivity = 1, nGridProductivity
do nCapital = 1, nGridCapital
mOutput(nCapital, nProductivity) = vProductivity(nProductivity)*(vGridCapital(nCapital)**aalpha)
end do
end do
!----------------------------------------------------------------
! 4. Main Iteration
!----------------------------------------------------------------
maxDifference = 10.0
iteration = 0
do while (maxDifference>tolerance)
expectedValueFunction = matmul(mValueFunction,transpose(mTransition));
do nProductivity = 1,nGridProductivity
! We start from previous choice (monotonicity of policy function)
gridCapitalNextPeriod = 1
do nCapital = 1,nGridCapital
valueHighSoFar = -100000.0
do nCapitalNextPeriod = gridCapitalNextPeriod,nGridCapital
consumption = mOutput(nCapital,nProductivity)-vGridCapital(nCapitalNextPeriod)
valueProvisional = (1.0-bbeta)*log(consumption)+bbeta*expectedValueFunction(nCapitalNextPeriod,nProductivity)
if (valueProvisional>valueHighSoFar) then ! we break when we have achieved the max
valueHighSoFar = valueProvisional
capitalChoice = vGridCapital(nCapitalNextPeriod)
gridCapitalNextPeriod = nCapitalNextPeriod
else
exit
end if
end do
mValueFunctionNew(nCapital,nProductivity) = valueHighSoFar
mPolicyFunction(nCapital,nProductivity) = capitalChoice
end do
end do
maxDifference = maxval((abs(mValueFunctionNew-mValueFunction)))
mValueFunction = mValueFunctionNew
iteration = iteration+1
if (mod(iteration,10)==0 .OR. iteration==1) then
print *, 'Iteration:', iteration, 'Sup Diff:', MaxDifference
end if
end do
!----------------------------------------------------------------
! 5. PRINT RESULTS
!----------------------------------------------------------------
print *, 'Iteration:', iteration, 'Sup Diff:', MaxDifference
print *, ' '
print *, 'My check:', mPolicyFunction(1000,3)
print *, ' '
call CPU_TIME (time_end)
print *, 'Elapsed time is ', time_end - time_begin
end program RBC_F90