Skip to content

Instantly share code, notes, and snippets.

View StuartGordonReid's full-sized avatar

Stuart Gordon Reid StuartGordonReid

View GitHub Profile
results <- emh::is_random(my.zoo.object)
emh::plot_results(results)
View(results)
library(devtools)
devtools::install_github(repo="stuartgordonreid/emh")
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
bettingStrategyProof <- function(t = (252 * 7), w = 5, sd = 0.02, sims = 30) {
# Store market and strategy returns.
markets <- NULL
strats <- NULL
for (i in 1:sims) {
# Generate an underlying signal.
signal <- sin(seq(1, t)) / 50
signal <- signal - mean(signal)
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
@StuartGordonReid
StuartGordonReid / CompressionTest.R
Created April 16, 2016 20:17
Compression Test
compressionTest <- function(code, years = 7, algo = "g") {
# The generic Quandl API key for TuringFinance.
Quandl.api_key("t6Rn1d5N1W6Qt4jJq_zC")
# Download the raw price data.
data <- Quandl(code, rows = -1, type = "xts")
# Extract the variable we are interested in.
ix.ac <- which(colnames(data) == "Adjusted Close")
if (length(ix.ac) == 0)
#' @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.