[R] R function for estimating historical-VaR

Joshua Ulrich josh.m.ulrich at gmail.com
Tue Mar 5 13:56:42 CET 2013


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.
>



More information about the R-help mailing list