Internal Functions

Metrics for distances between CovarianceMatrix structs

HighFrequencyCovariance.calculate_mean_abs_distanceFunction
calculate_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 CovarianceMatrixs. Undefined if any labels differ between the two CovarianceMatrixs.

Inputs

  • cov1 - The first CovarianceMatrix
  • cov2 - The second CovarianceMatrix
  • 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 both CovarianceMatrixs

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 CovarianceMatrixs.

Inputs

  • d1 - The first Dict
  • d2 - The second Dict

Returns

  • A scalar with the mean distance between matching elements.
source
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 CovarianceModels. Undefined if any labels differ between the two CovarianceModels.

Inputs

  • cov1 - The first CovarianceModel
  • cov2 - The second CovarianceModel
  • 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 both CovarianceMatrixs

Returns

  • An Tuple with the distance for correlations in first entry and distance for volatilities in the second.
source
HighFrequencyCovariance.squared_frobeniusFunction
squared_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.
source

Used in volatility estimation techniques

HighFrequencyCovariance.default_num_gridsFunction
default_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.
source

Used in covariance estimation techniques

HighFrequencyCovariance.get_all_refresh_timesFunction
get_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.
source
HighFrequencyCovariance.latest_valueFunction
latest_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.
source
HighFrequencyCovariance.time_between_refreshesFunction
time_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 - A Vector of labels.

Returns

  • A DataFrame summarising the average number of time between ticks for each asset.
source
HighFrequencyCovariance.random_value_in_intervalFunction
random_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.
source
HighFrequencyCovariance.gConstant
const 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.

source
HighFrequencyCovariance.get_refresh_times_and_pricesFunction
get_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.
source
HighFrequencyCovariance.time_period_ratioFunction
time_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 period
  • oldd - A time period

Returns

  • A real number.
source
HighFrequencyCovariance.safe_multiply_periodFunction
safe_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 number
  • period - A time period

Returns

  • A time period expressed in Nanosecond units.
source
HighFrequencyCovariance.weighted_meanFunction
weighted_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 - A Vector of the values you want to average.
  • w - A Vector of the weights you want to use.

Returns

  • A Real number for the weighted mean.
source
HighFrequencyCovariance.is_missing_nan_infFunction
is_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.
source
HighFrequencyCovariance.next_tickFunction
next_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 or Missing 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 or Missing 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.
source

Kernels used in the BNHLS method

Used in nearest correlation regularisation

HighFrequencyCovariance.project_to_SFunction
project_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 a Diagonal or a Hermitian. Note that if you input a Diagonal 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 of W_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

source
HighFrequencyCovariance.project_to_UFunction
project_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 space
  • invW - The inverse weighting matrix.

Outputs

  • A Diagonal or a Hermitian.

References

Higham, N. J. 2001. Bottom of page 335.

source
HighFrequencyCovariance.iterate_highamFunction
iterate_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 of W.
  • W_inv - The inverse of W.
  • W_inv_sqrt - The root of the inverse of W.

Outputs

  • A Hermitian.
  • An updated Dykstra correction matrix.

References

Higham, N. J. 2001. Algorithm 3.3

source