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

## Longstaff-Schwartz Algorithm   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

### 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
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,

# European put option for comparison
npv_european = constant_rate_df(t, 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 #### Favourable exercise  ### 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.

## History

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.

## Project details

This version 0.1.1 0.1.0

Uploaded `source`
Uploaded `py2` `py3`