Skip to main content

Longstaff-Schwartz Algorithm

https://img.shields.io/pypi/v/longstaff_schwartz.svg https://github.com/luphord/longstaff_schwartz/actions/workflows/build-test.yml/badge.svg Documentation Status

A Python implementation of the Longstaff-Schwartz linear regression algorithm for the evaluation of call rights and American options.

  • Seminal paper: Francis A. Longstaff, Eduardo S. Schwartz, Valuing American Options by Simulation: A Simple Least-Squares Approach (The Review of Financial Studies) (2001) Vol 14, No 1, pp. 113-147

  • Documentation: https://longstaff-schwartz.readthedocs.io

  • Free software: MIT license

Talks

Usage

from longstaff_schwartz.algorithm import longstaff_schwartz
from longstaff_schwartz.stochastic_process import GeometricBrownianMotion
import numpy as np

# Model parameters
t = np.linspace(0, 5, 100)  # timegrid for simulation
r = 0.01  # riskless rate
sigma = 0.15  # annual volatility of underlying
n = 100  # number of simulated paths

# Simulate the underlying
gbm = GeometricBrownianMotion(mu=r, sigma=sigma)
rnd = np.random.RandomState(1234)
x = gbm.simulate(t, n, rnd)  # x.shape == (t.size, n)

# Payoff (exercise) function
strike = 0.95

def put_payoff(spot):
    return np.maximum(strike - spot, 0.0)

# Discount factor function
def constant_rate_df(t_from, t_to):
    return np.exp(-r * (t_to - t_from))

# Approximation of continuation value
def fit_quadratic(x, y):
    return np.polynomial.Polynomial.fit(x, y, 2, rcond=None)

# Selection of paths to consider for exercise
# (and continuation value approxmation)
def itm(payoff, spot):
    return payoff > 0

# Run valuation of American put option
npv_american = longstaff_schwartz(x, t, constant_rate_df,
                                  fit_quadratic, put_payoff, itm)

# European put option for comparison
npv_european = constant_rate_df(t[0], t[-1]) * put_payoff(x[-1]).mean()

# Check results
assert np.round(npv_american, 4) == 0.0734
assert np.round(npv_european, 4) == 0.0626
assert npv_american > npv_european

Plots

For details see PyData Meetup Jupyter Notebook.

Approximation of continuation value

docs/_static/approximated-continuation-and-exercise-value-1.svg

Favourable exercise

docs/_static/exercise-or-hold.svg docs/_static/first-exercises.svg

Credits

Main developer is luphord.

Primary source for the algorithm is Francis A. Longstaff, Eduardo S. Schwartz, Valuing American Options by Simulation: A Simple Least-Squares Approach (The Review of Financial Studies) (2001) Vol 14, No 1, pp. 113-147. There is no affiliation between the authors of the paper and this code.

This package was prepared with Cookiecutter and the audreyr/cookiecutter-pypackage project template.

History

0.2.0 (2022-11-10)

  • Drop support for Python 3.7

  • Add support for Python 3.9 and 3.10

  • Upgrade dependencies

  • Reformat code with black

  • Migrate from travis-ci.com to GitHub actions

  • Upgrade development status to alpha

0.1.1 (2020-12-01)

  • Support Python 3.8

  • Migrate to travis-ci.com

  • Increase number of simulated paths in example to prevent poor conditioning warning

0.1.0 (2019-10-03)

  • First release on PyPI.

Download files

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

Source Distribution

longstaff_schwartz-0.2.0.tar.gz (18.3 kB view details)

Uploaded Source

Built Distribution

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

longstaff_schwartz-0.2.0-py2.py3-none-any.whl (9.0 kB view details)

Uploaded Python 2Python 3

File details

Details for the file longstaff_schwartz-0.2.0.tar.gz.

File metadata

  • Download URL: longstaff_schwartz-0.2.0.tar.gz
  • Upload date:
  • Size: 18.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.13

File hashes

Hashes for longstaff_schwartz-0.2.0.tar.gz
Algorithm Hash digest
SHA256 f84cec73c279c382355d03caa1e038e42850d7368c4aba006f2ca9488b290d4f
MD5 8c5c7d21c40e285ea7992b2e9512949f
BLAKE2b-256 a7c79486bd08357f270eab3e462e457e487ca1285ee62d00fbb3674c9de980dc

See more details on using hashes here.

File details

Details for the file longstaff_schwartz-0.2.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for longstaff_schwartz-0.2.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 c5e8b3117be0336d1d14fa8876d5ed9dca1100c29daa21560c6470401a8d8be5
MD5 1afd5bf08a78ad7e9af3b8000b52df11
BLAKE2b-256 6f7a5fc68515f724e87b7f7931ba79e57b912aa963a2bc262d3a62759f0b5cd3

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 files

0.1.1

2 files

0.1.0

2 files

Supported by

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