Internal Functions
StochasticIntegrals.ForwardCovarianceStochasticIntegrals.ItoIntegralStochasticIntegrals.ItoProcessStochasticIntegrals.ItoSetStochasticIntegrals.MersenneStochasticIntegrals.NumberGeneratorStochasticIntegrals.SimpleCovarianceStochasticIntegrals.SobolGenStochasticIntegrals.Stable_RNGStochasticIntegrals.StochasticIntegralsCovarianceSobol.next!StochasticIntegrals.brownians_in_useStochasticIntegrals.correlationStochasticIntegrals.covarianceStochasticIntegrals.evolve!StochasticIntegrals.evolve_covar_and_ito_processes!StochasticIntegrals.generate_conditioned_distributionStochasticIntegrals.get_confidence_hypercubeStochasticIntegrals.get_drawsStochasticIntegrals.get_draws_matrixStochasticIntegrals.get_zero_drawsStochasticIntegrals.log_likelihoodStochasticIntegrals.make_covariance_matrixStochasticIntegrals.make_ito_process_non_syncronous_time_seriesStochasticIntegrals.make_ito_process_syncronous_time_seriesStochasticIntegrals.pdfStochasticIntegrals.to_arrayStochasticIntegrals.to_dataframeStochasticIntegrals.to_drawsStochasticIntegrals.update!StochasticIntegrals.varianceStochasticIntegrals.volatility
Main Structs
StochasticIntegrals.ItoIntegral — Type
A 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 ItoIntegrals where the integrand is a flat function is:
ItoIntegral(brownian_id_::Symbol, variance_::Real)StochasticIntegrals.ItoSet — Type
Creates an ItoSet. This contains :
- A correlation matrix of brownian motions.
- A vector giving the axis labels for this correlation matrix.
- A dict of
ItoInterals. Here the keys should be ids for the ito integrals and the values should beItoIntegrals.
Determine which Brownian processes are used in an array of ItoIntegrals.
StochasticIntegrals.StochasticIntegralsCovariance — Type
StochasticIntegralsCovarianceThis 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 — Type
ForwardCovarianceCreates 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 — Type
SimpleCovarianceCreates an SimpleCovariance struct. This is a simplified version of ForwardCovariance and is designed for ItoIntegrals 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 — Type
A `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 — Type
A NumberGenerator wrapper for `MersenneTwister`. This makes pseudorandom numbers.StochasticIntegrals.SobolGen — Type
A NumberGenerator wrapper for `SobolSeq`. This makes quasirandom numbersStochasticIntegrals.Stable_RNG — Type
A NumberGenerator wrapper for `MersenneTwister`. This makes pseudorandom numbers.Sobol.next! — Function
next!(number_generator::NumberGenerator)This extracts a random draw given a number generator struct (like a SobolGen, Mersenne or Stable_RNG).
StochasticIntegrals.get_draws — Function
get_draws(covar::ForwardCovariance; uniform_draw::Array{T,1} = rand(length(covar.covariance_labels_))) where T<:RealInputs
covar- AnForwardCovarianceorSimpleCovariancestruct 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- AnForwardCovarianceorSimpleCovariancestruct 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
VectorofDicts of draws.
StochasticIntegrals.get_draws_matrix — Function
get_draws_matrix(covar::Union{ForwardCovariance,SimpleCovariance}, num::Integer; number_generator::NumberGenerator = Mersenne(MersenneTwister(1234), length(covar.covariance_labels_)), antithetic_variates = false)High-performance variant of get_draws that returns a pre-allocated matrix instead of an array of Dicts. Uses in-place matrix multiplication and reusable buffers to minimise allocations.
Inputs
covar- AForwardCovarianceorSimpleCovariancestruct to draw from.num- The number of draws you want.number_generator- How you want to generate random draws.antithetic_variates- Do you want antithetic variate sampling.
Returns
- A
Matrix{Float64}of size(num, dims)where each row is a draw and each column corresponds to an integral. - A
Vector{Symbol}of column labels matchingcovar.covariance_labels_.
When antithetic_variates = true, num is rounded so that the returned matrix has 2 * round(num/2) rows, with rows 2i-1 and 2i summing to zero.
StochasticIntegrals.get_zero_draws — Function
get_zero_draws(covar::ForwardCovariance)get a draw of zero for all ItoIntegrals. This may be handy for bug hunting.
Inputs
- covar - the
ForwardCovariancestruct that you want zero draws from
Outputs
- A
Dictof zero draws
get_zero_draws(covar::ForwardCovariance, num::Integer)get an array of zero draws for all ItoIntegrals. May be handy for bug hunting.
Inputs
covar- theForwardCovariancestruct that you want zero draws fromnum- The number of zero draws you want.
Outputs
- A
VectorofDicts of zero draws
Ito Processes
StochasticIntegrals.ItoProcess — Type
A struct representing an Itoprocess.
Members
t0- The initial time of theItoProcessvalue- The initial value of theItoProcessdrift- The drift of theItoProcess.stochastic- The stochastic process.
StochasticIntegrals.evolve! — Function
evolve!(itoprocess::ItoProcess, stochastic::Real, new_time::Real)Evolve the ItoProcess forward. This changes the time (t0) as well as the value.
Inputs
itoprocess- TheItoProcessto 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<:RealEvolve the ItoProcess forward. This changes the time (t0) as well as the value.
Inputs
itoprocesses- ADictofItoProcessesstochastic- ADictof draws from theItoIntegral.new_time- The new time.
Return
Nothing. It changes the input ItoProcess
StochasticIntegrals.evolve_covar_and_ito_processes! — Function
evolve_covar_and_ito_processes!(itoprocesses::Union{Dict{Symbol,ItoProcess{R}},Dict{Symbol,ItoProcess}}, covar::ForwardCovariance, new_time::Real; number_generator::NumberGenerator) where R<:RealEvolve the ItoProcesses forward.
Inputs
itoprocesses- ADictofItoProcessescovar- The covariance matrix.new_time- The new time.number_generator- The number generator
Return
- The updated
ItoProcesses - 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<:RealEvolve the ItoProcesses forward.
Inputs
itoprocesses- ADictofItoProcessescovar- The covariance matrix.new_time- The new time.number_generator- The number generator
Return
- The updated
ItoProcesses - The Covariance matrix
StochasticIntegrals.make_ito_process_syncronous_time_series — Function
make_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<:RealEvolve the ItoProcesses forward.
Inputs
itoprocesses- ADictofItoProcessescovar- The covariance matrix.timegap- The time gap between ticks.total_number_of_ticks- The total number of ticks.number_generatorTheNumberGeneratorused for the RNG.
Return
- A DataFrame with the ticks.
StochasticIntegrals.make_ito_process_non_syncronous_time_series — Function
make_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<:DistributionEvolve the ItoProcesses forward.
Inputs
itoprocesses- ADictofItoProcessescovar- 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_twisterTheMersenneTwisterused for the RNG.
Return
- A DataFrame with the ticks.
Helper Functions
StochasticIntegrals.volatility — Function
volatility(ito::ItoIntegral, on::Union{Date,DateTime})Get the volatility of an ItoIntegral on a certain date.
Inputs
ito- TheItoIntegralyou 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- AnItoSetthat you want the volatility for.ito_integral_id- The key of the ito dict that you are interested inonThe 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- AnForwardCovariancethat you want the volatility for.index- The key of the ito dict that you are interested inonThe time or instant you want the volatility for.
Returns
- A scalar
StochasticIntegrals.variance — Function
variance(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- TheItoIntegralyou 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- AnForwardCovariancethat you want the variance for.idorindex- The key/index of the ito dict that you are interested in
Returns
- A scalar
StochasticIntegrals.covariance — Function
covariance(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 ItoIntegrals over a certain period given the underlying Brownian processes have a correlation of gaussian_correlation.
Inputs
ito1- The firstItoIntegralito2- The secondItoIntegralfrom- 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- AnForwardCovariancethat you want the covariance for.index_1orid1- The key/index of the first ito that you are interested inindex_2orid2- The key/index of the second ito that you are interested in
Returns
- A scalar
StochasticIntegrals.correlation — Function
correlation(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- AnItoSetthat you want the correlation for two itos within.index1orbrownian_id1- The index/key for the first ito integral.index2orbrownian_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 ItoIntegrals over a period.
Inputs
covar- AnForwardCovariancethat you want the correlation for.index_1orid1- The key/index of the first ito that you are interested inindex_2orid2- The key/index of the second ito that you are interested in
Returns
- A scalar
StochasticIntegrals.pdf — Function
pdf(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- theForwardCovariancestruct that you want to evaluate the pdfcoordinates- The coordinates you want to examine.
Outputs
- A scalar
StochasticIntegrals.make_covariance_matrix — Function
make_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_- AnItoSetyou 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
Hermitiancovariance matrix. - A
Vector{Symbol}of labels for the covariance matrix.
StochasticIntegrals.log_likelihood — Function
log_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- theForwardCovariancestruct that you want to evaluate the log likelihood.coordinates- The coordinates you want to examine.
Outputs
- A scalar
StochasticIntegrals.brownians_in_use — Function
brownians_in_use(itos::Array{ItoIntegral,1}, brownians::Array{Symbol,1})Determine which Browninan processes are used in an array of ItoIntegrals.
Inputs
- itos - A dict containing each of the ito integrals
- brownians - All possible brownians
Outputs
- A
Vectorof what brownians are in use in itos - A
Vectorwith the indices of these brownians.
StochasticIntegrals.update! — Function
update!(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- TheSimpleCovariancestruct.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 — Function
to_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 Dicts containing draws.
StochasticIntegrals.to_dataframe — Function
to_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 Arrays or Vectors of Dicts to a DataFrame.
StochasticIntegrals.to_array — Function
to_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 — Function
get_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 — Function
generate_conditioned_dist(covar::ForwardCovariance, conditioning_draws::Array{Symbol,T}) where T<:RealGiven some subset of known stochastic integral values this generates a conditional multivariate normal distribution.