SAGA06
这两天在学习bootstrap,并结合原来学的练习,无奈程序总出错。
原始原始样本x(样本个数是300个)要计算99%置信度下VaR 和ES
> nx.gpd=gpd(-x,threshold=0.1873)
> riskmeasures(nx.gpd,0.99)
运行结果是
p quantile sfall
[1,] 0.99 0.3391784 0.399171
现在俺想进行bootstrap1000次,这样,得到的应该是1000个类似数列
p quantile sfall
[1,] 0.99 0.3391784 0.399171
[2,].........................................
[3,].........................................
..............................................
[1000,]....................................
俺昨晚在一个同学指点下貌似明白一点,于是先用sample有放回抽取300个,进行1000次,写了下边两个:
(一)
for(i in 1:1000)
{sample(x,300,replace=T)
{for(i in 1:300){
b=gpd(-x,threshold=0.1873)
c=riskmeasures(b,0.99)}
cat(c,"\n",file="e:/extremelogr.txt",append=TRUE)
}
(二)
for(i in 1:10)
{sample(x,300,replace=T)
{a=x[1:300]
b=gpd(-a,threshold=0.1873)
c=riskmeasures(b,0.99)}
cat(c,"\n",file="e:/extremelogr.txt",append=TRUE)
}
这两种情况运行的结果是一样的1000个数据,我可能没把抽样10000次的放到循环里,但是不知道问题出来哪里,而且(一)比(二)时间长很多
又看了下r文档,用bootstrap重新写了一个,如下运行结果,崩溃了。关于这个问题的另一个疑问是赋值问题,因为最后结果相当于得到一个面板数据,results<-bootstrap(x,1000,y),这个赋值也有问题,貌似理解和别人告诉的是只能放序列~~~~~~~~多谢多谢!!!
(三)
> y<-function(x){for(i in 1:300){
+ b=gpd(-x,threshold=0.1873)
+ c=riskmeasures(b,0.99)}
+ }
> results<-bootstrap(x,1000,y)
错误于optim(theta, negloglik, hessian = TRUE, ..., tmp = excess) :
non-finite finite-difference value [1]
此外: Warning messages:
1: In log(x) : 产生了NaNs
2: In log(x) : 产生了NaNs
SAGA06
[quote]引用第1楼snoopyzhao于2009-02-22 10:52发表的“”:
还是看看 boot 包的文档吧,有现成的,为什么要自己写呢?[/quote]
看了一些bootstrap的包,咋整不明白,俺现在跟循环飚上了,刚才 试着改了下瞎猫碰到死耗子成功一次不知道为啥,后来几次又报错,有以下提示:
> z<-numeric(10)
> for(i in 1:10){
+ sample<-numeric(i)
+ y<-sample(x,300,replace=TRUE)
+ a=y[1:300]
+ b=gpd(-a,threshold=0.187)
+ c=riskmeasures(b,0.99)
+ cat(c,"\n",file="e:/extremelogr.txt",append=TRUE)}
错误于optim(theta, negloglik, hessian = TRUE, ..., tmp = excess) :
non-finite finite-difference value [1]
>
yihui
额滴个神呀,你这代码是何等的乱!!
循环有前括号{,没有后括号},写文件用cat而不用write.table
gpd、riskmeasures是什么也没交待,整段程序要做什么事情也没说
sample(x,300,replace=T)是干什么的?既然没有赋值,那么这里的抽样是为了测试机器的运算性能?跟电脑玩游戏呢?
2楼的回帖中sample<-numeric(i)是干什么的,sample本身是函数名,你给它赋值了想干什么?