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