Internal Functions
StochasticIntegrals.ForwardCovariance
StochasticIntegrals.ItoIntegral
StochasticIntegrals.ItoProcess
StochasticIntegrals.ItoSet
StochasticIntegrals.Mersenne
StochasticIntegrals.NumberGenerator
StochasticIntegrals.SimpleCovariance
StochasticIntegrals.SobolGen
StochasticIntegrals.StochasticIntegralsCovariance
Sobol.next!
StochasticIntegrals.brownians_in_use
StochasticIntegrals.correlation
StochasticIntegrals.covariance
StochasticIntegrals.evolve!
StochasticIntegrals.evolve_covar_and_ito_processes!
StochasticIntegrals.generate_conditioned_distribution
StochasticIntegrals.get_confidence_hypercube
StochasticIntegrals.get_draws
StochasticIntegrals.get_zero_draws
StochasticIntegrals.log_likelihood
StochasticIntegrals.make_covariance_matrix
StochasticIntegrals.make_ito_process_non_syncronous_time_series
StochasticIntegrals.make_ito_process_syncronous_time_series
StochasticIntegrals.pdf
StochasticIntegrals.to_array
StochasticIntegrals.to_dataframe
StochasticIntegrals.to_draws
StochasticIntegrals.update!
StochasticIntegrals.variance
StochasticIntegrals.volatility
Main Structs
StochasticIntegrals.ItoIntegral
— TypeA struct detailing an ito integral. It contains a UnivariateFunction detailing the integrand as well as a symbol detailing an id of the integral's processes.
Usual (and most general) contructor is:
ItoIntegral(brownian_id_::Symbol, f_::UnivariateFunction)
Convenience constructor for ItoIntegral
s where the integrand is a flat function is:
ItoIntegral(brownian_id_::Symbol, variance_::Real)
StochasticIntegrals.ItoSet
— TypeCreates an ItoSet. This contains :
- A correlation matrix of brownian motions.
- A vector giving the axis labels for this correlation matrix.
- A dict of
ItoInteral
s. Here the keys should be ids for the ito integrals and the values should beItoIntegral
s.
Determine which Brownian processes are used in an array of ItoIntegral
s.
StochasticIntegrals.StochasticIntegralsCovariance
— TypeStochasticIntegralsCovariance
This is an abstract type which represents structs that represent covariances over spans of time. The concrete instances of this type should support extracting correlations, covariances, volatilities and random number generation.
StochasticIntegrals.ForwardCovariance
— TypeForwardCovariance
Creates an ForwardCovariance
struct. This contains :
- An
Itoset
. - The time from which the covariance period starts.
- The time to which the covariance period extends to.
And in the constructor the following items are generated and stored in the object:
- A covariance matrix
- Labels for the covariance matrix.
- The cholesky decomposition of the covariance matrix.
- The inverse of the covariance matrix.
- The determinant of the covariance matrix.
The constructors are:
ForwardCovariance(ito_set_::ItoSet, from_::Real, to_::Real;
calculate_chol::Bool = true, calculate_inverse::Bool = true, calculate_determinant::Bool = true)
ForwardCovariance(ito_set_::ItoSet, from::Union{Date,DateTime}, to::Union{Date,DateTime})
ForwardCovariance(old_ForwardCovariance::ForwardCovariance, from::Real, to::Real)
ForwardCovariance(old_ForwardCovariance::ForwardCovariance, from::Union{Date,DateTime}, to::Union{Date,DateTime})
StochasticIntegrals.SimpleCovariance
— TypeSimpleCovariance
Creates an SimpleCovariance
struct. This is a simplified version of ForwardCovariance
and is designed for ItoIntegral
s that are constant (so correlations do not need to be recalculated). This computational saving is the only advantage - ForwardCovariance
is more general. It contains the same elements as a ForwardCovariance
:
- An Itoset
- The time from which the covariance period starts.
- The time to which the covariance period extends to.
And in the constructor the following items are generated and stored in the object:
- A covariance matrix
- Labels for the covariance matrix.
- The cholesky decomposition of the covariance matrix.
- The inverse of the covariance matrix.
- The determinant of the covariance matrix.
The constructors are:
SimpleCovariance(ito_set_::ItoSet, from_::Real, to_::Real;
calculate_chol::Bool = true, calculate_inverse::Bool = true, calculate_determinant::Bool = true)
Random Number Generation
StochasticIntegrals.NumberGenerator
— TypeA `NumberGenerator` must always return a vector of uniforms when called with the function `next!(::NumberGenerator)`.
It can contain whatever it wants as long as it does that method.
StochasticIntegrals.Mersenne
— TypeA NumberGenerator wrapper for `MersenneTwister`. This makes pseudorandom numbers.
StochasticIntegrals.SobolGen
— TypeA NumberGenerator wrapper for `SobolSeq`. This makes quasirandom numbers
Sobol.next!
— Functionnext!(number_generator::NumberGenerator)
This extracts a random draw given a number generator struct (like a SobolGen
, Mersenne
or Stable_RNG
).
StochasticIntegrals.get_draws
— Functionget_draws(covar::ForwardCovariance; uniform_draw::Array{T,1} = rand(length(covar.covariance_labels_))) where T<:Real
Inputs
covar
- AnForwardCovariance
orSimpleCovariance
struct that you want to draw from.uniform_draw
- The draw vector (from the uniform distribution)
Returns
- A Dict of draws.
get_draws(covar::Union{ForwardCovariance,SimpleCovariance}, num::Integer; number_generator::NumberGenerator = Mersenne(MersenneTwister(1234), length(covar.covariance_labels_)), antithetic_variates = false)
get pseudorandom draws from a ForwardCovariance
struct. Other schemes (like quasirandom) can be done by inserting quasirandom numbers in as the uniformdraw. If the `antitheticvariates` control is set to true then every second set of draws will be antithetic to the previous.
Inputs
covar
- AnForwardCovariance
orSimpleCovariance
struct that you want to draw from.num
- The number of draws you wantnumber_generator
- How you want to generate random draws.antithetic_variates
- Do you want antithetic variate sampling.
Returns
- A
Vector
ofDict
s of draws.
StochasticIntegrals.get_zero_draws
— Functionget_zero_draws(covar::ForwardCovariance)
get a draw of zero for all ItoIntegral
s. This may be handy for bug hunting.
Inputs
- covar - the
ForwardCovariance
struct that you want zero draws from
Outputs
- A
Dict
of zero draws
get_zero_draws(covar::ForwardCovariance, num::Integer)
get an array of zero draws for all ItoIntegral
s. May be handy for bug hunting.
Inputs
covar
- theForwardCovariance
struct that you want zero draws fromnum
- The number of zero draws you want.
Outputs
- A
Vector
ofDict
s of zero draws
Ito Processes
StochasticIntegrals.ItoProcess
— TypeA struct representing an Itoprocess.
Members
t0
- The initial time of theItoProcess
value
- The initial value of theItoProcess
drift
- The drift of theItoProcess
.stochastic
- The stochastic process.
StochasticIntegrals.evolve!
— Functionevolve!(itoprocess::ItoProcess, stochastic::Real, new_time::Real)
Evolve the ItoProcess
forward. This changes the time (t0
) as well as the value
.
Inputs
itoprocess
- TheItoProcess
to be evolved.stochastic
- A draw from theItoIntegral
.new_time
- The new time.
Return
Nothing. It changes the input ItoProcess
evolve!(itoprocesses::Union{Dict{Symbol,ItoProcess{R}},Dict{Symbol,ItoProcess}}, stochastics::Union{Dict{Symbol,R},Dict{Symbol,Real}}, new_time::Real) where R<:Real
Evolve the ItoProcess
forward. This changes the time (t0
) as well as the value
.
Inputs
itoprocesses
- ADict
ofItoProcess
esstochastic
- ADict
of draws from theItoIntegral
.new_time
- The new time.
Return
Nothing. It changes the input ItoProcess
StochasticIntegrals.evolve_covar_and_ito_processes!
— Functionevolve_covar_and_ito_processes!(itoprocesses::Union{Dict{Symbol,ItoProcess{R}},Dict{Symbol,ItoProcess}}, covar::ForwardCovariance, new_time::Real; number_generator::NumberGenerator) where R<:Real
Evolve the ItoProcess
es forward.
Inputs
itoprocesses
- ADict
ofItoProcess
escovar
- The covariance matrix.new_time
- The new time.number_generator
- The number generator
Return
- The updated
ItoProcess
es - The Covariance matrix
evolve_covar_and_ito_processes!(itoprocesses::Union{Dict{Symbol,ItoProcess{R}},Dict{Symbol,ItoProcess}}, covar::SimpleCovariance, new_time::Real; number_generator::NumberGenerator) where R<:Real
Evolve the ItoProcess
es forward.
Inputs
itoprocesses
- ADict
ofItoProcess
escovar
- The covariance matrix.new_time
- The new time.number_generator
- The number generator
Return
- The updated
ItoProcess
es - The Covariance matrix
StochasticIntegrals.make_ito_process_syncronous_time_series
— Functionmake_ito_process_syncronous_time_series(ito_processes::Union{Dict{Symbol,ItoProcess{T}},Dict{Symbol,ItoProcess}},
covar::Union{ForwardCovariance,SimpleCovariance}, timegap::R, total_number_of_ticks::Integer;
number_generator::NumberGenerator = Mersenne(MersenneTwister(2), length(collect(keys(ito_processes))))) where R<:Real where T<:Real
Evolve the ItoProcess
es forward.
Inputs
itoprocesses
- ADict
ofItoProcess
escovar
- The covariance matrix.timegap
- The time gap between ticks.total_number_of_ticks
- The total number of ticks.number_generator
TheNumberGenerator
used for the RNG.
Return
- A DataFrame with the ticks.
StochasticIntegrals.make_ito_process_non_syncronous_time_series
— Functionmake_ito_process_non_syncronous_time_series(ito_processes::Union{Dict{Symbol,ItoProcess{R}},Dict{Symbol,ItoProcess}},
covar::Union{ForwardCovariance,SimpleCovariance}, update_rates::Union{OrderedDict{Symbol,D},Dict{Symbol,D},OrderedDict{Symbol,Distribution},Dict{Symbol,Distribution}},
total_number_of_ticks::Integer;
timing_twister::Union{StableRNG,MersenneTwister} = MersenneTwister(1),
ito_number_generator::NumberGenerator = Mersenne(MersenneTwister(2), length(collect(keys(ito_processes))))
) where R<:Real where D<:Distribution
Evolve the ItoProcess
es forward.
Inputs
itoprocesses
- ADict
ofItoProcess
escovar
- The covariance matrix.update_rates
- The update rates of the exponential waiting times between ticks for each asset.total_number_of_ticks
- The total number of ticks.ito_twister
TheMersenneTwister
used for the RNG.
Return
- A DataFrame with the ticks.
Helper Functions
StochasticIntegrals.volatility
— Functionvolatility(ito::ItoIntegral, on::Union{Date,DateTime})
Get the volatility of an ItoIntegral
on a certain date.
Inputs
ito
- TheItoIntegral
you want the volatility for.on
- What instant do you want the volatility for.
Outputs
- A scalar
volatility(ito::ItoSet, ito_integral_id::Symbol, on::Union{Date,DateTime})
Get volatility of an ito_integral
on a date.
Inputs
ito
- AnItoSet
that you want the volatility for.ito_integral_id
- The key of the ito dict that you are interested inon
The time or instant you want the volatility for.
Returns
- A scalar
volatility(covar::ForwardCovariance, index::Integer, on::Union{Date,DateTime})
volatility(covar::ForwardCovariance, id::Symbol, on::Union{Date,DateTime})
Get the volatility of an ForwardCovariance
on a date.
Inputs
covar
- AnForwardCovariance
that you want the volatility for.index
- The key of the ito dict that you are interested inon
The time or instant you want the volatility for.
Returns
- A scalar
StochasticIntegrals.variance
— Functionvariance(ito::ItoIntegral, from::Real, to::Real)
variance(ito::ItoIntegral, base::Union{Date,DateTime}, from::Union{Date,DateTime}, to::Union{Date,DateTime})
Get the variance of an ItoIntegral
from one point of time to another.
Inputs
ito
- TheItoIntegral
you want the variance for.from
- The time at which the integration starts.to
- The time at which the integration ends.
Outputs
- A scalar
variance(covar::ForwardCovariance, id::Symbol)
variance(covar::ForwardCovariance, index::Integer)
Get the variance of an ForwardCovariance
over a period.
Inputs
covar
- AnForwardCovariance
that you want the variance for.id
orindex
- The key/index of the ito dict that you are interested in
Returns
- A scalar
StochasticIntegrals.covariance
— Functioncovariance(ito1::ItoIntegral,ito2::ItoIntegral, from::Real, to::Real, gaussian_correlation::Real)
covariance(ito1::ItoIntegral,ito2::ItoIntegral, base::Union{Date,DateTime}, from::Union{Date,DateTime}, to::Union{Date,DateTime}, gaussian_correlation::Real)
Get the covariance of two ItoIntegral
s over a certain period given the underlying Brownian processes have a correlation of gaussian_correlation
.
Inputs
ito1
- The firstItoIntegral
ito2
- The secondItoIntegral
from
- The start of the periodto
- The end of the periodgaussian_correlation
- The correlation between the brownians for each of the two itos. This should be in the range [-1,1].on
- What instant do you want the volatility for.
Outputs
- A scalar
covariance(covar::ForwardCovariance, index_1::Integer, index_2::Integer)
covariance(covar::ForwardCovariance, id1::Symbol, id2::Symbol)
Get the covariance of two ito integrals in a ForwardCovariance
over a period.
Inputs
covar
- AnForwardCovariance
that you want the covariance for.index_1
orid1
- The key/index of the first ito that you are interested inindex_2
orid2
- The key/index of the second ito that you are interested in
Returns
- A scalar
StochasticIntegrals.correlation
— Functioncorrelation(ito::ItoSet, index1::Integer, index2::Integer)
correlation(ito::ItoSet, brownian_id1::Symbol, brownian_id2::Symbol)
Get correlation between brownian motions in an ItoSet
.
Inputs
ito
- AnItoSet
that you want the correlation for two itos within.index1
orbrownian_id1
- The index/key for the first ito integral.index2
orbrownian_id2
- The index/key for the second ito integral.
Returns
- A scalar
correlation(covar::ForwardCovariance, index_1::Integer, index_2::Integer)
correlation(covar::ForwardCovariance, id1::Symbol, id2::Symbol)
Get the correlation of two ItoIntegral
s over a period.
Inputs
covar
- AnForwardCovariance
that you want the correlation for.index_1
orid1
- The key/index of the first ito that you are interested inindex_2
orid2
- The key/index of the second ito that you are interested in
Returns
- A scalar
StochasticIntegrals.pdf
— Functionpdf(covar::ForwardCovariance, coordinates::Dict{Symbol,Real})
get the value of the pdf at some coordinates. Note that it is assumed that the mean of the multivariate gaussian is the zero vector.
Inputs
covar
- theForwardCovariance
struct that you want to evaluate the pdfcoordinates
- The coordinates you want to examine.
Outputs
- A scalar
StochasticIntegrals.make_covariance_matrix
— Functionmake_covariance_matrix(ito_set_::ItoSet, from::Real, to::Real)
Make a covariance matrix given an ItoSet and a period of time. This returns a Hermitian covariance matrix as well as a vector of symbols representing the axis labelling on this Hermitian
.
Inputs
ito_set_
- AnItoSet
you want to make a covariance matrix from.from
- The (numeric) time from which the covariance span starts.to
- The (numeric) time at which the covariance span ends.
Returns
- A
Hermitian
covariance matrix. - A
Vector{Symbol}
of labels for the covariance matrix.
StochasticIntegrals.log_likelihood
— Functionlog_likelihood(covar::ForwardCovariance, coordinates::Dict{Symbol,Real})
get the log likelihood at some coordinates. Note that it is assumed that the mean of the multivariate gaussian is the zero vector.
Inputs
covar
- theForwardCovariance
struct that you want to evaluate the log likelihood.coordinates
- The coordinates you want to examine.
Outputs
- A scalar
StochasticIntegrals.brownians_in_use
— Functionbrownians_in_use(itos::Array{ItoIntegral,1}, brownians::Array{Symbol,1})
Determine which Browninan processes are used in an array of ItoIntegral
s.
Inputs
- itos - A dict containing each of the ito integrals
- brownians - All possible brownians
Outputs
- A
Vector
of what brownians are in use in itos - A
Vector
with the indices of these brownians.
StochasticIntegrals.update!
— Functionupdate!(sc::SimpleCovariance, from::Real, to::Real)
This takes a SimpleCovariance
and updates it for a new span in time. The new span in time is between from and to. For SimpleCovariance
this is done by just adjusting the covariances for the new time span (with corresponding adjustments) to the cholesky, inverse, etc.
The corresponding technique for a ForwardCovariance
(which is also a StochasticIntegralsCovariance
) is to feed it into a new ForwardCovariance
constructor which will recalculate for the new span.
Inputs
sc
- TheSimpleCovariance
struct.from
- The time from which you want the covariance for.to
- The time to which you want the covariance for.
Returns
Nothing. It juts updates the sc struct you pass in as an input.
Internal Functions
StochasticIntegrals.to_draws
— Functionto_draws(X::Array{T,2}; labels::Array{Symbol,1} = Symbol.("x", 1:size(X)[2]))
to_draws(dd::DataFrame; labels::Array{Symbol,1} = Symbol.(names(dd)))
Convert array or dataframe to a vector of Dict
s containing draws.
StochasticIntegrals.to_dataframe
— Functionto_dataframe(X::Array{T,2}; labels::Array{Symbol,1} = Symbol.("x", 1:size(X)[2]))
to_dataframe(draws::Array{Dict{Symbol,T},1}; labels::Array{Symbol,1} = collect(keys(draws[1])))
Convert Array
s or Vector
s of Dict
s to a DataFrame
.
StochasticIntegrals.to_array
— Functionto_array(draws::Array{Dict{Symbol,T},1}; labels::Array{Symbol,1} = collect(keys(draws[1]))) where T<:Real
to_array(dd::DataFrame; labels::Array{Symbol,1} = Symbol.(names(dd)))
Convert draws or a DataFrame
to an array and a vector of column labels.
StochasticIntegrals.get_confidence_hypercube
— Functionget_confidence_hypercube(covar::ForwardCovariance, confidence_level::Real, data::Array{T,2}; tuning_parameter::Real = 1.0)
This returns the endpoints of a hypercube that contains confidence_level (%) of the dataset.
StochasticIntegrals.generate_conditioned_distribution
— Functiongenerate_conditioned_dist(covar::ForwardCovariance, conditioning_draws::Array{Symbol,T}) where T<:Real
Given some subset of known stochastic integral values this generates a conditional multivariate normal distribution.