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

Robert Sams robert at sanctumfi.com
Thu Jan 10 14:06:41 CET 2008


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", "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")
> 
> 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 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