-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgsreg_HMC_example_09242024.jl
323 lines (234 loc) · 7.51 KB
/
gsreg_HMC_example_09242024.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
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
# gsres function example
cd("C:\\Users\\millsjf\\OneDrive - University of Cincinnati\\Class\\9011 2024")
## data here:
using Distributions, Plots, StatsPlots, StatsBase, CSV, DataFrames
using CSV, DataFrames, Plots, StatsPlots, Distributions, LinearAlgebra
using Turing, StatsFuns, Statistics #, PrettyTables
n = 100
x = randn(n)
y = 1.0 .+ 0.5.*x .+ 0.4.*randn(n)
plot(x,y,st=:scatter)
## AR(1) DGP
n = 100
ϕ = 1.0
α = 0.01
z = zeros(n+20)
for t = 2:(n+20)
z[t] = α + ϕ*z[t-1] + randn(1)[1]
end
y = z[21:(n+20)]
plot(y)
z = rand(Gamma(2,5), 1000)
plot(z)
pz95 = quantile(z,[0.025,0.975])
hline!(pz95, linecolor = "green", label=false)
hline!([mean(z)], label=false, linecolor="black")
mean(z)
std(z)
plot(z, st=:density)
pz95 = quantile(z,[0.025,0.975])
vline!(pz95, linecolor = "green", label=false)
z1 = cumsum(z)
plot(z1)
mean(z1[1:500])
mean(z1[501:end])
std(z1[1:500])
std(z1[501:end])
plot(z1, st=:density)
plot(z1, st=:histogram)
[z z1]
za = autocor(z)
plot(za, st=:bar, label="white noise")
z1a = autocor(z1)
plot(z1a, st=:bar, label="I(1)")
ya = autocor(y)
plot(ya, st=:bar, label="AR(1) with ϕ = $phi")
z2 = cumsum(z1)
plot(z2)
dz2 = diff(z2)
plot(dz2)
plot!(z1, linestyle = :dash)
d2z2 = diff(dz2)
plot(d2z2)
plot!(z, linestyle = :dash)
# generate a trend stationary variable
T = 200
t = 1:T
a = 0.5
b = 0.2
zz = a .+ b.*t + randn(T)
plot(zz)
# zz is nonstationary
azz = autocor(zz)
plot(azz, st=:bar)
# deterministic trend, different functional form:
zzl = exp.(a .+ b.*t + randn(T))
plot(zzl)
lzz1 = log.(zzl)
plot(lzz1)
# not working?!
pya = StatsBase.pacf(z1, 20)
plot(pya, st=:bar, label="AR(1) with ϕ = $phi", title = "PACF")
###############################################
include("gsreg.jl")
include("useful_functions_mcmc.jl")
## gsreg(y::Array{Float64},X::Array{Float64}; M=10000::Int64, burnin = Int(floor(M/10.0))::Int64,tau=[1.0]::Array{Float64},iB0=[0.0001]::Array{Float64},b0=[0.0]::Array{Float64},d0=0.0001::Float64, a0=3.0::Float64)
# uninformative prior
b = [0.0; 0.0] # prior coeff. means
iB = inv([0.0001 0.0; 0.0 0.0001])
X = [ones(n) x]
bdraws,s2draws = gsreg(y,X)
plot(bdraws[:,2], st=:density, fill=true, alpha=0.5, title = "β posterior", label= "uninformative" )
mean(bdraws[:,2])
std(bdraws[:,2])
quantile(bdraws[:,2],[0.025,0.975])
plot(bdraws[:,1], st=:density, fill=true, alpha=0.5, title = "α posterior", label= "uninformative" )
mean(bdraws[:,1])
std(bdraws[:,1])
quantile(bdraws[:,1],[0.025,0.975])
# Informative prior (still uninformative for variance parameter)
b = [0.0; 1.0] # prior coeff. means
iB = inv([1000.0 0.0; 0.0 0.001]) # prior cov matrix
bdraws,s2draws = gsreg(y,X, b0=b, iB0=iB)
plot!(bdraws[:,2],st=:density,linecolor=:red,label="informative")
mean(bdraws[:,2])
std(bdraws[:,2])
quantile(bdraws[:,2],[0.025,0.975])
plot(s2draws, st=:density, fill=true, alpha=0.5, title = "σ-squared posterior", label= false )
z = randn(1000,2).*0.3 .+ 0.5
plot(z[:,1], z[:,2], st = :scatter) #, xlims = (-0.2,1.2), ylims = (-0.2,1.2), label=false)
#vline!([0.0 1.0], label=false)
#hline!([0.0 1.0], label=false)
# correlated variables
z3 = 0.5*z[:,1] .+ randn(1000).*0.2
cor(z[:,1], z3)
plot(z[:,1], z3, st = :scatter, xlabel = "z1", ylabel="z3")
# AR(1) model estimation
# uninformative prior
b = [0.0; 0.0] # prior coeff. means
iB = inv([0.0001 0.0; 0.0 0.0001])
yt = y[2:n]
yt1 = y[1:(n-1)]
X = [ones(n-1) yt1]
bdraws,s2draws = gsreg(yt,X, M = 30000)
plot(bdraws[:,2], st=:density, fill=true, alpha=0.5, title = "phi posterior", label= "uninformative" )
mean(bdraws[:,2])
std(bdraws[:,2])
quantile(bdraws[:,2],[0.025,0.975])
coefnames = ["α" "ϕ"]
summary_mcmc_array(bdraws,coefnames)
# try using HMC in Turing.jl to do this
using Turing
@model simple_regression(y,X, ::Type{TV}=Vector{Float64}) where {TV} =begin
n, D = size(X)
#alpha ~ Normal(0,1)
sig ~ Uniform(0.01,10)
#m ~ Truncated(Normal(-2,3),-999.9,0.999)
beta = TV(undef,(D))
# sd<10 too restrictive for beta coeffs
for k in 1:(D)
beta[k] ~ Normal(0, 20.0)
end
#delta ~ Normal(0,3.0)
# mu = logistic.(Matrix(X) * beta)
for i in 1:n
y[i] ~ Normal(X[i,:]'*beta, sig)
end
end
@model informative_prior_model(y,X, ::Type{TV}=Vector{Float64}) where {TV} =begin
n, D = size(X)
#alpha ~ Normal(0,1)
sig ~ Uniform(0.01,10)
beta = TV(undef,(D))
# sd<10 too restrictive for beta coeffs
#for k in 1:(D)
beta[1] ~ Normal(0, 20.0)
beta[2] ~ Truncated(Normal(0.0,0.001),0.0,1.1)
#end
#delta ~ Normal(0,3.0)
# mu = logistic.(Matrix(X) * beta)
for i in 1:n
y[i] ~ Normal(X[i,:]'*beta, sig)
end
end
model = simple_regression(yt, X)
model = informative_prior_model(yt, X)
Turing.setprogress!(true)
iter = 6000
@time cc = sample(model, NUTS(0.65),iter)
cc
plot(cc)
# one parameter (on MCMC chain)
sigma = get(cc,:sig)
sigma_est = Array(sigma.sig)
mean(sigma_est)
std(sigma_est)
plot(sigma_est, st=:density)
# several (k) parameters (MCMC chains) in a vector
k = 2
b = get(cc,:beta)
bs = zeros(iter,k)
for i in 1:k
bs[:,i] = Array(b.beta[i])
end
summary_mcmc_array(bs,coefnames)
# bs2 = bs[:,j]
j = 2
plot(bs[:,j], st=:density, fill=true, alpha=0.5, label="HMC posterior") #, xlims=(-0,1.2), grid=false)
plot!(bdraws[:,j], st=:density, fill=true, alpha=0.5, label="Gibbs posterior")
prior_HMC = rand(Normal(0, 20.0),100000)
plot!(prior_HMC, st=:density, fill=true, alpha=0.5, label="HMC prior")
prior_Gibbs = rand(Normal(0, sqrt(1000.0)),100000)
plot!(prior_Gibbs, st=:density, fill=true, alpha=0.5, color=:red, label="Gibbs prior")
#savefig("posteriors_priors_qu6")
# informative prior for phi?
ϕ_draws = rand(Truncated(Normal(0.0,0.001),0.0,1.1), 100000)
plot!(ϕ_draws, st=:density, fill=true, alpha=0.5, color=:green, label="Informative prior?")
plot!(bs[:,j], st=:density, fill=true, alpha=0.5, color=:blue, label="HMC posterior", xlims=(-0,1.2), grid=false)
# BAD informative prior
ϕ_draws = rand(Truncated(Normal(0.0,0.1),0.0,1.1), 100000)
plot(ϕ_draws, st=:density, fill=true, alpha=0.5, color=:green, label="Informative prior?")
##### Estimated y_0 in an AR(1) model:
@model ar1_with_y0(y, ::Type{TV}=Vector{Float64}) where {TV} =begin
n = length(y)
D = 2
# priors
y0 ~ Normal(y[1],std(y[1:5])*1.5) # reasonable prior for y0?
sig ~ Uniform(0.01,10)
beta = TV(undef,(D))
# for k in 1:(D)
# beta[k] ~ Normal(0, 20.0)
# end
beta[1] ~ Normal(0, 20.0)
beta[2] ~ Truncated(Normal(0, 10), -0.9999, 0.9999)
# likelihood
for i in 1:n
if i == 1
y[i] ~ Normal((beta[1] + beta[2]*y0), sig)
else
y[i] ~ Normal(beta[1] + beta[2]*y[i-1], sig)
end
end
end
model = ar1_with_y0(y)
Turing.setprogress!(true)
@time cc = sample(model, NUTS(0.65),3000)
cc
plot(cc)
### need to get draws from cc to do:
coefnames = ["y0" "alpha" "phi" "sig"]
summary_mcmc_array(bs,coefnames)
#################################
th = get(cc,:θ)
th_est = Array(th.θ)[burn+1:end]
mean(th_est)
# theta HMC draws
plot(th_est)
# theta posterior
plot(th_est, st=:density, label = "theta posterior")
vline!([theta], label="true theta")
ts = zeros(s-burn,n_ind)
for i in 1:size(ts,2)
ts[:,i] = Array(t.t[i])[burn+1:end]
end
summary_mcmc_array(bs,coefnames)