Skip to content

Instantly share code, notes, and snippets.

@StuartGordonReid
Created February 6, 2016 15:19
Show Gist options
  • Save StuartGordonReid/dfce554feee7f0b27228 to your computer and use it in GitHub Desktop.
Save StuartGordonReid/dfce554feee7f0b27228 to your computer and use it in GitHub Desktop.
#' @title A more efficient estimator for the value of sigma. Sigma is the
#' standard deviation of the random component of returns in the Geometric
#' Brownian Motion model. This estimate can be calculated in an unbiased manner.
#'
#' @description Given a log price process and a parameter, q, which specifies
#' the sampling intervel this function estimates the value of Sigma. Sigma
#' represents the standarddeviation of the random disturbance component of
#' daily returns. This estimate can be annualized and can be computed in
#' a biased or unbiased manner.
#'
#' @details The difference between this estimator and the estimator defined in
#' the calibrateSigma function is that this method makes use of overlapping
#' windows of log price data. Whilst this does increase the number of
#' observations and improve the accuracy of the estimator it is no longer
#' unbiased. That said, Monte Carlo simulations indicate that this bias is
#' negligable and, in fact, this estimator is more accurate than the one
#' defined by calibrateSigma.
#'
#' @inheritParams calibrateSigma
#' @return sd.est double :: The estimated value of Sigma.
#'
calibrateSigmaOverlapping <- function(X, q = 1, annualize = TRUE,
unbiased = TRUE) {
# Get the estimate value for the drift component.
mu.est <- calibrateMu(X, annualize = FALSE)
# Ensure that the format of X is appropriate.
X <- as.numeric(as.vector(X))
# Calculate the number of times q goes into the length of X.
n <- floor(length(X)/q)
sd.est <- 0.0
for (t in (q + 1):(n * q))
sd.est <- sd.est + (X[t] - X[t - q] - (q * mu.est))^2
# Calculate the average sigma using the unbiased or biased method.
if (!unbiased) sd.est <- sd.est / (n * (q^2))
else sd.est <- sd.est / ((q * ((n * q) - q + 1)) * (1 - (q / (n * q))))
if (!annualize) return(sd.est)
else return(sqrt((sd.est * 252)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment