Skip to content

Commit

Permalink
add qme, lyap and sylv options to filtering funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
thorek1 committed Dec 18, 2024
1 parent bbc35aa commit f5b31a1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
38 changes: 26 additions & 12 deletions src/filter/inversion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3488,21 +3488,25 @@ end


function filter_data_with_model(𝓂::β„³,
data_in_deviations::KeyedArray{Float64},
::Val{:second_order}, # algo
::Val{:inversion}; # filter
quadratic_matrix_equation_solver::Symbol = :schur,
warmup_iterations::Int = 0,
filter_algorithm::Symbol = :LagrangeNewton,
smooth::Bool = true,
verbose::Bool = false)
data_in_deviations::KeyedArray{Float64},
::Val{:second_order}, # algo
::Val{:inversion}; # filter
quadratic_matrix_equation_solver::Symbol = :schur,
sylvester_algorithm::Symbol = :doubling,
warmup_iterations::Int = 0,
filter_algorithm::Symbol = :LagrangeNewton,
smooth::Bool = true,
verbose::Bool = false)

T = 𝓂.timings

variables = zeros(T.nVars, size(data_in_deviations,2))
shocks = zeros(T.nExo, size(data_in_deviations,2))

sss, converged, SS_and_pars, solution_error, βˆ‡β‚, βˆ‡β‚‚, 𝐒₁, 𝐒₂ = calculate_second_order_stochastic_steady_state(𝓂.parameter_values, 𝓂, verbose = verbose)
sss, converged, SS_and_pars, solution_error, βˆ‡β‚, βˆ‡β‚‚, 𝐒₁, 𝐒₂ = calculate_second_order_stochastic_steady_state(𝓂.parameter_values, 𝓂,
quadratic_matrix_equation_solver = quadratic_matrix_equation_solver,
sylvester_algorithm = sylvester_algorithm,
verbose = verbose)

if !converged || solution_error > 1e-12
@error "Could not find 2nd order stochastic steady state"
Expand Down Expand Up @@ -3708,6 +3712,7 @@ function filter_data_with_model(𝓂::β„³,
::Val{:pruned_second_order}, # algo
::Val{:inversion}; # filter
quadratic_matrix_equation_solver::Symbol = :schur,
sylvester_algorithm::Symbol = :doubling,
warmup_iterations::Int = 0,
filter_algorithm::Symbol = :LagrangeNewton,
smooth::Bool = true,
Expand All @@ -3720,7 +3725,11 @@ function filter_data_with_model(𝓂::β„³,

observables = get_and_check_observables(𝓂, data_in_deviations)

sss, converged, SS_and_pars, solution_error, βˆ‡β‚, βˆ‡β‚‚, 𝐒₁, 𝐒₂ = calculate_second_order_stochastic_steady_state(𝓂.parameter_values, 𝓂, pruning = true, verbose = verbose)
sss, converged, SS_and_pars, solution_error, βˆ‡β‚, βˆ‡β‚‚, 𝐒₁, 𝐒₂ = calculate_second_order_stochastic_steady_state(𝓂.parameter_values, 𝓂,
quadratic_matrix_equation_solver = quadratic_matrix_equation_solver,
sylvester_algorithm = sylvester_algorithm,
pruning = true,
verbose = verbose)

if solution_error > 1e-12 || isnan(solution_error)
@error "No solution for these parameters."
Expand Down Expand Up @@ -3993,7 +4002,9 @@ function filter_data_with_model(𝓂::β„³,

observables = get_and_check_observables(𝓂, data_in_deviations)

sss, converged, SS_and_pars, solution_error, βˆ‡β‚, βˆ‡β‚‚, βˆ‡β‚ƒ, 𝐒₁, 𝐒₂, 𝐒₃ = calculate_third_order_stochastic_steady_state(𝓂.parameter_values, 𝓂, verbose = verbose)
sss, converged, SS_and_pars, solution_error, βˆ‡β‚, βˆ‡β‚‚, βˆ‡β‚ƒ, 𝐒₁, 𝐒₂, 𝐒₃ = calculate_third_order_stochastic_steady_state(𝓂.parameter_values, 𝓂,
quadratic_matrix_equation_solver = quadratic_matrix_equation_solver,
verbose = verbose)

if !converged || solution_error > 1e-12
@error "Could not find 3rd order stochastic steady state"
Expand Down Expand Up @@ -4305,7 +4316,10 @@ function filter_data_with_model(𝓂::β„³,

observables = get_and_check_observables(𝓂, data_in_deviations)

sss, converged, SS_and_pars, solution_error, βˆ‡β‚, βˆ‡β‚‚, βˆ‡β‚ƒ, 𝐒₁, 𝐒₂, 𝐒₃ = calculate_third_order_stochastic_steady_state(𝓂.parameter_values, 𝓂, pruning = true, verbose = verbose)
sss, converged, SS_and_pars, solution_error, βˆ‡β‚, βˆ‡β‚‚, βˆ‡β‚ƒ, 𝐒₁, 𝐒₂, 𝐒₃ = calculate_third_order_stochastic_steady_state(𝓂.parameter_values, 𝓂,
pruning = true,
quadratic_matrix_equation_solver = quadratic_matrix_equation_solver,
verbose = verbose)

if !converged || solution_error > 1e-12
@error "Could not find pruned 3rd order stochastic steady state"
Expand Down
12 changes: 10 additions & 2 deletions src/filter/kalman.jl
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@ function filter_data_with_model(𝓂::β„³,
::Val{:first_order}, # algo
::Val{:kalman}; # filter
quadratic_matrix_equation_solver::Symbol = :schur,
lyapunov_algorithm::Symbol = :doubling,
warmup_iterations::Int = 0,
smooth::Bool = true,
verbose::Bool = false)
Expand All @@ -568,7 +569,10 @@ function filter_data_with_model(𝓂::β„³,

obs_symbols = obs_axis isa String_input ? obs_axis .|> Meta.parse .|> replace_indices : obs_axis

filtered_and_smoothed = filter_and_smooth(𝓂, data_in_deviations, obs_symbols; verbose = verbose)
filtered_and_smoothed = filter_and_smooth(𝓂, data_in_deviations, obs_symbols;
quadratic_matrix_equation_solver = quadratic_matrix_equation_solver,
lyapunov_algorithm = lyapunov_algorithm,
verbose = verbose)

variables = filtered_and_smoothed[smooth ? 1 : 5]
standard_deviations = filtered_and_smoothed[smooth ? 2 : 6]
Expand All @@ -584,6 +588,7 @@ function filter_and_smooth(𝓂::β„³,
data_in_deviations::AbstractArray{Float64},
observables::Vector{Symbol};
verbose::Bool = false,
quadratic_matrix_equation_solver::Symbol = :schur,
lyapunov_algorithm::Symbol = :doubling,
tol::AbstractFloat = 1e-12)
# Based on Durbin and Koopman (2012)
Expand All @@ -604,7 +609,10 @@ function filter_and_smooth(𝓂::β„³,

βˆ‡β‚ = calculate_jacobian(parameters, SS_and_pars, 𝓂)# |> Matrix

sol, qme_sol, solved = calculate_first_order_solution(βˆ‡β‚; T = 𝓂.timings, initial_guess = 𝓂.solution.perturbation.qme_solution, verbose = verbose)
sol, qme_sol, solved = calculate_first_order_solution(βˆ‡β‚; T = 𝓂.timings,
quadratic_matrix_equation_solver = quadratic_matrix_equation_solver,
initial_guess = 𝓂.solution.perturbation.qme_solution,
verbose = verbose)

if solved 𝓂.solution.perturbation.qme_solution = qme_sol end

Expand Down

0 comments on commit f5b31a1

Please sign in to comment.