Create a mobile ranking based on a 252-day window

Asked

Viewed 80 times

0

I have a time series of implied volatility index (retivol) in daily data. Sample of 9 years. I want to create a 20-percentile mobile ranking based on a 252-day mobile window t and compare the date t with the ranking, if the value in t is greater than the maximum ranking value of 20 percentiles in the previous 252-day interval, this day t would have ranked 21 in the ranking, and then scroll this ranking, daily, for the period of 9 years.

I created the following script to try to make you understand:

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

library(dplyr)
#criando a função rank
therank = function(x,y,z, na.rm=FALSE) {
  mutate(x) 
  quantile_rank = ntile(y,z)
}

#tentativa de fazer a janela movel do rank
#função default: rollapplyr(x, 252, fun, na.rm = FALSE) where x is the data 
#(a vector) and fun is a function that calculates the desired value.
library(zoo)

window_rank<- rollapplyr(base, 252, therank, na.rm = FALSE)

Theoretically, the rollapplyr should play the role of the mobile window, but when I run the code window_rank<- rollapplyr(base, 252, therank, na.rm = FALSE) R returns the following error: Error in UseMethod("mutate_") :no applicable method for 'mutate_' applied to an object of class "character"

  • Only mutate(x) does nothing. The variable x changed in what or how?

  • Rui, mutate(x) was just the syntax of the function. Does the variable that will be in the mutate bracket necessarily need to have the name "x"? Pq in case, the therank function does what I need, but not inside the rollapply in the form of the mobile window...

  • The mutate should take the form mutate(base, x = <cálculo>) with or without the name x. The variable x if it does not exist in the database it is created with the value of the calculation and if it exists it will have that value.

  • Rui, the script is as follows: window_rank<- rollapplyr(ivolvec, 252, therank, na.rm = FALSE), onde ivolvec é a base, 252 é a janela em dias, therank é a função que criei). &#xA;&#xA;O mutatisestá dentro da funçãotherank, pra eu aplicar a theranknorollapply`just play the name of the function once it is built in R? That’s what I did and back with this error...

  • You need to post a minimally reproducible example in order to give you the solution. It is clear, however, that mutate(x) is a misguided construction. You are injecting x in dplyr::mutate() but x is not a data.frame. That’s why dplyr::mutate() does not recognize.

  • Jdemello, I edited the entire post with a playable code! Helppppppp!!

  • @Aparecidamartins Could you provide a didactic example on the subject or indicate/cite some study material? For I think that for this problem it is necessary prior knowledge in the object of study. I myself know nothing about this.

  • Check this article, bbiasi: https://www.dropbox.com/s/i4ur1qy6qid6r9e/Giot_2005_JPM_Relationships%20Between%20Implied%20Volatility%20Indexes%20and%20Stock%20Index%20Returns.pdf? dl=0

  • I removed the methodology from that article!

Show 4 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.