United Statesmatthieugomez/EconPDEs.jl
Solve nonlinear PDEs arising from economic models
Featured achievements
The project's latest awards
#1 Repository of the week
#1 Repository of the week
#1 Repository of the month
This package provides the function pdesolve that solves (system of) nonlinear ODEs/PDEs arising in economic models (i.e. PDEs arising from HJB equations). It is:
- robust: upwinding + fully implicit time stepping (see here)
- fast: sparse matrices + Newton acceleration
- simple-to-use
A Simple Example
Let us solve the PDE for the price-dividend ratio in the Long Run Risk model with time-varying drift:
using EconPDEs
# Define a discretized state space
# An OrderedDict in which each key corresponds to a state variable
# Grids can be non-homogeneous
stategrid = OrderedDict(:μ => range(-0.05, 0.1, length = 500))
# Define an initial guess for the value functions
# An OrderedDict in which each key corresponds to a value function to solve for,
# specified as an array with as many dimensions as there are state variables
solend = OrderedDict(:V => ones(500))
# Define a function that encodes the PDE.
# The function takes three arguments:
# 1. A named tuple giving the current value of the state.
# 2. A named tuple giving the value function(s) (as well as its derivatives)
# at the current value of the state.
# 3. (Optional) Current time t
# It must return a named tuple with the time derivatives
function f(state::NamedTuple, sol::NamedTuple)
μbar = 0.018 ; ϑ = 0.00073 ; θμ = 0.252 ; νμ = 0.528 ; ρ = 0.025 ; ψ = 1.5 ; γ = 7.5
μ = state.μ
V, Vμ_up, Vμ_down, Vμμ = sol.V, sol.Vμ_up, sol.Vμ_down, sol.Vμμ
# note that pdesolve will directly compute the derivatives of the valuefunction.
# up and down correspond to the upward and downard derivative obtained by first difference
Vμ = (μ <= μbar) ? Vμ_up : Vμ_down
Vt = - (1 - ρ * V + (1 - 1 / ψ) * (μ - 0.5 * γ * ϑ) * V + θμ * (μbar - μ) * Vμ +
0.5 * νμ^2 * ϑ * Vμμ + 0.5 * (1 / ψ - γ) / (1- 1 / ψ) * νμ^2 * ϑ * Vμ^2/V)
(Vt = Vt,)
end
# The function `pdesolve` takes four arguments:
# 1. the function encoding the PDE
# 2. the discretized state space
# 3. the terminal value function
# 4. (Optional) a time grid
ys, residual_norms = pdesolve(f, stategrid, solend, range(0, 1000, length = 100))
# To solve directly for the stationary solution,
# i.e. the solution of the PDE with ∂tV = 0,
# simply omit the time grid
y, residual_norm = pdesolve(f, stategrid, solend)
More complicated ODEs / PDES (including PDE with two state variables or systems of multiple PDEs) can be found in the examples folder.
Boundary Conditions
When solving a PDE using a finite scheme approach, one needs to specify the value of the solution outside the grid ("ghost node") to construct the second derivative and, in some cases, the first derivative at the boundary.
By default, the values at the ghost node is assumed to equal the value at the boundary node (reflecting boundaries). Specify different values for values at the ghost node using the option bc (see BoltonChenWang.jl for an example).
EconPDEs v1.0.0
The 1.0 release has the following set of breaking changes:
- The first argument of pdesolve (the function encoding the PDE) must accept directional first derivatives and it must return a named tuple of time derivatives
- The fourth argument of pdesolve (the time grid) must be in increasing order
- pdesolve now returns a type with fieldnames zero (for the solution) and residual_norm (for the norm of residuals)
See the updated examples for the new syntax.
Optimal Stopping
Optimal stopping problems are also supported, as exemplified in Leland.jl. These problems are solved with "HJB variational inequality" (HJBVI), i.e.:
where S(x) is the value of exercising the option. Notice the traditional "value matching" (S(x̲)=v(x̲)) and "smooth pasting" (S'(x̲)=v'(x̲)) conditions are implied in the HJBVI formulation. See the deck of notes from Ben Moll on stopping time problems for more details. The S(x) can be provided to the solver as a vector defined on the grid via the keyword y̲ (or ȳ as the upper bound for cost minimization problems).
Examples
The examples folder solves a variety of models:
- Habit Model: Campbell Cochrane (1999) and Wachter (2005)
- Long Run Risk Model: Bansal Yaron (2004)
- Disaster Model: Wachter (2013)
- Heterogeneous Agent Models: He Krishnamurthy (2013), Brunnermeir Sannikov (2013), Garleanu Panageas (2015), Di Tella (2017), Haddad (JMP)
- Consumption with Borrowing Constraint: Wang Wang Yang (2016), Achdou Han Lasry Lions Moll (2018)
- Investment with Borrowing Constraint: Bolton Chen Wang (2009)
- Growth Model: deterministic neoclassical (Ramsey) growth model, on a grid centered on its closed-form steady state
Installation
The package is registered in the General registry and so can be installed at the REPL with ] add EconPDEs.
Citation
If you use EconPDEs.jl in your work, please cite it. You can use the "Cite this repository" button on the GitHub page (generated from CITATION.cff), or the following BibTeX entry:
@software{EconPDEs.jl,
author = {Gomez, Matthieu},
title = {{EconPDEs.jl}: {Solving PDEs in economics}},
url = {https://github.com/matthieugomez/EconPDEs.jl},
}
Similar Projects
Discover other similar projects and AI models.
An acausal modeling framework for automatically parallelized scientific machine learning (SciML) in Julia. A computer algebra system for integrated symbolics for physics-informed machine learning and automated transformations of differential equations
Scientific machine learning (SciML) benchmarks, AI for science, and (differential) equation solvers. Covers Julia, Python (PyTorch, Jax), MATLAB, R
Universal modeling and simulation of fluid mechanics upon machine learning. From the Boltzmann equation, heading towards multiscale and multiphysics flows.
ETH course - Solving PDEs in parallel on GPUs Fall 2026
GPU-capable dynamical core for the CliMA Earth System Model: spectral-element and finite-difference discretization tools