Skip to content

Instantly share code, notes, and snippets.

Created February 6, 2016 12:28
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
#' @title 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 a biased or 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 parameter, q, specifies the sampling interval to use when
#' estimating sigma. When q = 1 the function will use every day's prices, when
#' q = 2 the function will use every second day's prices, and so on and so
#' forth. For a sufficient number of days the estimation of sigma under
#' different sampling intervales e.g. 2 and 4, should converge.
#' @param X vector :: A log price process.
#' @param q int :: The sampling interval for the estimator.
#' @param annualize logical :: Annualize the parameter estimate. True or False.
#' @param unbiased logical :: Use the unbiased estimate. True or False.
#' @return sd.est double :: The estimated value of Sigma.
calibrateSigma <- 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 2:n)
sd.est <- sd.est + (X[t * q] - X[(t * q) - q] - (q * mu.est))^2
# Calculate the average sigma using the unbiased or biased method.
if (!unbiased) sd.est <- sd.est / (q * n)
else sd.est <- sd.est / (q * n - 1)
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