Skip to content

Instantly share code, notes, and snippets.

View StuartGordonReid's full-sized avatar

Stuart Gordon Reid StuartGordonReid

View GitHub Profile
# Plot fifteen asset price paths without stochastic volatility.
charts.PerformanceSummary(returnProcesses(15, t = (252*5),
stochastic.volatility = FALSE),
colorset = seq(1,15))
# Plot fifteen asset price paths with stochastic volatility.
charts.PerformanceSummary(returnProcesses(15, t = (252*5),
stochastic.volatility = TRUE),
colorset = seq(1,15))
testRobustness <- function(t = (252 * 7)) {
# Generate an underlying signal.
signal <- sin(seq(1, t)) / 50
signal <- signal - mean(signal)
# For different noise levels
sds <- seq(0.0, 0.020, 0.0005)
cratios <- c()
for (s in sds) {
# Generate a noisy signal
#' @title Given a log price process, X, compute the Z-score which can be used
#' to accept or reject the hypothesis that the process evolved according to a
#' Brownian Motion model with drift and stochastic volatility.
#'
#' @description Given a log price process, X, and a sampling interval, q, this
#' method returns a Z score indicating the confidence we have that X evolved
#' according to a Brownian Motion mode with drift and stochastic volatility. This
#' heteroskedasticity-consistent variance ratio test essentially checks to see
#' whether or not the observed Mr statistic for the number of observations, is
#' within or out of the limiting distribution defined by the Asymptotic Variance.
#' @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.
#' @title Compute the Mr statistic.
#'
#' @description Compute the Mr statistic. The Md statistic is the ratio between
#' two estimate values for Sigma computed using the calibrateSigma function for
#' sampling intervals 1 and the estimate value for Sima computed using the
#' CalibrateSigmaOverlapping function for sampling inveral q minus 1.
#' This statistic should converge to zero.
#'
#' @inheritParams calibrateSigma
#' @return Mr double :: The Mr statistic defined by Lo and MacKinlay.
#' @title Compute the Md statistic.
#'
#' @description Compute the Md statistic. The Md statistic is the difference
#' between two estimate values for Sigma computed using the calibrateSigma
#' function for sampling intervals 1 and the estimate value for Sima computed
#' using the CalibrateSigmaOverlapping function for sampling inveral q.
#' This statistic should converge to zero.
#'
#' @inheritParams calibrateSigma
#' @return Md double :: The Md statistic defined by Lo and MacKinlay.
#' @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.
#'
#' @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.
#'
#' @title Estimator for the value of mu. Mu is the drift component in the
#' Geometric Brownian Motion model.
#'
#' @description Given a log price process, this function estimates the value of
#' mu. Mu is the daily component of the returns which is attributable to upward,
#' or downward, drift. This estimate can be annualized.
#'
#' @param X vector :: A log price process.
#' @param annualize logical :: Annualize the parameter estimate.
#' @return mu.est double :: The estimated value of mu.
# Load the required packages.
library(PerformanceAnalytics)
library(xts)
#' @title Sample a random disturbance from either a normal distribution with a
#' constant standard deviation (Geometric Brownian Motion model) or from a
#' distribution with a stochastic standard deviation (Stochastic Volatility GBM)
#'
#' @description Given a long run random disturbance mean, mu, and a standard