Skip to content

Instantly share code, notes, and snippets.

@StuartGordonReid
Created April 17, 2016 10:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save StuartGordonReid/e52f03fd7b832f95a346c1967a4c4de2 to your computer and use it in GitHub Desktop.
Save StuartGordonReid/e52f03fd7b832f95a346c1967a4c4de2 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)
}
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)
# Add some noise to get a return series.
returns <- signal + rnorm(t, mean = 0.0, sd = sd)
# Keep track of our weights.
weights <- rep(0.0, w)
for (t in (w + 1):length(returns)) {
# Calculate the cumulative return to yesterday.
cumret <- mean(1 + returns[(t-w):(t-1)]) - 1
# Determine if we are risk-on or risk-off:
if (cumret < 0) weights <- c(weights, 1.0)
else weights <- c(weights, -1.0)
}
# Turn our hypothetical returns into time series.
returns.weighted <- returns * weights
dates <- dates <- seq.Date(Sys.Date(), Sys.Date() +
length(returns) - 1, 1)
# Keep track of each of the market returns.
if (is.null(markets)) markets <- xts(returns, order.by = dates)
else markets <- merge.xts(markets, xts(returns, order.by = dates))
# Keep track of each of the strategy returns.
if (is.null(strats)) strats <- xts(returns.weighted, order.by = dates)
else strats <- merge.xts(strats, xts(returns.weighted, order.by = dates))
}
# Plot the returns generated by our toy markets.
charts.PerformanceSummary(xts(markets, order.by = dates),
colorset = (1:ncol(markets)),
legend.loc = NULL)
# Plot the returns generated by out strategy.
charts.PerformanceSummary(xts(strats, order.by = dates),
colorset = (1:ncol(strats)),
legend.loc = NULL)
# Plot the averages over the simulations to make the picture clearer.
mean.markets <- xts(rowSums(markets) / sims, order.by = index(markets))
mean.strats <- xts(rowSums(strats) / sims, order.by = index(markets))
charts.PerformanceSummary(merge.xts(mean.markets, mean.strats),
legend.loc = NULL)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment