Skip to content

Instantly share code, notes, and snippets.

@StuartGordonReid
Created February 6, 2016 21:19
Show Gist options
  • Save StuartGordonReid/325177b59a31e0167524 to your computer and use it in GitHub Desktop.
Save StuartGordonReid/325177b59a31e0167524 to your computer and use it in GitHub Desktop.
#' @title Estimator for the value of the asymptotic variance of the Mr statistic.
#' This is equivalent to a weighted sum of the asymptotic variances for each of
#' the autocorrelation co-efficients under the null hypothesis.
#'
#' @details Given a log price process, X, and a sampling interval, q, this
#' method is used to estimate the asymptoticvariance of the Mr statistic in the
#' presence of stochastic volatility. In other words, it is a heteroskedasticity
#' consistent estimator of the variance of the Mr statistic. This parameter is
#' used to estimate the probability that the given log price process was
#' generated by a Brownian Motion model with drift and stochastic volatility.
#'
#' @param X vector :: A log price process.
#' @param q int :: The sampling interval for the estimator.
#'
calibrateAsymptoticVariance <- function(X, q) {
avar <- 0.0
for (j in 1:(q - 1)) {
theta <- calibrateDelta(X, q, j)
avar <- avar + (2 * (q - j) / q) ^ 2 * theta
}
return(avar)
}
#' @title Helper function for the calibrateAsymptoticVariance function.
#'
calibrateDelta <- function(X, q, j) {
# Get the estimate value for the drift component.
mu.est <- calibrateMu(X, FALSE)
# Estimate the asymptotice variance given q and j.
n <- floor(length(X)/q)
numerator <- 0.0
for (k in (j + 2):(n * q)) {
t1 <- (X[k] - X[k - 1] - mu.est)^2
t2 <- (X[k - j] - X[k - j - 1] - mu.est)^2
numerator <- numerator + (t1 * t2)
}
denominator <- 0.0
for (k in 2:(n * q))
denominator <- denominator + (X[k] - X[k - 1] - mu.est)^2
# Compute and return the statistic.
thetaJ <- (n * q * numerator) / (denominator^2)
return(thetaJ)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment