This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ssa = function(unq_n, size, sort=FALSE) { | |
if (unq_n > size) return(sample.int(unq_n, size)) | |
unq_sub = seq_len(unq_n) | |
ans = sample(c(unq_sub, sample(unq_sub, size=max(size-unq_n, 0), replace=TRUE))) | |
if (sort) sort(ans) else ans | |
} | |
set.seed(108) | |
library(data.table) | |
options(width=200) | |
options(datatable.auto.index=FALSE, datatable.verbose=FALSE) ## not needed but just to be future proof if forder will setindex |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ssa = function(unq_n, size, sort=FALSE) { | |
if (unq_n > size) return(sample.int(unq_n, size)) | |
unq_sub = seq_len(unq_n) | |
ans = sample(c(unq_sub, sample(unq_sub, size=max(size-unq_n, 0), replace=TRUE))) | |
if (sort) sort(ans) else ans | |
} | |
set.seed(108) | |
library(data.table) | |
options(width=200) | |
options(datatable.auto.index=FALSE, datatable.verbose=FALSE) ## not needed but just to be future proof if forder will setindex |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(data.table) | |
setDTthreads(40L) | |
test.data.table("mergelist.Rraw") ## warmup | |
set.seed(108) | |
N = 1e8L | |
## medium cardinality | |
region = data.table(region_id=seq_len(N/1e3), key="region_id") | |
division = data.table(division_id=seq_len(N/1e2), region_id=sample(N/1e3, N/1e2, TRUE), key="division_id") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## credits | |
# https://dzone.com/articles/olap-operation-r | |
# Setup the dimension tables | |
state_table <- data.frame(key=c("CA", "NY", "WA", "ON", "QU"), | |
name=c("California", "new York", "Washington", "Ontario", "Quebec"), | |
country=c("USA", "USA", "USA", "Canada", "Canada")) | |
month_table <- data.frame(key=1:12, | |
desc=c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"), | |
quarter=c("Q1","Q1","Q1","Q2","Q2","Q2","Q3","Q3","Q3","Q4","Q4","Q4")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
f = seq(3,100,3) | |
b = seq(5,100,5) | |
fb = f[f %in% b] | |
f = f[!f %in% fb] | |
b = b[!b %in% fb] | |
x = as.character(1:100) | |
x[f] = "Fizz" | |
x[b] = "Buzz" | |
x[fb] = "FizzBuzz" | |
cat(x, sep = "\n") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(TTR) | |
library(xts) | |
library(data.table) | |
# - [x] use list element names instead of `label` argument | |
# - [x] allow to reuse preceeded indicators, reduce amount of indicator computation | |
# - [x] cleaner way of providing indicators and signals, provide R language object directly instead of `get` object by name | |
ma_crossover = function(ma_slow, ma_fast){ | |
setDT(list(sig_buy = ma_fast > ma_slow, sig_sell = ma_fast < ma_slow))[sig_buy==TRUE, sig := 1L][sig_sell==TRUE, sig := -1L][!sig%in%c(1L,-1L), sig := 0L]$sig |