Session 3 Statistical Learning

Objectives and Learning

Tools and Packages

Simple descriptive statistics

##    price.cad         vol.cad         price.usd         vol.usd      
##  Min.   : 231.9   Min.   :  2.47   Min.   : 201.6   Min.   : 103.6  
##  1st Qu.: 345.1   1st Qu.: 25.76   1st Qu.: 304.5   1st Qu.: 823.2  
##  Median : 488.6   Median : 40.77   Median : 434.6   Median :1669.5  
##  Mean   : 502.1   Mean   : 44.29   Mean   : 458.7   Mean   :1682.9  
##  3rd Qu.: 605.3   3rd Qu.: 58.28   3rd Qu.: 565.0   3rd Qu.:2399.4  
##  Max.   :2238.8   Max.   :155.59   Max.   :1334.4   Max.   :3805.7  
##                                                                     
##       year          month        price.usd.ret       price.cad.ret       
##  Min.   :2014   Min.   : 1.000   Min.   :-623.7100   Min.   :-1664.1400  
##  1st Qu.:2014   1st Qu.: 4.000   1st Qu.: -47.5250   1st Qu.:  -24.8525  
##  Median :2015   Median : 6.000   Median :   0.7100   Median :    0.0000  
##  Mean   :2015   Mean   : 6.188   Mean   :  -0.5668   Mean   :   -0.5413  
##  3rd Qu.:2015   3rd Qu.: 9.000   3rd Qu.:  42.1400   3rd Qu.:   28.3150  
##  Max.   :2016   Max.   :12.000   Max.   : 902.9900   Max.   : 1641.0800  
##                                  NA's   :1           NA's   :1
##            price.cad vol.cad price.usd vol.usd year month price.usd.ret
## 1398124800   569.037  19.589   530.665 599.444 2014   4.0         5.553
## 1398297600   568.250  20.261   525.112 591.956 2014   4.0         8.909
## 1398384000   559.379  19.782   516.203 581.668 2014   4.1         4.174
## 1398470400   569.745  21.748   512.029 590.979 2014   4.2         4.863
## 1398556800   561.883  22.951   507.166 614.774 2014   4.3         4.682
## 1398643200   553.366  24.443   502.484 611.701 2014   4.4        10.320
##            price.cad.ret
## 1398124800         0.787
## 1398297600         8.871
## 1398384000       -10.366
## 1398470400         7.862
## 1398556800         8.517
## 1398643200         6.052

Regresssion and Statistical Inference

##      Index             price.cad         vol.cad         price.usd     
##  Min.   :1.398e+09   Min.   : 231.9   Min.   :  2.47   Min.   : 201.6  
##  1st Qu.:1.415e+09   1st Qu.: 345.1   1st Qu.: 25.76   1st Qu.: 304.5  
##  Median :1.432e+09   Median : 488.6   Median : 40.77   Median : 434.6  
##  Mean   :1.433e+09   Mean   : 502.1   Mean   : 44.29   Mean   : 458.7  
##  3rd Qu.:1.451e+09   3rd Qu.: 605.3   3rd Qu.: 58.28   3rd Qu.: 565.0  
##  Max.   :1.467e+09   Max.   :2238.8   Max.   :155.59   Max.   :1334.4  
##                                                                        
##     vol.usd            year          month        price.usd.ret      
##  Min.   : 103.6   Min.   :2014   Min.   : 1.000   Min.   :-623.7100  
##  1st Qu.: 823.2   1st Qu.:2014   1st Qu.: 4.000   1st Qu.: -47.5250  
##  Median :1669.5   Median :2015   Median : 6.000   Median :   0.7100  
##  Mean   :1682.9   Mean   :2015   Mean   : 6.188   Mean   :  -0.5668  
##  3rd Qu.:2399.4   3rd Qu.:2015   3rd Qu.: 9.000   3rd Qu.:  42.1400  
##  Max.   :3805.7   Max.   :2016   Max.   :12.000   Max.   : 902.9900  
##                                                   NA's   :1          
##  price.cad.ret       
##  Min.   :-1664.1400  
##  1st Qu.:  -24.8525  
##  Median :    0.0000  
##  Mean   :   -0.5413  
##  3rd Qu.:   28.3150  
##  Max.   : 1641.0800  
##  NA's   :1
##            price.cad vol.cad price.usd vol.usd year month price.usd.ret
## 1398124800   569.037  19.589   530.665 599.444 2014   4.0         5.553
## 1398297600   568.250  20.261   525.112 591.956 2014   4.0         8.909
## 1398384000   559.379  19.782   516.203 581.668 2014   4.1         4.174
## 1398470400   569.745  21.748   512.029 590.979 2014   4.2         4.863
## 1398556800   561.883  22.951   507.166 614.774 2014   4.3         4.682
## 1398643200   553.366  24.443   502.484 611.701 2014   4.4        10.320
##            price.cad.ret
## 1398124800         0.787
## 1398297600         8.871
## 1398384000       -10.366
## 1398470400         7.862
## 1398556800         8.517
## 1398643200         6.052

