Draft
Fix Caldara model failing to solve with precompile=true: expand trivial algebraic simplification#279
precompile=true: expand trivial algebraic simplification#279Conversation
…roper allocation in calculations
…iltering functions
…ctions to improve performance
…emove redundant calls to get_steady_state
…and caching logic
…rkspace management for first-order solutions
…ution calculations for improved type compatibility
…quadratic matrix equation solver
… 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
…streamline usage of workspaces
… usage and improve consistency
…ng workspaces for improved performance and clarity
…ating related calculations for improved clarity and consistency
…e to isapprox checks
… for improved performance and correctness
…elling.jl into optim_LFI_alloc
…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.
…ation solvers for consistency
…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.
…ond and third order solutions
…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.
…ibility in steady state calculations
… usage and improve performance across multiple functions.
…d NaN defaults when mean or covariance calculations fail
… handling for unsolved calculations
… and streamline derivative calculations
Codecov Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
With
@model ... precompile=true, SymPy'ssimplify()is skipped, leaving ratio expressions likec[1]/c[0]and(1-l[1])/(1-l[0])unsimplified at steady state. These become spurious auxiliary variables (➕₄, ➕₅) with initial guess1e12, causing➕₅^ν = (1e12)^(1e12)→ overflow → solver failure.Changes
src/macros.jl— Replace the bare passthrough withtrivial_simplify(): converts the expression to SS form (strips time subscripts sol[1],l[0]→l), then applies a full set of algebraic rewrites usingpostwalk(from MacroTools) in bottom-up order, so reductions chain:A/A → 1c[1]/c[0] → 1,(1-l[1])/(1-l[0]) → 1A - A → 0log(σ[0]) - log(σ[-1]) → 0A^0 → 1,A^1 → A,1^A → 11^(1-ν) → 1A*1 → A,1*A → A,A*0 → 0A+0 → A,0+A → A,A-0 → AA/1 → A,0/A → 0Chaining is automatic:
(c[1]/c[0]) * (k[0]/k[0])reduces each ratio to1, then1*1 → 1. Applied in allprecompilebranches: nonneg-expression bases under^,log,norminvcdf, and full SS equations.src/nsss_solver.jl— Inmake_equation_robust_to_domain_errors, widenx.args[2] isa Float64 ? x :tox.args[2] isa Number ? x :. After trivial simplification, expressions like1^(1-ν)reach this function with anIntbase; accessing.headonInt64was throwing aFieldError. Also appliestrivial_simplifyin all fiveprecompilebranches.💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.