Hi everyone!! I am new in R and I want to create a simple R function for estimating historical-VaR. In y_IBM returns, there are 2300 observations. For evaluation I take the next 2000 observations, then I abandon the latest 300 observations. Firstly, I use the window which has the fix length and contains the observations from 1 to 2000 to estimate the VaR. At first I take 2000 obs. and reorder these series in ascending order, from smallest return to largest return. Each ordered return is assigned an index value (1, 2, ...). At the 99% confidence level, the daily VaR under historical simulation method equals the return corresponding to the index number calculated as follows: (1-0.99)*2000 (the number of our window) =20. The return corresponding to index 20 is the daily historical simulation VaR. I repeat the first step except the window changes the observations from 2 to 2001. Such a process provides 300 one-step ahead VaR. My function is: VaR_foc <- function (returns, value = 1000, p = 0.01, n=251) { T = length(returns) x_foc = vector(length=n) N = T-(n+1) m=sort(returns[1:N]) op = as.integer(N*p) # p % smallest for (i in 2:n) { g= returns[i:(N+i)] ys = sort(g) # sort returns x_foc[[1]] = -m[op]*value # VaR number x_foc[i] = -ys[op]*value } return(x_foc) } VaR_foc (returns=y_IBM) But the fucntion doesn't work, can smbd help me wh [[alternative HTML version deleted]]
Does it just not work or does it not do the right thing? The reason it doesn't work is that you are writing 'T = length(returns) x_foc = vector(length=n) N = T-(n+1)' on one line instead of using three lines. However, your description of what you want to do also doesn't seem to correspond to the function. Please clarify what exactly you want the function to do. You could also write the current function as follows. VaR_foc <- function(returns, value=1000, p=.01, n=300) { N <- length(returns)-n-1 op <- N*p unlist(lapply(1:n, function(i) {-sort(returns[i:(N+i)])[op]*value})) } Nello Blaser -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of ????? ??????? Sent: Montag, 4. M?rz 2013 14:07 To: R-help at r-project.org Subject: [R] R function for estimating historical-VaR Hi everyone!! I am new in R and I want to create a simple R function for estimating historical-VaR. In y_IBM returns, there are 2300 observations. For evaluation I take the next 2000 observations, then I abandon the latest 300 observations. Firstly, I use the window which has the fix length and contains the observations from 1 to 2000 to estimate the VaR. At first I take 2000 obs. and reorder these series in ascending order, from smallest return to largest return. Each ordered return is assigned an index value (1, 2, ...). At the 99% confidence level, the daily VaR under historical simulation method equals the return corresponding to the index number calculated as follows: (1-0.99)*2000 (the number of our window) =20. The return corresponding to index 20 is the daily historical simulation VaR. I repeat the first step except the window changes the observations from 2 to 2001. Such a process provides 300 one-step ahead VaR. My function is: VaR_foc <- function (returns, value = 1000, p = 0.01, n=251) { T = length(returns) x_foc = vector(length=n) N = T-(n+1) m=sort(returns[1:N]) op = as.integer(N*p) # p % smallest for (i in 2:n) { g= returns[i:(N+i)] ys = sort(g) # sort returns x_foc[[1]] = -m[op]*value # VaR number x_foc[i] = -ys[op]*value } return(x_foc) } VaR_foc (returns=y_IBM) But the fucntion doesn't work, ?can smbd help me wh [[alternative HTML version deleted]]
Cross-posted, verbatim, on stackoverflow: http://stackoverflow.com/q/15203347/271616 -- Joshua Ulrich | about.me/joshuaulrich FOSS Trading | www.fosstrading.com R/Finance 2013: Applied Finance with R | www.RinFinance.com On Mon, Mar 4, 2013 at 7:07 AM, ????? ??????? <nysanaskar at mail.ru> wrote:> > > Hi everyone!! I am new in R and I want to create a simple R function for estimating historical-VaR. In y_IBM returns, there are 2300 observations. For evaluation I take the next 2000 observations, > then I abandon the latest 300 observations. Firstly, I use the window which has the fix > length and contains the observations from 1 to 2000 to estimate the VaR. At first I take 2000 obs. and reorder these series in ascending order, from smallest return to largest return. Each ordered return is assigned an index value (1, 2, ...). At the 99% confidence level, the daily VaR under historical simulation method equals the return corresponding to the index number calculated as follows: > (1-0.99)*2000 (the number of our window) =20. The return corresponding to index 20 is the daily historical simulation VaR. > I repeat the first step except the window changes the observations from 2 to 2001. Such a process provides 300 one-step ahead VaR. > My function is: > > > > VaR_foc <- function (returns, value = 1000, p = 0.01, n=251) { > T = length(returns) > x_foc = vector(length=n) > N = T-(n+1) > m=sort(returns[1:N]) > op = as.integer(N*p) # p % smallest > for (i in 2:n) { > g= returns[i:(N+i)] > ys = sort(g) # sort returns > x_foc[[1]] = -m[op]*value # VaR number > x_foc[i] = -ys[op]*value > } > return(x_foc) > } > VaR_foc (returns=y_IBM) > > But the fucntion doesn't work, can smbd help me wh > > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >