Skip to content

Commit 9d7ac6b

Browse files
authored
Refactor take2 (#238)
1 parent 307b1cf commit 9d7ac6b

31 files changed

Lines changed: 2267 additions & 1204 deletions

β€ŽAGENTS.mdβ€Ž

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ like `[0]` (present), `[1]` (future), `[-1]` (past), and `[x]` (shock).
3030

3131
## Tests
3232

33+
Before running tests, activate the test environment and instantiate dependencies:
34+
35+
```julia
36+
using Pkg
37+
Pkg.activate("test")
38+
Pkg.instantiate()
39+
```
40+
3341
Tests are organized by test sets specified via `TEST_SET`:
3442

3543
```bash

β€ŽAGENT_PROGRESS.mdβ€Ž

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
## Summary
2+
- Reworked first-order index cache to avoid `Union{Nothing,...}` and added cached identity-slice matrices for `expand` usage.
3+
- Switched `calculate_first_order_solution` to positional `𝓂` wrappers (Real and Dual) and updated call sites to stop passing caches explicitly.
4+
- Added a runnable script for RBC baseline pruned third-order standard deviations and updated the standalone cache test to use the new initializer and positional wrapper.
5+
- Fixed pruned mean calculation to use state-only kron indices and added a runnable script for RBC baseline pruned second-order mean.
6+
- Fixed `get_steady_state` axis labels for models with calibration equations and added a runnable script to exercise calibration equations.
7+
- Added a moments cache for kron selectors, `I_plus_s_s`, and `e⁴`/`e⁢`, plus substate/dependency caches, and switched `moments.jl` to use cached values.
8+
- Extended model structure caching with steady-state selector matrices, full NSSS display names, and cached variable indices; updated steady-state and variable parsing code to use cached values.
9+
- Added a targeted cache validation script for steady-state expansion, custom steady state mapping, and cached variable indices.
10+
- Moved cache creation helpers into `src/options_and_caches.jl` (name display, computational constants, model structure, first-order indices, selector matrix, moments) and removed duplicate definitions from `src/MacroModelling.jl` and `src/moments.jl`.
11+
- Refactored cache usage to call ensure helpers once per high-level call and use `𝓂.caches` in `src/moments.jl`, `src/perturbation.jl`, `src/get_functions.jl`, and filter paths; updated steady-state/model-structure callers to use cached selectors.
12+
- Fixed package load/precompile failures caused by include-order cycles (`timings` and `β„³`) by introducing `src/timings.jl` and removing the `::β„³` annotation from `initialize_caches!`.
13+
- Removed obsolete `T = 𝓂.timings` keywords from second/third-order solution call sites and fixed a typo (`β„‚C` β†’ `cache`) in `calculate_third_order_solution`.
14+
- Made `get_loglikelihood(..., algorithm = :pruned_third_order)` work by adding `get_computational_constants(::timings)` and fixing the inversion filter’s pruned third-order Jacobian construction (stateΒ² term needed an explicit volatility slot).
15+
- Added `cache::caches` overloads for `determine_efficient_order` and switched `src/moments.jl` call sites from `𝓂.timings` to `𝓂.caches`.
16+
- Replaced cache→timings wrapper overloads by making cache-carrying methods the concrete implementations for `determine_efficient_order`, `create_*_order_auxiliary_matrices`, `solve_quadratic_matrix_equation`, and `parse_*_input_to_index`/`parse_covariance_groups`.
17+
- Added a targeted `get_std` script test for pruned second/third order on RBC_CME models with/without calibration equations.
18+
- Fixed `calculate_first_order_solution` (Dual path) to use `cache.first_order_index_cache` fields (`nabla_zero_cols`, `nabla_e_start`) after cache refactor.
19+
- Added a targeted script test covering the three `run_rbc_*.jl` entrypoints.
20+
- Restored `apply_custom_name` and `normalize_superscript` helpers needed by `StatsPlotsExt` so `plot_model_estimates` works again.
21+
- Added a targeted script test for `plot_model_estimates` / `plot_model_estimates!`.
22+
- Fixed `name_display_cache` axis fields to be concrete (`Vector{Symbol}`) so `get_var_axis(::β„³)` is type-stable and doesn’t trip DispatchDoctor.
23+
- Added a targeted script test for `get_var_axis` / `get_exo_axis` return types.
24+
- Added ChainRules `rrule` for `mat_mult_kron` to make Zygote gradients work (avoids mutation errors from `mul!` into views).
25+
- Fixed the `rrule` signature for `calculate_second_order_solution` to match current argument ordering so Zygote doesn’t trace into LAPACK solves.
26+
- Added a targeted script test for Zygote gradients of `get_loglikelihood` at `:third_order` and `:pruned_third_order`.
27+
- Moved second/third-order auxiliary matrices into `caches`, introduced a `workspaces` struct for higher-order scratch buffers, and removed `nothing`-based timings initialization (added `Empty_timings` + `is_empty_timings`).
28+
- Fixed `ensure_name_display_cache!` to initialize `calib_axis` correctly when curly-brace formatting is used.
29+
- Moved auxiliary indices into `caches`, added a dedicated `workspaces` field on `β„³`, and updated higher-order solution calls to pass workspaces explicitly.
30+
- Updated `run_cache_steady_state_checks.jl` to only call mean/std for supported algorithms.
31+
32+
## Tests
33+
- `TEST_SET=basic julia --project -e 'using Pkg; Pkg.test()'` failed: unsatisfiable requirements during Pkg resolve (DynamicPPL/Pigeons/Turing/JET constraints).
34+
- `julia --project -e 'using Test; include("test/test_standalone_function.jl")'` failed: `FiniteDifferences` not found in current environment.
35+
- `julia --project run_rbc_pruned_third_order_std.jl` succeeded (prints `get_std` output for `RBC_baseline`, algorithm `:pruned_third_order`).
36+
- Re-ran `julia --project run_rbc_pruned_second_order_mean.jl`: succeeded (prints `get_mean` output for `RBC_baseline`, algorithm `:pruned_second_order`).
37+
- Re-ran `julia --project run_rbc_pruned_third_order_std.jl`: succeeded (prints `get_std` output for `RBC_baseline`, algorithm `:pruned_third_order`).
38+
- Re-ran `julia --project run_rbc_pruned_third_order_std.jl`: succeeded (prints `get_std` output for `RBC_baseline`, algorithm `:pruned_third_order`).
39+
- `julia --project run_rbc_pruned_second_order_mean.jl` succeeded (prints `get_mean` output for `RBC_baseline`, algorithm `:pruned_second_order`).
40+
- `julia --project run_rbc_pruned_third_order_std.jl` succeeded (prints `get_std` output for `RBC_baseline`, algorithm `:pruned_third_order`).
41+
- `julia --project run_cache_steady_state_checks.jl` failed: `get_relevant_steady_states` not exported (used unqualified call).
42+
- `julia --project run_cache_steady_state_checks.jl` succeeded after qualifying `MacroModelling.get_relevant_steady_states`.
43+
- `julia --project run_rbc_calibration_equations_ss.jl` succeeded (prints calibration equations, calibrated parameter values, and steady state for `RBC_CME_calibration_equations`).
44+
- `julia --project -e 'using MacroModelling; include("models/RBC_baseline.jl"); MacroModelling.calculate_second_order_moments(RBC_baseline.parameter_values, RBC_baseline);'` failed: `calculate_hessian` method mismatch (hessian functions not initialized for second-order).
45+
- `julia --project -e 'using MacroModelling; include("models/RBC_baseline.jl"); solve!(RBC_baseline); MacroModelling.calculate_second_order_moments(RBC_baseline.parameter_values, RBC_baseline);'` failed: same `calculate_hessian` method mismatch.
46+
- `julia --project -e 'using MacroModelling; include("models/RBC_baseline.jl"); solve!(RBC_baseline; algorithm = :second_order); MacroModelling.calculate_second_order_moments(RBC_baseline.parameter_values, RBC_baseline);'` succeeded.
47+
- `julia --project run_rbc_pruned_second_order_mean.jl` succeeded (prints `get_mean` output for `RBC_baseline`, algorithm `:pruned_second_order`).
48+
- `julia --project run_rbc_pruned_second_order_mean.jl` succeeded (prints `get_mean` output for `RBC_baseline`, algorithm `:pruned_second_order`).
49+
- `julia --project run_rbc_pruned_third_order_std.jl` succeeded (prints `get_std` output for `RBC_baseline`, algorithm `:pruned_third_order`).
50+
- `julia --project run_cache_steady_state_checks.jl` succeeded (prints steady-state cache checks for `RBC_baseline` and `RBC_switch`).
51+
- `julia --project -e 'using MacroModelling; println(\"loaded\")'` succeeded.
52+
- `julia --project scripts/test_get_loglikelihood_pruned_third.jl` succeeded (prints finite pruned third-order loglikelihood).
53+
- `julia --project scripts/test_get_std_pruned_second_third.jl` succeeded (pruned second/third order `get_std` for RBC_CME models with/without calibration equations).
54+
- Re-ran `julia --project scripts/test_get_loglikelihood_pruned_third.jl`: succeeded.
55+
- Re-ran `julia --project scripts/test_get_std_pruned_second_third.jl`: succeeded after cache-propagation refactor.
56+
- `julia --project run_rbc_pruned_second_order_mean.jl` failed: `caches` has no field `nabla_zero_cols`.
57+
- `julia --project run_rbc_pruned_third_order_std.jl` failed: `caches` has no field `nabla_zero_cols`.
58+
- `julia --project run_rbc_pruned_second_order_mean.jl` succeeded after fixing Dual first-order cache indexing.
59+
- `julia --project run_rbc_pruned_third_order_std.jl` succeeded after fixing Dual first-order cache indexing.
60+
- `julia --project scripts/test_run_rbc_scripts.jl` succeeded (pruned second/third moments + calibration equations steady state).
61+
- `julia --project scripts/test_get_std_pruned_second_third.jl` succeeded.
62+
- `julia --project scripts/test_get_loglikelihood_pruned_third.jl` succeeded.
63+
- `julia --project -e 'using MacroModelling, StatsPlots; include(\"test/models/RBC_CME.jl\"); sim = simulate(m); plot_model_estimates(m, sim([:k], :,:simulate); show_plots=false, save_plots=false)'` succeeded after restoring plotting helpers.
64+
- `julia --project scripts/test_plot_model_estimates.jl` succeeded.
65+
- `julia --project scripts/test_get_var_axis.jl` succeeded.
66+
- `julia --project scripts/test_zygote_loglikelihood_third_pruned_third.jl` succeeded (Zygote gradients for `:third_order` and `:pruned_third_order`).
67+
- `julia --project run_cache_steady_state_checks.jl` succeeded.
68+
- `julia --project run_cache_steady_state_checks.jl` succeeded after updating the script’s mean/std algorithm list.
69+
70+
## Remaining
71+
- Resolve test environment dependencies and rerun tests.

β€ŽCLAUDE.mdβ€Ž

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ MacroModelling.jl is a Julia package for developing and solving Dynamic Stochast
1313

1414
### Running Tests
1515

16+
Before running tests, activate the test environment and instantiate dependencies:
17+
18+
```julia
19+
using Pkg
20+
Pkg.activate("test")
21+
Pkg.instantiate()
22+
```
23+
1624
Tests are organized by test sets specified via the `TEST_SET` environment variable:
1725

1826
```bash

β€Žbenchmark/benchmarks.jlβ€Ž

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,18 @@ function run_benchmarks!(𝓂::β„³, SUITE::BenchmarkGroup)
4343

4444
SUITE[𝓂.model_name]["qme"] = BenchmarkGroup()
4545

46-
sol, qme_sol, solved = calculate_first_order_solution(βˆ‡β‚; T = 𝓂.timings, opts = merge_calculation_options(quadratic_matrix_equation_algorithm = :schur))
46+
sol, qme_sol, solved = calculate_first_order_solution(βˆ‡β‚, 𝓂.caches; opts = merge_calculation_options(quadratic_matrix_equation_algorithm = :schur))
4747

4848
clear_solution_caches!(𝓂, :first_order)
4949

50-
SUITE[𝓂.model_name]["qme"]["schur"] = @benchmarkable calculate_first_order_solution($βˆ‡β‚; T = $𝓂.timings, opts = merge_calculation_options(quadratic_matrix_equation_algorithm = :schur)) setup = clear_solution_caches!($𝓂, :first_order)
50+
SUITE[𝓂.model_name]["qme"]["schur"] = @benchmarkable calculate_first_order_solution($βˆ‡β‚, $𝓂.caches; opts = merge_calculation_options(quadratic_matrix_equation_algorithm = :schur)) setup = clear_solution_caches!($𝓂, :first_order)
5151

52-
SUITE[𝓂.model_name]["qme"]["doubling"] = @benchmarkable calculate_first_order_solution($βˆ‡β‚; T = $𝓂.timings, opts = merge_calculation_options(quadratic_matrix_equation_algorithm = :doubling)) setup = clear_solution_caches!($𝓂, :first_order)
52+
SUITE[𝓂.model_name]["qme"]["doubling"] = @benchmarkable calculate_first_order_solution($βˆ‡β‚, $𝓂.caches; opts = merge_calculation_options(quadratic_matrix_equation_algorithm = :doubling)) setup = clear_solution_caches!($𝓂, :first_order)
5353

5454

55-
A = @views sol[:, 1:𝓂.timings.nPast_not_future_and_mixed] * β„’.diagm(ones(𝓂.timings.nVars))[𝓂.timings.past_not_future_and_mixed_idx,:]
55+
A = @views sol[:, 1:𝓂.caches.timings.nPast_not_future_and_mixed] * β„’.diagm(ones(𝓂.caches.timings.nVars))[𝓂.caches.timings.past_not_future_and_mixed_idx,:]
5656

57-
C = @views sol[:, 𝓂.timings.nPast_not_future_and_mixed+1:end]
57+
C = @views sol[:, 𝓂.caches.timings.nPast_not_future_and_mixed+1:end]
5858

5959
CC = C * C'
6060

@@ -103,7 +103,7 @@ run_benchmarks!(Smets_Wouters_2007, SUITE)
103103
# end
104104
# end
105105

106-
# If a cache of tuned parameters already exists, use it, otherwise, tune and cache
106+
# If a caches of tuned parameters already exists, use it, otherwise, tune and caches
107107
# the benchmark parameters. Reusing cached parameters is faster and more reliable
108108
# than re-tuning `SUITE` every time the file is included.
109109
# paramspath = joinpath(dirname(@__FILE__), "params.json")

0 commit comments

Comments
Β (0)