Internal Functions
HighFrequencyCovariance.bnhls_2008
HighFrequencyCovariance.fejer
HighFrequencyCovariance.g
HighFrequencyCovariance.parzen
HighFrequencyCovariance.quadratic_spectral
HighFrequencyCovariance.tukey_hanning
HighFrequencyCovariance.HFC_Kernel
HighFrequencyCovariance.calculate_mean_abs_distance
HighFrequencyCovariance.default_num_grids
HighFrequencyCovariance.get_all_refresh_times
HighFrequencyCovariance.get_refresh_times_and_prices
HighFrequencyCovariance.is_missing_nan_inf
HighFrequencyCovariance.iterate_higham
HighFrequencyCovariance.latest_value
HighFrequencyCovariance.next_tick
HighFrequencyCovariance.project_to_S
HighFrequencyCovariance.project_to_U
HighFrequencyCovariance.random_value_in_interval
HighFrequencyCovariance.safe_multiply_period
HighFrequencyCovariance.simple_volatility_given_returns
HighFrequencyCovariance.squared_frobenius
HighFrequencyCovariance.squared_frobenius_distance
HighFrequencyCovariance.time_between_refreshes
HighFrequencyCovariance.time_period_ratio
HighFrequencyCovariance.weighted_mean
Metrics for distances between CovarianceMatrix structs
HighFrequencyCovariance.calculate_mean_abs_distance
— Functioncalculate_mean_abs_distance(
cov1::CovarianceMatrix,
cov2::CovarianceMatrix,
decimal_places::Integer = 8;
return_nans_if_symbols_dont_match::Bool = true,
)
Calculates the mean absolute distance (elementwise in L1 norm) between two CovarianceMatrix
s. Undefined if any labels differ between the two CovarianceMatrix
s.
Inputs
cov1
- The firstCovarianceMatrix
cov2
- The secondCovarianceMatrix
decimal_places
- How many decimal places to show the result to.return_nans_if_symbols_dont_match
- If the symbols don't match should it be an error. Or if false we only compare common symbols in bothCovarianceMatrix
s
Returns
An
Tuple
with the distance for correlations in first entry and distance for volatilities in the second.calculatemeanabs_distance(d1::Dict{Symbol,<:Real}, d2::Dict{Symbol,<:Real})
Calculates the mean absolute distance (elementwise in L1 norm) between two CovarianceMatrix
s.
Inputs
d1
- The firstDict
d2
- The secondDict
Returns
- A scalar with the mean distance between matching elements.
calculate_mean_abs_distance(
cov1::CovarianceModel,
cov2::CovarianceModel,
decimal_places::Integer = 8;
return_nans_if_symbols_dont_match::Bool = true,
)
Calculates the mean absolute distance (elementwise in L1 norm) between two CovarianceModel
s. Undefined if any labels differ between the two CovarianceModel
s.
Inputs
cov1
- The firstCovarianceModel
cov2
- The secondCovarianceModel
decimal_places
- How many decimal places to show the result to.return_nans_if_symbols_dont_match
- If the symbols don't match should it be an error. Or if false we only compare common symbols in bothCovarianceMatrix
s
Returns
- An
Tuple
with the distance for correlations in first entry and distance for volatilities in the second.
HighFrequencyCovariance.squared_frobenius
— Functionsquared_frobenius(x1::AbstractMatrix)
Returns the squared frobenius norm of a matrix. This is a real.
Inputs
x1
The matrix that you want the squared frobenius norm for.
Returns
- A Scalar.
HighFrequencyCovariance.squared_frobenius_distance
— Functionsquared_frobenius_distance(x1::AbstractMatrix, x2::AbstractMatrix = x1)
Returns the squared frobenius distance between two matrices. This is a real.
Inputs
x1
The first matrix.x2
The second matrix.
Returns
- A Scalar.
Used in volatility estimation techniques
HighFrequencyCovariance.default_num_grids
— Functiondefault_num_grids(ts::SortedDataFrame)
This gives a default number of intervals to divide a series of ticks over for the purposes of estimating returns and volatility.
Inputs
ts
- The tick data.
Returns
- An integer for the number of intervals.
Used in covariance estimation techniques
HighFrequencyCovariance.get_all_refresh_times
— Functionget_all_refresh_times(
ts::SortedDataFrame,
assets::Vector{Symbol} = get_assets(ts);
start_time::R = minimum(ts.df[:, ts.time]),
) where R<:Real
Get a vector of all refresh times when all assets have an updated price. So if there are assets A and B that trade at times (1,5,6,7,10) and (2,5,7,9) then the refresh times are (2,5,7,10) as at these four times there are updated prices for all assets that have happened since the previous refresh time.
Inputs
ts
- The tick data.assets
- The assets of interest.start_time
- From what time should we start looking for updated prices.
Returns
- A
Vector
of refresh times.
HighFrequencyCovariance.latest_value
— Functionlatest_value(
ts::SortedDataFrame,
at_times::Vector{<:Real};
assets::Vector{Symbol} = get_assets(ts),
)
Get the latest price at a each input time.
Inputs
ts
- The tick data.at_times
- The times you want the latest prices for.assets
- The assets you want latest prices for.
Returns
- A
DataFrame
. Rows are for each time specified in at_times. Columns are for each asset.
HighFrequencyCovariance.time_between_refreshes
— Functiontime_between_refreshes(
ts::SortedDataFrame;
assets::Vector{Symbol} = get_assets(ts),
)
Get a DataFrame
showing how many time is between each refresh and how many ticks in total.
Inputs
ts
- Tick data.assets
- AVector
of labels.
Returns
- A
DataFrame
summarising the average number of time between ticks for each asset.
HighFrequencyCovariance.random_value_in_interval
— Functionrandom_value_in_interval(
ts::SortedDataFrame,
at_times::Vector{<:Real};
assets::Vector{Symbol} = get_assets(ts),
twister_arb_value_in_interval::MersenneTwister = MersenneTwister(2604),
)
Get a random value in an interval. So if you input times 1,7,8 then for the second entry it will pick a random update (if any exist) between times 1 and 7.
Inputs
ts
- The tick data.at_times
- The times that seperate the intervals of interest.assets
- The assets of interest.twister_arb_value_in_interval
- The RNG used in selecting the random interval.
Returns
- A
DataFrame
with prices for each asset from random ticks in each interval.
HighFrequencyCovariance.g
— Constantconst g = (f = x-> min(x, 1-x), psi = 0.25)
This named tuple gives the preaveraging kernel function and integral over the unit interval that was used in the paper. It is used by default in the preaveraged_covariance method but can be overwritten with alternative kernels.
References
Christensen K, Podolskij M, Vetter M (2013). “On covariation estimation for multivariate continuous Itô semimartingales with noise in non-synchronous observation schemes.” Journal of Multivariate Analysis, 120, 59–84. doi:10.1016/j.jmva.2013.05.002.
HighFrequencyCovariance.get_refresh_times_and_prices
— Functionget_refresh_times_and_prices(ts::SortedDataFrame, asset1::Symbol, asset2::Symbol)
This returns a vector of prices and refresh times given two input symbols
Inputs
ts
- The tick data.asset1
- The first asset's name.asset2
- The second asset's name.
Returns
- A
Vector
of prices - A
Vector
of times corresponding to these prices.
HighFrequencyCovariance.time_period_ratio
— Functiontime_period_ratio(neww::Dates.Period, oldd::Dates.Period)
This calculates the ratio of the interval length between two periods. So if neww is twice as long a period as oldd it will return a 2.0.
Inputs
neww
- A time periodoldd
- A time period
Returns
- A real number.
HighFrequencyCovariance.safe_multiply_period
— Functionsafe_multiply_period(scalar::Real, period::Dates.Period)
This multiplies a time period by a scalar. So if period is Dates.Hour(1) and we multiply by 2 we will get two hours (although that will be expressed in Nanosecond units).
Inputs
scalar
- A real numberperiod
- A time period
Returns
- A time period expressed in Nanosecond units.
HighFrequencyCovariance.weighted_mean
— Functionweighted_mean(x::Vector, w::Vector)
This calculates a weighted mean given vectors for values and for weights. If the sum of the absolute values of the weights is close to zero a simple mean of x is returned instead.
Inputs
x
- AVector
of the values you want to average.w
- AVector
of the weights you want to use.
Returns
- A
Real
number for the weighted mean.
HighFrequencyCovariance.is_missing_nan_inf
— Functionis_missing_nan_inf(x) = (ismissing(x) || isnan(x)) || isinf(x)
This tests if a value is missing, nan or inf and returns true if one of these things is true.
Inputs
x
- The object to test for missing, inf, nan.
Returns
- A
Bool
for whether or not one of these conditions is true.
HighFrequencyCovariance.next_tick
— Functionnext_tick(ts::SortedDataFrame, from_index::I;
assets::Vector{Symbol} = get_assets(ts)) where I<:Integer
This gets the next tick by which every asset has a refreshed price after a certain row index.
Inputs
ts
- The tick data.from_index
- The index in your ts.df to start looking from.assets
- The vector of assets that you want to get a refresh time by which each has a refreshed price.
Returns
- A
Real
orMissing
for the refresh time. If it is a real it is the time. If one asset did not update in your data then a missing is returned. - An
Integer
orMissing
for the refresh tick. for what index in your data the refresh happened by. If one asset did not refresh this will be a missing.
HighFrequencyCovariance.simple_volatility_given_returns
— Functionsimple_volatility_given_returns(returns::Vector{R}) where R<:Real
Calculates volatility using a vector of returns using the square root of squared returns.
Inputs
returns
- A vector of returns
Returns
- A
Real
Kernels used in the BNHLS method
HighFrequencyCovariance.HFC_Kernel
— TypeA kernel used in the bnhls covariance method.
HighFrequencyCovariance.parzen
— ConstantA parzen kernel used in the bnhls covariance method.
HighFrequencyCovariance.quadratic_spectral
— ConstantA quadratic_spectral kernel used in the bnhls covariance method.
HighFrequencyCovariance.fejer
— ConstantA fejer kernel used in the bnhls covariance method.
HighFrequencyCovariance.tukey_hanning
— ConstantA tukey_hanning kernel used in the bnhls covariance method.
HighFrequencyCovariance.bnhls_2008
— ConstantA bnhls_2008 kernel used in the bnhls covariance method.
Used in nearest correlation regularisation
HighFrequencyCovariance.project_to_S
— Functionproject_to_S(
A::Hermitian,
W_root::Union{Hermitian,Diagonal};
W_inv_sqrt::Union{Hermitian,Diagonal} = sqrt_psd(inv(W_root^2)),
)
project_to_S(
A::Diagonal,
W_root::Union{Hermitian,Diagonal};
W_inv_sqrt::Union{Hermitian,Diagonal,Missing} = missing,
)
This maps a matrix to the nearest psd matrix. W_root
should be the principal square root of a psd Hermitian weighting matrix, W
. W_inv_sqrt
should be the corresponding square root of the inverse of W
. nearest_psd_matrix
is a simpler interface for this function however it does not allow weighting matrices to be specified.
Inputs
A
- The matrix you want to project to the S space. This can be aDiagonal
or aHermitian
. Note that if you input aDiagonal
matrix then it is already in the S space and so it will be returned without any calculation.W_root
- The inverse weighting matrix.W_inv_sqrt
- The root ofW_root
. This is calculated if you don't have it but it can save some calculation effort if you already have it.
Outputs
- A
Hermitian
.
References
Higham, N. J. 2001. Theorem 3.2
HighFrequencyCovariance.project_to_U
— Functionproject_to_U(A::Union{Diagonal,Hermitian}, invW::Hermitian)
project_to_U(A::Union{Diagonal,Hermitian}, invW::Diagonal)
This maps the Hermitian/Hermitian matrix A
to the nearest matrix in the U space (the space of all unit diagonal matrices as defined by Higham 2001). The inverse weight matrix invW
determines how much to adjust each element to get it to be unit diagonal. In other words it is used to determine what is the nearest correlation matrix. The weight matrix must be Hermitian positive definite. We use the W-norm (as defined by Higham 2001).
Inputs
A
- The matrix you want to project to the U spaceinvW
- The inverse weighting matrix.
Outputs
- A
Diagonal
or aHermitian
.
References
Higham, N. J. 2001. Bottom of page 335.
HighFrequencyCovariance.iterate_higham
— Functioniterate_higham(
Y::Union{Hermitian,Diagonal},
Dykstra::Union{Hermitian,Diagonal},
W_root::Union{Hermitian,Diagonal},
W_inv::Union{Hermitian,Diagonal},
W_inv_sqrt::Union{Hermitian,Diagonal},
)
Do one iterate mapping the input matrix to the S space (of psd matrices) and then to the U space (unit diagonal and all other entries below 1 in absolute value). Returns the updated matrix and the next iterate's Dykstra correction.
Inputs
Y
- The matrix you want to project to the iterate towards the space of valid correlation matrices.Dykstra
- The Dykstra correction matrix.W_root
- The root ofW
.W_inv
- The inverse ofW
.W_inv_sqrt
- The root of the inverse ofW
.
Outputs
- A
Hermitian
. - An updated Dykstra correction matrix.
References
Higham, N. J. 2001. Algorithm 3.3