Type and function reference
Miletus.AcquisitionDateObs
Miletus.AmericanCall
Miletus.AmericanPut
Miletus.Amount
Miletus.Anytime
Miletus.At
Miletus.Before
Miletus.BinomialGeomRWModel
Miletus.Both
Miletus.CRRModel
Miletus.Cond
Miletus.ConstObs
Miletus.CoreForwardModel
Miletus.CoreModel
Miletus.DateObs
Miletus.DiscreteSwingContract
Miletus.Either
Miletus.EuropeanCall
Miletus.EuropeanPut
Miletus.GeomBMModel
Miletus.GeomOUModel
Miletus.Give
Miletus.HullWhiteTrinomialModel
Miletus.LatticeStateContract
Miletus.MaskedToepelitzTransition
Miletus.MonteCarloModel
Miletus.MonteCarloScenario
Miletus.SABRModel
Miletus.Scale
Miletus.SplineInterpolation
Miletus.SplineVolatilityModel
Miletus.TianModel
Miletus.Until
Miletus.When
Miletus.YieldModel
Miletus.Zero
Miletus._black
Miletus._black_erfcx
Miletus._black_ivol
Miletus._black_normcdf
Miletus._black_small_t
Miletus._black_vega
Miletus._cblack
Miletus.black
Miletus.black_ivol
Miletus.date2index
Miletus.delta
Miletus.fit_ivol
Miletus.fit_ivol
Miletus.gamma
Miletus.greek
Miletus.greek
Miletus.householder_update
Miletus.interpolate
Miletus.ivol
Miletus.ivol
Miletus.maturitydate
Miletus.montecarlo
Miletus.poscubicroot
Miletus.powm1
Miletus.price
Miletus.rational_cubic_interpolation
Miletus.rho
Miletus.sabr_alpha
Miletus.scenarios
Miletus.strikeprice
Miletus.vega
Miletus.AcquisitionDateObs
— TypeAcquisitionDateObs() <: Observable{Date}
The acquisition date of the contract.
Miletus.AmericanCall
— TypeAmericanCall(date, c, strike)
An American call contract, with maturity date
, on underlying contract c
at price strike
.
Miletus.AmericanPut
— TypeAmericanPut(date, c, strike)
An American put contract, with maturity date
, on underlying contract c
at price strike
.
Miletus.Amount
— TypeAmount(o::Observable) <: Contract
Receive amount of o
.
Miletus.Anytime
— TypeAnytime(p::Observable{Bool}, c::Contract)
May acquire c
at any point when p
is true
.
Miletus.At
— TypeAt(t::Date) <: Observable{Bool}
An observable that is true
when the date is t
.
Miletus.Before
— TypeBefore(t::Date) <: Observable{Bool}
An observable that is true
when the date is before or equal to t
.
Miletus.BinomialGeomRWModel
— TypeBinomialGeomRWModel
A model for a Binomial Geometric Random Walk (aka Binomial tree).
Arguments:
startdate
: start date of processenddate
: start date of processnsteps
: number of steps in the treeS₀
: inital valueΔt
: the time-difference between steps, typicallydays(startdate - enddate) / (365*nsteps)
iR
: discount rate,exp(-Δt*interestrate)
u
: scale factor for upd
: scale factor for downp
: up probabilityq
: down probability,1-p
Miletus.Both
— TypeBoth(c1::Contract, c2::Contract)
Acquire both contracts c1
and c2
(and
in PJE2000).
Miletus.CRRModel
— MethodCRRModel
Cox-Ross-Rubenstein binomial model.
Miletus.Cond
— TypeCond(p::Observable{Bool}, c1::Contract, c2::Contract)
If p
is true at the point of aquisition, acquire c1
else acquire c2
.
Miletus.ConstObs
— TypeConstObs(x)
A constant observable
Miletus.CoreForwardModel
— TypeCoreForwardModel(forwarddates, forwardprices, yieldcurve)
Specifies the value of a SingleStock
in terms of its forward prices.
forwarddates
: ordered dates for which forward prices are availableforwardprices
: forward prices of the assetyieldcurve
: risk-free discount curve
Miletus.CoreModel
— TypeCoreModel(startdate, startprice, interestrate, carryrate)
Contains the core parameters for the model of SingleStock
:
startprice
: initial price atstartdate
yieldcurve
: risk free rate of return for the numeraire.carrycurve
: the carry rate, i.e. the net income for holding the asset:- for stocks this is typically positive (i.e. dividends)
- for commodities this is typically negative (i.e. cost-of-carry)
The yieldcurve
and carryrate
are all specified on a continously compounded, Actual/365 basis.
The exact dynamics of the model are unspecified, but has the property that
\[E[S_t] = S_0 \times e^{(r-q)t}\]
Miletus.DateObs
— TypeDateObs() <: Observable{Date}
A singleton type representing the "free" date observable.
Miletus.DiscreteSwingContract
— TypeDiscreteSwingContract
Represents a swing contract. The arguments are:
default
: this is the contract that is executed on each date if no swing occursalternate
: this is the contract that is executed if the swing occurspenalty
: this contract is executed on the last exercise date, and is scaled by the number of swings below the minimum (nxmin
)exdates
: the exercise dates of the swing optionnxmin
: the minimum number of swings (thepenalty
contract is incurred if the total number of swings is below this number)nxmax
: the maximum number of swings that may be taken
Miletus.Either
— TypeEither(c1::Contract, c2::Contract)
Acquire either contract c1
or c2
(or
in PJE2000).
Miletus.EuropeanCall
— TypeEuropeanCall(date, c, strike)
A European call contract, with maturity date
, on underlying contract c
at price strike
.
Miletus.EuropeanPut
— TypeEuropeanPut(date, c, strike)
A European put contract, with maturity date
, on underlying contract c
at price strike
.
Miletus.GeomBMModel
— TypeGeomBMModel(startdate, startprice, interestrate, carryrate, volatility)
A model for SingleStock
, following a geometric Brownian motion.
startdate
startprice
: initial price atstartdate
interestrate
: risk free rate of return.carryrate
: the carry rate, i.e. the net return for holding the asset:- for stocks this is typically positive (i.e. dividends)
- for commodities this is typically negative (i.e. cost-of-carry)
volatility
:
The interestrate
, carryrate
and volatility
are all specified on a continously compounded, Actual/365 basis.
The price is assumed to follow the PDE:
\[d S_t = \kappa S_t dt + \sigma S_t dW_t\]
or
\[d \log S_t = (\kappa - \sigma^2/2) dt + \sigma dW_t\]
where $W_t$ is a Wiener process, and κ = interestrate - carryrate
.
Miletus.GeomOUModel
— TypeGeomOUModel
Assumes that the stock price follows a geometric Ornstein–Uhlenbeck process
\[\frac{dS_t}{S_t} = [k(t) - \theta \log S_t] dt + \sigma dW_t\]
where the parameters:
- $\theta$ is the rate of mean reversion (sometimes called "speed")
- $\sigma$ is the OU volatility
and $k(t)$ is chosen based on the forward curve so that $S_t$ is risk-neutral. This is equivalent to the "one factor model" of Clewlow and Strickland (1999), with dynamics on the forward price of
\[\frac{d F_{t,T}}{F_{t,T}} = \sigma e^{- heta (T-t)} dW_t\]
where $F_{t,T}$ is the forward price of maturity date $T$ at current time $t$.
- Clewlow, L. and Strickland, C. (1999) "Valuing Energy Options in a One Factor Model Fitted to Forward Prices". https://ssrn.com/abstract=160608
Miletus.Give
— TypeGive(c::Contract)
Take the opposite side of contract c
.
Miletus.HullWhiteTrinomialModel
— TypeHullWhiteTrinomialModel
This is a Hull-White trinomial model, a discrete approximation of a GeomOUModel
. The arguments are:
m::GeomOUModel
: the model to approximatedates::AbstractVector{Date}
(optional): the dates at which to compute the approximation. By default, the forward dates are used (if available).k::Int
(default=1): the number of discrete steps per day to usecap::Int
(default=0.2): a parameter between 0.184 and 0.577 used to determine how to truncate the branching (larger values give less truncation).
Miletus.LatticeStateContract
— TypeLatticeStateContract
Represents a contract with multiple states, where at each exercise date the holder may move up or down states. This can represent a variety of multiple exercise contracs, such as storage options.
transition
: a square matrix of contracts: at each exercise date, the contracttransition[from, to]
is exercised when transitioning from statefrom
toto
.ends
: a vector of contracts exercised at the final exercise date. Thei
th contract is exercised if the contract is in statei
after the final exercise.exdates
: the exercise dates of the contractinitstate
: the initial state of the contract (default =1
).
Note that ups
and downs
must be of the same length, and ends
must be 1 greater (being the number of states in the lattice).
Miletus.MaskedToepelitzTransition
— TypeMaskedToepelitzTransition(mask, offsets, elements)
Represents a masked Toepelitz-structured transition matrix: that is, a matrix in which the elements on a common diagonal are identical. The aruments are:
mask
: a squareBitMatrix
. Anyfalse
values are considered invalid transitions.offsets
: a range indicating which diagonals have values (0 = main diagonal, +1 = superdiagonal, -1 = subdiagonal).elements
: the elements on each diagonal inoffsets
.
MaskedToepelitzTransition(BitArray(true for i = 1:5, j = 1:5), -2:1, (Receive(2), Receive(1), Zero(), Pay(1)))
Miletus.MonteCarloModel
— TypeMonteCarloModel(core, dates, paths)
A MonteCarloModel
is the result of a simulation of a series of asset prices.
core
: a referenceCoreModel
dates
: anAbstractVector{Date}
paths
: a matrix of the scenario paths: the rows are the scenarios, and the columns are the values at each date indates
.
Miletus.MonteCarloScenario
— TypeMonteCarloScenario(core, dates, path)
A MonteCarloScenario
is a single simulation scenario of a MonteCarloModel
.
core
: a referenceCoreModel
dates
: anAbstractVector{Date}
paths
: anAbstractVector
of the values at each date indates
.
Miletus.SABRModel
— TypeSABRModel
The SABR model is a stochastic volatility model of the form
dS = σ F^β dW
dσ = ν σ dZ
with σ(0) = α, where W and Z are correlated Wiener processes with parameter ρ.
Unlike Black-Scholes, this is not scale invariant unless β == 1
.
startdate
startprice
: initial price atstartdate
interestrate
: risk free rate of return.carryrate
: the carry rate, i.e. the net return for holding the asset:- for stocks this is typically positive (i.e. dividends)
- for commodities this is typically negative (i.e. cost-of-carry)
α
: initial volatlity (note: this is not equivalent to Black-Scholes volatlity)β
: constantρ
: correlation between stock price and volatlityν
: volatility of volatility
If ν == 0
and β == 1
, then it is equivalent to the GeomBMModel
with σ = α
.
Miletus.Scale
— TypeScale(s::Observable, c::Contract)
Scale the contract c
by s
.
Miletus.SplineInterpolation
— TypeSplineInterpolation
This is used to model interpolation between any two discrete points on a discrete convex curve. This implements double quadratic interpolation.
- `x`: An Array of the discrete values on the x axis
- `y`: An Array of the discrete values on the y axis
- `weights`: An Array of tuples of the weights of every quadratic curve
modelled between two discrete points on the curve
Miletus.SplineVolatilityModel
— TypeSplineVolatilityModel
The SplineVolatilityModel is used to interpolate between two values of
strikeprice to find the appropriate volatility on the volatility smile.
startdate
startprice
: initial price atstartdate
interestrate
: risk free rate of return.carryrate
: the carry rate, i.e. the net return for holding the asset:- for stocks this is typically positive (i.e. dividends)
- for commodities this is typically negative (i.e. cost-of-carry)
date
: Date of Maturityvolcurve
: A SplineInterpolation type that models the interpolation
Miletus.TianModel
— MethodTianModel
Tian binomial model.
Miletus.Until
— TypeUntil(p::Observable{Bool}, c::Contract)
Acts like contract c
until p
is true
, at which point it is abandoned (and hence worthless).
Miletus.When
— TypeWhen(p::Observable{Bool}, c::Contract)
Acquire the contract c
at the point when p
becomes true
.
Miletus.YieldModel
— TypeA model for the time value of money
Miletus.Zero
— TypeZero() <: Contract
A "null" contract.
Miletus._black
— Function_black(h, t, K = exp(-2*h*t))
The black kernel function:
Φ(h + t) - K*Φ(h - t)
where K = exp(-2*h*t)
(this is provided as an extra argument, as it is typically known a priori.
Assumes h <= 0
, t >= 0
, which implies K >= 1
.
Miletus._black_erfcx
— Method_black_erfcx(h, t)
Compute the black kernel function using the erfcx
function:
b = Φ(h+t) - exp(-2⋅h⋅t) ⋅ Φ(h-t)
exp(-(h²+t²)/2)
= --------------- · [ Φ(h+t)/ϕ(h+t) - Φ(h-t)/ϕ(h-t) ]
√(2π)
= exp(-(h²+t²)/2) ⋅ [erfcx(-(h+t)/√2) - erfcx(-(h-t)/√2)]/2
Miletus._black_ivol
— Method_black_ivol(K, β)
The kernel of the inverse black function: returns the value s
such that
black(K,s) == β
where K ≥ 1
and 0 ≤ β ≤ 1
.
Miletus._black_normcdf
— Function_black_normcdf(h, t, K=exp(-2*h*t))
Computes the black kernel function directly using the normal CDF.
Miletus._black_small_t
— Method_black_small_t(h, t)
Compute the black kernel function using Taylor series around t=0
:
b = Φ(h+t) - exp(-2⋅h⋅t) ⋅ Φ(h-t)
exp(-(h²+t²)/2)
= --------------- · [ Φ(h+t)/ϕ(h+t) - Φ(h-t)/ϕ(h-t) ]
√(2π)
= exp(-(h²+t²)/2) ⋅ [erfcx(-(h+t)/√2) - erfcx(-(h-t)/√2)]/2
Using
Y(h) = Φ(h)/φ(h) = √(π/2)·erfcx(-h/√2)
a = 1+h·Y(h)
Note that due to h < 0
, and h·Y(h) -> -1
(from above) as h -> -∞
, we also have that a > 0
and a -> 0
as h -> -∞
.
Miletus._black_vega
— Method_black_vega(h, t)
The vega corresponding to the black kernel function:
Φ(h + t) - K*Φ(h - t)
where h = -log(K)/s
and t = s/2
.
Miletus._cblack
— Function_cblack(h, t, K=exp(-2*h*t))
The complement of the black kernel function
1 - _black(h, t, K)
Miletus.black
— Methodblack(F,K,s)
The Black function of standardised arguments:
black(F,K,s) = F ⋅ Φ(log(F/K)/s + s/2) - K ⋅ Φ(log(F/K)/s - s/2)
This is the Black-Scholes price of a call option with spot price F
, strike K
and volatility s
, assuming the discount rate r
and carry rate q
are zero, and the time to maturity is one.
This is monotonic increasing in s
, with
black(K,F,0) == max(F-K, 0)
black(K,F,∞) == F
The corresponding put price is:
black(K,F,s)
and so put-call parity implies:
black(F,K,s) + K = black(K,F,s) + F
black(F,K,s)
is the expectation of max(exp(Y)-K,0)
where Y
has a Normal(log(F)-s^2/2, s)
distribution.
This can be used to compute the complementary CDF of the inverse Gaussian distribution with parameters (λ,μ)
at x
via,
P[X > x] = black(1, exp(2λ/μ), 2*sqrt(λ/x))
This is largely based on the method of Jäckel (2015).
References
- Jäckel, P. (2015), Let's Be Rational. Wilmott, 2015(75): 40–53. doi:10.1002/wilm.10395
Miletus.black_ivol
— Methodblack_ivol(F, K, β)
The inverse black function: returns the value s
such that
black(K,s) == β
where F > 0
, K > 0and
max(F-K, 0) ≤ β ≤ F`.
Miletus.date2index
— Methoddate2index(m::Union{MonteCarloScenario, MonteCarloModel}, dt::Date)
Returns the index of dt
in the path(s) of m
.
Miletus.delta
— Methoddelta(m::GeomBMModel, c::Contract)
delta(m::GeomBMModel, c::Contract, MonteCarloModel, npaths::Integer)
Calculate the delta Greek: the first derivative of the option price with respect to the spot price of the underlying for the contract c
under the model m
Inputs
m::GeomBMModel
- The model under which the contract is being valuedc::Contract
- The contract whose greek is desired.npaths::Integer
- Number of Monte Carlo paths to use.
Miletus.fit_ivol
— Methodfit_ivol(SABRModel, mcore::AbstractCoreModel, contracts, ivols)
Fit a SABRModel
using from a collection of contracts (contracts
) and their respective implied volatilities (ivols
), under the assumptions of mcore
.
Miletus.fit_ivol
— Methodfit_ivol(SplineVolatilityModel, mcore::AbstractCoreModel, contracts, ivols)
Fit a SplineVolatilityModel
using from a collection of contracts (contracts
) and their respective implied volatilities (ivols
), under the assumptions of mcore
.
Miletus.gamma
— Methodgamma(m::GeomBMModel, c::Contract)
gamma(m::GeomBMModel, c::Contract, MonteCarloModel, npaths::Integer)
Calculate the gamma Greek: the second derivative of the option price with respect to the price of the underlying for the contract c
under the model m
Inputs
m::GeomBMModel
- The model under which the contract is being valuedc::Contract
- The contract whose greek is desired.npaths::Integer
- Number of Monte Carlo paths to use.
Miletus.greek
— Methodgreek(m::GeomBMModel, c::Contract; metric = :vol, n = 1)
Calculate the nth derivative of the option price with respect to different metrics.
Inputs
m::GeomBMModel
- The model under which the contract is being valuedc::Contract
- The contract whose greek is desired.metric::Symbol
(optional) - The metric with respect to which the derivative
is evaluated. (default = :vol, other options - :strike, :interest)
n::Int
- (optional) The derivative desired. (default = 1)
Miletus.greek
— Method`greek(m::MonteCarloModel, c::Contract; metric = :vol, n = 1)`
Calculate the nth derivative of the option price with respect to different
metrics.
Inputs
m::MonteCarloModel
- The model under which the contract is being valuedc::Contract
- The contract whose greek is desired.metric::Symbol
(optional) - The metric with respect to which the derivative
is evaluated. (default = :vol, other options - :strike, :interest)
n::Int
- (optional) The derivative desired. (default = 1)
Miletus.householder_update
— Methodhouseholder_update(N, [H2, [H3]])
The update incrememnt for n
th order Householder's method (where n
is the number of arguments) for finding the root of
f(x) == 0
n == 1
is Newton's methodn == 2
is Halley's method
Arguments:
N
: the Newton update-f(x) / f′(x)
H2
: the scaled 2nd order derivativef′′(x) / f′(x)
H3
: the scaled 3rd order derivativef′′′(x) / f′(x)
Miletus.interpolate
— Methodinterpolate(m::SplineInterpolation, c::European)
Compute the implied Black-Scholes volatility of an option c
under the SplineVolatilityModel m
.
Miletus.ivol
— Methodivol(m::CoreModel, c::Contract, price)
Compute the Black-Scholes implied volatility of contract c
at price
, under the assumptions of model m
(ignoring the volatility value of m
).
See also: fit
Miletus.ivol
— Methodivol(m::SABRModel, c::European)
Compute the implied Black-Scholes volatility of an option c
under the SABR model m
.
The SABR model was originally explored in detail in Hagan et. al. (2002). This uses the correction by Obloj (2008).
P. S. Hagan, D. Kumar, A. S. Lesniewski, and D. E. Woodward. (2002) 'Managing smile risk". Wilmott Magazine, pages 84–108.
J. Obloj (2008) "Fine-tune your smile: Correction to Hagan et al", arXiv: 0708.0998
Miletus.maturitydate
— Methodmaturitydate(c::Contract)
The maturity date of a contract c
is the date at which contract is completed or expires.
Miletus.montecarlo
— Methodmontecarlo(m::GeomBMModel, dates, npaths)
Sample npaths
Monte Carlo paths of the model m
, at time dates
.
Miletus.poscubicroot
— Methodposcubicroot(p3,p2,p1,0)
Find the smallest positive real root of the polynomial
p3*x^3 + p2*x^2 + p1*x + p0
Miletus.powm1
— Methodpowm1(x,y)
Compute x^y-1
accurately when x
is near 1. Based on Boost's implementation
Miletus.price
— Methodprice(c::Contract)
The price of a Buy
or Sell
contract.
Miletus.rational_cubic_interpolation
— Methodrational_cubic_interpolation(x, x₋, x₊, y₋, y₊, d₋, d₊, r)
Compute a shape preserving rational interpolation.
Arguments
x
the value at which to interpolatex₋
,x₊
: the lower and upper bounds of the intervaly₋
,y₊
: the value of the function at the lower and upper boundsd₋
,d₊
: the value of the derivative at the lower and upper boundsr
: the control parameter, which must be greater than -1.- If
r == 3
, then it is the usual Hermite cubic polynomial - As
r → ∞
, it approaches linear interpolation.
- If
If Δ = (y₊ - y₋)/(x₋ - x₊) > 0
, and d₋ >= 0
, d₊ >= 0
, then the interpolant is guaranteed monotonic increasing if
r >= (d₋ + d₊)/Δ
[DG85, eq. 3.8].
If d₋ < Δ < d₊
, then the interpolant is convex if and only if
r >= 1 + max(d₊ - Δ, Δ - d₋) / min(d₊ - Δ, Δ - d₋)
[DG85, eq. 3.18].
Note: for the purposes of computation, the bounds are actually interchangable (i.e. x₋
can be the upper bound).
References
- R. Delbourgo and J. A. Gregory (1985) "Shape Preserving Piecewise Rational Interpolation", SIAM J. Sci. and Stat. Comput., 6(4), 967–976. doi:10.1137/0906065
Miletus.rho
— Methodrho(m::GeomBMModel, c::Contract)
rho(m::GeomBMModel, c::Contract, MonteCarloModel, npaths::Integer)
Calculate the rho Greek: the first derivative of the option price with respect to the interest rate, for the contract c
under the model m
Inputs
m::GeomBMModel
- The model under which the contract is being valuedc::Contract
- The contract whose greek is desired.npaths::Integer
- Number of Monte Carlo paths to use.
Miletus.sabr_alpha
— Methodsabr_alpha(F, t, σATM, β, ν, ρ)
Compute the α parameter (initial volatility) for the SABR model from the Black-Scholes at-the-money volatility.
F
: Forward pricet
: time to maturityσATM
: Black-Scholes at-the-money volatilityβ
,ν
,ρ
: parameters from SABR model.West, G. (2005). Calibration of the SABR model in illiquid markets. Applied Mathematical Finance, 12(4), 371-385.
Miletus.scenarios
— Methodscenarios(m::MonteCarloModel)
Returns an iterator over each MonteCarloScenario
in m
.
Miletus.strikeprice
— Methodstrikeprice(c::Contract)
The price at which an option or forward can be exercised.
Miletus.vega
— Methodvega(m::GeomBMModel, c::Contract)
vega(m::GeomBMModel, c::Contract, MonteCarloModel, npaths::Integer)
Calculate the vega Greek: the first derivative of the option price with respect to volatility for the contract c
under the model m
Inputs
m::GeomBMModel
- The model under which the contract is being valuedc::Contract
- The contract whose Greek is desired.npaths::Integer
- Number of Monte Carlo paths to use.
StatsAPI.fit
— Methodfit(SABRModel, mcore::AbstractCoreModel, contracts, prices)
Fit a SABRModel
using from a collection of contracts (contracts
) and their respective prices (prices
), under the assumptions of mcore
.
StatsAPI.fit
— Methodfit(SplineVolatilityModel, mcore::AbstractCoreModel, contracts, prices)
Fit a SplineVolatilityModel
using from a collection of contracts (contracts
) and their respective prices (prices
), under the assumptions of mcore
.
StatsAPI.fit
— Methodfit(GeomBMModel, m::Union{CoreModel,CoreForwardModel}, c::Contract, price)
Fit a GeomBMModel
using the implied volatility of c
at price
, using the parameters of m
.
See also: ivol