Skip to content

Commit da9590b

Browse files
committed
fix model estimates, no longer in absolute deviations
1 parent f6d27ca commit da9590b

1 file changed

Lines changed: 35 additions & 26 deletions

File tree

ext/StatsPlotsExt.jl

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ function plot_model_estimates(𝓂::ℳ,
237237

238238
pruning = false
239239

240-
@assert !(algorithm [:second_order, :third_order] && shock_decomposition) "Decomposition implemented for first order, pruned second and third order. Second and third order solution decomposition is not yet implemented."
240+
@assert !(algorithm [:second_order, :third_order] && shock_decomposition) "Decomposition implemented for first order, pruned second and third order. Second and third order solution decomposition is not yet implemented."
241241

242242
if algorithm [:second_order, :third_order]
243243
filter = :inversion
@@ -323,7 +323,8 @@ function plot_model_estimates(𝓂::ℳ,
323323
StatsPlots.plot!(#date_axis,
324324
shocks_to_plot[shock_idx[i - length(var_idx)],periods],
325325
title = replace_indices_in_symbol(𝓂.timings.exo[shock_idx[i - length(var_idx)]]) * "₍ₓ₎",
326-
ylabel = shock_decomposition ? "Absolute Δ" : "Level",label = "",
326+
ylabel = "Level",
327+
label = "",
327328
xformatter = x -> string(date_axis[max(1,min(ceil(Int,x),length(date_axis)))]),
328329
xrotation = length(string(date_axis[1])) > 6 ? 30 : 0,
329330
color = shock_decomposition ? estimate_color : :auto)
@@ -334,17 +335,15 @@ function plot_model_estimates(𝓂::ℳ,
334335
else
335336
SS = reference_steady_state[var_idx[i]]
336337

337-
if shock_decomposition SS = zero(SS) end
338-
339-
can_dual_axis = gr_back && all((variables_to_plot[var_idx[i],:] .+ SS) .> eps(Float32)) && (SS > eps(Float32)) && !shock_decomposition
338+
can_dual_axis = gr_back && all((variables_to_plot[var_idx[i],:] .+ SS) .> eps(Float32)) && (SS > eps(Float32))
340339

341340
push!(pp,begin
342-
StatsPlots.plot()
341+
p = StatsPlots.plot()
343342

344343
if shock_decomposition
345344
additional_indices = pruning ? [size(decomposition,2)-1, size(decomposition,2)-2] : [size(decomposition,2)-1]
346345

347-
StatsPlots.groupedbar!(#date_axis,
346+
StatsPlots.groupedbar!(p,
348347
decomposition[var_idx[i],[additional_indices..., shock_idx...],periods]',
349348
bar_position = :stack,
350349
xformatter = x -> string(date_axis[max(1,min(ceil(Int,x),length(date_axis)))]),
@@ -356,50 +355,60 @@ function plot_model_estimates(𝓂::ℳ,
356355
# xformatter = x -> string(date_axis[Int(x)]),
357356
alpha = transparency)
358357
end
359-
360-
StatsPlots.plot!(#date_axis,
361-
variables_to_plot[var_idx[i],periods] .+ SS,
362-
title = replace_indices_in_symbol(𝓂.timings.var[var_idx[i]]),
363-
ylabel = shock_decomposition ? "Absolute Δ" : "Level",
358+
359+
StatsPlots.plot!(p,
360+
variables_to_plot[var_idx[i],periods],
361+
title = replace_indices_in_symbol(𝓂.timings.var[var_idx[i]]),
362+
ylabel = "Level",
364363
xformatter = x -> string(date_axis[max(1,min(ceil(Int,x),length(date_axis)))]),
365364
xrotation = length(string(date_axis[1])) > 6 ? 30 : 0,
366-
label = "",
365+
label = "",
367366
# xformatter = x -> string(date_axis[Int(x)]),
368367
color = shock_decomposition ? estimate_color : :auto)
369368

370369
if var_idx[i] obs_idx
371-
StatsPlots.plot!(#date_axis,
372-
data_in_deviations[indexin([var_idx[i]],obs_idx),periods]' .+ SS,
370+
StatsPlots.plot!(p,
371+
data_in_deviations[indexin([var_idx[i]],obs_idx),periods]',
373372
title = replace_indices_in_symbol(𝓂.timings.var[var_idx[i]]),
374-
ylabel = shock_decomposition ? "Absolute Δ" : "Level",
375-
label = "",
373+
ylabel = "Level",
374+
label = "",
376375
xformatter = x -> string(date_axis[max(1,min(ceil(Int,x),length(date_axis)))]),
377376
xrotation = length(string(date_axis[1])) > 6 ? 30 : 0,
378377
# xformatter = x -> string(date_axis[Int(x)]),
379-
color = shock_decomposition ? data_color : :auto)
378+
color = shock_decomposition ? data_color : :auto)
380379
end
381380

382-
if can_dual_axis
383-
StatsPlots.plot!(StatsPlots.twinx(),
384-
# date_axis,
385-
100*((variables_to_plot[var_idx[i],periods] .+ SS) ./ SS .- 1),
381+
# Get the current y limits
382+
lo, hi = StatsPlots.ylims(p)
383+
384+
# Compute nice ticks on the shifted range
385+
ticks_shifted, _ = StatsPlots.optimize_ticks(lo + SS, hi + SS, k_min = 4, k_max = 8)
386+
387+
labels = Showoff.showoff(ticks_shifted, :auto)
388+
# Map tick positions back by subtracting the offset, keep shifted labels
389+
yticks_positions = ticks_shifted .- SS
390+
391+
StatsPlots.plot!(p; yticks = (yticks_positions, labels))
392+
393+
if can_dual_axis
394+
StatsPlots.plot!(StatsPlots.twinx(p),
395+
ylims = (100 * ((lo + SS) / SS - 1), 100 * ((hi + SS) / SS - 1)),
386396
ylabel = LaTeXStrings.L"\% \Delta",
387397
xformatter = x -> string(date_axis[max(1,min(ceil(Int,x),length(date_axis)))]),
388398
xrotation = length(string(date_axis[1])) > 6 ? 30 : 0,
389399
label = "")
390400

391401
if var_idx[i] obs_idx
392-
StatsPlots.plot!(StatsPlots.twinx(),
393-
# date_axis,
394-
100*((data_in_deviations[indexin([var_idx[i]],obs_idx),periods]' .+ SS) ./ SS .- 1),
402+
StatsPlots.plot!(StatsPlots.twinx(p),
403+
ylims = (100 * ((lo + SS) / SS - 1), 100 * ((hi + SS) / SS - 1)),
395404
ylabel = LaTeXStrings.L"\% \Delta",
396405
xformatter = x -> string(date_axis[max(1,min(ceil(Int,x),length(date_axis)))]),
397406
xrotation = length(string(date_axis[1])) > 6 ? 30 : 0,
398407
label = "")
399408
end
400409
end
401410

402-
StatsPlots.hline!(can_dual_axis ? [SS 0] : [SS],
411+
StatsPlots.hline!(can_dual_axis ? [0 0] : [0],
403412
color = :black,
404413
label = "")
405414
end)

0 commit comments

Comments
 (0)