Skip to content

Commit

Permalink
IRF utility functions (#159)
Browse files Browse the repository at this point in the history
* IRFs
  • Loading branch information
jlperla authored Aug 10, 2022
1 parent dd63e72 commit 2709222
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 601 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "DifferentiableStateSpaceModels"
uuid = "beacd9db-9e5e-4956-9b09-459a4b2028df"
authors = ["Jesse Perla <jesseperla@gmail.com> and contributors"]
version = "0.4.21"
version = "0.4.22"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
603 changes: 5 additions & 598 deletions notebooks/estimate_rbc.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/DifferentiableStateSpaceModels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export make_perturbation_model, default_model_cache_location, PerturbationModel,
SolverCache, PerturbationSolverSettings, @include_example_module,
@make_and_include_perturbation_model, generate_perturbation,
generate_perturbation_derivatives!, model_H_latex, model_steady_states_iv_latex,
model_steady_states_latex, verify_steady_state
model_steady_states_latex, verify_steady_state, irf

# export solve # will be replaced by SciML

Expand Down
15 changes: 15 additions & 0 deletions src/sequential.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,19 @@ function DifferenceEquations.QuadraticStateSpaceProblem(sol::AbstractSecondOrder
sol.C_1, sol.C_2,
observables_noise,
observables, noise, syms)
end

function irf(sol::AbstractFirstOrderPerturbationSolution, ϵ0, T; x0 = zeros(sol.n_x))
@assert(length(ϵ0) == sol.n_ϵ)
noise = hcat(ϵ0, zeros(sol.n_ϵ, T - 1)) # the ϵ shocks are "noise" in DifferenceEquations for SciML compatibility
problem = LinearStateSpaceProblem(sol, x0, (0, T); noise, observables_noise = nothing) # do not include observation noise
return solve(problem)
end

function irf(sol::AbstractSecondOrderPerturbationSolution, ϵ0, T; x0 = zeros(sol.n_x))
@assert(length(ϵ0) == sol.n_ϵ)
noise = hcat(ϵ0, zeros(sol.n_ϵ, T - 1)) # the ϵ shocks are "noise" in DifferenceEquations for SciML compatibility
problem = QuadraticStateSpaceProblem(sol, x0, (0, T); noise,
observables_noise = nothing) # do not include observation noise
return solve(problem)
end
36 changes: 35 additions & 1 deletion test/first_order_sequence.jl
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,38 @@ settings = PerturbationSolverSettings(; tol_cholesky = 1e9,
kalman_test(p_d_input, p_f, m, z, settings)
test_rrule(Zygote.ZygoteRuleConfig(),
p_d_input -> kalman_test_alt_prior(p_d_input, p_f, m, z, settings),
p_d_input; rrule_f = rrule_via_ad, check_inferred = false)
p_d_input; rrule_f = rrule_via_ad, check_inferred = false)

@testset "irf" begin
m = @include_example_module(Examples.rbc_observables) # const required due to #117 bug
p_f == 0.2, δ = 0.02, σ = 0.01, Ω_1 = 0.1)
p_d == 0.5, β = 0.95)
sol = generate_perturbation(m, p_d, p_f)
ϵ0 = [1.0] # only 1 shock here
T = 20
val = irf(sol, ϵ0, T)
@test val.u[4] [-0.07183218632163027,
-0.0004000000000000003]
@test val.z[4] [-0.0071511417965463755
-0.07183218632163027]
end

function irf_last(p_d, ϵ0; p_f, m, T = 20)
sol = generate_perturbation(m, p_d, p_f)
val = irf(sol, ϵ0, T)
u = val.u
return u[end][1] # return last element
end

# trouble putting in testset for the above reason
p_f == 0.2, δ = 0.02, σ = 0.01, Ω_1 = 0.1)
p_d == 0.5, β = 0.95)
ϵ0 = [1.0]
m = @include_example_module(Examples.rbc_observables)
irf_last(p_d, ϵ0; p_f, m)

gradient((p_d, ϵ0) -> irf_last(p_d, ϵ0; p_f, m), p_d, ϵ0)

test_rrule(Zygote.ZygoteRuleConfig(),
(p_d, ϵ0) -> irf_last(p_d, ϵ0; p_f, m), p_d, ϵ0;
rrule_f = rrule_via_ad, check_inferred = false, rtol = 1e-7)
34 changes: 34 additions & 0 deletions test/second_order_sequence.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,37 @@ res = gradient((args...) -> likelihood_test_joint_second(args..., m, z), p_d, p_
@test res[4] [-562.7501911667255; -4462.244956879686] # Regression test, not sure of the correct number
# CRTU is preferred but FiniteDifferences is having trouble at this point
# end

# @testset "irf" begin
m = @include_example_module(Examples.rbc_observables) # const required due to #117 bug
p_f == 0.2, δ = 0.02, σ = 0.01, Ω_1 = 0.1)
p_d == 0.5, β = 0.95)
sol = generate_perturbation(m, p_d, p_f, Val(2))
ϵ0 = [1.0] # only 1 shock here
T = 20
val = irf(sol, ϵ0, T)
@test val.u[4] [-0.071737439933804
-0.0004000000000000003]
@test val.z[4] [-0.007064576215795991
-0.071737439933804]
# end

function irf_last_2(p_d, ϵ0; p_f, m, T = 20)
sol = generate_perturbation(m, p_d, p_f, Val(2))
val = irf(sol, ϵ0, T)
u = val.u
return u[end][1] # return last element
end

# Gradients for 2nd order differenceeuqations are not complete
# p_f = (ρ = 0.2, δ = 0.02, σ = 0.01, Ω_1 = 0.1)
# p_d = (α = 0.5, β = 0.95)
# ϵ0 = [1.0]
# m = @include_example_module(Examples.rbc_observables)
# irf_last_2(p_d, ϵ0; p_f, m)

# gradient((p_d, ϵ0) -> irf_last_2(p_d, ϵ0; p_f, m), p_d, ϵ0)

# test_rrule(Zygote.ZygoteRuleConfig(),
# (p_d, ϵ0) -> irf_last_2(p_d, ϵ0; p_f, m), p_d, ϵ0;
# rrule_f = rrule_via_ad, check_inferred = false)

2 comments on commit 2709222

@jlperla
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/65992

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.4.22 -m "<description of version>" 270922258a4ab439445fced707d19dbdf59e1bef
git push origin v0.4.22

Please sign in to comment.