Skip to main content
Join the official 2019 Python Developers SurveyStart the survey!

Economic Scenario Generator in Python

Project description


Economic Scenario Generator for Python.


I'm not aware of any comprehensive ESG library available for Python today. I think such a library should have the following components:

  1. A suite of stochastic models - inspired by the "fit"/"predict" scikit-learn API
    • that are easy to calibrate ("fit") using historical data, or to manually provide parameters.
    • that can simulate future economic paths ("predict")
  2. A suite of model and scenario evaluation tools to
    • evaluate the goodness of fit of models
    • calculate significance measures of generated scenarios (to select subsets of scenarios if desired)
  3. Replicate the existing SOA and AAA Excel generator so actuarial teams can migrate to Python.
  4. Minimal dependencies - relying on the standard scientific python stack: numpy, pandas, and scipy, with optional plotting from matplotlib.

I expect that these objectives may shift or expand as I continue working on the library. Please let me know if you think anything is missing!


You can install pyesg from the command prompt with the following:

pip install pyesg


Open Source and licensed under MIT, Copyright (c) 2019 Jason Ash


Vasicek Interest Rate Model

The project is in its early stages, but I've recently implemented the Vasicek interest rate model. At the moment, the API looks like this.

import pyesg

# load a dataset of historical US Treasury rates, contained in pyesg.datasets
# ust is a pandas dataframe of rates for various maturities, indexed by year and month
ust = pyesg.datasets.load_ust_historical()

# for this example, we'll train on the following data:
# y - the 3-month US treasury rate
# X - the time value of each observation, starting at zero, increasing by monthly steps
y = ust.loc['3-month'].values
X = np.full(len(y), 1/12).cumsum()

# create a vasicek model object, just like you would create an estimator model from sklearn
vasicek = pyesg.Vasicek()

# fit the model by passing the X and y vectors; the model is now trained, y)

# sample future paths from the model
# specify the number of scenarios (e.g. 1000)
# length of projection (e.g. 30 years)
# and time step (e.g. weekly, or 52 time-steps per year)
scenarios = vasicek.sample(size=(1000, 30, 52))

Nelson-Siegel and Nelson-Siegel-Svensson Curve Interpolators

We can almost always observe interest rates at key maturities, for example, bonds trading with maturies of 1, 2, 3, 5, 7, or 10 years. If we want to estimate the interest rate for an 8-year bond, we need to interpolate between the observed values. Simple techniques like linear interpolation are possible, but have certain obvious disadvantages - namely that the interest rate curve is non-linear. Instead, better techniques like the Nelson-Siegel and Nelson-Siegel-Svensson interpolators might give better results. Both interpolators are availabe in pyesg.

import pyesg
import matplotlib.pyplot as plt

# load a dataset of historical US Treasury rates, contained in pyesg.datasets
# ust is a pandas dataframe of rates for various maturities, indexed by year and month
ust = pyesg.datasets.load_ust_historical()

# we will be interpolating rates from the file:
# y - the observed US Treasury rate for the given maturity for a select observation date
# X - the maturity of the bond measured in years
y = data.iloc[-10].values
X = np.array([0.25, 0.5, 1, 2, 3, 5, 7, 10, 20, 30])

# create Nelson-Siegel and Nelson-Siegel-Svensson interpolator objects
# then fit both models using the historical data
ns = pyesg.NelsonSiegel()
nss = pyesg.NelsonSiegelSvensson(), y), y)

Project details

Download files

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

Files for pyesg, version 0.1.1
Filename, size File type Python version Upload date Hashes
Filename, size pyesg-0.1.1-py3-none-any.whl (29.0 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size pyesg-0.1.1.tar.gz (194.3 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page