From 65d28832a0930b01f902bc260ba97a8d2cabdbd6 Mon Sep 17 00:00:00 2001 From: Boyan Bejanov Date: Wed, 17 Apr 2024 17:39:01 -0400 Subject: [PATCH 1/2] Fix tests for TimeSeriesEcon v0.7 --- Project.toml | 2 +- test/logsimtests.jl | 2 +- test/simdatatests.jl | 2 +- test/simtests.jl | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Project.toml b/Project.toml index 8a2df7e..447f487 100644 --- a/Project.toml +++ b/Project.toml @@ -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" diff --git a/test/logsimtests.jl b/test/logsimtests.jl index 478ff26..0e55020 100644 --- a/test/logsimtests.jl +++ b/test/logsimtests.jl @@ -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 diff --git a/test/simdatatests.jl b/test/simdatatests.jl index 33e0136..b2cfbdd 100644 --- a/test/simdatatests.jl +++ b/test/simdatatests.jl @@ -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} diff --git a/test/simtests.jl b/test/simtests.jl index dfbc313..d7eb9ca 100644 --- a/test/simtests.jl +++ b/test/simtests.jl @@ -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) From 889ee4d19a41d7a7a8864fbff47fc7e2236c66a5 Mon Sep 17 00:00:00 2001 From: Boyan Bejanov Date: Fri, 10 May 2024 16:41:39 -0400 Subject: [PATCH 2/2] tests for shockdecomp stitch from different solvers --- test/sim_fo.jl | 67 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/test/sim_fo.jl b/test/sim_fo.jl index 28e83cc..26132e2 100644 --- a/test/sim_fo.jl +++ b/test/sim_fo.jl @@ -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. ################################################################################## @@ -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