/
draw_parameters.jl
127 lines (98 loc) · 3.52 KB
/
draw_parameters.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
"""
draw_parameters()
Description.
"""
function draw_parameters(Y::Matrix{Float64}, X::Matrix{Float64}, σ2::Float64)
β = draw_coefficients(Y,X,σ2)
σ2 = draw_error_variance(Y,X,β)
return β::Vector{Float64}, σ2::Float64
end
function draw_parameters(Y::Vector{Float64}, X::Matrix{Float64}, σ2::Float64)
β = draw_coefficients(Y, X, σ2)
σ2 = draw_error_variance(Y, X, β)
return β::Vector{Float64}, σ2::Float64
end
function draw_parameters(Y::Matrix{Float64}, X::Vector{Float64}, σ2::Float64)
β = draw_coefficients(Y, X, σ2)
σ2 = draw_error_variance(Y, X, β)
return β::Float64, σ2::Float64
end
function draw_parameters(Y::Vector{Float64}, X::Vector{Float64}, σ2::Float64)
β = draw_coefficients(Y, X, σ2)
σ2 = draw_error_variance(Y, X, β)
return β::Float64, σ2::Float64
end
"""
draw_parameters()
Description.
"""
function draw_parameters(Y::Matrix{Float64}, X::Matrix{Float64}, ϕ::Vector{Float64}, σ2::Float64)
## Generate X*
X_star = similar(X[(1+length(ϕ)):end, :])
for i = 1:size(X)[2] # iterate over variables in X
x_temp = X[:, i]
for p = 1:length(ϕ) # iterate over lag params in ϕ
x_temp[(1+length(ϕ)):end, :] = x_temp[(1+length(ϕ)):end, :] - ϕ[p] .* ShiftedArrays.lag(x_temp, p)[(1+length(ϕ)):end, :]
end
x_temp = x_temp[(1+length(ϕ)):end, :]
X_star[:, i] = x_temp
end
## Generate Y*
Y_star = similar(Y[(1+length(ϕ)):end, :])
y_temp = Y
for p = 1:length(ϕ)
y_temp = Y
y_temp = y_temp[(1+length(ϕ)):end, :] - ϕ[p] .* ShiftedArrays.lag(y_temp, p)[(1+length(ϕ)):end, :]
end
Y_star = y_temp
## Draw β
β = draw_coefficients(Y_star, X_star, σ2)
## Generate error
error = Y - X * β
errors = zeros(length(error), length(ϕ))
for p = 1:length(ϕ)
errors[:, p] = ShiftedArrays.lag(error, p, default = 0.0)
end
errors = errors[(1+length(ϕ)):end,:]
error = error[(1+length(ϕ)):end]
## Draw ϕ
ϕ = draw_coefficients(error, errors, σ2)
## Draw σ2
σ2 = draw_error_variance(Y_star, X_star, β)
return β::Vector{Float64}, ϕ::Vector{Float64}, σ2::Float64
end
function draw_parameters(Y::Vector{Float64}, X::Matrix{Float64}, ϕ::Vector{Float64}, σ2::Float64)
## Generate X*
X_star = similar(X[(1+length(ϕ)):end, :])
for i = 1:size(X)[2] # iterate over variables in X
x_temp = X[:, i]
for p = 1:length(ϕ) # iterate over lag params in ϕ
x_temp[(1+length(ϕ)):end, :] = x_temp[(1+length(ϕ)):end, :] - ϕ[p] .* ShiftedArrays.lag(x_temp, p)[(1+length(ϕ)):end, :]
end
x_temp = x_temp[(1+length(ϕ)):end, :]
X_star[:, i] = x_temp
end
## Generate Y*
Y_star = similar(Y[(1+length(ϕ)):end, :])
y_temp = Y
for p = 1:length(ϕ)
y_temp = Y
y_temp = y_temp[(1+length(ϕ)):end, :] - ϕ[p] .* ShiftedArrays.lag(y_temp, p)[(1+length(ϕ)):end, :]
end
Y_star = y_temp
## Draw β
β = draw_coefficients(Y_star, X_star, σ2)
## Generate error
error = Y - X * β
errors = zeros(length(error), length(ϕ))
for p = 1:length(ϕ)
errors[:, p] = ShiftedArrays.lag(error, p, default=0.0)
end
errors = errors[(1+length(ϕ)):end, :]
error = error[(1+length(ϕ)):end]
## Draw ϕ
ϕ = draw_coefficients(error, errors, σ2)
## Draw σ2
σ2 = draw_error_variance(Y_star, X_star, β)
return β::Vector{Float64}, ϕ::Vector{Float64}, σ2::Float64
end