我需要根据t天之前所有天的累计窗口进行每日排名。在这种情况下,我必须从252天的窗口开始,对观察进行排序,并对20个均分百分位数进行排名。然后获得第253天的观察值,与该等级进行比较,如果等级值在此范围内,则根据该等级进行排序,如果该等级小于最小值,则等级为0,如果大于最大值,则等级为21。完成第一步后,我需要在整个数据库中运行该功能,增加排名窗口,从252天开始,直到253天,等等。
我有以下代码,该代码与我刚开始所说的完全一样,但是固定的窗口时间为252天。我需要做的是类似的事情,但是新功能必须计算一个汇总的窗口,而不是固定的窗口,该窗口以252天开始,以观察总数为-1的窗口结束。
install.packages("bizdays")
library(bizdays)
dados<- as.data.frame(bizseq('2011-08-01', '2013-08-01', cal = "Brazil/ANBIMA"))
names(dados)<- c("data")
ivol<- as.data.frame(c("21.28711","23.56743","27.32514","28.54277","29.76039","38.40667","47.05294",
"27.75004","33.76601","24.37536","28.58706","28.50753","28.42801","28.34848",
"28.26896","34.54205","28.226","31.71554","27.61506","26.99005","26.2452",
"27.02077","26.57059","25.27205","25.62745","30.41709","30.28928","32.27394","34.2586","36.24326","36.41168",
"29.67935","29.76384","28.98926","27.79914","26.60903","32.7296","38.85017","41.72757","42.45641","35.48816",
"33.4138","33.17446","38.66989","44.16533","29.40715","38.68258","34.65883","31.78003","28.90124","35.86376",
"30.09062","30.2993","26.5146","27.6774","28.84019","30.93672","29.43715","27.37192","26.24751","25.50525",
"25.58114","22.27847","24.53868","29.56798","25.89955","25.62683","25.14079","27.00007","28.85935","28.65313",
"28.4469","24.9024","29.67527","29.18929","24.76481","35.90339","29.26105","31.60401","30.04701","31.86597",
"25.35371","29.55908","24.25097","27.24362","26.0865","27.92838","26.58823","37.58755","25.83994","30.78934",
"28.63408","26.47881","25.92757","28.11764","27.20345","25.35408","25.29016","25.76206","24.50687","23.52267",
"24.56973","25.61678","26.66384","23.14008","23.132","23.12393","23.17977","23.72229","23.1107","21.90203",
"24.00631","20.12761","23.41764","22.58384","23.15579","21.61337","22.57287","22.19599","21.0872","19.83334",
"18.18826","19.44424","19.73723","20.03022","18.87538","21.83447","19.94553","19.30423","20.12327","20.31634",
"22.5208","18.95442","17.88235","21.26344","21.56118","19.39781","19.76489","20.49098","20.05735","20.25546",
"19.42526","17.62972","19.64706","20.71675","19.38761","20.96161","19.54165","21.21596","21.3454","23.25998",
"22.7128","21.86852","20.76671","18.82955","18.40445","18.39674","20.23708","20.07432","19.06276","19.61001",
"22.36438","19.94096","20.45359","16.62309","18.38752","21.30607","20.14841","19.75784","19.36288","22.72852",
"23.30168","19.04908","24.26608","24.14334","23.68773","23.38313","18.41681","24.86972","23.11208","19.85312",
"22.99611","22.91279","21.61691","23.89977","21.60447","19.39232","21.9426","21.50965","19.49969","18.71056",
"19.46965","19.32327","22.63971","22.113","20.61101","21.01649","27.54151","28.96247","25.58761","26.32041","28.63712",
"24.06426","22.23268","34.27249","31.41806","24.22399","32.14149","28.24585","26.57704","23.55219","28.61884","24.66197",
"20.94144","27.0632","24.98184","27.34932","26.38455","29.07777","24.41478","25.91706","23.47051","22.43838","23.24079",
"22.41391","23.02232","25.72487","26.26527","25.10686","30.60878","26.01148","23.09491","21.65578","21.44434","22.24085",
"23.53456","25.41174","27.28891","22.63747","18.35763","24.23522","21.33616","24.65956","23.20137","23.39885","22.76095",
"22.12304","23.16902","25.51094","17.30929","27.56798","25.01272","23.84245","26.11669","28.9215","23.04916","23.33632",
"21.58664","22.78128","25.99777","22.36353","23.82258","22.38859","22.6706","23.87853","24.92409","23.10635","23.45014",
"24.09447","22.24359","24.38719","23.31092","22.23465","25.48968","27.21431","24.76347","25.36156","25.58649","22.7045",
"25.42561","22.20385","23.62826","25.47909","25.87671","23.38551","21.71917","22.81986","22.20451","21.6723","20.83679",
"22.77583","23.89389","20.59727","22.82012","25.02949","24.43143","24.28433","24.25042","21.27869","20.82378","23.63432",
"24.12969","23.81493","24.06198","22.52014","24.60091","20.61036","22.46888","23.03237","23.60265","23.09116","24.44142",
"24.17373","22.85317","23.36192","22.47905","23.79545","22.642","21.38135","20.92037","22.56495","21.52126","23.30794",
"24.23245","22.79641","25.48476","25.30342","22.87122","23.17207","23.47292","19.94111","21.79934","21.09003","23.71056",
"23.20562","22.62287","21.44512","22.12571","22.54461","23.16875","23.07137","23.11263","21.11079","18.45346","22.02743",
"22.18733","22.16706","22.14679","21.1788","21.36772","22.0525","20.35522","20.61244","20.86966","21.57509","22.28053",
"21.47767","20.67481","18.50791","21.24009","18.85158","20.86887","20.04319","19.76428","20.57738","19.73062","18.80244",
"20.47401","18.27169","17.81884","20.38176","20.41285","16.90357","19.77132","20.19383","20.61634","18.8055","18.62108",
"16.27306","20.52252","21.5628","21.04483","20.41759","19.91348","19.86341","18.50267","19.28119","19.4508","19.45208",
"19.94868","19.80569","20.0529","19.51828","18.75703","19.93031","18.72769","18.18218","19.17099","18.09319","18.63018",
"19.54754","18.59208","18.45659","18.67694","18.89729","19.11765","18.89925","19.7338","19.73766","21.42095","19.85507",
"20.10499","20.51085","20.58047","19.84328","19.15041","18.33888","18.84041","19.34194","19.84347","20.34499","20.84652",
"21.34805","20.45468","21.28042","19.86063","21.49564","23.13064","21.013","22.05263","21.97945","22.73436","21.76791",
"21.16098","21.49375","20.58698","21.69745","21.27429","21.2697","22.36205","22.00988","18.99451","20.18173","20.75463",
"20.38411","21.68759","20.3392","21.78222","20.39415","19.17879","19.82962","17.29045","20.04898","21.35839","21.70304",
"19.20146","20.40494","19.09858","20.58075","22.71482","21.68574","22.76031","23.22045","24.75409","25.10623","23.6726",
"26.27497","29.42851","27.17491","27.28767","26.9298","26.85252","28.11517","29.68584","30.51626","30.91683","30.15717",
"27.39766","26.24382","24.01179","25.19244","27.26174","26.24734","24.85707","27.99068","25.31346","25.16461","25.01576",
"22.98344","23.63146","25.66155","24.72497","25.06169","24.30915","23.4451","22.94747","22.97816","23.49057","22.04397",
"22.82086","25.198","23.09666","24.13238","23.86527"))
names(ivol)<- c("retivol")
#fazendo retorno do ivol
ivolnumeric<- as.numeric(ivol$retivol)
ivoln1=c(NA, diff(log(ivolnumeric)))
#criando a base
base<- as.data.frame(dados)
base$rivoln <-ivoln1
base_sna<- na.omit(base)
qtl.rank2<-function(x,obs.1=1,obs.t=252,qtl.intl=20)
{
#x: list;
#obs.1: calibragem, 1a obs;
#obs.t: calibragem, última obs;
#qtl.intl: quantidade de intervalos.
x.length<-length(x) #tamanho da lista
i<-t #contador dos quantis
j<-1 #contador
qtl.width<-100/qtl.intl #tamanho do intervalo
pi<-0 #inicializa variável que armazena o quantil
pi.vec<-matrix(0,nrow=qtl.intl,ncol=1) #matriz para armazenar os percentis
calibration.list<-list(0,obs.t)
matriz.resultados<-matrix(0,nrow=(x.length-obs.t),2)
for(i in obs.1:(x.length-obs.t))
{
calibration.list <- x[i:(i+obs.t-1)]
pctl<-quantile(calibration.list,1:qtl.intl*qtl.width/100)
pi.vec<-as.matrix(pctl)
rownames(pi.vec)<-rownames(as.matrix(pctl))
colnames(pi.vec)<-"Percentis"
new.pi.vec<-matrix(c(pi.vec,0),nrow=qtl.intl+1,ncol=1)
new.pi.vec[qtl.intl+1,]<-x[i+obs.t]
o<-order(new.pi.vec, decreasing=FALSE)
new.pi.vec<-as.matrix(new.pi.vec[o,])
res<-c(x[i+obs.t],which(new.pi.vec[,1]==x[i+obs.t],arr.ind=TRUE))
num_aval<-res[1]
qtl.order<-res[2]
if(qtl.order==1)
{
print(calibration.list)
if(num_aval<min(calibration.list))
{
qtl.order<-0
}
res<-c(num_aval,qtl.order)
}
matriz.resultados[i,1]<-res[1]
matriz.resultados[i,2]<-res[2]
if(i<x.length-(obs.t+1)){print(matriz.resultados[i+1,])}
print(c(i,res))
}
return(matriz.resultados)
}
a<- qtl.rank2(base_sna$rivoln)
finaldatabase<- subset(base_sna, data> "2012-07-31",
select=c(data, rivoln))
finaldatabase$rankfixo<- a[,2]
print(finaldatabase)
data rivoln rankfixo
254 2012-08-01 0.239298992 16
255 2012-08-02 0.140876149 15
256 2012-08-03 -0.565759343 3
257 2012-08-06 0.097091124 14
258 2012-08-07 -0.473568695 3
259 2012-08-08 0.320010960 17
260 2012-08-09 0.473627760 18
261 2012-08-10 -0.587786665 3
262 2012-08-13 0.352947074 18
263 2012-08-14 -0.343687748 4
264 2012-08-15 0.066840018 14
265 2012-08-16 0.286603905 17
266 2012-08-17 0.115983503 15
267 2012-08-20 -0.288643149 5
268 2012-08-21 0.102278849 14
269 2012-08-22 0.092781733 14
270 2012-08-23 -0.403884080 4
271 2012-08-24 0.447234521 18
272 2012-08-27 -0.164303051 7
273 2012-08-28 -0.292455351 5
274 2012-08-29 0.571168754 18
275 2012-08-30 0.112362968 14
276 2012-08-31 -0.191055237 6
277 2012-09-03 0.065086617 13
278 2012-09-04 0.024358444 12
279 2012-09-05 -0.473217344 3
280 2012-09-06 0.454323394 18
281 2012-09-10 -0.577485679 3
282 2012-09-11 0.365855970 17
283 2012-09-12 0.214350799 16
284 2012-09-13 0.039952051 12
285 2012-09-14 -0.299060751 5
286 2012-09-17 -0.428618413 4
287 2012-09-18 0.272705618 16
288 2012-09-19 -0.160342650 7
289 2012-09-20 -0.139761942 7
290 2012-09-21 -0.295117051 5
291 2012-09-24 0.582799123 18
292 2012-09-25 0.255933374 16
293 2012-09-26 -0.916290732 2
294 2012-09-27 0.676886660 18
295 2012-09-28 0.363617998 17
296 2012-10-01 -0.055651255 10
297 2012-10-02 -0.015174798 11
298 2012-10-03 -0.009216655 11
299 2012-10-04 -0.743790913 2
300 2012-10-05 -0.146603474 8
301 2012-10-08 0.810094445 19
302 2012-10-09 0.058458200 12
303 2012-10-10 -0.038589997 10
304 2012-10-11 0.029081209 12
305 2012-10-15 -0.346715604 4
306 2012-10-16 0.417405548 17
307 2012-10-17 -0.991769193 2
308 2012-10-18 0.565313809 18
309 2012-10-19 0.143706721 14
310 2012-10-22 0.153025187 14
预期结果是每日排名以及汇总的校准窗口。最后,它是脚本的“最终数据库”。