Skip to content

Instantly share code, notes, and snippets.

@StuartGordonReid
Created April 17, 2016 08:04
Show Gist options
  • Save StuartGordonReid/5e1345253fccab0b2e37409e4a032e69 to your computer and use it in GitHub Desktop.
Save StuartGordonReid/5e1345253fccab0b2e37409e4a032e69 to your computer and use it in GitHub Desktop.
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
noise <- rnorm(t, mean = 0, sd = s)
returns <- signal + noise
# Binarize the data.
returns[returns < 0] <- 0
returns[returns > 0] <- 1
# Convert into raw hexadecimal.
hexrets <- bin2rawhex(returns)
# Compute the compression ratio
cratios <- c(cratios, length(memCompress(hexrets)) /
length(hexrets))
# Plot the returns for the eye ball test.
dates <- seq.Date(Sys.Date(), Sys.Date() + (252 * 7) - 1, 1)
# plot.ts(xts(signal + noise, order.by = dates))
if (s %in% c(0.010, 0.015, 0.020)) {
charts.PerformanceSummary(xts(signal + noise, order.by = dates))
}
}
# Plot the compression ratios.
plot(sds, cratios)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment