# Modeling and solving systems of nonlinear dynamic equations

**URL:** <https://discourse.julialang.org/t/modeling-and-solving-systems-of-nonlinear-dynamic-equations/108556>\
**Category:** Modelling & Simulations\
**Tags:** package, jump, nlsolve, nonlinear, dynamical-systems\
**Created:** [January 9, 2024, 11:44am UTC](https://discourse.julialang.org/t/modeling-and-solving-systems-of-nonlinear-dynamic-equations/108556 "2024-01-09T11:44:01Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![enzo.salvatore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/enzo.salvatore/32/204249_2.png) [@enzo.salvatore](https://discourse.julialang.org/u/enzo.salvatore)\
**Post date:** [January 9, 2024, 11:44am UTC](https://discourse.julialang.org/t/modeling-and-solving-systems-of-nonlinear-dynamic-equations/108556/1 "2024-01-09T11:44:01Z")

</div>

I am currently a Ph.D. student in economics, and I am looking to find a way to model and solve dynamic systems of nonlinear equations. The goal is to create a dynamic Computable General Equilibrium (CGE) model in Julia. I am currently a beginner in dynamic systems and have never programmed such a model for conducting simulations. Thank you in advance for your responses.

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [January 9, 2024, 11:52am UTC](https://discourse.julialang.org/t/modeling-and-solving-systems-of-nonlinear-dynamic-equations/108556/2 "2024-01-09T11:52:43Z")

</div>

I have experience in dynamical systems in Julia but not much experience in economics modelling. Can you describe in more detail what a "dynamic Computable General Equilibrium (CGE) " means?

---

<div class="post-metadata">

**Author:** ![MichelJuillard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/micheljuillard/32/10555_2.png) [@MichelJuillard](https://discourse.julialang.org/u/MichelJuillard)\
**Post date:** [January 9, 2024, 1:00pm UTC](https://discourse.julialang.org/t/modeling-and-solving-systems-of-nonlinear-dynamic-equations/108556/3 "2024-01-09T13:00:10Z")

</div>

You may want to take a look at [GitHub - DynareJulia/Dynare.jl: A Julia rewrite of Dynare: solving, simulating and estimating DSGE models.](https://GitHub.com/DynareJulia/Dynare.jl)

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 9, 2024, 1:14pm UTC](https://discourse.julialang.org/t/modeling-and-solving-systems-of-nonlinear-dynamic-equations/108556/4 "2024-01-09T13:14:10Z")

</div>

> [@enzo.salvatore](#):
>
> a dynamic Computable General Equilibrium (CGE) model in Julia

A more specific description of your model would help. Eg parameters, endogenous objects, relevant functional equations (equilibrium conditions, optimality), state space, whether it is continuous or discrete time, etc. And whether you just want to solve a few times, or estimate (ie solve a gazillion of times, fast).

---

<div class="post-metadata">

**Author:** ![enzo.salvatore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/enzo.salvatore/32/204249_2.png) [@enzo.salvatore](https://discourse.julialang.org/u/enzo.salvatore)\
**Post date:** [January 9, 2024, 4:03pm UTC](https://discourse.julialang.org/t/modeling-and-solving-systems-of-nonlinear-dynamic-equations/108556/5 "2024-01-09T16:03:50Z")

</div>

A CGE (Computable General Equilibrium) is a mathematical model that comprises a set of equations (often nonlinear) describing the behaviors of various economic agents within an economy. These models enable the modeling of an economy and conducting simulations. For instance, they help understand how an increase in public spending affects the initial state of an economy and towards which new equilibrium state different economic aggregates will converge. As far as I understand, dynamic models like these allow predictions for the future. I hope this clarifies it.

---

<div class="post-metadata">

**Author:** ![enzo.salvatore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/enzo.salvatore/32/204249_2.png) [@enzo.salvatore](https://discourse.julialang.org/u/enzo.salvatore)\
**Post date:** [January 9, 2024, 4:08pm UTC](https://discourse.julialang.org/t/modeling-and-solving-systems-of-nonlinear-dynamic-equations/108556/6 "2024-01-09T16:08:14Z")

</div>

I don’t have specifications yet for the model I want to build. I wanted to know if someone knew how to create a small dynamic model to solve, such as the Ramsey model, so that I can understand how this can be done in JULIA.

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [January 9, 2024, 4:30pm UTC](https://discourse.julialang.org/t/modeling-and-solving-systems-of-nonlinear-dynamic-equations/108556/7 "2024-01-09T16:30:47Z")

</div>

> [@enzo.salvatore](#):
>
> a set of equations (often nonlinear) describing the behaviors of various economic agents within an economy

Are these equations differential equations? or are they stochastic processes? The packages that can be helpful for you depend on this context. But in any case, to the best of my understanding, Julia seems to have the best-of-class packages for various flavours of dynamical modelling. Perhaps have a look in the functionality offered by:

- DifferentialEquations.jl for differential equations
- JumpProcesses.jl for stochastic processes
- NonlinearSolve.jl for finding steady states of differentialk equations (via the `SteadyStateProblem`)
- ModelingToolkit.jl for building a model based on symbolic notation
- DynamicalSystems.jl for analyzing the model, not building it

I am sure more people from the economics side can add more information here about relevant packages. E.g., the Dynare.jl mentioned above seems to be directly relevant for you.

---

<div class="post-metadata">

**Author:** ![RJDennis](https://avatars.discourse-cdn.com/v4/letter/r/90db22/32.png) [@RJDennis](https://discourse.julialang.org/u/RJDennis)\
**Post date:** [January 9, 2024, 5:05pm UTC](https://discourse.julialang.org/t/modeling-and-solving-systems-of-nonlinear-dynamic-equations/108556/8 "2024-01-09T17:05:51Z")

</div>

There is also [GitHub - RJDennis/SolveDSGE.jl: A Julia package to solve, simulate, and analyze nonlinear DSGE models.](https://github.com/RJDennis/SolveDSGE.jl)

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 10, 2024, 11:29am UTC](https://discourse.julialang.org/t/modeling-and-solving-systems-of-nonlinear-dynamic-equations/108556/9 "2024-01-10T11:29:39Z")

</div>

> [@enzo.salvatore](#):
>
> so that I can understand how this can be done in JULIA.

The same as in other languages, but note that a lot hinges on the methodology you use. Eg using a spectral/collocation method,

1. you write out the functional equations
2. set up a scheme of function approximations, parametrized by some vector \theta
3. calculate r residuals at collocation points,
4. encode the mapping r(\theta) in a callable,
5. solve for r(\theta) \approx 0.

For 2. and 5., I use

> **[GitHub - tpapp/SpectralKit.jl: Building blocks of spectral methods for Julia.](https://github.com/tpapp/SpectralKit.jl)**
>
> Building blocks of spectral methods for Julia. Contribute to tpapp/SpectralKit.jl development by creating an account on GitHub.

and

> **[GitHub - tpapp/TrustRegionMethods.jl: Trust region methods for nonlinear...](https://github.com/tpapp/TrustRegionMethods.jl)**
>
> Trust region methods for nonlinear systems of equations in Julia. - GitHub - tpapp/TrustRegionMethods.jl: Trust region methods for nonlinear systems of equations in Julia.

which I find very robust (I use them for larger models). A Ramsey model is about 50 lines of code I guess.

A good textbook that will get you started is [eg Miranda and Fackler](https://mitpress.mit.edu/9780262633093/applied-computational-economics-and-finance/). Reading it would allow you to code the Ramsey model from first principles, and then you can decide if you want a toolkit.

I am assuming that the Ramsey model is just for learning. The method you will eventually find useful will depend on the complexity of the model and your requirements (nonlinear or linear solution).

> [@Datseris](#):
>
> Perhaps have a look in the functionality offered by:

While some econ models (especially continuous time) have a PDE representation, classic PDE solvers are usually not very useful outside special cases since they expect traditional boundary conditions, while in most simple macro models agents live forever. My preference is spectral methods for mid-scale models, but larger models are still tricky (some are using [deep learning](https://www.sas.upenn.edu/~jesusfv/Continuous_Time_0.pdf)).

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [January 10, 2024, 9:26pm UTC](https://discourse.julialang.org/t/modeling-and-solving-systems-of-nonlinear-dynamic-equations/108556/10 "2024-01-10T21:26:01Z")

</div>

What do you think about instead jump-based standard nonlinear optimization with automatic discretion of the time dimension using InfiniteOpt ?

---

<div class="post-metadata">

**Author:** ![00krishna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/00krishna/32/8843_2.png) [@00krishna](https://discourse.julialang.org/u/00krishna)\
**Post date:** [January 11, 2024, 7:27pm UTC](https://discourse.julialang.org/t/modeling-and-solving-systems-of-nonlinear-dynamic-equations/108556/11 "2024-01-11T19:27:52Z")

</div>

I suppose it really depends upon the specific model that the user is interested in studying. Most CGE models are done in an optimization context where there is some objective function, and then a set of constraints that explain how the world works–things like you cannot spend in the next period more than you had in the prior period.

So I have found that `NonlinearSolve.jl` is generally pretty good for numerically solving these problems. Now keep in mind, in many cases econ papers will try to find some analytic solution to these simple model, which are easier to interpret. An analytic solution will let you see the functions for the optimal values of the quantities or variables of interest.

In the case of DSGE or such models, I think a lot of that can be done with the `ControlSystems.jl` package. Many DSGE models are solved using linear quadratic regulators. So the `lqr()` function in `ControlSystems` will do what you want.

Packages like `InfiniteOpt` are design for trajectory optimization or more complex optimal control problems. The user could use these, but I think that any kind of analysis would generally begin with LQR and then move to something more sophisticated when LQR does not work. For example, LQR usually assumes that the system begins at a fixed point and defines the control law–or in the econ case, the consumption vector or something–that keeps the system at that optimal fixed point. But if you question involves how do you get from a point far from the fixed point to the fixed point then you would need tools like `InfiniteOpt`, etc., and Trajectory Optimization.

---

<div class="post-metadata">

**Author:** ![Longblackcoffee](https://avatars.discourse-cdn.com/v4/letter/l/a5b964/32.png) [@Longblackcoffee](https://discourse.julialang.org/u/Longblackcoffee)\
**Post date:** [January 17, 2024, 3:48pm UTC](https://discourse.julialang.org/t/modeling-and-solving-systems-of-nonlinear-dynamic-equations/108556/12 "2024-01-17T15:48:08Z")

</div>

What year are you in? QuantEcon is a fairly comprehensive resource - it provides code for alot of the canonical dynamic models in economics.

> **[Quantitative Economics with Julia](https://julia.quantecon.org/intro.html)**
>
> This website presents a set of lectures on quantitative economic modeling, designed and written by Jesse Perla, Thomas J. Sargent and John Stachurski. The language instruction is Julia.
