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.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.CRRModel
— MethodCRRModel
Cox-Ross-Rubenstein binomial model.
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
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:
or
where $W_t$ is a Wiener process, and κ = interestrate - carryrate
.
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.Scale
— TypeScale(s::Observable, c::Contract)
Scale the contract c
by s
.
Miletus.TianModel
— MethodTianModel
Tian binomial model.
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.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.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.montecarlo
— Methodmontecarlo(m::GeomBMModel, dates, npaths)
Sample npaths
Monte Carlo paths of the model m
, at time dates
.
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.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.
StatsBase.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
Miletus.AcquisitionDateObs
— TypeAcquisitionDateObs() <: Observable{Date}
The acquisition date of the contract.
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.Both
— TypeBoth(c1::Contract, c2::Contract)
Acquire both contracts c1
and c2
(and
in PJE2000).
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.DateObs
— TypeDateObs() <: Observable{Date}
A singleton type representing the "free" date observable.
Miletus.Either
— TypeEither(c1::Contract, c2::Contract)
Acquire either contract c1
or c2
(or
in PJE2000).
Miletus.Give
— TypeGive(c::Contract)
Take the opposite side of contract c
.
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.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.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.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.maturitydate
— Methodmaturitydate(c::Contract)
The maturity date of a contract c
is the date at which contract is completed or expires.
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.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.