Skip to content

Instantly share code, notes, and snippets.

View ProbablePattern's full-sized avatar
💭
\m/(-_-)\m/

Stephen ProbablePattern

💭
\m/(-_-)\m/
View GitHub Profile
@ProbablePattern
ProbablePattern / keybase.md
Created August 14, 2014 11:26
Keybase Verification

Keybase proof

I hereby claim:

  • I am ProbablePattern on github.
  • I am probablepattern (https://keybase.io/probablepattern) on keybase.
  • I have a public key whose fingerprint is 9FC3 23BE 11D8 E85F 6551 4725 4C92 54DA B583 A3AE

To claim this, I am signing this object:

@ProbablePattern
ProbablePattern / VPIN
Created May 15, 2014 23:20
VPIN calculation using bulk-volume classification
#### VPIN calculation #########################################################
#install.packages('fasttime',repos='http://www.rforge.net/')
require(data.table); require(fasttime); require(plyr)
# Assuming TAQ data is arranged in 1 year stock csv files
stock=fread('/TAQ_data.csv'); stock=stock[,1:3,with=FALSE]
setnames(stock,colnames(stock),c('DateTime','Price','Volume'));
stock[,DateTime:=paste(paste(substr(DateTime,1,4),substr(DateTime,5,6),
substr(DateTime,7,8),sep='-'),substr(DateTime,10,17))]
setkey(stock,DateTime);
stock=as.xts(stock[,2:3,with=FALSE],unique=FALSE,
# Read data
dataset=read.csv("/yourdirectory/yourfile.csv",skip=1,header=TRUE)
# Create DB
testdb=dbConnect(SQLite(), dbname = "/yourdirectory/testdb.dat")
# Write the data set into a table called table1
dbWriteTable(testdb,"table1",dataset)
# List the tables available in the database
dbListTables(testdb)
# Remove the original data to free up memory
rm(dataset)
require(boot)
# Simple example of bootstrapping mean from 100 random numbers
x <- rnorm(100)
y <- tsboot(x, mean, R=10, l=20, sim=”fixed”)
# histogram of R number of statistic (index = t of interest from tsboot output)
plot(y, index=1)
# matrix of the data index (column) occurring in bootstrap replicate (row)
y.array <- boot.array(y)
# Write to CSV
write.csv(y.array,”/yourdirectory/yourfile1.csv”)
require(foreach); require(xtable)
doublesort=function(data,x,y,q) {
breaks1=quantile(data[,x],probs=seq(0,1,1/q))
tiles=foreach(i=1:q,.combine=cbind) %dopar% {
Q=data[data[,x]>=breaks1[i] & data[,x]<breaks1[i+1],y]
breaks2=quantile(Q,probs=seq(0,1,1/q))
foreach(j=1:q,.combine=rbind) %dopar% mean(Q[Q>=breaks2[j] & Q<breaks2[j+1]])
}
colnames(tiles)=foreach(i=1:q) %do% paste(x,i)
row.names(tiles)=foreach(i=1:q) %do% paste(y,i)