-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIS_PS05 Q1 Tauchen.jl
233 lines (195 loc) · 7.43 KB
/
IS_PS05 Q1 Tauchen.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
226
227
228
229
230
231
232
233
#PS05 Q1 Tauchen 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; std_dev = sqrt(σ^2 / (1 - ρ^2))
#This function uses the Tauchen method to discretize an AR(1) process with parameters ρ and σ into a Markov chain with N states.
#N=5
function tauchen86_5(ρ,σ,N,Ω::Any=3)
# Create z grid
z = range(-Ω*σ/sqrt(1-ρ^2),Ω*σ/sqrt(1-ρ^2),length=N)
N=5
# Define intermediate step length
h = (z[2]-z[1])/2
# Define auxiliary matrices
z_0 = repeat(z ,1,N) # Matrix of today's z each row is a value, columns are equal
z_1 = repeat(z',N,1) # Matrix of tomorrow's z each column is a value, rows are equal
# Define intervals
z_lim = zeros(N,N,2) # First matrix is lower bounds. Second matrix is uppor bounds.
z_lim[:,1 ,1] .= -Inf
z_lim[:,2:end ,1] .= ( z_1[:,2:end ] - ρ*z_0[:,2:end ] .- h )./σ
z_lim[:,1:end-1,2] .= ( z_1[:,1:end-1] - ρ*z_0[:,1:end-1] .+ h )./σ
z_lim[:,end ,2] .= Inf
# Define reference distribution
F(x) = cdf.(Normal(),x)
# Fill in transition matrix
Π_z = F.(z_lim[:,:,2]) - F.(z_lim[:,:,1])
Π_z = Π_z./repeat(sum(Π_z,dims=2),1,N)
# Get stationary distribution of markov chain
PDF_z = real(eigvecs(Π_z')[:,end]); PDF_z = PDF_z/sum(PDF_z) ;
CDF_z = cumsum(PDF_z)
# 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 = tauchen86_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 tauchen86_15(ρ,σ,N,Ω::Any=3)
# Create z grid
z = range(-Ω*σ/sqrt(1-ρ^2),Ω*σ/sqrt(1-ρ^2),length=N)
N=15
# Define intermediate step length
h = (z[2]-z[1])/2
# Define auxiliary matrices
z_0 = repeat(z ,1,N) # Matrix of today's z each row is a value, columns are equal
z_1 = repeat(z',N,1) # Matrix of tomorrow's z each column is a value, rows are equal
# Define intervals
z_lim = zeros(N,N,2) # First matrix is lower bounds. Second matrix is uppor bounds.
z_lim[:,1 ,1] .= -Inf
z_lim[:,2:end ,1] .= ( z_1[:,2:end ] - ρ*z_0[:,2:end ] .- h )./σ
z_lim[:,1:end-1,2] .= ( z_1[:,1:end-1] - ρ*z_0[:,1:end-1] .+ h )./σ
z_lim[:,end ,2] .= Inf
# Define reference distribution
F(x) = cdf.(Normal(),x)
# Fill in transition matrix
Π_z = F.(z_lim[:,:,2]) - F.(z_lim[:,:,1])
Π_z = Π_z./repeat(sum(Π_z,dims=2),1,N)
# Get stationary distribution of markov chain
PDF_z = real(eigvecs(Π_z')[:,end]); PDF_z = PDF_z/sum(PDF_z) ;
CDF_z = cumsum(PDF_z)
# Return
return MP(N=N,grid=z,Π=Π_z,PDF=PDF_z,CDF=CDF_z)
end
# Simulate the Markov chain
n = 10000
MP_T15 = tauchen86_15(ρ,σ,15)
# 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_T5 = tauchen86_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)