Skip to content

Fix Caldara model failing to solve with precompile=true: expand trivial algebraic simplification - #279

Draft
thorek1 with Copilot wants to merge 300 commits into
mainfrom
copilot/improve-model-parsing-speed
Draft

Fix Caldara model failing to solve with precompile=true: expand trivial algebraic simplification#279
thorek1 with Copilot wants to merge 300 commits into
mainfrom
copilot/improve-model-parsing-speed

Conversation

Copilot AI commented Mar 6, 2026

Copy link
Copy Markdown
Contributor

With @model ... precompile=true, SymPy's simplify() is skipped, leaving ratio expressions like c[1]/c[0] and (1-l[1])/(1-l[0]) unsimplified at steady state. These become spurious auxiliary variables (➕₄, ➕₅) with initial guess 1e12, causing ➕₅^ν = (1e12)^(1e12) → overflow → solver failure.

Changes

  • src/macros.jl — Replace the bare passthrough with trivial_simplify(): converts the expression to SS form (strips time subscripts so l[1], l[0]l), then applies a full set of algebraic rewrites using postwalk (from MacroTools) in bottom-up order, so reductions chain:

    Rule Example
    A/A → 1 c[1]/c[0] → 1, (1-l[1])/(1-l[0]) → 1
    A - A → 0 log(σ[0]) - log(σ[-1]) → 0
    A^0 → 1, A^1 → A, 1^A → 1 1^(1-ν) → 1
    A*1 → A, 1*A → A, A*0 → 0 eliminates trivial products
    A+0 → A, 0+A → A, A-0 → A eliminates trivial additions
    A/1 → A, 0/A → 0 eliminates trivial divisions

    Chaining is automatic: (c[1]/c[0]) * (k[0]/k[0]) reduces each ratio to 1, then 1*1 → 1. Applied in all precompile branches: nonneg-expression bases under ^, log, norminvcdf, and full SS equations.

function trivial_simplify(ex)
    ss_ex = convert_to_ss_equation(ex)   # strip time subscripts: l[1], l[0] → l
    result = postwalk(ss_ex) do x
        # ... algebraic rewrite rules applied bottom-up ...
    end
    return result
end
  • src/nsss_solver.jl — In make_equation_robust_to_domain_errors, widen x.args[2] isa Float64 ? x : to x.args[2] isa Number ? x :. After trivial simplification, expressions like 1^(1-ν) reach this function with an Int base; accessing .head on Int64 was throwing a FieldError. Also applies trivial_simplify in all five precompile branches.

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Thore Kockerols and others added 30 commits February 17, 2026 14:24
…rkspace management for first-order solutions
…ution calculations for improved type compatibility
… solver

- Introduced `solve_lu_right!` and `solve_lu_left!` functions for improved LU factorization handling.
- Updated `solve_quadratic_matrix_equation` to utilize LU factorization with caching for efficiency.
- Enhanced `schur_workspace` and `qme_workspace` structures to include additional LU workspaces and dimensions.
- Modified various functions to accept and utilize the new `schur_ws` parameter for better performance in calculations.
- Improved caching mechanism for solutions in `calculate_first_order_solution` and related functions.
- Ensured compatibility with fast LAPACK routines for LU and Schur decompositions.
…e of workspaces and improve code consistency
…ng workspaces for improved performance and clarity
…ating related calculations for improved clarity and consistency
thorek1 and others added 28 commits March 29, 2026 09:03
…optimize loop in get_non_stochastic_steady_state_residuals
…ality tests

- Updated tolerance parameters in `calculate_first_order_solution`, `calculate_second_order_solution`, and `calculate_third_order_solution` functions to use the new structure for tolerances.
- Modified functionality tests to replace deprecated tolerance settings with the new `NsssTolerances` structure, ensuring consistency across tests.
- Adjusted comments and cleaned up code for better readability and maintainability.
…elative tolerances for improved numerical stability
…lattening and converting tolerance structures to dictionaries, improving clarity and usability in solver functions.
…re accurate parameter handling during subsequent calculations.
…ty checks and ensure accurate parameter retrieval in solution functions.
…o improve performance and avoid redundant calculations in multiple functions.
…s for improved performance and memory management
- Added a `caching` parameter to several functions to enable caching of results based on input parameters.
- Implemented cache validation checks to ensure cached results are only used when parameters match.
- Updated the `solve_quadratic_matrix_equation`, `get_NSSS_and_parameters`, and `calculate_first_order_solution` functions to utilize caching effectively.
- Modified related functions in `rrules`, `moments`, and `perturbation` modules to support caching and parameter validation.
- Introduced new fields in `valid_for_caches` structure to accommodate additional cached results for jacobian, hessian, and third-order derivatives.
… usage and improve performance across multiple functions.
…d NaN defaults when mean or covariance calculations fail
@codecov-commenter

codecov-commenter commented Apr 3, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 83.28343% with 335 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.84%. Comparing base (180b1e1) to head (fc707f2).
⚠️ Report is 30 commits behind head on main.

Files with missing lines Patch % Lines
src/moments.jl 58.29% 88 Missing ⚠️
src/custom_autodiff_rules/forwarddiff.jl 70.11% 75 Missing ⚠️
src/algorithms/lyapunov.jl 64.67% 59 Missing ⚠️
src/algorithms/fast_lapack_wrappers.jl 58.57% 29 Missing ⚠️
src/algorithms/sylvester.jl 86.40% 28 Missing ⚠️
src/options_and_caches.jl 90.96% 27 Missing ⚠️
src/nsss_solver.jl 89.04% 8 Missing ⚠️
src/macros.jl 89.36% 5 Missing ⚠️
src/algorithms/quadratic_matrix_equation.jl 95.50% 4 Missing ⚠️
src/filter/inversion.jl 97.75% 4 Missing ⚠️
... and 4 more
Additional details and impacted files
@@             Coverage Diff             @@
##             main     #279       +/-   ##
===========================================
+ Coverage   65.87%   83.84%   +17.97%     
===========================================
  Files          23       25        +2     
  Lines       14063    20357     +6294     
===========================================
+ Hits         9264    17069     +7805     
+ Misses       4799     3288     -1511     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Base automatically changed from optim_LFI_alloc to main June 4, 2026 13:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants