Skip to content

Commit

Permalink
Add tests (especially for solve)
Browse files Browse the repository at this point in the history
  • Loading branch information
Johannes Nägele committed Oct 29, 2023
1 parent 1b652dc commit df75aac
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 16 deletions.
3 changes: 1 addition & 2 deletions src/Helpers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ function remove_expr(x::Array)
return untangled
end

remove_blocks(e::Any) = e

"""
Sometimes our equation input might have lines which are themselves blocks (with only a single line).
This happens e.g. if we have two variables on the left hand side.
Expand Down Expand Up @@ -88,6 +86,7 @@ function left_symbol(line::Expr) # TODO: why do we not need to check heads?
end
end
end
# fallback
return nothing
end

Expand Down
32 changes: 18 additions & 14 deletions src/models/SIM.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,25 @@ function SIM()
α_2 = 0.4
end

sim = model(
endos=@variables(Y, T, YD, C, H_s, H_h, H),
exos=@variables(G),
params=params,
eqs=@equations begin
Y = C + G
T = θ * Y
YD = Y - T
C = α_1 * YD + α_2 * H[-1]
H_s + H_s[-1] = G - T
H_h + H_h[-1] = YD - C
H = H_s + H_s[-1] + H[-1]
end
)

Dict(
:params => params,
:model => model(
endos=@variables(Y, T, YD, C, H_s, H_h, H),
exos=@variables(G),
params=params,
eqs=@equations begin
Y = C + G
T = θ * Y
YD = Y - T
C = α_1 * YD + α_2 * H[-1]
H_s + H_s[-1] = G - T
H_h + H_h[-1] = YD - C
H = H_s + H_s[-1] + H[-1]
end
)
:exos => [20.0][:, :],
:lags => fill(0.0, length(sim.endogenous_variables), 1),
:model => sim
)
end
23 changes: 23 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,18 @@ using Test

@testset "Consistent.jl" begin

@testset "Types" begin
let vars = @variables Y, C, G
@test Consistent.Variables(vars) == vars
end

@test Consistent.Variables([:a, :b]) == @variables [a, b]
end

@testset "Internals" begin
@test Consistent.left_symbol(:(Y - C = G)) == :Y
@test isnothing(Consistent.left_symbol(:(1 = 1)))

@test Consistent.remove_expr([:x :(a in b) :y]) == [:x, :a, :in, :b, :y]
test_eqs = quote
z = y * (y[-1] + 0.5 * z) * θ + x[-1]
Expand Down Expand Up @@ -107,4 +118,16 @@ using Test

PC_complete = PC_gdp + PC_hh
end

@testset "Solve" begin
sim = Consistent.SIM()
let sol = solve(
sim[:model],
sim[:lags],
sim[:exos],
map(x -> sim[:params][x], sim[:model].parameters)
)
@test round(sol[1]) == 38.0
end
end
end

0 comments on commit df75aac

Please sign in to comment.