-
Notifications
You must be signed in to change notification settings - Fork 19
/
graphics.jl
208 lines (199 loc) · 6.08 KB
/
graphics.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
using Plots
function graph_display(g)
if get(ENV, "TERM_PROGRAM", "") == "vscode"
display(g)
end
end
function plot(
variables::Vector{Vector{Float64}};
bar_variable::Vector{Float64} = Vector{Float64}([]),
first_period::Int64 = 1,
plot_title::String = "Smoothed value",
plot_legend::Tuple = (),
plot_filename::String = "",
plot_legend_position::Symbol = :topright,
)
local myplot
# deal first with bar variable
if length(bar_variable) > 0
variables = pushfirst!(variables, bar_variable)
end
for (i, v) in enumerate(variables)
if length(plot_legend) > 0
thislabel = plot_legend[i]
else
thislabel = ""
plot_legend_position = false
end
if i == 1
if length(bar_variable) > 0
len = length(bar_variable)
xb = collect(range(first_period, length = len))
myplot = Plots.bar(
xb,
bar_variable,
label = thislabel,
legend = plot_legend_position,
title = plot_title,
)
twinx()
else
x = collect(range(first_period, length = length(v)))
myplot = Plots.plot(
x,
v,
label = thislabel,
legend = plot_legend_position,
title = plot_title,
linewidth = 3,
)
end
else
x = collect(range(first_period, length = length(v)))
myplot = Plots.plot!(x, v, label = thislabel, linewidth = 3)
end
end
graph_display(myplot)
if length(plot_filename) > 0
Plots.savefig(plot_filename)
end
end
function plot(
variable::Vector{Float64};
bar_variable::Vector{Float64} = Vector{Float64}([]),
first_period::Int64 = 1,
plot_title::String = "Smoothed value",
plot_legend::Tuple = (),
plot_filename::String = "",
plot_legend_position::Symbol = :topright,
)
local myplot
# deal first with bar variable
if length(plot_legend) > 0
thislabel = plot_legend
else
thislabel = ""
plot_legend_position = false
end
if length(bar_variable) > 0
len = length(bar_variable)
xb = collect(range(first_period, length = len))
myplot = Plots.bar(
xb,
bar_variable,
label = thislabel[1],
legend = plot_legend_position,
title = plot_title,
)
x = collect(range(first_period, length = length(variable)))
lims = Plots.ignorenan_extrema(myplot[1].attr[:yaxis])
m, M = extrema(variable)
tb = (lims[2] - lims[1]) / (M - m)
variable = tb * (variable .- m) .+ lims[1]
Plots.plot!(x, variable, label = thislabel[2], title = plot_title, linewidth = 3)
myplot = twinx()
myplot = Plots.plot!(myplot, ylims = (m, M))
else
x = collect(range(first_period, length = length(variable)))
myplot = Plots.plot(
x,
variable,
label = thislabel[1],
legend = plot_legend_position,
title = plot_title,
linewidth = 3,
)
end
graph_display(myplot)
if length(plot_filename) > 0
Plots.savefig(plot_filename)
end
end
function plot_irfs(irfs, model, symboltable, filepath)
x = 1:TimeDataFrames.nrow(first(irfs)[2])
endogenous_names = get_endogenous_longname(symboltable)
exogenous_names = get_exogenous_longname(symboltable)
for i = 1:model.exogenous_nbr
exogenous_name = exogenous_names[i]
(nbplt, nr, nc, lr, lc, nstar) = pltorg(model.original_endogenous_nbr)
firstvar = 1
for p in 1:nbplt - 1
filename = "$(filepath)_$(exonenous_name)_$(nbplt).png"
plot_irf_panel(x, irfs[Symbol(exogenous_name)], endogenous_names, exogenous_name,
firstvar, nr, nc, nr*nc, filename)
firstvar += nr*nc
end
filename = "$(filepath)_$(exogenous_name)_$(nbplt).png"
plot_irf_panel(x, irfs[Symbol(exogenous_name)], endogenous_names, exogenous_name, firstvar, lr, lc,
nstar, filename)
end
end
function plot_irf_panel(x, tdf, endogenous_names, exogenous_name, firstvar, nr, nc,
nstar, filename)
sp = [Plots.plot(showaxis=false, ticks=false, grid=false) for i in 1:nr*nc]
for i = 1:nstar
ivar = firstvar + i - 1
title = (i == 1) ? "Orthogonal shock to $(exogenous_name)" : ""
yy = tdf[!, Symbol(endogenous_names[i])]
if all(yy .> 0)
lims = (0, Inf)
elseif all(yy .< 0)
lims = (-Inf, 0)
else
lims = (-Inf, Inf)
end
sp[i] = Plots.plot(x, yy,
title = title,
ylims = lims,
label = endogenous_names[ivar])
end
pl = Plots.plot(sp..., layout = (nr, nc), size = (900, 900))
graph_display(pl)
savefig(filename)
end
function pltorg(number)
nrstar = 3
ncstar = 3
nstar = nrstar*ncstar
nbplt = 0
nr = 0
nc = 0
lr = 0
lc = 0
nbplt = ceil(number/nstar)
(d, r) = divrem(number, nstar)
if r == 0
return (d, nrstar, ncstar, nrstar, ncstar, nstar)
else
(lr, lc) = pltorg_0(r)
return (d + 1, nrstar, ncstar, lr, lc, r)
end
end
function pltorg_0(number)
@assert number < 10
if number == 1
lr = 1
lc = 1
elseif number == 2
lr = 2
lc = 1
elseif number == 3
lr = 3
lc = 1
elseif number == 4
lr = 2
lc = 2
elseif number < 7
lr = 3
lc = 2
elseif number < 10
lr = 3
lc = 3
end
return (lr, lc)
end
function plot(tdf::TimeDataFrame; label=(), title="", filename="")
pl = Plots.plot(tdf, label=label, title=title)
graph_display(pl)
savefig(filename)
end