{"id":2434,"date":"2025-12-02T10:39:31","date_gmt":"2025-12-02T10:39:31","guid":{"rendered":"https:\/\/blogs.mathworks.com\/finance\/?p=2434"},"modified":"2025-12-02T13:06:41","modified_gmt":"2025-12-02T13:06:41","slug":"speeding-up-dynare-models-practical-paths-to-performance-gains","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/finance\/2025\/12\/02\/speeding-up-dynare-models-practical-paths-to-performance-gains\/","title":{"rendered":"Speeding Up Dynare Models: Practical Paths to Performance Gains"},"content":{"rendered":"<p>Dynamic Stochastic General Equilibrium (DSGE) models are essential tools for policy analysis and forecasting, but estimation runs often exceed 24 hours\u2014particularly for large-scale models or Bayesian approaches. This blog presents three practical techniques for accelerating Dynare workflows using the parallel computing capabilities in MATLAB. These methods deliver measurable performance gains with minimal changes to existing code.<\/p>\n<h1>Three Ways to Improve Performance<\/h1>\n<ol style=\"padding: 12px 18px; border-left: 3px solid #666; background: #f7f7f7; border-radius: 4px;\">\n<li style=\"margin-left: 10px;\"><strong>Parallelise optimization: <\/strong>Use built-in flags to compute gradients concurrently during likelihood-based estimation.<\/li>\n<li style=\"margin-left: 10px;\"><strong>Accelerate Bayesian estimation: <\/strong>Distribute independent Markov Chain Monte Carlo (MCMC) across workers for Metropolis-Hastings estimation.<\/li>\n<li style=\"margin-left: 10px;\"><strong>Manage large experiment grids: <\/strong>Use Experiment Manager to organise and audit multiple calibrations without manual loops.<\/li>\n<\/ol>\n<p>Each strategy is examined in detail below, with implementation guidance for Dynare workflows.<\/p>\n<h1><strong>Parallelizing Optimization<\/strong><\/h1>\n<p class=\"font-claude-response-body whitespace-normal break-words\">Estimating a DSGE model typically involves solving a complex optimization problem to identify parameter values that maximize the likelihood function. Dynare supports various algorithms for this task, including the MATLAB fmincon and other built-in routines.<\/p>\n<p class=\"font-claude-response-body whitespace-normal break-words\">Most of these algorithms rely on gradient-based methods, which require computing derivatives of the objective function. This step is computationally expensive but highly parallelizable, making it an ideal candidate for performance improvement.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-2370 aligncenter\" style=\"border: 1px solid #ccc; padding: 4px;\" src=\"http:\/\/blogs.mathworks.com\/finance\/files\/2025\/11\/Curve-image-1.png\" alt=\"\" width=\"794\" height=\"538\" \/><\/p>\n<p>MATLAB users may be familiar with the <tt>UseParallel<\/tt> flag, which calculates directional gradients in parallel to reduce bottlenecks in high-dimensional problems. A natural approach would be to enable this in Dynare as follows:<\/p>\n<p><code style=\"font-size: 16px;\">estimation(optim=('UseParallel', true));<\/code><\/p>\n<p class=\"font-claude-response-body whitespace-normal break-words\">However, as of Dynare 6, this approach will produce incorrect results when used without additional configuration. The issue stems from Dynare&#8217;s persistent internal state, which does not automatically transfer to parallel workers.<\/p>\n<p class=\"font-claude-response-body whitespace-normal break-words\">The solution requires carrying Dynare&#8217;s state into the parallel environment\u2014a process that is not immediately obvious. To address this, we have published a utility function on GitHub that handles the state transfer automatically:<\/p>\n<p><code style=\"font-size: 16px;\">dynareParallel(\u201cmyModel.mod\u201d)<\/code><\/p>\n<p class=\"font-claude-response-body whitespace-normal break-words\">This function ensures that parallel execution is both correct and efficient while preserving the standard Dynare syntax.<\/p>\n<p class=\"font-claude-response-body whitespace-normal break-words\"><strong>Performance tip:<\/strong> Match the number of workers to the number of optimisation variables. Using 100 workers for 10 variables wastes resources.<\/p>\n<h1><strong>Accelerating Bayesian Estimation<\/strong><\/h1>\n<p>Bayesian estimation with Metropolis-Hastings (MH) represents another significant computational bottleneck in Dynare workflows. MH sampling can be configured with multiple chains and blocks:<\/p>\n<p><code style=\"font-size: 16px;\">estimation(mh_replic = 20000, mh_nblocks = 4);<\/code><\/p>\n<p class=\"font-claude-response-body whitespace-normal break-words\">Because each chain is independent, this configuration is well suited for parallel execution. Distributing four chains across four workers can reduce runtime by a factor of four\u2014for example, reducing a 40-hour estimation to approximately 10 hours with minimal code changes.<\/p>\n<p class=\"font-claude-response-body whitespace-normal break-words\">As with gradient-based optimization, parallelizing MH chains requires proper handling of Dynare&#8217;s internal state when using the MATLAB Parallel Computing Toolbox. The same <tt>dynareParallel<\/tt> helper function introduced above can be used for both Dynare 5 and 6. This function distributes chains across workers and ensures correct state transfer.<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-2370\" style=\"border: 1px solid #ccc; padding: 4px;\" src=\"http:\/\/blogs.mathworks.com\/finance\/files\/2025\/11\/MATLAB-client-image-2.png\" alt=\"\" width=\"911\" height=\"439\" \/><\/p>\n<p>&nbsp;<\/p>\n<h1><strong>Managing Large Experiment Grids<\/strong><\/h1>\n<p class=\"font-claude-response-body whitespace-normal break-words\">In some cases, the challenge is not a single long-running estimation but rather hundreds of smaller simulations for testing different calibrations or policy scenarios.<\/p>\n<p class=\"font-claude-response-body whitespace-normal break-words\">The MATLAB Experiment Manager app provides a structured framework for managing these workflows. The tool enables users to:<\/p>\n<ul class=\"[&amp;:not(:last-child)_ul]:pb-1 [&amp;:not(:last-child)_ol]:pb-1 list-disc space-y-2.5 pl-7\">\n<li class=\"whitespace-normal break-words\"><strong>Parameterize inputs<\/strong> such as country identifiers or shock persistence values.<\/li>\n<li class=\"whitespace-normal break-words\"><strong>Launch runs<\/strong> sequentially or in parallel.<\/li>\n<li class=\"whitespace-normal break-words\"><strong>Track results automatically<\/strong> including inputs, outputs, and visualizations.<\/li>\n<\/ul>\n<p class=\"font-claude-response-body whitespace-normal break-words\">Unlike ad hoc <tt>parfor<\/tt> loops, Experiment Manager ensures auditability and robustness. If one run fails, others continue unaffected, and all results are stored in organized directories for straightforward retrieval and analysis.<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-2370\" style=\"border: 1px solid #ccc; padding: 4px;\" src=\"http:\/\/blogs.mathworks.com\/finance\/files\/2025\/11\/Exhaustive-Sweep-result-image-3.png\" alt=\"\" width=\"1079\" height=\"593\" \/><\/p>\n<h1>Implications for Institutions<\/h1>\n<p class=\"font-claude-response-body whitespace-normal break-words\">These techniques offer benefits beyond runtime reduction. They have broader implications for how institutions manage modelling workflows:<\/p>\n<p class=\"font-claude-response-body whitespace-normal break-words\"><strong>Elastic scaling:<\/strong> Cloud computing infrastructure and MATLAB Parallel Server enable dynamic allocation of computing resources. Institutions can provision hundreds or thousands of workers as needed, then scale down when estimation is complete.<\/p>\n<p class=\"font-claude-response-body whitespace-normal break-words\"><strong>Auditability:<\/strong> Built-in tracking mechanisms ensure reproducibility and traceability\u2014critical requirements for policy work and research that must withstand institutional scrutiny.<\/p>\n<p class=\"font-claude-response-body whitespace-normal break-words\"><strong>Accessibility:<\/strong> These improvements typically require minimal modifications to existing Dynare scripts. In many cases, implementation involves adding a flag or using a wrapper function.<\/p>\n<h1 class=\"font-claude-response-heading text-text-100 mt-1 -mb-0.5\">Ready to Apply These Ideas?<\/h1>\n<ol class=\"[&amp;:not(:last-child)_ul]:pb-1 [&amp;:not(:last-child)_ol]:pb-1 list-decimal space-y-2.5 pl-7\">\n<li>Start with profiling: identify true bottlenecks before parallelizing.<\/li>\n<li class=\"whitespace-normal break-words\">Enable <tt>UseParallel<\/tt> for optimization and MCMC chains\u2014big gains, little effort.<\/li>\n<li>For batch experiments, Experiment Manager beats manual loops for robustness and clarity.<\/li>\n<\/ol>\n<h1><strong>Learn More<\/strong><\/h1>\n<ul>\n<li><a href=\"https:\/\/www.dynare.org\/resources\/\" target=\"_blank\" rel=\"noopener\">Dynare Resources<\/a><\/li>\n<li><a href=\"https:\/\/www.mathworks.com\/products\/parallel-computing.html\" target=\"_blank\" rel=\"noopener\">Parallel Computing Toolbox<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/ebenetce\/DynareUtils\" target=\"_blank\" rel=\"noopener\">DynareUtils helper functions<\/a><\/li>\n<\/ul>\n<p>For questions about or to discuss specific Dynare workflows, <a class=\"wp-block-button_link has-white-color has-text-color has-background wp-element-button\" style=\"background-color: #d78825;\" href=\"mailto:centralbanks@mathworks.com\" target=\"_blank\" rel=\"noreferrer noopener\">Contact Us<\/a> to explore how these techniques can be applied to your models.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"http:\/\/blogs.mathworks.com\/finance\/files\/2025\/11\/Curve-image-1.png\" onError=\"this.style.display ='none';\" \/><\/div>\n<p>Dynamic Stochastic General Equilibrium (DSGE) models are essential tools for policy analysis and forecasting, but estimation runs often exceed 24 hours\u2014particularly for large-scale models or Bayesian&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/finance\/2025\/12\/02\/speeding-up-dynare-models-practical-paths-to-performance-gains\/\">read more >><\/a><\/p>\n","protected":false},"author":233,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[16,37,34],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/finance\/wp-json\/wp\/v2\/posts\/2434"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/finance\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/finance\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/finance\/wp-json\/wp\/v2\/users\/233"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/finance\/wp-json\/wp\/v2\/comments?post=2434"}],"version-history":[{"count":91,"href":"https:\/\/blogs.mathworks.com\/finance\/wp-json\/wp\/v2\/posts\/2434\/revisions"}],"predecessor-version":[{"id":2539,"href":"https:\/\/blogs.mathworks.com\/finance\/wp-json\/wp\/v2\/posts\/2434\/revisions\/2539"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/finance\/wp-json\/wp\/v2\/media?parent=2434"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/finance\/wp-json\/wp\/v2\/categories?post=2434"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/finance\/wp-json\/wp\/v2\/tags?post=2434"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}