FinancialDerivatives Overview

A binomial tree grows from one point, turns into random price paths, and the path ends form a bell curve

FinancialDerivatives — Module

Compute the fair value of financial derivatives.

The package has the following object:

  • FinancialInstrument: features that characterizes the contract, for example the strike and the expiry.
  • AbstractMarketData: the market observables, for example the spot price and the volatility.
  • Models.Model: the stochastic dynamics mathematical modelling of the underlying, and the parameters of the dynamics.
  • PricingEngine: the numerical method and its settings, for example the number of tree steps.

price is the only pricing function:

price(engine, instrument, model, market_data)

Multiple dispatch selects the method from the types of the four arguments. The closed-form formula, the binomial tree and the Monte Carlo simulation of the lognormal models are generic methods. A generic method accepts each model that implements the functions that the method calls. A closed-form formula of one model, for example the Heston model, has one method for its combination of types.

simulate returns the simulated paths of a model:

simulate(engine, model, market_data, t)

Design patterns

The package is based on the following software design patterns:

  • Strategy: each algorithm has its own type, and one common function runs all the algorithms. Here, the engine and the model define the computational strategies, and price is the common function. The caller passes them to price to select the algorithm. The engine also holds the simulation settings of its algorithm, for example steps or num_paths. The BinomialScheme is a strategy of the binomial engine: the scheme selects the moves of the tree.
  • Bridge: the description of an object and the computation on the object are in two independent type hierarchies. Each hierarchy can change without a change to the other one. Here, the instrument types are the description, and the engine types are the computation. Unlike the classic Bridge, the instrument does not hold a reference to the engine.
  • Template method: one generic method holds the steps of an algorithm, and each type supplies only the steps that are different. Here, the closed-form method, the tree method and the Monte Carlo method of the lognormal models are generic. The instrument supplies payoff, the model supplies growth_rate, and the scheme supplies the moves of the tree. simulate is also generic: a short-rate model supplies drift and diffusion. Unlike the classic Template method, the types supply the steps through multiple dispatch, not through inheritance.
  • Facade: one module gives the caller a single point of access to many submodules. After using FinancialDerivatives, the caller does not need the names of the submodules.

The package also applies the dependency inversion principle to the market data, the models, and the schemes. The engines read them only through functions, and do not read their fields. For example, the engines read the market data through the accessors, such as spot and discount_rate. If a new type implements those functions, the engines accept the new type. The instruments are different: the engines read the fields k and t of an option. A new option type must have those fields, and must have payoff and isamerican methods.

Examples

These calls compute the value of the same option with three different algorithms.

option = AmericanOption(90.0, 0.5, false)
md = EquityMarketData(; spot_price=100.0, risk_free_rate=0.05, dividend_yield=0.0, volatility=0.3)

price(BinomialEngine(; scheme=CoxRossRubinstein()), option, BlackScholes(), md)
price(BinomialEngine(; scheme=JarrowRudd()), option, BlackScholes(), md)
price(MonteCarloEngine(), option, BlackScholes(), md)
source

Architecture

The documentation build makes this diagram from the types and the methods of the package. The diagram does not show the combinations of types that price accepts.

classDiagram direction LR namespace Engines { class AnalyticEngine { price() } class BinomialEngine { steps::Int64 scheme::BinomialScheme price() } class BinomialScheme { <<abstract>> tree_steps() } class CoxRossRubinstein { tree_parameters() } class FiniteDifferenceEngine { } class JarrowRudd { tree_parameters() } class LeisenReimer { tree_parameters() tree_steps() } class MonteCarloEngine { num_paths::Int64 num_steps::Int64 rng price() simulate() } class PricingEngine { <<abstract>> } class RendlemanBartterTree { tree_parameters() } class Tian { tree_parameters() } } namespace Instruments { class Actual365Fixed { } class AmericanOption { k::Real t::Real call::Bool payoff() } class AsianOption { k::Real t::Real call::Bool } class Business252 { calendar } class DayCount { <<abstract>> } class Derivative { <<abstract>> } class EuropeanOption { k::Real t::Real call::Bool payoff() } class FXOption { k::Real t::Real call::Bool payoff() } class FinancialInstrument { <<abstract>> } class Forward { <<abstract>> } class Future { <<abstract>> } class Option { <<abstract>> isamerican() iscall() isput() } class Swap { <<abstract>> } class Swaption { <<abstract>> } } namespace MarketData { class AbstractMarketData { <<abstract>> discount_factor() } class EquityMarketData { spot_price::Real risk_free_rate dividend_yield::Real volatility discount_rate() dividend_yield() spot() volatility() } class FXMarketData { spot_price::Real domestic_rate foreign_rate volatility discount_rate() foreign_rate() spot() volatility() } class InterestRateMarketData { short_rate::Real short_rate() } } namespace Models { class LognormalModel { <<abstract>> initial_state() transition() } class Model { <<abstract>> } class OneFactorModel { <<abstract>> transition() } class PricingModel { <<abstract>> } class ShortRateModel { <<abstract>> initial_state() } class BlackKarasinski { k::Real θ::Real σ::Real diffusion() drift() transition() } class BlackScholes { growth_rate() } class BrennanSchwartz { k::Real θ::Real σ::Real diffusion() drift() } class CoxIngersollRoss { k::Real θ::Real σ::Real diffusion() drift() } class GarmanKohlhagen { growth_rate() } class HestonModel { v::Real v̄::Real κ::Real σ::Real ρ::Real } class RendlemanBartter { θ::Real σ::Real diffusion() drift() } class Vasicek { k::Real θ::Real σ::Real diffusion() drift() } } PricingEngine <|-- AnalyticEngine PricingEngine <|-- BinomialEngine BinomialEngine *-- BinomialScheme : scheme BinomialScheme <|-- CoxRossRubinstein PricingEngine <|-- FiniteDifferenceEngine BinomialScheme <|-- JarrowRudd BinomialScheme <|-- LeisenReimer PricingEngine <|-- MonteCarloEngine BinomialScheme <|-- RendlemanBartterTree BinomialScheme <|-- Tian DayCount <|-- Actual365Fixed Option <|-- AmericanOption Option <|-- AsianOption DayCount <|-- Business252 FinancialInstrument <|-- Derivative Option <|-- EuropeanOption Option <|-- FXOption Derivative <|-- Forward Derivative <|-- Future Derivative <|-- Option Derivative <|-- Swap Derivative <|-- Swaption AbstractMarketData <|-- EquityMarketData AbstractMarketData <|-- FXMarketData AbstractMarketData <|-- InterestRateMarketData OneFactorModel <|-- LognormalModel PricingModel <|-- Model Model <|-- OneFactorModel OneFactorModel <|-- ShortRateModel ShortRateModel <|-- BlackKarasinski LognormalModel <|-- BlackScholes ShortRateModel <|-- BrennanSchwartz ShortRateModel <|-- CoxIngersollRoss LognormalModel <|-- GarmanKohlhagen Model <|-- HestonModel ShortRateModel <|-- RendlemanBartter ShortRateModel <|-- Vasicek

Contents