@@ -820,23 +820,7 @@ function plot_irf(𝓂::ℳ;
820820 push! (variable_names, (new_name))
821821 end
822822
823- push! (pp,begin
824- StatsPlots. plot (Y[i,:,shock] .+ SS,
825- title = variable_name,
826- ylabel = " Level" ,
827- label = " " )
828-
829- if can_dual_axis
830- StatsPlots. plot! (StatsPlots. twinx (),
831- 100 * ((Y[i,:,shock] .+ SS) ./ SS .- 1 ),
832- ylabel = LaTeXStrings. L "\% \D elta" ,
833- label = " " )
834- end
835-
836- StatsPlots. hline! (can_dual_axis ? [SS 0 ] : [SS],
837- color = :black ,
838- label = " " )
839- end )
823+ push! (pp, plot_irf_subplot (Y[i,:,shock], SS, variable_name, can_dual_axis))
840824
841825 if ! (plot_count % plots_per_page == 0 )
842826 plot_count += 1
@@ -963,6 +947,7 @@ function plot_irf(𝓂::ℳ;
963947 :sylvester_algorithm => sylvester_algorithm,
964948 :lyapunov_algorithm => lyapunov_algorithm,
965949 :plot_data => Y,
950+ :reference_steady_state => reference_steady_state,
966951 :variable_names => variable_names,
967952 :shock_names => shock_names,
968953 :shock_idx => shock_idx,
@@ -978,6 +963,62 @@ function plot_irf(𝓂::ℳ;
978963end
979964
980965
966+ function plot_irf_subplot (irf_data:: AbstractVector{S} , steady_state:: S , variable_name:: String , can_dual_axis:: Bool ) where S <: AbstractFloat
967+ p = StatsPlots. plot (irf_data .+ steady_state,
968+ title = variable_name,
969+ ylabel = " Level" ,
970+ label = " " )
971+
972+ if can_dual_axis
973+ StatsPlots. plot! (StatsPlots. twinx (),
974+ 100 * ((irf_data .+ steady_state) ./ steady_state .- 1 ),
975+ ylabel = LaTeXStrings. L "\% \D elta" ,
976+ label = " " )
977+ end
978+
979+ StatsPlots. hline! (can_dual_axis ? [steady_state 0 ] : [steady_state],
980+ color = :black ,
981+ label = " " )
982+ return p
983+ end
984+
985+ function plot_irf_subplot (irf_data:: Vector{<:AbstractVector{S}} , steady_state:: Vector{S} , variable_name:: String , can_dual_axis:: Bool ) where S <: AbstractFloat
986+ can_dual_axis = can_dual_axis && (maximum (steady_state) - minimum (steady_state) < 1e-12 )
987+
988+ ylabel1 = can_dual_axis ? " Level" : " abs. " * LaTeXStrings. L "\D elta"
989+
990+ plot_dat = []
991+ plot_dat_dual = []
992+
993+ for i in 1 : length (irf_data)
994+ if can_dual_axis
995+ push! (plot_dat, irf_data[i] .+ steady_state[i])
996+ push! (plot_dat_dual, 100 * ((irf_data[i] .+ steady_state[i]) ./ steady_state[i] .- 1 ))
997+ else
998+ push! (plot_dat, irf_data[i])
999+ end
1000+ end
1001+
1002+
1003+ p = StatsPlots. plot (plot_dat,
1004+ title = variable_name,
1005+ ylabel = ylabel1,
1006+ label = " " )
1007+
1008+ if can_dual_axis
1009+ StatsPlots. plot! (StatsPlots. twinx (),
1010+ plot_dat_dual,
1011+ ylabel = LaTeXStrings. L "\% \D elta" ,
1012+ label = " " )
1013+ end
1014+
1015+ StatsPlots. hline! (can_dual_axis ? [steady_state[1 ] 0 ] : [0 ],
1016+ color = :black ,
1017+ label = " " )
1018+
1019+ return p
1020+ end
1021+
9811022
9821023function plot_irf! (𝓂:: ℳ;
9831024 periods:: Int = 40 ,
@@ -1261,6 +1302,10 @@ function plot_irf!(𝓂::ℳ;
12611302 shock_dir = " "
12621303 end
12631304
1305+ Ys = [[i[:plot_data ] for i in irf_active_plot_container]. .. , Y]
1306+
1307+ reference_steady_states = [[i[:reference_steady_state ] for i in irf_active_plot_container]. .. , reference_steady_state]
1308+
12641309 return_plots = []
12651310
12661311 shock_names = []
@@ -1290,24 +1335,10 @@ function plot_irf!(𝓂::ℳ;
12901335 if length (new_name) > 0
12911336 push! (variable_names, (new_name))
12921337 end
1293-
1294- push! (pp,begin
1295- StatsPlots. plot (Y[i,:,shock] .+ SS,
1296- title = variable_name,
1297- ylabel = " Level" ,
1298- label = " " )
1299-
1300- if can_dual_axis
1301- StatsPlots. plot! (StatsPlots. twinx (),
1302- 100 * ((Y[i,:,shock] .+ SS) ./ SS .- 1 ),
1303- ylabel = LaTeXStrings. L "\% \D elta" ,
1304- label = " " )
1305- end
1306-
1307- StatsPlots. hline! (can_dual_axis ? [SS 0 ] : [SS],
1308- color = :black ,
1309- label = " " )
1310- end )
1338+
1339+ # push!(pp, plot_irf_subplot(Y[i,:,shock], SS, variable_name, can_dual_axis))
1340+
1341+ push! (pp, plot_irf_subplot ([k[i,:,shock] for k in Ys], [k[var_idx[i]] for k in reference_steady_states], variable_name, can_dual_axis))
13111342
13121343 if ! (plot_count % plots_per_page == 0 )
13131344 plot_count += 1
@@ -1434,6 +1465,7 @@ function plot_irf!(𝓂::ℳ;
14341465 :sylvester_algorithm => sylvester_algorithm,
14351466 :lyapunov_algorithm => lyapunov_algorithm,
14361467 :plot_data => Y,
1468+ :reference_steady_state => reference_steady_state,
14371469 :variable_names => variable_names,
14381470 :shock_names => shock_names,
14391471 :shock_idx => shock_idx,
0 commit comments