Compute Portfolio Variance for multiple dates and multiple portfolios from 2 dataframes

24 views Asked by At

I am trying to compute the portfolio variance for each portfolio at each date based on the 2 dataframes below:

wgts <- data.frame(portfolio= c("P1", "P1", "P2", "P2","P1", "P1", "P2", "P2"),
    DATE = c("20080331", "20080331", "20080331","20080331",  "20080630", "20080630", "20080630", "20080630"),
    ID = c("Asset1", "Asset2", "Asset1", "Asset2","Asset1", "Asset2", "Asset1", "Asset2"),
    Wgt = c(0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8)
)

covar <- data.frame(
    DATE = c("20080331", "20080331", "20080331","20080331",  "20080630", "20080630", "20080630", "20080630"),
    ID1 = c("Asset1", "Asset2", "Asset1", "Asset2", "Asset1", "Asset2", "Asset1", "Asset2"),
    ID2 = c("Asset1", "Asset1", "Asset2", "Asset2", "Asset1", "Asset1", "Asset2", "Asset2"),
    cov = c(0.011,      0.012,   0.012,    0.022,   .0011,      0.0012,   0.0012,    0.0022)
)

So the target output should be something like this:

               P1       P2
20080331 0.001470 0.007390
20080630 0.001787 0.003291
0

There are 0 answers