-
Notifications
You must be signed in to change notification settings - Fork 17
Add dynamic equations function to model structure for direct equation evaluation #160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
…to check everything works as intended
…ling.jl into combine_plots
…ations Before processing calibration equations, variables with timing indices (₍₀₎, ₍₋₁₎, ₍₁₎) are now replaced with their steady state equivalents. This ensures calibration equations correctly reference steady state values. Co-authored-by: thorek1 <13523097+thorek1@users.noreply.github.com>
src/MacroModelling.jl
Outdated
| substitution_dict[𝔙[offset + i]] = present_sym[var_idx] | ||
| end | ||
|
|
||
| # Map past variables |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot here you now also need to create # Map steady state variables
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in latest commit. Added steady state variables mapping section that maps both 𝔓 entries and corresponding 𝔙 entries to ss_sym array.
Map steady state variables from both 𝔓 (parameter vector) and 𝔙 (variable vector) to ss_sym array. This ensures steady state variables are properly connected to the steady_state input parameter. Co-authored-by: thorek1 <13523097+thorek1@users.noreply.github.com>
src/MacroModelling.jl
Outdated
| # Create symbolic arrays for full vectors (not indexed subsets) | ||
| Symbolics.@variables params_sym[1:n_params] calib_params_sym[1:n_calib_params] ss_sym[1:n_ss] future_sym[1:n_vars] present_sym[1:n_vars] past_sym[1:n_vars] shocks_sym[1:n_exo] | ||
|
|
||
| # Create substitution mapping from 𝔓 and 𝔙 to the new separate variables |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot instead of mapping back to the new separate values. do the mapping starting from 𝓂.dyn_equations and 𝓂.calibration_equations to the new separate values. take into account as well the variables in steady state in 𝓂.calibration_equations
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in latest commit. Changed to map directly from 𝓂.dyn_equations and 𝓂.calibration_equations to the new separate symbolic arrays. Steady state variables in calibration equations are properly handled by converting timing indices to ₍ₛₛ₎ before mapping to ss_sym.
Changed approach to start from 𝓂.dyn_equations and 𝓂.calibration_equations directly, mapping original symbols to new separate symbolic arrays (params_sym, calib_params_sym, ss_sym, future_sym, present_sym, past_sym, shocks_sym). Properly handles steady state variables in calibration equations. Co-authored-by: thorek1 <13523097+thorek1@users.noreply.github.com>
thorek1
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot Now, write another script where you load a model, you get the stochastic steady state, and then you take draws for the shocks from a Sobol sequence. use quasimontecarlo.jl package for that. And then over these number of draws, you evaluate the dynamic equations function and try to minimize the residual across the shock draws by choosing the variables which are mapped to past, present, and future. the steady state values refer to the non stochastic steady state.
Created demonstration script that: - Loads and solves a model - Generates Sobol quasi-random sequences for shocks using QuasiMonteCarlo.jl - Optimizes variables (past, present, future) to minimize residuals across shock draws - Uses non-stochastic steady state as requested - Includes comprehensive README with usage instructions Co-authored-by: thorek1 <13523097+thorek1@users.noreply.github.com>
Created in latest commit. The script
|
…s and update optimization test script for improved clarity and functionality
| calib_params = zeros(length(m.calibration_equations_parameters)) | ||
|
|
||
| # Define objective function: sum of squared residuals across all shock draws | ||
| function objective(vars_flat, shock_draws, model, SS_non_stochastic, calib_params) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot solve this with nonlinarsolve.jl restructure the problem so that per equations average residuals are returned that are compatible nonlinearsolve.jl
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in latest commit. Restructured to use NonlinearSolve.jl with Newton-Raphson solver. The residual function now returns per-equation average residuals (not a scalar sum), making it compatible with NonlinearSolve.jl. Updated README with new approach.
Changed from optimization to nonlinear solving: - Use NonlinearSolve.jl instead of Optimization.jl - Return per-equation average residuals (not scalar sum) - Use Newton-Raphson solver with autodiff - Transform Sobol draws to standard normal distribution - Updated README with new approach and requirements Co-authored-by: thorek1 <13523097+thorek1@users.noreply.github.com>
…ions and enhance residual function compatibility with NonlinearSolve.jl
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## combine_plots #160 +/- ##
================================================
Coverage ? 55.39%
================================================
Files ? 19
Lines ? 12177
Branches ? 0
================================================
Hits ? 6746
Misses ? 5431
Partials ? 0 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Implementation Complete ✅
Successfully implemented dynamic equations function feature for MacroModelling.jl with requested changes.
Changes Per Review Feedback
New Function Signature
get_dynamic_residuals(residual, parameters, calibration_parameters, past, present, future, steady_state, shocks, model)The helper function is now extremely lightweight with zero allocations - it simply calls the generated function directly.
Implementation Details
The generated function now takes full vectors and handles indexing internally:
𝓂.dyn_equationsand𝓂.calibration_equations, symbols are directly mapped to new separate symbolic arraysFiles Modified
src/MacroModelling.jl: Generate function with direct symbol mapping from original equationssrc/get_functions.jl: Minimal wrapper function with documentationtest_dynamic_equations_optimization.jl: NEW - Demonstration script using NonlinearSolve.jltest_dynamic_equations_optimization_README.md: NEW - Usage instructions for demonstration scriptUsage Example
Demonstration Script
A comprehensive demonstration script (
test_dynamic_equations_optimization.jl) showcases advanced usage:The script demonstrates how the dynamic equations function can be integrated into nonlinear solving workflows for:
The residual function returns per-equation average residuals (not a scalar sum), making it fully compatible with NonlinearSolve.jl and other nonlinear solvers that expect vector-valued residuals.
See
test_dynamic_equations_optimization_README.mdfor detailed usage instructions.Performance
Status: Ready for review
Original prompt
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.