作者naturalsmen (日日夜夜)
看板R_Language
标题[问题] Shiny上做平行运算(rugarch)
时间Sat Dec 19 11:42:31 2015
[问题类型]:
程式谘询(我想用R 做某件事情,但是我不知道要怎麽用R 写出来)
效能谘询(我想让R 跑更快)
[软体熟悉度]:
入门(写过其他程式,只是对语法不熟悉)
[问题叙述]:
最近在用rugarch pkg中的ugarchroll做模型回测
是用过去1000天的time window为建模样本 预测隔天(t+1)的波动度
每天都会重新fit一个新的模型 图示如下
T daily returns:
|---------|--|--------|
1 t t+1 T
1000 daily returns to fit AR(1)-GARCH(1,1):
|-----------|
t-999 t
1-step forecast:
|---|
t t+1
图如果不好理解我再做修改
假设某指数有10000天日报酬率
每天用过去1000天的moving window做1-step forecast
就会有9900笔预测值
但可能是因为这样就要重新fit 9900次AR(1)-GARCH(1,1)
所以跑很慢
但ugarchroll里有一个参数可以设cluster
所以我参考这个网站里的方法:
http://unstarched.net/2012/12/26/rolling-garch-forecasts/
在本机上跑是ok的
用RStudio runApp也没问题
速度大概快了3~4倍
但一发布到Shiny Server上就直接断掉(网页变成灰色)
想请问是什麽原因
或是有没有什麽建议的替代方法
现在没加cluster的情况下 要跑快5分钟...
[程式范例]:
Server部分比较精简 :http://pastie.org/10641054
cluster部分在server的backtest最後几行
目前是可执行状况 只是因为server有省略一些
所以基本上跑不出图
若需要完整版code我再提供
没有加cluster部分会如下网址的输出
http://naturalsmen.shinyapps.io/shinyTry
加了cluster後 切到Backtesting页签会直接灰屏...
现在只是跑比较久
问题主要是「在shiny上加快ugarchroll的处理速度」
怕讲太多背景会模糊掉焦点@@
如果有需要补充什麽再请告知
谢谢各位大大<(_ _)>
PS. 我一个礼拜後可能会把shiny先关掉
因为免费帐户一个月有时数限制...
怕直接被耗光囧
[环境叙述]:
R version 3.2.1 (2015-06-18)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 8 x64 (build 9200)
locale:
[1] LC_COLLATE=Chinese (Traditional)_Taiwan.950 LC_CTYPE=Chinese (Traditional
)_
[3] LC_MONETARY=Chinese (Traditional)_Taiwan.950 LC_NUMERIC=C
[5] LC_TIME=Chinese (Traditional)_Taiwan.950
attached base packages:
[1] parallel stats graphics grDevices utils datasets methods base
other attached packages:
[1] xtable_1.7-4 rugarch_1.3-6 fExtremes_3010.81 fTrading_3010.78
[5] fGarch_3010.82 fBasics_3011.87 timeSeries_3012.99 timeDate_3012.10
0
[9] quantmod_0.4-5 TTR_0.23-0 Quandl_2.7.0 xts_0.9-7
[13] zoo_1.7-12 shiny_0.12.2
loaded via a namespace (and not attached):
[1] codetools_0.2-11 digest_0.6.8 htmltools_0.2.6
[4] R6_2.1.0 SkewHyperbolic_0.3-2 expm_0.999-0
[7] DistributionUtils_0.5-1 Rsolnp_1.15 grid_3.2.1
[10] stringr_1.0.0 numDeriv_2014.2-1 httr_1.0.0
[13] GeneralizedHyperbolic_0.8-1 rgl_0.95.1429 httpuv_1.3.3
[16] lattice_0.20-31 mime_0.3 Matrix_1.2-1
[19] KernSmooth_2.23-14 rstudioapi_0.3.1 jsonlite_0.9.16
[22] rsconnect_0.4.1.4 multicool_0.1-9 spd_2.0-1
[25] stringi_0.5-5 magrittr_1.5 tools_3.2.1
[28] mvtnorm_1.0-3 truncnorm_1.0-7 ks_1.10.0
[31] nloptr_1.0.4 Rcpp_0.12.2 misc3d_0.8-4
[关键字]:
shiny, parallel, rugarch
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 140.113.136.217
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/R_Language/M.1450496557.A.FB5.html
1F:→ naturalsmen: 补充:灰屏没有显示任何警告或错误讯息 12/19 11:47
2F:→ naturalsmen: 啊范例程式的ugarchroll忘了加cluster进去 晚点修改 12/19 11:57
3F:→ naturalsmen: 抱歉 12/19 11:57
4F:→ cywhale: and what you mean "data = -data()$r" ? 12/19 13:18
5F:→ cywhale: Igot "Error in -data()$r : invalid argument to unary" 12/19 13:19
6F:→ naturalsmen: cywhale: data is the DAX index loaded from 'quand 12/19 14:10
7F:→ naturalsmen: l' and r is the daily log return calculated from 12/19 14:10
8F:→ naturalsmen: the DAX data, containing Date, Open Price, Close 12/19 14:10
9F:→ naturalsmen: Price, and so on. i put them in the reactive var 12/19 14:10
10F:→ naturalsmen: iable 'data', i.e., i make a list to contain the 12/19 14:10
11F:→ naturalsmen: variables i need in 'data'. In my computer, i ca12/19 14:10
12F:→ naturalsmen: n use data()$r to call the log return in the reac12/19 14:10
13F:→ naturalsmen: tive variable 'data'. The negative sign represent12/19 14:10
14F:→ naturalsmen: s the 'negative' log return. i'll check it later,12/19 14:10
15F:→ naturalsmen: and thanks for replying:)12/19 14:10
※ 编辑: naturalsmen (42.70.107.229), 12/19/2015 15:00:33
16F:→ naturalsmen: I have just checked the problem and the data()$r 12/19 16:36
17F:→ naturalsmen: works fine as usual. Maybe u can re-confirm if t 12/19 16:36
18F:→ naturalsmen: he quandl function works. Many thanks:) 12/19 16:36
19F:推 cywhale: ya.. the issue is my fault when try and 12/19 17:30
20F:→ cywhale: It' ok to use cluster and get result on shiny-server 12/19 17:31
21F:→ cywhale: on linux, but I got troubles to deploy shinyapps.io 12/19 17:32
22F:→ cywhale: At least, I have no error runing code on shiny-server 12/19 17:32
23F:→ cywhale: (and also no error on local computer). What message 12/19 17:34
24F:→ cywhale: when your app crash and press F-12 & select "console" 12/19 17:35
25F:→ naturalsmen: Maybe I mixed up the definition of the "shiny ser 12/19 21:02
26F:→ naturalsmen: ver" and the "shiny apps.io", sorry about that. W 12/19 21:02
27F:→ naturalsmen: hat I mean "shiny server" in the article is the " 12/19 21:02
28F:→ naturalsmen: shiny apps.io". I'll fix it as soon as possible. 12/19 21:02
29F:→ naturalsmen: I can run my app locally but run into trouble on 12/19 21:02
30F:→ naturalsmen: shinyapps.io. Anyways, thanks a lot:) 12/19 21:02
31F:→ naturalsmen: I may give it a shot using your method to see if 12/19 21:04
32F:→ naturalsmen: I can get any warning message. 12/19 21:04
hey, thanks for the advice. i just got these messages:
http://imgur.com/8zYVcMo
※ 编辑: naturalsmen (114.43.220.140), 12/19/2015 22:13:29
34F:→ cywhale: but only no error if I change code to: 12/19 23:18
35F:→ cywhale: cluster = makePSOCKcluster(4). If using (10),it crash 12/19 23:20
36F:→ cywhale: and got error: server responded with a status of 500 12/19 23:26
37F:→ naturalsmen: so maybe it's due to the constraints as a free 12/20 00:03
38F:→ naturalsmen: user? 12/20 00:04