foreach (iterator) %dopar%past.rinfinance.com/agenda/2009/UIC-Lewis 4-25-09.pdf · 2010. 1. 19. ·...
Transcript of foreach (iterator) %dopar%past.rinfinance.com/agenda/2009/UIC-Lewis 4-25-09.pdf · 2010. 1. 19. ·...
![Page 1: foreach (iterator) %dopar%past.rinfinance.com/agenda/2009/UIC-Lewis 4-25-09.pdf · 2010. 1. 19. · CONFIDENTIAL REvolution Computing 2009 9 A simple example: backtesting a technical](https://reader033.fdocuments.us/reader033/viewer/2022060915/60a8ff9e0adc9a790b07438a/html5/thumbnails/1.jpg)
![Page 2: foreach (iterator) %dopar%past.rinfinance.com/agenda/2009/UIC-Lewis 4-25-09.pdf · 2010. 1. 19. · CONFIDENTIAL REvolution Computing 2009 9 A simple example: backtesting a technical](https://reader033.fdocuments.us/reader033/viewer/2022060915/60a8ff9e0adc9a790b07438a/html5/thumbnails/2.jpg)
CONFIDENTIAL
2REvolution Computing 2009
foreach + iterators
foreach• A for-loop/lapply hybrid• Similar to foreach and listcomprehensions in Python and otherlanguages
iterators• Similar to Java iterators• nextElem ()
The content of this web site is Copyright 2009 by REvolution Computing, Inc. All rights reserved. The contents of this presentation is Copyright 2009 by REvolution Computing, Inc. All rights reserved
![Page 3: foreach (iterator) %dopar%past.rinfinance.com/agenda/2009/UIC-Lewis 4-25-09.pdf · 2010. 1. 19. · CONFIDENTIAL REvolution Computing 2009 9 A simple example: backtesting a technical](https://reader033.fdocuments.us/reader033/viewer/2022060915/60a8ff9e0adc9a790b07438a/html5/thumbnails/3.jpg)
CONFIDENTIAL
3REvolution Computing 2009
foreach (iterator) %dopar%
{
statements
}
The contents of this presentation is Copyright 2009 by REvolution Computing, Inc. All rights reserved
![Page 4: foreach (iterator) %dopar%past.rinfinance.com/agenda/2009/UIC-Lewis 4-25-09.pdf · 2010. 1. 19. · CONFIDENTIAL REvolution Computing 2009 9 A simple example: backtesting a technical](https://reader033.fdocuments.us/reader033/viewer/2022060915/60a8ff9e0adc9a790b07438a/html5/thumbnails/4.jpg)
CONFIDENTIAL
4REvolution Computing 2009
> foreach (j=1:4) %dopar% {sqrt (j)}
[[1]]
[1] 1
[[2]]
[1] 1.414214
[[3]]
[1] 1.732051
[[4]]
[1] 2
The contents of this presentation is Copyright 2009 by REvolution Computing, Inc. All rights reserved
![Page 5: foreach (iterator) %dopar%past.rinfinance.com/agenda/2009/UIC-Lewis 4-25-09.pdf · 2010. 1. 19. · CONFIDENTIAL REvolution Computing 2009 9 A simple example: backtesting a technical](https://reader033.fdocuments.us/reader033/viewer/2022060915/60a8ff9e0adc9a790b07438a/html5/thumbnails/5.jpg)
CONFIDENTIAL
5REvolution Computing 2009
Aggregation/reduction wih .combine:
> foreach(j=1:4, .combine=c) %dopar% {sqrt(j)}
[1] 1.000000 1.414214 1.732051 2.000000
> foreach(j=1:4, .combine='+') %dopar% sqrt(j)
[1] 6.146264
The contents of this presentation is Copyright 2009 by REvolution Computing, Inc. All rights reserved
![Page 6: foreach (iterator) %dopar%past.rinfinance.com/agenda/2009/UIC-Lewis 4-25-09.pdf · 2010. 1. 19. · CONFIDENTIAL REvolution Computing 2009 9 A simple example: backtesting a technical](https://reader033.fdocuments.us/reader033/viewer/2022060915/60a8ff9e0adc9a790b07438a/html5/thumbnails/6.jpg)
CONFIDENTIAL
6REvolution Computing 2009
Foreach is more general than most implementations of parallel lapply. The following typically doesn’t work with miscellaneous parLapplys:
> z <- 2
> f <- function (x) sqrt (x + z)
> foreach (j=1:4, .combine='+') %dopar% f(j)
[1] 8.417609
The contents of this presentation is Copyright 2009 by REvolution Computing, Inc. All rights reserved
![Page 7: foreach (iterator) %dopar%past.rinfinance.com/agenda/2009/UIC-Lewis 4-25-09.pdf · 2010. 1. 19. · CONFIDENTIAL REvolution Computing 2009 9 A simple example: backtesting a technical](https://reader033.fdocuments.us/reader033/viewer/2022060915/60a8ff9e0adc9a790b07438a/html5/thumbnails/7.jpg)
CONFIDENTIAL
7REvolution Computing 2009
Here is a simple simulation:
birthday <- function(n) {
ntests <- 1000
pop <- 1:365
anydup <- function(i)
any(duplicated(
sample(pop, n, replace=TRUE)))
sum(sapply(seq(ntests), anydup)) / ntests
}
x <- foreach (j=1:100) %dopar% birthday (j)
The contents of this presentation is Copyright 2009 by REvolution Computing, Inc. All rights reserved
![Page 8: foreach (iterator) %dopar%past.rinfinance.com/agenda/2009/UIC-Lewis 4-25-09.pdf · 2010. 1. 19. · CONFIDENTIAL REvolution Computing 2009 9 A simple example: backtesting a technical](https://reader033.fdocuments.us/reader033/viewer/2022060915/60a8ff9e0adc9a790b07438a/html5/thumbnails/8.jpg)
CONFIDENTIAL
8REvolution Computing 2009
%dopar%
Modular parallel backends:
• doSEQ (the default)• doNWS (NetWorkSpaces)• doSNOW• doRMPI• doSMP• doMulticore
The contents of this presentation is Copyright 2009 by REvolution Computing, Inc. All rights reserved
![Page 9: foreach (iterator) %dopar%past.rinfinance.com/agenda/2009/UIC-Lewis 4-25-09.pdf · 2010. 1. 19. · CONFIDENTIAL REvolution Computing 2009 9 A simple example: backtesting a technical](https://reader033.fdocuments.us/reader033/viewer/2022060915/60a8ff9e0adc9a790b07438a/html5/thumbnails/9.jpg)
CONFIDENTIAL
9REvolution Computing 2009
A simple example: backtesting a technical trading rule(with TTR, quantmod, PerformanceAnalytics and foreach):
startDate <- "2007-01-01"
endDate <- Sys.Date()
MSFT <- getSymbols("MSFT", …
IEF <- getSymbols("IEF", …
![Page 10: foreach (iterator) %dopar%past.rinfinance.com/agenda/2009/UIC-Lewis 4-25-09.pdf · 2010. 1. 19. · CONFIDENTIAL REvolution Computing 2009 9 A simple example: backtesting a technical](https://reader033.fdocuments.us/reader033/viewer/2022060915/60a8ff9e0adc9a790b07438a/html5/thumbnails/10.jpg)
CONFIDENTIAL
10REvolution Computing 2009
Ra <- Return.calculate (Cl(MSFT))
Rb <- Return.calculate (Cl(IEF))
chart.CumReturns (cbind (Ra,Rb))
The contents of this presentation is Copyright 2009 by REvolution Computing, Inc. All rights reserved
![Page 11: foreach (iterator) %dopar%past.rinfinance.com/agenda/2009/UIC-Lewis 4-25-09.pdf · 2010. 1. 19. · CONFIDENTIAL REvolution Computing 2009 9 A simple example: backtesting a technical](https://reader033.fdocuments.us/reader033/viewer/2022060915/60a8ff9e0adc9a790b07438a/html5/thumbnails/11.jpg)
CONFIDENTIAL
11REvolution Computing 2009
A very simple trading rule:
simpleRule <- function (z, fast=12, slow=26,
signal=9, instr, benchmark)
{
x <- MACD (z, nFast=fast, nSlow=slow,
nSig=signal, maType="EMA")
position <- sign(x[,1]-x[,2])
s <- xts(position,order.by=index(z))
return (instr*(s>0) + benchmark*(s<=0))
}
The contents of this presentation is Copyright 2009 by REvolution Computing, Inc. All rights reserved
![Page 12: foreach (iterator) %dopar%past.rinfinance.com/agenda/2009/UIC-Lewis 4-25-09.pdf · 2010. 1. 19. · CONFIDENTIAL REvolution Computing 2009 9 A simple example: backtesting a technical](https://reader033.fdocuments.us/reader033/viewer/2022060915/60a8ff9e0adc9a790b07438a/html5/thumbnails/12.jpg)
CONFIDENTIAL
12REvolution Computing 2009
Brute-force optimization of the fast and slow parameters:
M <- 100
S <- matrix(0,M,M)
for (j in 1:(M-1)) {
for (k in min ((j+2),M):M) {
R <- simpleRule (Cl (MSFT),j,k,9, Ra, Rb)
Dt <- na.omit (R - Rb)
S[j,k] <- mean (Dt)/sd(Dt)
}
}
The contents of this presentation is Copyright 2009 by REvolution Computing, Inc. All rights reserved
![Page 13: foreach (iterator) %dopar%past.rinfinance.com/agenda/2009/UIC-Lewis 4-25-09.pdf · 2010. 1. 19. · CONFIDENTIAL REvolution Computing 2009 9 A simple example: backtesting a technical](https://reader033.fdocuments.us/reader033/viewer/2022060915/60a8ff9e0adc9a790b07438a/html5/thumbnails/13.jpg)
CONFIDENTIAL
13REvolution Computing 2009
With foreach:
M <- 100
S <- foreach (j=1:(M-1), .combine=rbind,
.packages=c('xts','TTR')) %dopar% {
x <- rep(0,M)
for (k in min ((j+2),M):M) {
R <- simpleRule (Cl (MSFT),j,k,9,Ra,Rb)
Dt <- na.omit (R - Rb)
x[k] <- mean (Dt)/sd( Dt)
}
return(x)
}The contents of this presentation is Copyright 2009 by REvolution Computing, Inc. All rights reserved
![Page 14: foreach (iterator) %dopar%past.rinfinance.com/agenda/2009/UIC-Lewis 4-25-09.pdf · 2010. 1. 19. · CONFIDENTIAL REvolution Computing 2009 9 A simple example: backtesting a technical](https://reader033.fdocuments.us/reader033/viewer/2022060915/60a8ff9e0adc9a790b07438a/html5/thumbnails/14.jpg)
CONFIDENTIAL
14REvolution Computing 2009
j <- which (S==max(S), arr.ind=TRUE)
Ropt <- simpleRule (Cl (MSFT),j[1],j[2],9,Ra,Rb)
chart.CumReturns (cbind (Ra,Rb,Ropt))
The contents of this presentation is Copyright 2009 by REvolution Computing, Inc. All rights reserved
![Page 15: foreach (iterator) %dopar%past.rinfinance.com/agenda/2009/UIC-Lewis 4-25-09.pdf · 2010. 1. 19. · CONFIDENTIAL REvolution Computing 2009 9 A simple example: backtesting a technical](https://reader033.fdocuments.us/reader033/viewer/2022060915/60a8ff9e0adc9a790b07438a/html5/thumbnails/15.jpg)
CONFIDENTIAL
15REvolution Computing 2009
require ("spatstat")
function showIm (S) {
(wrapper for image) … }
for (j in 3:20) {
S <- S3[,,j]
showIm(S)
}
Thee parameters are nicely visualized with the spatstat
package
The contents of this presentation is Copyright 2009 by REvolution Computing, Inc. All rights reserved
![Page 16: foreach (iterator) %dopar%past.rinfinance.com/agenda/2009/UIC-Lewis 4-25-09.pdf · 2010. 1. 19. · CONFIDENTIAL REvolution Computing 2009 9 A simple example: backtesting a technical](https://reader033.fdocuments.us/reader033/viewer/2022060915/60a8ff9e0adc9a790b07438a/html5/thumbnails/16.jpg)
CONFIDENTIAL
16REvolution Computing 2009
foreach (iterator) %dopar% {tasks}
foreach …
task task
foreach …
task task
CLUSTER
SMP
An example of explicit multi-paradigm ||ism
The contents of this presentation is Copyright 2009 by REvolution Computing, Inc. All rights reserved
![Page 17: foreach (iterator) %dopar%past.rinfinance.com/agenda/2009/UIC-Lewis 4-25-09.pdf · 2010. 1. 19. · CONFIDENTIAL REvolution Computing 2009 9 A simple example: backtesting a technical](https://reader033.fdocuments.us/reader033/viewer/2022060915/60a8ff9e0adc9a790b07438a/html5/thumbnails/17.jpg)
CONFIDENTIAL
17REvolution Computing 2009
require (‘snow’)
require (‘foreach’)
require (‘doSNOW’)
cl <- makeCluster (c (‘n1’, ‘n2’))
registerDoSNOW ()
foreach (iterator,
.packages=c (‘foreach’, ‘doMETHOD’)
%dopar%
{
registerMETHOD ()
foreach (iterator) %dopar% {
tasks…
}
}The contents of this presentation is Copyright 2009 by REvolution Computing, Inc. All rights reserved
![Page 18: foreach (iterator) %dopar%past.rinfinance.com/agenda/2009/UIC-Lewis 4-25-09.pdf · 2010. 1. 19. · CONFIDENTIAL REvolution Computing 2009 9 A simple example: backtesting a technical](https://reader033.fdocuments.us/reader033/viewer/2022060915/60a8ff9e0adc9a790b07438a/html5/thumbnails/18.jpg)
CONFIDENTIAL
18REvolution Computing 2009
Summary
Foreach is a simple approach to parallelcomputing with R that maps naturally on to a number of existing systems for distributedcomputing.
The contents of this presentation is Copyright 2009 by REvolution Computing, Inc. All rights reserved