[R-SIG-Finance] business day to monthly or quarterly aggregation.

Gabor Grothendieck ggrothendieck at gmail.com
Thu Jan 10 16:23:03 CET 2008


Using zoo, convert the dates to Date class.
Then convert them to yearmon class so every
date in a month will be labelled the same.  Replace
duplicates with NAs and use locf (last observation
carried forward to fill them back in).
library(zoo)

dt <- as.Date(ourdates)
ym <- as.yearmon(dt)
isdup <- duplicated(ym)
dt[isdup] <- NA
na.locf(dt)

To do the above with quarterly data use as.yearqtr instead of as.yearmon.

In your example the first 4 points are missing from the output.  If
that was intentional
then you could retain just those months with at least 20 points, say,
by replacing last
line with:

na.locf(dt)[ ave(seq_along(dt), ym, FUN = length) >= 20 ]

Another thing to consider is it would be easy to pick out just the first date
in each month using !isdup that we calculated above if you wanted to apply
that to the values being weighted rather than repeating the weights.

For more on zoo read the two zoo vignettes:

library(zoo)
vignette("zoo")
vignette("zoo-quickref")

On Jan 10, 2008 6:14 AM, Murali Menon <feanor0 at hotmail.com> wrote:
>
> Folks,
>
> I have a matrix of portfolio weights whose rownames are daily dates (weekends elided).
> I'd like to create a new matrix of portfolio weights such that the weight on the first
> day of the month (quarter) is copied to the rest of the month (quarter). The idea is
> that I rebalance my portfolio only on the first of the month (quarter). Is there a
> neat way to do this?
>
> The reason I'm thinking of copying weights till the end of each period is that then,
> in order to compute rebalancing costs, I just need to do a abs(diff) on the weights matrix and multiply by the transaction cost.
>
> I guess if I can just convert the vector of dates into a vector of repeated first-of-period
> dates, I can use this vector to index into the portfolio weights matrix. But I can't
> think how I could achieve this vector conversion either.
>
> Any suggestions?
>
> Thanks,
> Murali
>
>
> ourdates <- c("1997-01-29", "1997-01-30", "1997-01-31", "1997-02-03", "1997-02-04",
> "1997-02-05", "1997-02-06", "1997-02-07", "1997-02-10", "1997-02-11",
> "1997-02-12", "1997-02-13", "1997-02-14", "1997-02-17", "1997-02-18",
> "1997-02-19", "1997-02-20", "1997-02-21", "1997-02-24", "1997-02-25",
> "1997-02-26", "1997-02-27", "1997-02-28", "1997-03-03", "1997-03-04",
> "1997-03-05", "1997-03-06", "1997-03-07", "1997-03-10", "1997-03-11",
> "1997-03-12", "1997-03-13", "1997-03-14", "1997-03-17", "1997-03-18",
> "1997-03-19", "1997-03-20", "1997-03-21", "1997-03-24", "1997-03-25",
> "1997-03-26", "1997-03-27", "1997-03-28", "1997-03-31", "1997-04-01",
> "1997-04-02", "1997-04-03", "1997-04-04", "1997-04-07", "1997-04-08",
> "1997-04-09", "1997-04-10", "1997-04-11", "1997-04-14", "1997-04-15",
> "1997-04-16", "1997-04-17", "1997-04-18", "1997-04-21", "1997-04-22",
> "1997-04-23", "1997-04-24", "1997-04-25", "1997-04-28", "1997-04-29",
> "1997-04-30", "1997-05-01", "1997-05-02", "1997-05-05", "1997-05-06",
> "1997-05-07", "1997-05-08", "1997-05-09", "1997-05-12", "1997-05-13",
> "1997-05-14", "1997-05-15", "1997-05-16", "1997-05-19", "1997-05-20",
> "1997-05-21", "1997-05-22", "1997-05-23", "1997-05-26", "1997-05-27",
> "1997-05-28", "1997-05-29", "1997-05-30", "1997-06-02", "1997-06-03",
> "1997-06-04", "1997-06-05", "1997-06-06", "1997-06-09", "1997-06-10",
> "1997-06-11", "1997-06-12", "1997-06-13", "1997-06-16", "1997-06-17"
> )
>
> set.seed(123)
>
> pWt <- matrix(rnorm(1000), ncol = 10)
>
> somehow create new vector newdates: ("1997-02-03", "1997-02-03", "1997-02-03", "1997-02-03", "1997-02-03", "1997-02-03", "1997-02-03", "1997-02-03", "1997-02-03", "1997-02-03", "1997-02-03", "1997-02-03", "1997-02-03", "1997-02-03", "1997-02-03", "1997-02-03", "1997-02-03", "1997-02-03", "1997-02-03", "1997-02-03", "1997-03-03", "1997-03-03", "1997-03-03", "1997-03-03", "1997-03-03", "1997-03-03", "1997-03-03", "1997-03-03", "1997-03-03", "1997-03-03", "1997-03-03", "1997-03-03", "1997-03-03", "1997-03-03", "1997-03-03", "1997-03-03", "1997-03-03", "1997-03-03", "1997-03-03", "1997-03-03", "1997-03-03", "1997-04-01", "1997-04-01", "1997-04-01", "1997-04-01", "1997-04-01", "1997-04-01", "1997-04-01", "1997-04-01", "1997-04-01", "1997-04-01", "1997-04-01", "1997-04-01", "1997-04-01", "1997-04-01", "1997-04-01", "1997-04-01", "1997-04-01", "1997-04-01", "1997-04-01", "1997-04-01", "1997-04-01", "1997-04-01", "1997-05-01", "1997-05-01", "1997-05-01", "1997-05-01", "1997-05-01", !
>  "1997-05-01", "1997-05-01", "1997-05-01", "1997-05-01", "1997-05-01", "1997-05-01", "1997-05-01", "1997-05-01", "1997-05-01", "1997-05-01", "1997-05-01", "1997-05-01", "1997-05-01", "1997-05-01", "1997-05-01", "1997-05-01", "1997-06-02", "1997-06-02", "1997-06-02", "1997-06-02", "1997-06-02", "1997-06-02", "1997-06-02", "1997-06-02", "1997-06-02", "1997-06-02", "1997-06-02", "1997-06-02")
>
> pWt[newdates, ] will give me the repeated monthly weights that I want.
>
> And similarly for repeated quarterly weights.
> _________________________________________________________________
> Share life as it happens with the new Windows Live.
>
> 08
> _______________________________________________
> 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