

Hello,
I am fairly new to R and trying to calculate value at risk with exponentially decreasing weights.My function works for a single vector of returns but does not work with rollapply(), which is what I want to use. The function I am working on should assig exponentially decreasing weights to the K most recent returns and then order the returns in an ascending order. Subsequently it should pick the last return for which the cumulative sum of the weights is smaller or equal to a significance level. Thus, I am trying to construct a cumulative distribution function and find a quantile.
This is the function I wrote:
VaRfun < function(x, lambda = 0.94) {
#create data.frame and order returns such that the lates return is the first df < data.frame(weight = c(1:length(x)), return = rev(x)) K < nrow(df) constant < (1lambda)/(1lambda^(K))#assign weights to the returns for(i in 1:nrow(df)) { df$weight[i] < lambda^(i1) * constant }#order returns in an ascending order df < df[order(df$return),]
#add the cumulative sum of the weights df$cum.weight < cumsum(df$weight)
#calculate value at risk VaR < tail((df$return[df$cum.weight <= .05]), 1) signif(VaR, digits = 3)}
It works for a single vector of returns but if I try to use it with rollapply(), such as
rollapply(r, width = list(500, 1), FUN = VaRfun),
it outputs a vector of NAs and I don't know why.
Thank you for your help!
[[alternative HTML version deleted]]
______________________________________________
[hidden email] mailing list  To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


Maybe you want this.It computes VaRfun(r[c(i500, i1)] for each i for
which the argument to r makes sense.
rollapply(r, width = list(c(500, 1)), FUN = VaRfun),
On Sat, May 27, 2017 at 5:29 PM, Sepp via Rhelp < [hidden email]> wrote:
> Hello,
> I am fairly new to R and trying to calculate value at risk with exponentially decreasing weights.My function works for a single vector of returns but does not work with rollapply(), which is what I want to use. The function I am working on should assig exponentially decreasing weights to the K most recent returns and then order the returns in an ascending order. Subsequently it should pick the last return for which the cumulative sum of the weights is smaller or equal to a significance level. Thus, I am trying to construct a cumulative distribution function and find a quantile.
> This is the function I wrote:
> VaRfun < function(x, lambda = 0.94) {
> #create data.frame and order returns such that the lates return is the first df < data.frame(weight = c(1:length(x)), return = rev(x)) K < nrow(df) constant < (1lambda)/(1lambda^(K))#assign weights to the returns for(i in 1:nrow(df)) { df$weight[i] < lambda^(i1) * constant }#order returns in an ascending order df < df[order(df$return),]
> #add the cumulative sum of the weights df$cum.weight < cumsum(df$weight)
> #calculate value at risk VaR < tail((df$return[df$cum.weight <= .05]), 1) signif(VaR, digits = 3)}
> It works for a single vector of returns but if I try to use it with rollapply(), such as
> rollapply(r, width = list(500, 1), FUN = VaRfun),
> it outputs a vector of NAs and I don't know why.
> Thank you for your help!
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list  To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/rhelp> PLEASE do read the posting guide http://www.Rproject.org/postingguide.html> and provide commented, minimal, selfcontained, reproducible code.

Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1877GKXGROUP
email: ggrothendieck at gmail.com
______________________________________________
[hidden email] mailing list  To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


This is exactly what I want. However, with my function it produces a vector of NAs ...
Gabor Grothendieck < [hidden email]> schrieb am 16:23 Sonntag, 28.Mai 2017:
Maybe you want this.It computes VaRfun(r[c(i500, i1)] for each i for
which the argument to r makes sense.
rollapply(r, width = list(c(500, 1)), FUN = VaRfun),
On Sat, May 27, 2017 at 5:29 PM, Sepp via Rhelp < [hidden email]> wrote:
> Hello,
> I am fairly new to R and trying to calculate value at risk with exponentially decreasing weights.My function works for a single vector of returns but does not work with rollapply(), which is what I want to use. The function I am working on should assig exponentially decreasing weights to the K most recent returns and then order the returns in an ascending order. Subsequently it should pick the last return for which the cumulative sum of the weights is smaller or equal to a significance level. Thus, I am trying to construct a cumulative distribution function and find a quantile.
> This is the function I wrote:
> VaRfun < function(x, lambda = 0.94) {
> #create data.frame and order returns such that the lates return is the first df < data.frame(weight = c(1:length(x)), return = rev(x)) K < nrow(df) constant < (1lambda)/(1lambda^(K))#assign weights to the returns for(i in 1:nrow(df)) { df$weight[i] < lambda^(i1) * constant }#order returns in an ascending order df < df[order(df$return),]
> #add the cumulative sum of the weights df$cum.weight < cumsum(df$weight)
> #calculate value at risk VaR < tail((df$return[df$cum.weight <= .05]), 1) signif(VaR, digits = 3)}
> It works for a single vector of returns but if I try to use it with rollapply(), such as
> rollapply(r, width = list(500, 1), FUN = VaRfun),
> it outputs a vector of NAs and I don't know why.
> Thank you for your help!
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list  To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/rhelp> PLEASE do read the posting guide http://www.Rproject.org/postingguide.html> and provide commented, minimal, selfcontained, reproducible code.

Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1877GKXGROUP
email: ggrothendieck at gmail.com
______________________________________________
[hidden email] mailing list  To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


This is the function in plain text because it looked messy before:
VaRfun < function(x, lambda = 0.94) {
#create data.frame and order returns such that the lates return is the first
df < data.frame(weight = c(1:length(x)), return = rev(x))
K < nrow(df)
constant < (1lambda)/(1lambda^(K))
#assign weights to the returns
for(i in 1:nrow(df)) {
df$weight[i] < lambda^(i1) * constant
}
#order returns in an ascending order
df < df[order(df$return),]
#add the cumulative sum of the weights
df$cum.weight < cumsum(df$weight)
#calculate value at risk
VaR < tail((df$return[df$cum.weight <= .05]), 1)
signif(VaR, digits = 3)
}
It works for a single vector of returns but if I try to use it with rollapply(), such as
rollapply(r, width = list(500, 1), FUN = VaRfun),
it outputs a vector of NAs and I don't know why.
______________________________________________
[hidden email] mailing list  To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.
______________________________________________
[hidden email] mailing list  To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


In reply to this post by R help mailing list2
You will get better help if you read the Posting Guide mentioned at the foot if every posting including this one carefully and pay attention.
A) You need to post in plain text, as your code came through the mailing list damaged.
B) You need to include sample data and make your code run from a clean R environment. See [1][2][3].
C) You need to make sure your function returns sensible results for short input vectors or input vectors with NA in them, as rollapply/embed need to be told how to handle the beginning/end of the series.
[1] http://stackoverflow.com/questions/5963269/howtomakeagreatrreproducibleexample[2] http://advr.had.co.nz/Reproducibility.html[3] https://cran.rproject.org/web/packages/reprex/index.html
Sent from my phone. Please excuse my brevity.
On May 28, 2017 7:58:59 AM PDT, Sepp via Rhelp < [hidden email]> wrote:
>This is exactly what I want. However, with my function it produces a
>vector of NAs ...
>
>
>Gabor Grothendieck < [hidden email]> schrieb am 16:23 Sonntag,
>28.Mai 2017:
>
>
>
>Maybe you want this.It computes VaRfun(r[c(i500, i1)] for each i for
>which the argument to r makes sense.
>
>rollapply(r, width = list(c(500, 1)), FUN = VaRfun),
>
>
>On Sat, May 27, 2017 at 5:29 PM, Sepp via Rhelp < [hidden email]>
>wrote:
>> Hello,
>> I am fairly new to R and trying to calculate value at risk with
>exponentially decreasing weights.My function works for a single vector
>of returns but does not work with rollapply(), which is what I want to
>use. The function I am working on should assig exponentially decreasing
>weights to the K most recent returns and then order the returns in an
>ascending order. Subsequently it should pick the last return for which
>the cumulative sum of the weights is smaller or equal to a significance
>level. Thus, I am trying to construct a cumulative distribution
>function and find a quantile.
>> This is the function I wrote:
>> VaRfun < function(x, lambda = 0.94) {
>> #create data.frame and order returns such that the lates return is
>the first df < data.frame(weight = c(1:length(x)), return = rev(x))
>K < nrow(df) constant < (1lambda)/(1lambda^(K))#assign weights to
>the returns for(i in 1:nrow(df)) { df$weight[i] < lambda^(i1) *
>constant }#order returns in an ascending order df <
>df[order(df$return),]
>> #add the cumulative sum of the weights df$cum.weight <
>cumsum(df$weight)
>> #calculate value at risk VaR < tail((df$return[df$cum.weight <=
>.05]), 1) signif(VaR, digits = 3)}
>> It works for a single vector of returns but if I try to use it with
>rollapply(), such as
>> rollapply(r, width = list(500, 1), FUN = VaRfun),
>> it outputs a vector of NAs and I don't know why.
>> Thank you for your help!
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] mailing list  To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/rhelp>> PLEASE do read the posting guide
> http://www.Rproject.org/postingguide.html>> and provide commented, minimal, selfcontained, reproducible code.
______________________________________________
[hidden email] mailing list  To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.