Time Series Forcasting

##     Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
## 710       532.9656 408.0431 657.8882 341.9132 724.0181
## 711       528.9791 400.3290 657.6292 332.2258 725.7325
## 712       529.4507 398.7647 660.1367 329.5837 729.3176
## 713       526.5083 395.0025 658.0140 325.3876 727.6289
## 714       530.3989 397.0482 663.7497 326.4566 734.3413

Outlier Detection

#install.packages("devtools")
#devtools::install_github("twitter/AnomalyDetection")
#install.packages("devtools")
#devtools::install_github("twitter/BreakoutDetection")
library(BreakoutDetection)
library(AnomalyDetection)
value<-prices[,c("date","price.usd")]
value$date<-as.POSIXlt(value$date,format="%Y-%m-%d")
res = AnomalyDetectionTs(value,max_anoms=0.1,direction='both',plot=TRUE)
res$plot

names(value)<-c("timestamp","count")
res = breakout(value, min.size=24, method='multi', beta=.001, degree=1, plot=TRUE)
res$plot

Machine Learning

l.1<-lag(prices.xts$price.usd.ret,n=1);names(l.1)<-"L1";prices.xts<-merge(prices.xts,l.1)
l.2<-lag(prices.xts$price.usd.ret,n=2);names(l.2)<-"L2";prices.xts<-merge(prices.xts,l.2)
l.3<-lag(prices.xts$price.usd.ret,n=3);names(l.3)<-"L3";prices.xts<-merge(prices.xts,l.3)
l.5<-lag(prices.xts$price.usd.ret,n=5);names(l.5)<-"L5";prices.xts<-merge(prices.xts,l.5)
l.10<-lag(prices.xts$price.usd.ret,n=10);names(l.10)<-"L10";prices.xts<-merge(prices.xts,l.10)
tmp.stg<-data.frame(prices.xts,row.names=NULL)
require(caret)
tmp.stg<-na.omit(tmp.stg)
featurePlot(x=tmp.stg[,-7],y=tmp.stg$price.usd.ret,type=c("g","p","smooth"))
forTraining<-createDataPartition(y=tmp.stg$price.usd.ret,p=3/4)[[1]]
trainingSet<-tmp.stg[forTraining,]
testSet<-tmp.stg[-forTraining,]
set.seed(975)

modFormula<-paste("price.usd.ret ~ L1+L2+L3+L5+L10")
modFormula<-as.formula(modFormula)
controlObject<-trainControl(
                      method="repeatedcv",
                      repeats=5,
                      number=10)

lmModel<-train(modFormula,data=trainingSet,method="lm",trControl=controlObject)

svmModel<-train(modFormula,data=trainingSet,
                 method="svmRadial",
                 preProc=c("center","scale"),
                 trControl=controlObject)


nnetGrid<-expand.grid(.decay=c(0.001,0.01,0.1),.size=seq(1,27,by=2),.bag=FALSE)
#nnetModel<-train(modFormula,data=trainingSet,
#                 method="avNNet",
#                 tuneGrid=nnetGrid,
#                 preProc=c("center","scale"),
#                 trControl=controlObject,
#                 linout=TRUE,
#                 trace=FALSE,
#                 maxit=1000
#                 )

rpartModel<-train(modFormula,data=trainingSet,
                 method="rpart",
                 tuneLength=30,
                 trControl=controlObject)

allResamples<-resamples(list("Linear"=lmModel,"SVM"=svmModel,"CART"=rpartModel))

caret::parallelplot.resamples(allResamples,metric = "Rsquared")