[R] R function for estimating historical-VaR

Patrick Burns pburns at pburns.seanet.com
Tue Mar 5 11:10:47 CET 2013


You might have a look at:

http://www.portfolioprobe.com/2012/12/17/a-look-at-historical-value-at-risk/

which points to a function for historical VaR.

As Nello said, we really need to know what it is
that you think doesn't work, before we can help
you with what you have.

It probably doesn't really matter, but doing a
full sort is wasteful compared with what 'quantile'
does.

If you have further questions and they are finance
oriented as opposed to being about R programming, then
you should post to R-sig-finance (you have to subscribe
before posting).

Pat


On 04/03/2013 16:25, Blaser Nello wrote:
> 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]]
>
> ______________________________________________
> 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.
>

-- 
Patrick Burns
pburns at pburns.seanet.com
twitter: @burnsstat @portfolioprobe
http://www.portfolioprobe.com/blog
http://www.burns-stat.com
(home of:
  'Impatient R'
  'The R Inferno'
  'Tao Te Programming')



More information about the R-help mailing list