From 55f87f76e93f7c30f102add7fc1ffc489e77647d Mon Sep 17 00:00:00 2001 From: thorek1 Date: Fri, 10 Nov 2023 19:54:40 +0100 Subject: [PATCH] works on all examples --- src/MacroModelling.jl | 23 ++++++++++++----------- src/get_functions.jl | 39 ++++++++++++++++++++++----------------- src/plotting.jl | 37 +++++++++++++++++++++---------------- 3 files changed, 55 insertions(+), 44 deletions(-) diff --git a/src/MacroModelling.jl b/src/MacroModelling.jl index 8e40b7bc..09d15239 100644 --- a/src/MacroModelling.jl +++ b/src/MacroModelling.jl @@ -313,36 +313,37 @@ function write_obc_violation_equations(๐“‚) if dyn_1 if maximisation push!(cond1, :(push!(constraint_values, $(x.args[3].args[2])))) - push!(cond2, :(push!(constraint_values, $(x.args[3].args[2])))) + # push!(cond2, :(push!(constraint_values, $(x.args[3].args[2])))) else push!(cond1, :(push!(constraint_values, -$(x.args[3].args[2])))) - push!(cond2, :(push!(constraint_values, -$(x.args[3].args[2])))) # RBC + # push!(cond2, :(push!(constraint_values, -$(x.args[3].args[2])))) # RBC end end if dyn_2 if maximisation push!(cond1, :(push!(constraint_values, $(x.args[3].args[3])))) - push!(cond2, :(push!(constraint_values, $(x.args[3].args[3])))) # testmax + # push!(cond2, :(push!(constraint_values, $(x.args[3].args[3])))) # testmax else push!(cond1, :(push!(constraint_values, -$(x.args[3].args[3])))) - push!(cond2, :(push!(constraint_values, -$(x.args[3].args[3])))) # RBC + # push!(cond2, :(push!(constraint_values, -$(x.args[3].args[3])))) # RBC end end if maximisation push!(cond1, :(push!(shock_sign_indicators, true))) - push!(cond2, :(push!(shock_sign_indicators, true))) + # push!(cond2, :(push!(shock_sign_indicators, true))) else push!(cond1, :(push!(shock_sign_indicators, false))) - push!(cond2, :(push!(shock_sign_indicators, false))) + # push!(cond2, :(push!(shock_sign_indicators, false))) end - :(if isapprox($plchldr, $ineq_plchldr_1, atol = 1e-12) - $(Expr(:block, cond1...)) - else - $(Expr(:block, cond2...)) - end) + # :(if isapprox($plchldr, $ineq_plchldr_1, atol = 1e-12) + # $(Expr(:block, cond1...)) + # else + # $(Expr(:block, cond2...)) + # end) + :($(Expr(:block, cond1...))) end : x : x : diff --git a/src/get_functions.jl b/src/get_functions.jl index 34f17d53..82ff05c4 100644 --- a/src/get_functions.jl +++ b/src/get_functions.jl @@ -921,25 +921,26 @@ function get_irf(๐“‚::โ„ณ; JuMP.@variable(model, x[1:num_shocks*periods_per_shock]) # Now loop through obc_shock_bounds to set the bounds on these variables. - # for (idx, v) in enumerate(๐“‚.var[obc_inequalities_idx]) + # maxmin_indicators = ๐“‚.obc_violation_function(x, past_states, past_shocks, state_update, reference_steady_state, ๐“‚, unconditional_forecast_horizon, JuMP.AffExpr.(present_shocks))[2] + # for (idx, v) in enumerate(maxmin_indicators) # idxs = (idx - 1) * periods_per_shock + 1:idx * periods_per_shock - # # if contains(string(v), "แต’แต‡แถœโบ") - # if ๐“‚.obc_violation_function(x, past_states, past_shocks, state_update, reference_steady_state, ๐“‚, unconditional_forecast_horizon, JuMP.AffExpr.(present_shocks))[2][idx] - # # JuMP.set_upper_bound.(x[idxs], 0) - # JuMP.set_lower_bound.(x[idxs], 0) - # else - # JuMP.set_upper_bound.(x[idxs], 0) - # # JuMP.set_lower_bound.(x[idxs], 0) - # end - # # else - # # if ๐“‚.obc_violation_function(x, past_states, past_shocks, state_update, reference_steady_state, ๐“‚, unconditional_forecast_horizon, JuMP.AffExpr.(present_shocks))[2][idx] - # # JuMP.set_lower_bound.(x[idxs], 0) - # # else - # # JuMP.set_upper_bound.(x[idxs], 0) - # # end - # # end + # if v + # # if ๐“‚.obc_violation_function(x, past_states, past_shocks, state_update, reference_steady_state, ๐“‚, unconditional_forecast_horizon, JuMP.AffExpr.(present_shocks))[2][idx] + # JuMP.set_upper_bound.(x[idxs], 0) + # # JuMP.set_lower_bound.(x[idxs], 0) + # else + # # JuMP.set_upper_bound.(x[idxs], 0) + # JuMP.set_lower_bound.(x[idxs], 0) + # end + # # # else + # # # if ๐“‚.obc_violation_function(x, past_states, past_shocks, state_update, reference_steady_state, ๐“‚, unconditional_forecast_horizon, JuMP.AffExpr.(present_shocks))[2][idx] + # # # JuMP.set_lower_bound.(x[idxs], 0) + # # # else + # # # JuMP.set_upper_bound.(x[idxs], 0) + # # # end + # # # end # end - + JuMP.@objective(model, Min, x' * โ„’.I * x) JuMP.@constraint(model, ๐“‚.obc_violation_function(x, past_states, past_shocks, state_update, reference_steady_state, ๐“‚, unconditional_forecast_horizon, JuMP.AffExpr.(present_shocks))[1] .<= 0) @@ -948,6 +949,10 @@ function get_irf(๐“‚::โ„ณ; solved = JuMP.termination_status(model) โˆˆ [JuMP.OPTIMAL,JuMP.LOCALLY_SOLVED] + # precision = JuMP.objective_value(model) + + # if precision > eps(Float32) @warn "Bounds enforced up to reduced precision: $precision" end # I need the dual value (constraints). this relates to the shock size + present_states = state_update(past_states,JuMP.value.(past_shocks)) present_shocks[contains.(string.(๐“‚.timings.exo),"แต’แต‡แถœ")] .= JuMP.value.(x) diff --git a/src/plotting.jl b/src/plotting.jl index 4234c31d..7f7c87f4 100644 --- a/src/plotting.jl +++ b/src/plotting.jl @@ -482,23 +482,24 @@ function plot_irf(๐“‚::โ„ณ; JuMP.@variable(model, x[1:num_shocks*periods_per_shock]) # Now loop through obc_shock_bounds to set the bounds on these variables. - # for (idx, v) in enumerate(๐“‚.var[obc_inequalities_idx]) + # maxmin_indicators = ๐“‚.obc_violation_function(x, past_states, past_shocks, state_update, reference_steady_state, ๐“‚, unconditional_forecast_horizon, JuMP.AffExpr.(present_shocks))[2] + # for (idx, v) in enumerate(maxmin_indicators) # idxs = (idx - 1) * periods_per_shock + 1:idx * periods_per_shock - # # if contains(string(v), "แต’แต‡แถœโบ") - # if ๐“‚.obc_violation_function(x, past_states, past_shocks, state_update, reference_steady_state, ๐“‚, unconditional_forecast_horizon, JuMP.AffExpr.(present_shocks))[2][idx] - # # JuMP.set_upper_bound.(x[idxs], 0) - # JuMP.set_lower_bound.(x[idxs], 0) - # else - # JuMP.set_upper_bound.(x[idxs], 0) - # # JuMP.set_lower_bound.(x[idxs], 0) - # end - # # else - # # if ๐“‚.obc_violation_function(x, past_states, past_shocks, state_update, reference_steady_state, ๐“‚, unconditional_forecast_horizon, JuMP.AffExpr.(present_shocks))[2][idx] - # # JuMP.set_lower_bound.(x[idxs], 0) - # # else - # # JuMP.set_upper_bound.(x[idxs], 0) - # # end - # # end + # if v + # # if ๐“‚.obc_violation_function(x, past_states, past_shocks, state_update, reference_steady_state, ๐“‚, unconditional_forecast_horizon, JuMP.AffExpr.(present_shocks))[2][idx] + # JuMP.set_upper_bound.(x[idxs], 0) + # # JuMP.set_lower_bound.(x[idxs], 0) + # else + # # JuMP.set_upper_bound.(x[idxs], 0) + # JuMP.set_lower_bound.(x[idxs], 0) + # end + # # # else + # # # if ๐“‚.obc_violation_function(x, past_states, past_shocks, state_update, reference_steady_state, ๐“‚, unconditional_forecast_horizon, JuMP.AffExpr.(present_shocks))[2][idx] + # # # JuMP.set_lower_bound.(x[idxs], 0) + # # # else + # # # JuMP.set_upper_bound.(x[idxs], 0) + # # # end + # # # end # end JuMP.@objective(model, Min, x' * โ„’.I * x) @@ -508,6 +509,10 @@ function plot_irf(๐“‚::โ„ณ; JuMP.optimize!(model) solved = JuMP.termination_status(model) โˆˆ [JuMP.OPTIMAL,JuMP.LOCALLY_SOLVED] + + # precision = JuMP.objective_value(model) + + # if precision > eps(Float32) @warn "Bounds enforced up to reduced precision: $precision" end # I need the dual value (constraints). this relates to the shock size present_states = state_update(past_states,JuMP.value.(past_shocks)) present_shocks[contains.(string.(๐“‚.timings.exo),"แต’แต‡แถœ")] .= JuMP.value.(x)