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.

Metadata

Release files for longstaff-schwartz 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for longstaff-schwartz 0.2.0
File Size Uploaded Provenance Actions
longstaff_schwartz-0.2.0.tar.gz 18.3 kB Without provenance Details for longstaff_schwartz-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for longstaff-schwartz 0.2.0
File Interpreter ABI Platform Actions
longstaff_schwartz-0.2.0-py2.py3-none-any.whl Python 3, Python 2 none any Details for longstaff_schwartz-0.2.0-py2.py3-none-any.whl

Total release size: 27.3 kB

Release files / longstaff_schwartz-0.2.0.tar.gz

Download URL longstaff_schwartz-0.2.0.tar.gz
Size 18.3 kB
Tags Source
SHA-256 checksum
How to use checksums
f84cec73c279c382355d03caa1e038e42850d7368c4aba006f2ca9488b290d4f
BLAKE2b-256 checksum
How to use checksums
a7c79486bd08357f270eab3e462e457e487ca1285ee62d00fbb3674c9de980dc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.8.13

Release files / longstaff_schwartz-0.2.0-py2.py3-none-any.whl

Download URL longstaff_schwartz-0.2.0-py2.py3-none-any.whl
Size 9.0 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
c5e8b3117be0336d1d14fa8876d5ed9dca1100c29daa21560c6470401a8d8be5
BLAKE2b-256 checksum
How to use checksums
6f7a5fc68515f724e87b7f7931ba79e57b912aa963a2bc262d3a62759f0b5cd3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.8.13

Security for longstaff-schwartz 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page