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

Fix tests for TimeSeriesEcon v0.7 #56

Merged
merged 2 commits into from
May 11, 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 Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Printf = "1.7"
Random = "1.7"
SparseArrays = "1.7"
Suppressor = "0.2"
TimeSeriesEcon = "0.6"
TimeSeriesEcon = "0.7"
TimerOutputs = "0.5"
julia = "1.7"

Expand Down
2 changes: 1 addition & 1 deletion test/logsimtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@

# update exogenous data accordingly
ed[firstdate(p)] = k[firstdate(p)]
ed[lastdate(p), 1:2] .= rand(2)
ed[lastdate(p), m.variables] .= rand(2)

@test simulate(m, p, ed, fctype=fcslope) ≈ k
@test simulate(m, p, ed, fctype=fcnatural) ≈ k
Expand Down
67 changes: 66 additions & 1 deletion test/sim_fo.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
##################################################################################
# This file is part of StateSpaceEcon.jl
# BSD 3-Clause License
# Copyright (c) 2020-2023, Bank of Canada
# Copyright (c) 2020-2024, Bank of Canada
# All rights reserved.
##################################################################################

Expand Down Expand Up @@ -343,4 +343,69 @@ end
end
end

@testset "initdcmp.mx" begin
# test if we're able to mix and match, i.e., can we stitch together shockdecomps ran with different solvers.

atol = 1e-13
rtol = 1e-10
ignoremissing = true
quiet = true

m = E6.newmodel()
m.p_dly = 0
m.p_dlp = 0
empty!(m.sstate.constraints)
@steadystate m lp = 1.5
@steadystate m ly = 1.1
@steadystate m lyn = 1.1

clear_sstate!(m)
sssolve!(m)
linearize!(m)
solve!(m; solver=:stackedtime)
solve!(m; solver=:firstorder)

rng = 2001Q1:2024Q4
fctype = fcnatural
shks = filter(v -> isshock(v) || isexog(v), m.allvars)
vars = filter(v -> !isshock(v) && !isexog(v), m.allvars)

p = Plan(m, rng)
exog = steadystatedata(m, p)
exog[begin:first(rng)-1, vars] = rand(m.maxlag, m.nvars)
ref_s = shockdecomp(m, p, exog; solver=:stackedtime, fctype)
ref_f = shockdecomp(m, p, exog; solver=:firstorder)

# we've called linearize!, so stacked time and first order must be identical
@test compare(ref_f, ref_s; atol, rtol, ignoremissing, quiet)
for v in vars, s in shks
@test norm(ref_s.sd[v][:, s], Inf) < 1e-14
end

# stitch with the other solver
rng1 = rng[5:end]
p1 = Plan(m, rng1)
exog1 = zerodata(m, p1)
exog1[begin:first(rng1)-1, vars] = ref_f.s
exog1[rng1, shks] = ref_f.s[rng1, shks]
if fctype === fclevel
exog1[last(rng1)+1:end, vars] = ref_f.s
end

res_fs = shockdecomp(m, p1, exog1; solver=:stackedtime, fctype, initdecomp=ref_f)
res_ss = shockdecomp(m, p1, exog1; solver=:stackedtime, fctype, initdecomp=ref_s)
res_sf = shockdecomp(m, p1, exog1; solver=:firstorder, initdecomp=ref_s)
res_ff = shockdecomp(m, p1, exog1; solver=:firstorder, initdecomp=ref_f)

# res_fs.sd.:($var).term has NaN in the beginning
foreach(values(res_fs.sd)) do x
replace!(x -> isnan(x) ? 0 : x, x)
end

@test compare(res_fs, res_ss; atol, rtol, ignoremissing, quiet)
@test compare(res_ss, res_sf; atol, rtol, ignoremissing, quiet)
@test compare(res_sf, res_ff; atol, rtol, ignoremissing, quiet)
@test compare(res_ff, ref_s; atol, rtol, ignoremissing, quiet)

end

2 changes: 1 addition & 1 deletion test/simdatatests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
@test sd[:, 1] == sd[:, 2]
sd.a = zeros(size(dta, 1))
@test sum(abs, sd.a.values) == 0
@test_throws BoundsError sd.a = ones(length(sd.a) + 5)
@test_throws DimensionMismatch sd.a = ones(length(sd.a) + 5)
# access to rows by MIT
sd[:] = dta[:]
@test sd[2000Q1] isa Vector{Float64}
Expand Down
2 changes: 1 addition & 1 deletion test/simtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ end
let m = getE3()
p = Plan(m, 3:177)
ed = zerodata(m, p)
ed[3U, m.shocks] .= 0.2 * rand(1, 3)
ed[3U, m.shocks] .= 0.2 * rand(3)
ed[3U:end, m.variables] .= rand(178, 3)
empty!(m.sstate.constraints)
clear_sstate!(m)
Expand Down
Loading