business day to monthly or quarterly aggregation.
A quick two functions to get the quarters (and more), though this doesn't address your full issue (or the issue Patrick brings up): This is from the new xts package we have put up on CRAN and on R-forge ( http://r-forge.r-project.org/projects/xts/ ). the as.xts is what you'll need xts for, though I am sure you can make a non-generic solution instead. `endpoints` <- function(x,on='months') { x <- as.xts(x) if(on=='quarters') { xi <- (as.POSIXlt(index(x))$mon%/%3) + 1 c(0,which(diff(xi) != 0),NROW(x)) } else { on.opts <- list(secs='%S',seconds='%S',mins='%M',minutes='%M', hours='%H',days='%j', weeks='%W',months='%m',years='%y') c(0,which(diff(as.numeric(format(index(x),on.opts[[on]]))) != 0),NROW(x)) } } `startof` <- function(x,by='months') { ep <- endpoints(x,on=by) (ep+1)[-length(ep)] } A quick intro to xts: An extension of zoo to enforce time-based indexing, while allowing for arbitrary attributes to be added on to an object. The idea is to allow for quick time based subsetting, as well as clean conversion among the different R data classes, both back and forth, while maintaining all the available object information. It basically makes time-series life easy in R by standardizing time-series behavior to 'zoo-like' behavior. And all the greatness of zoo is preserved! The SVN code has some very handy sub-setting methods in the works: library(quantmod); library(its) getSymbols("QQQQ",src='yahoo',return.class='its') as.xts(QQQQ)['2007'] # all of 07 as.xts(QQQQ)['2007-01'] # just January as.xts(QQQQ)['2007-03::2007-03-20'] # March 07 - beginning of month to the 20th (even if the 20th doesn't exist) reclass(as.xts(QQQQ)) # though contrived returns the [now xts] QQQQ object back to its original class (whatever that may have been - ts, its, timeSeries, matrix, data.frame, zoo - exactly as found). The functions are not yet [exported] in the xts CRAN release - though in the SVN on r-forge. Jeff
On Jan 10, 2008 9:02 AM, Murali Menon <feanor0 at hotmail.com> wrote:
Hi,
Thanks, Robert, that works nicely. I'm trying to adapt it for quarterly.
Brian, I was not aware of the quantmod stuff: thanks for the pointer.
Cheers,
Murali> From: robert at sanctumfi.com> To: feanor0 at hotmail.com; r-sig-finance at stat.math.ethz.ch> Date: Thu, 10 Jan 2008 13:06:41 +0000> Subject: RE: [R-SIG-Finance] business day to monthly or quarterly aggregation.> > Hi Murali,> > Does this help?> > dateparts <- function (x) {> + list(mday = as.POSIXlt(x)$mday, month = as.POSIXlt(x)$mon + > + 1, year = as.POSIXlt(x)$year + 1900, wday => as.POSIXlt(x)$wday)> + }> > > > 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", "19!
97-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")> > > > n <- c(1, which(diff(dateparts(ourdates)$month) != 0) + 1,> length(ourdates))> > !
newdates <- ourdates> > for(i in 1:(length(n) - 1)){> + newdates[n[i]:
n[i+1]] <- ourdates[n[i]]> + }> > ## vector of start of months..> > newdates> [1] "1997-01-29" "1997-01-29" "1997-01-29" "1997-02-03" "1997-02-03"> "1997-02-03" "1997-02-03" "1997-02-03" "1997-02-03" "1997-02-03"> "1997-02-03"> [12] "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"> [23] "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"> [34] "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"> [45] "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"> [56] "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"> [67] "!
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"> [78] "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"> [89] "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"> [100] "1997-06-02"> > > > > > Robert Sams > > -----Original Message-----> From: r-sig-finance-bounces at stat.math.ethz.ch> [mailto:r-sig-finance-bounces at stat.math.ethz.ch] On Behalf Of Murali> Menon> Sent: 10 January 2008 11:14> To: r-sig-finance at stat.math.ethz.ch> Subject: [R-SIG-Finance] business day to monthly or quarterly> aggregation.> > > 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 th!
e month (quarter) is copied to the rest of> the month (quarter). The i
dea 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.
_________________________________________________________________
GLM_CPC_VideoChat_distantfamily_012008
[[alternative HTML version deleted]]
_______________________________________________
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.
There's a way to do it better - find it. Thomas A. Edison