[R-SIG-Finance] Rolling functions on matrices
Brian G. Peterson
brian at braverock.com
Mon Sep 24 19:06:13 CEST 2007
There are many examples on the archives of this list for using the
rollapply function. Perhaps you could start there.
Regards,
- Brian
Murali Menon wrote:
> Folks,
>
> I'm a bit stymied by the following problem. I have a matrix of returns for various equities, and a matrix of implied vols for these equities. So, e.g.:
>
> returns <- matrix(runif(40), ncol = 4)
> colnames(returns) <- c("IBM", "MOT", "NOK", "INTC")
> vols <- matrix(rnorm(40, 7, 1), ncol = 4)
> colnames(vols) <- colnames(returns)
>
> #I need weighted MA over lags of 2, 16, 32
>
> lagvals <- c(2, 16, 32)
> coeff <- sapply(lagvals , function(n) c(rep(0, max(lagvals ) - n), 1 : n))
> colnames(coeff) <- as.character(paste("MA", lagvals , sep = ""))
>
> Now I'd like to compute weighted returns for each equity i and each lag index j such that at each point in time (t), I have:
>
> wtRet[t, i, j] = returns[t, i] / vols[t, i] * average.vol.over.past.lag.days
>
> So for lag = 32, i would compute average.vol.over.past.lag.days = mean(vols[(t - 32 + 1) : t, i]),
>
> and so on for the other lags.
>
> And then, finally, do a rolling weighted mean where the weighting for each lag is given by the coeff matrix above.
>
> I hope this is not incomprehensible!
>
> Any suggestions? I'm getting lost with all the indexation required between equity and lags, and
> indeed the rolling function to apply.
>
> Also, for my real application, the matrices are biggish (10-20 years), so if I can minimise any computational time, that would be an added bonus.
>
> Thanks very much,
> Murali
> _________________________________________________________________
>
> Hotmail®. NOW with 5GB storage.
>
> ration_HM_mini_5G_0907
> _______________________________________________
> R-SIG-Finance at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only.
> -- If you want to post, subscribe first.
More information about the R-SIG-Finance
mailing list