Skip to main content

Solve nonlinear perfect foresight models

Project description

Contains simple tools to simulate perfect foresight models. The method is similar to the one introduced in Boehl & Hommes (2021), where we use it to solve for chaotic asset price dynamics. It can be understood as a policy function iteration where the initial state is the only fixed grid point and all other grid points are chosen endogenously (as in a “reverse” EGM) to map the expected trajectory.

The main advantage (in terms of robustness) over Fair-Taylor comes from exploiting the property that most determined perfect forsight models be a contraction mapping both, forward and backwards. The model is given by

f(x_{t-1}, x_t, x_{t+1}) = 0.

We iterate on the expected trajectory itself instead of the policy function. We hence require

d f(x_{t-1}, x_t, x_{t+1} ) < d x_{t-1},
d f(x_{t-1}, x_t, x_{t+1} ) < d x_{t+1}.

This is also the weakness of the method: not every DSGE model (that is Blanchard-Kahn determined) sense is such backward-and-forward contraction. In most cases the algorithm converges anyways, but convergence is not guaranteed.

The code is in alpha state and provided for reasons of collaboration, replicability and code sharing in the spirit of open science. You are welcome to get in touch if you are interested working with the package.

Documentation

There is no formal documentation (yet). Check this documentation for easy-going installation instructions (using econpizza instead of pydsge). The package optionally depends on the grgrlib package to check for determinancy, so maybe also install that one since you are already at it.

An small-scale nonlinear New Keynesian model with ZLB is provided as an example. Here is how to simulate it and plot some nonlinear impulse responses:

import numpy as np
import matplotlib.pyplot as plt
import econpizza as ep
from econpizza import example_nk

# load the example. The steady state is automatically solved for
# example_nk is nothing else but the path to the yaml, hence you could also use `filename = 'path_to/model.yaml'`
mod = ep.load(example_nk)

# get the steady state as an initial state
state = mod['stst'].copy()
# increase the discount factor by one percent
state['beta'] *= 1.02

# simulate the model
x, _, flag = ep.find_path(mod, state.values())

# plotting
for i,v in enumerate(mod['variables']):

    plt.figure()
    plt.plot(x[:,i])
    plt.title(v)

The impulse responses are the usual dynamics of a nonlinear DSGE.

The yaml files follow a simple structure:

  1. define all variables and shocks

  2. provide the nonlinear equations. Note that the dash at the beginning of each line is not a minus!

  3. provide the parameters and values.

  4. optionally provide some steady state values and/or values for initial guesses

  5. optionally provide some auxilliary equations that are not directly part of the nonlinear system (see the yaml for the BH model)

Lets go for a second, numerically more challenging example: the chaotic rational expectations model of Boehl & Hommes (2021)

import numpy as np
import matplotlib.pyplot as plt
import econpizza as ep
from econpizza import example_bh

# parse the yaml
mod = ep.load(example_bh, raise_errors=False)
# B-K conditions will complain because the model is not determined around the steady state. This is not a problem

# choose an interesting initial state
state = np.zeros(len(mod['variables']))
state[:-1] = [.1, .2, 0.]

# solve and simulate. The lower eps is not actually necessary
x, _, flag = ep.find_path(mod, state, T=1000, max_horizon=1000, tol=1e-8)

# plotting
for i,v in enumerate(mod['variables']):

    plt.figure()
    plt.plot(x[:,i])
    plt.title(v)

This will give you:

Dynamics of prices and fractions

Finally the package also provides a stacking algorithm for all those problems that cannot be solved nicely with the default pizza method. Here is an example, also introducing the use of shocks and the linear solution (which is always calculated on-the-fly):

import numpy as np
import matplotlib.pyplot as plt
import econpizza as ep
from econpizza import example_nk


# use the NK model again
mod = ep.load(example_nk)

# increase the discount factor by .02 (this is NOT percentage deviation!)
shk = ('e_beta', .02)

# use the stacking method. As above, you could also feed in the initial state instead
x, x_lin, flag = ep.find_path_stacked(mod, shock=shk)

# plotting. x_lin is the linearized first-order solution
for i,v in enumerate(mod['variables']):

    plt.figure()
    plt.plot(x[:,i])
    plt.plot(x_lin[:,i])
    plt.title(v)

Citation

econpizza is developed by Gregor Boehl to simulate nonlinear perfect foresight models. Please cite it with

@techreport{boehl2021rational,
title         = {Rational vs. Irrational Beliefs in a Complex World},
author        = {Boehl, Gregor and Hommes, Cars},
year          = 2021,
institution   = {IMFS Working Paper Series}
}

We appreciate citations for econpizza because it helps us to find out how people have been using the package and it motivates further work.

References

Boehl, Gregor and Hommes, Cars (2021). Rational vs. Irrational Beliefs in a Complex World. IMFS Working papers

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

econpizza-0.0.1.tar.gz (38.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

econpizza-0.0.1-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

Details for the file econpizza-0.0.1.tar.gz.

File metadata

  • Download URL: econpizza-0.0.1.tar.gz
  • Upload date:
  • Size: 38.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for econpizza-0.0.1.tar.gz
Algorithm Hash digest
SHA256 b06de2e5f0a610e54d78131ce4ba273983af8ccbf91110379aada592f2b3c8e7
MD5 c4da6c1e3d3aa6c2583efa1e0cd5946d
BLAKE2b-256 1796bd62fb3d1e97e74fa1780b53e532dd264662f11dee6c9b7915410c42dce8

See more details on using hashes here.

File details

Details for the file econpizza-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: econpizza-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 16.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for econpizza-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3bc7b6d32b08a6d6d2b8c6b6298538001c42b1fa6c5c253f55dbdeac39c90ad8
MD5 0f03cad20128fff95bb5f69e95db2218
BLAKE2b-256 624b5d917cadee6bee7fc9a3d88b05db631303006e8afba7c6f27adbc5462181

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page