forked from Schemelinina/9619
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPS05 Q1 Rouwenhorst.jl
225 lines (188 loc) · 6.71 KB
/
PS05 Q1 Rouwenhorst.jl
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
#PS05 Q1 Rouwenhorst; Code was adopted from Sergio and internet
using Parameters
using Random
using Distributions
using LinearAlgebra
using Statistics
using StatsBase
# Set random seed
Random.seed!(3486);
#-----------------------------------------------------------
# Define a markov process struct
# Generate structure for markov processes
@with_kw struct MP
# Model Parameters
N::Int64 # Number of states
grid # Grid of discrete markov process
Π # Transition matrix
PDF # Stationary distribution
CDF # Stationary distribution
end
#Q1a Simulate MC
# Define parameters
n = 10000 # Number of draws
ρ = 0.9
σ = 0.1
# Set initial value
z = zeros(n)
z[1] = 0.0
# Define distribution for error
η = Normal(0.0, σ)
# Generate AR1 using draws
for i in 2:n
z[i] = ρ * z[i-1] + rand(η)
end
#Q1b;
#This function uses the Rouwenhorst method
#N=5
function Rouwenhorst95_5(ρ,σ,N=5)
# Define parameters for Rouwenhorst's approximation
p = (1+ρ)/2
q = p
ψ = σ*sqrt((N-1)/(1-ρ^2)) # the size of the shock that corresponds to one standard deviation of the shock distribution
s = (1-q)/(2-(p+q))
# Fill in transition matrix
if N==2
Π_z = [p 1-p ; 1-q q]
else
MP_aux = Rouwenhorst95_5(ρ,σ,N-1)
o = zeros(N-1)
Π_z = p*[MP_aux.Π o ; o' 0] + (1-p)*[o MP_aux.Π ; 0 o'] + (1-q)*[o' 0 ; MP_aux.Π o] + q*[0 o' ; o MP_aux.Π]
# Adjust scale for double counting
Π_z = Π_z./repeat(sum(Π_z,dims=2),1,N)
end
# Distribution
PDF_z = pdf.(Binomial(N-1,1-s),(0:N-1))
CDF_z = cumsum(PDF_z)
# Create z grid
z = range(-ψ,ψ,length=N)
# Return
return MP(N=N,grid=z,Π=Π_z,PDF=PDF_z,CDF=CDF_z)
end
# Simulate the Markov chain
#simulates a Markov chain with Ns periods using the transition matrix Π from the MP structure. It returns a vector z_MC of simulated values of the Markov chain.
function simulation_5(Ns,MP::MP)
# Compute conditional CDF
Γ = cumsum(MP.Π,dims=2)
# Allocate simulated vector
z_ind = zeros(Int64,Ns)
z_MC = zeros(Ns)
# Starting value for simulation
z_ind[1] = Int(ceil(length(MP.grid)/2))
z_MC[1] = MP.grid[z_ind[1]]
# Simulate
for i=2:Ns # number of simulated periods
z_ind[i] = rand(Categorical(MP.Π[z_ind[i-1],:]))
z_MC[i] = MP.grid[z_ind[i]]
end
# Return result
return z_MC
end
#Q1c
# Moments function for sample
function moments_5(z_MC)
mean_MC = mean(z_MC)
std_MC = std(z_MC)
skewness_MC = skewness(z_MC)
kurtosis_MC = kurtosis(z_MC)
auto_corr_MC = cor(z_MC[1:end-1],z_MC[2:end])
auto_corr_MC4 = zeros(4)
for i in 2:5
auto_corr_MC4[i-1] = cor(z_MC[1:end-i],z_MC[1+i:end])[1]
end
return mean_MC, std_MC, skewness_MC, kurtosis_MC,auto_corr_MC4
end
#Report
Ns=10000
MP_T5 = Rouwenhorst95_5(ρ,σ,5)
z_MC = simulation_5(Ns, MP_T5)
mean_MC, std_MC, skewness_MC, kurtosis_MC,auto_corr_MC4 = moments_5(z_MC)
# Print the results
using DataFrames
function table_5(mean_MC, std_MC, skewness_MC, kurtosis_MC, auto_corr_MC4)
df = DataFrame(Statistics=["Mean", "St.dev.", "Skewness", "Kurtosis", "Autocorr1", "Autocorr2", "Autocorr3", "Autocorr4"],
Values=[mean_MC, std_MC, skewness_MC, kurtosis_MC, auto_corr_MC4[1], auto_corr_MC4[2], auto_corr_MC4[3], auto_corr_MC4[4]])
return df
end
df_Results_5 = table_5(mean_MC, std_MC, skewness_MC, kurtosis_MC, auto_corr_MC4)
println(df_Results_5)
using Plots
gr()
# Histograms ## There is error
#histogram(z_MC)
#The same as above but for N=15
function Rouwenhorst95_15(ρ,σ,N=15)
# Define parameters for Rouwenhorst's approximation
p = (1+ρ)/2
q = p
ψ = σ*sqrt((N-1)/(1-ρ^2)) # the size of the shock that corresponds to one standard deviation of the shock distribution
s = (1-q)/(2-(p+q))
# Fill in transition matrix
if N==2
Π_z = [p 1-p ; 1-q q]
else
MP_aux = Rouwenhorst95_15(ρ,σ,N-1)
o = zeros(N-1)
Π_z = p*[MP_aux.Π o ; o' 0] + (1-p)*[o MP_aux.Π ; 0 o'] + (1-q)*[o' 0 ; MP_aux.Π o] + q*[0 o' ; o MP_aux.Π]
# Adjust scale for double counting
Π_z = Π_z./repeat(sum(Π_z,dims=2),1,N)
end
# Distribution
PDF_z = pdf.(Binomial(N-1,1-s),(0:N-1))
CDF_z = cumsum(PDF_z)
# Create z grid
z = range(-ψ,ψ,length=N)
# Return
return MP(N=N,grid=z,Π=Π_z,PDF=PDF_z,CDF=CDF_z)
end
# Simulate the Markov chain
#simulates a Markov chain with Ns periods using the transition matrix Π from the MP structure. It returns a vector z_MC of simulated values of the Markov chain.
function simulation_15(Ns,MP::MP)
# Compute conditional CDF
Γ = cumsum(MP.Π,dims=2)
# Allocate simulated vector
z_ind = zeros(Int64,Ns)
z_MC15 = zeros(Ns)
# Starting value for simulation
z_ind[1] = Int(ceil(length(MP.grid)/2))
z_MC[1] = MP.grid[z_ind[1]]
# Simulate
for i=2:Ns # number of simulated periods
z_ind[i] = rand(Categorical(MP.Π[z_ind[i-1],:]))
z_MC15[i] = MP.grid[z_ind[i]]
end
# Return result
return z_MC15
end
#Q1c
# Moments function for sample
function moments_15(z_MC)
mean_MC = mean(z_MC)
std_MC = std(z_MC)
skewness_MC = skewness(z_MC)
kurtosis_MC = kurtosis(z_MC)
auto_corr_MC = cor(z_MC[1:end-1],z_MC[2:end])
auto_corr_MC4 = zeros(4)
for i in 2:5
auto_corr_MC4[i-1] = cor(z_MC[1:end-i],z_MC[1+i:end])[1]
end
return mean_MC, std_MC, skewness_MC, kurtosis_MC,auto_corr_MC4
end
#Report
Ns=10000
MP_T15 = Rouwenhorst95_15(ρ,σ,15)
z_MC = simulation_15(Ns, MP_T5)
mean_MC, std_MC, skewness_MC, kurtosis_MC,auto_corr_MC4 = moments_15(z_MC)
# Print the results
using DataFrames
function table_15(mean_MC, std_MC, skewness_MC, kurtosis_MC, auto_corr_MC4)
df = DataFrame(Statistics=["Mean", "St.dev.", "Skewness", "Kurtosis", "Autocorr1", "Autocorr2", "Autocorr3", "Autocorr4"],
Values=[mean_MC, std_MC, skewness_MC, kurtosis_MC, auto_corr_MC4[1], auto_corr_MC4[2], auto_corr_MC4[3], auto_corr_MC4[4]])
return df
end
df_Results_15 = table_15(mean_MC, std_MC, skewness_MC, kurtosis_MC, auto_corr_MC4)
println(df_Results_15)
#using Plots
#gr()
# Histograms ## There is error
#histogram(z_MC)