Good Afternoon Everyone,
I have a series of returns are was hoping that someone could help me create a variable based on a pre-specified set of lagged observations. For example, I would want to sum up the last 25 returns to make a new return column. I would also like to weight the returns by a factor of 0.25^i with i being the lag number. I am intuitively thinking a for loop but can not rationalize internally how to write this. I am trying to make a variable like below: newreturn = return(-1)*0.25^1 + return(-2)*0.25^2 + .... return(-25)*0.25^25 And obviously I would like to apply this to all new observations past the 25th observatios to the end of my dataset. I hope this is clear as I know the solution must be relatively simple. I hope to hear from you guys. Thanks, Am Gut [[alternative HTML version deleted]] _______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go. |
On Wed, Feb 15, 2017 at 2:11 PM, Am Gut <[hidden email]> wrote:
> Good Afternoon Everyone, > > I have a series of returns are was hoping that someone could help me create > a variable based on a pre-specified set of lagged observations. For > example, I would want to sum up the last 25 returns to make a new return > column. I would also like to weight the returns by a factor of 0.25^i with > i being the lag number. I am intuitively thinking a for loop but can not > rationalize internally how to write this. I am trying to make a variable > like below: > > newreturn = return(-1)*0.25^1 + return(-2)*0.25^2 + .... return(-25)*0.25^25 > > And obviously I would like to apply this to all new observations past the > 25th observatios to the end of my dataset. I hope this is clear as I know > the solution must be relatively simple. I hope to hear from you guys. > TTR::WMA. It calculates a weighted moving average, and the denominator in the average is the sum of the weights. So if you just want the sum, you can multiply the result by the sum of the weights. Or you can use a for loop, like you thought. # random data set.seed(21) x <- rnorm(10, 0, 0.01) n <- 5 # pre-allocate result y <- x * NA # weight vector w <- 0.25^(n:1) # loop over input for(i in n:length(x)) { y[i] <- sum(x[(i-n+1):i] * w) } # Use TTR's WMA function z <- TTR::WMA(x, n, w) * sum(w) # verify the results are the same all.equal(y, z) > Thanks, > > Am Gut > > [[alternative HTML version deleted]] > > _______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-sig-finance > -- Subscriber-posting only. If you want to post, subscribe first. > -- Also note that this is not the r-help list where general R questions should go. -- Joshua Ulrich | about.me/joshuaulrich FOSS Trading | www.fosstrading.com R/Finance 2017 | www.rinfinance.com _______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go. |
Free forum by Nabble | Edit this page |