项目作者: donlelef

项目描述 :
Stochastic models to price financial options
高级语言: Python
项目地址: git://github.com/donlelef/vanilla-option-pricing.git
创建时间: 2018-09-07T15:46:34Z
项目社区:https://github.com/donlelef/vanilla-option-pricing

开源协议:MIT License

下载


Vanilla Option Pricing

Actions Status
codecov
Documentation Status
Downloads
DOI

A Python package implementing stochastic models to price financial options.
The theoretical background and a comprehensive explanation of models and their parameters
can be found is the paper Fast calibration of two-factor models for energy option pricing
by Emanuele Fabbiani, Andrea Marziali and Giuseppe De Nicolao, freely available on arXiv. A software paper describing the repository can be found in Software Impact.

Installing

The preferred way to install the package is using pip,
but you can also download the code and install from source

To install the package using pip:

  1. pip install vanilla_option_pricing

Quickstart

Let’s create a call option.

  1. from datetime import datetime, timedelta
  2. from vanilla_option_pricing.option import VanillaOption
  3. option = VanillaOption(
  4. spot=100,
  5. strike=101,
  6. dividend=0,
  7. date=datetime.today(),
  8. maturity=datetime.today() + timedelta(days=30),
  9. option_type='c',
  10. price=1,
  11. instrument='TTF'
  12. )

We can compute the implied volatility and create a Geometric Brownian Motion
model with it. Of course, if now we ask price the option using the Black framework,
we’ll get back the initial price.

  1. from vanilla_option_pricing.models import GeometricBrownianMotion
  2. volatility = option.implied_volatility_of_undiscounted_price
  3. gbm_model = GeometricBrownianMotion(volatility)
  4. gbm_price = gbm_model.price_option_black(option)
  5. print(f'Actual price: {option.price}, model price: {gbm_price}')

But, if we adopt a different model, say a Log-spot price mean reverting to
generalised Wiener process model (MLR-GW), we will get a different price.

  1. import numpy as np
  2. from vanilla_option_pricing.models import LogMeanRevertingToGeneralisedWienerProcess
  3. p_0 = np.eye(2)
  4. model = LogMeanRevertingToGeneralisedWienerProcess(p_0, 1, 1, 1)
  5. lmrgw_price = model.price_option_black(option)
  6. print(f'Actual price: {option.price}, model price: {lmrgw_price}')

In the previous snippet, the parameters of the LMR-GW model were chosen
at random. We can also calibrate the parameters of a model against
listed options.

  1. from datetime import date
  2. from vanilla_option_pricing.option import VanillaOption
  3. from vanilla_option_pricing.models import OrnsteinUhlenbeck, GeometricBrownianMotion
  4. from vanilla_option_pricing.calibration import ModelCalibration
  5. data_set = [
  6. VanillaOption('TTF', 'c', date(2018, 1, 1), 2, 101, 100, date(2018, 2, 1)),
  7. VanillaOption('TTF', 'p', date(2018, 1, 1), 2, 98, 100, date(2018, 2, 1)),
  8. VanillaOption('TTF', 'c', date(2018, 1, 1), 5, 101, 100, date(2018, 5, 31))
  9. ]
  10. models = [
  11. GeometricBrownianMotion(0.2),
  12. OrnsteinUhlenbeck(p_0=0, l=100, s=2)
  13. ]
  14. calibration = ModelCalibration(data_set)
  15. print(f'Implied volatilities: {[o.implied_volatility_of_undiscounted_price for o in data_set]}\n')
  16. for model in models:
  17. result, trained_model = calibration.calibrate_model(model)
  18. print('Optimization results:')
  19. print(result)
  20. print(f'Calibrated parameters: {trained_model.parameters}\n\n')