Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding function checkmodel. #75

Merged
merged 3 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:

strategy:
matrix:
julia-version: ["1.7", "1.8", "1.9"]
julia-version: ["1.9", "lts", "1"]
julia-arch: [x64]
os: [ubuntu-latest, windows-latest, macOS-latest]

Expand Down
38 changes: 24 additions & 14 deletions src/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1359,6 +1359,27 @@ end
############################
### Initialization routines

"""
checkmodel(model)

Run diagnostic checks that may identify potential problems with the model
definition. Failed checks produce warning messages, not errors.
"""
function checkmodel(model::Model)
unused = get_unused_symbols(model; filter_known_unused=true)
if length(unused[:variables]) > 0
@warn "Model contains unused variables: $(unused[:variables])"
end
if length(unused[:shocks]) > 0
@warn "Model contains unused shocks: $(unused[:shocks])"
end
nvars = sum(!isexog(x) for x in model.variables)
neqns = length(model.equations)
if neqns != nvars
@warn "Model contains different numbers of equations ($neqns) and endogenous variables ($nvars)."
end
end

export @initialize, @reinitialize

"""
Expand Down Expand Up @@ -1403,17 +1424,12 @@ function initialize!(model::Model, modelmodule::Module)
# if dynss is true, then we need the steady state even for the standard MED
nothing
end
unused = get_unused_symbols(model; filter_known_unused=true)
if length(unused[:variables]) > 0
@warn "Model contains unused variables: $(unused[:variables])"
end
if length(unused[:shocks]) > 0
@warn "Model contains unused shocks: $(unused[:shocks])"
end
checkmodel(model)
model._state = :ready
return nothing
end


"""
reinitialize!(model, modelmodule)

Expand Down Expand Up @@ -1455,13 +1471,7 @@ function reinitialize!(model::Model, modelmodule::Module=moduleof(model))
# if dynss is true, then we need the steady state even for the standard MED
nothing
end
unused = get_unused_symbols(model; filter_known_unused=true)
if length(unused[:variables]) > 0
@warn "Model contains unused variables: $(unused[:variables])"
end
if length(unused[:shocks]) > 0
@warn "Model contains unused shocks: $(unused[:shocks])"
end
checkmodel(model)
model._state = :ready
return nothing
end
Expand Down
1 change: 1 addition & 0 deletions test/auxsubs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ using Test
(:info, "Found log(s), which is a shock or exogenous variable. Make sure s data is positive."),
(:info, "Found log(lx). Consider making lx a log variable."),
(:info, "Found log(lx). Consider making lx a log variable."),
(:warn, "Model contains different numbers of equations (6) and endogenous variables (2)."),
include_string(@__MODULE__, """module ASUBS

using ModelBaseEcon
Expand Down
35 changes: 19 additions & 16 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -664,17 +664,17 @@ end
:A => a[t] = cond ? 1.0 : -1.0
end
@initialize model
r, j = eval_RJ(zeros(1,1), model)
r, j = eval_RJ(zeros(1, 1), model)
r == [-1.0] && j == [1.0;;]
end
@test let model = Model()
@variables model b
@parameters model p = 0.5
@equations model begin
:B => b[t] = (0.0 <= p <= 1.0)
:B => b[t] = (0.0 <= p <= 1.0)
end
@initialize model
r, j = eval_RJ(zeros(1,1), model)
r, j = eval_RJ(zeros(1, 1), model)
r == [-1.0] && j == [1.0;;]
end
end
Expand Down Expand Up @@ -1499,7 +1499,10 @@ end
@test length(m.equations) == 2
@test collect(keys(m.equations)) == [:_EQ1, :_EQ3]

@test_logs (:warn, "Model contains unused shocks: [:b_shk]") @reinitialize m
@test_logs(
(:warn, "Model contains unused shocks: [:b_shk]"),
(:warn, "Model contains different numbers of equations (2) and endogenous variables (3)."),
@reinitialize m)

@equations m begin
b[t] = @sstate(b) * (1 - α) + α * b[t-1] + b_shk[t]
Expand Down Expand Up @@ -1534,7 +1537,7 @@ end
@test collect(keys(m.equations)) == [:_EQ1, :_EQ3]
m.options.unused_varshks = [:b_shk]

@test_logs @reinitialize m
@test_logs (:warn, "Model contains different numbers of equations (2) and endogenous variables (3).") @reinitialize m

# option to not show a warning
m = S1.newmodel()
Expand All @@ -1545,7 +1548,7 @@ end
@delete _SSEQ1
end
m.options.unused_varshks = [:a]
@test_logs @reinitialize m
@test_logs (:warn, "Model contains different numbers of equations (2) and endogenous variables (3).") @reinitialize m
end

@using_example E2sat
Expand Down Expand Up @@ -1750,24 +1753,24 @@ end
@variables model x
@shocks model x_shk
@equations model begin
:EQ01 => x[t] = (1-0.50)*@sstate(x) + 0.25*x[t-1] + 0.25*x[t+1] + x_shk[t]
:EQ01 => x[t] = (1 - 0.50) * @sstate(x) + 0.25 * x[t-1] + 0.25 * x[t+1] + x_shk[t]
end
@initialize model
@steadystate model x = 2.0
model.sstate.x.level = 2.0
sim_data = [0.5 0.0;
1.5980861244019138 0.0;
1.8923444976076556 0.0;
1.9712918660287082 0.0;
1.992822966507177 0.0;
2.0 0.0]
sim_data = [0.5 0.0;
1.5980861244019138 0.0;
1.8923444976076556 0.0;
1.9712918660287082 0.0;
1.992822966507177 0.0;
2.0 0.0]

eqtn = model.equations[:EQ01]
res = eval_equation(model, eqtn, sim_data)
@test isnan(res[1]) && isapprox(res[2:5],[0.0, 0.0, 0.0, 0.0]; atol=1e-12) && isnan(res[6])
@test isnan(res[1]) && isapprox(res[2:5], [0.0, 0.0, 0.0, 0.0]; atol=1e-12) && isnan(res[6])

sim_data[3,1] += 1
@test eval_equation(model, eqtn, sim_data,3:4) ≈ [1.0, -0.25]
sim_data[3, 1] += 1
@test eval_equation(model, eqtn, sim_data, 3:4) ≈ [1.0, -0.25]

@test_throws AssertionError eval_equation(model, eqtn, sim_data, 1:7)
end
Expand Down
Loading