I have my cov matrix and weights in 2 separate dataframes and I would like to compute portfolio variance (for each portfolio at each date, my example only has 1 portfolio and 2 dates for simplicity):
weights <- data.frame(Portfolio = c("P1", "P1", "P1", "P1", "P1", "P1"),
Date = c("2008-03-31", "2008-03-31", "2008-03-31", "2008-06-30", "2008-06-30", "2008-06-30"),
ID = c("Asset1", "Asset2", "Asset3", "Asset1", "Asset2", "Asset3"),
Wgt = c(0.1, 0.2, 0.3, 0.3, 0.2, 0.1))
covar <- data.frame(
Date = c("2008-03-31", "2008-03-31", "2008-03-31", "2008-03-31", "2008-03-31", "2008-03-31", "2008-03-31", "2008-03-31", "2008-03-31",
"2008-06-30", "2008-06-30", "2008-06-30", "2008-06-30", "2008-06-30", "2008-06-30", "2008-06-30", "2008-06-30", "2008-06-30"),
ID1 = c("Asset1", "Asset2", "Asset3", "Asset1", "Asset2", "Asset3", "Asset1", "Asset2", "Asset3",
"Asset1", "Asset2", "Asset3", "Asset1", "Asset2", "Asset3", "Asset1", "Asset2", "Asset3"),
ID2 = c("Asset1", "Asset1", "Asset1", "Asset2", "Asset2", "Asset2", "Asset3", "Asset3", "Asset3",
"Asset1", "Asset1", "Asset1", "Asset2", "Asset2", "Asset2", "Asset3", "Asset3", "Asset3"),
cov = c(0.011, 0.012, 0.013, 0.012, 0.022, 0.032, 0.013, 0.032, 0.033,
0.0011, 0.0012, 0.0013, 0.0012, 0.0022, 0.0032, 0.0013, 0.0032, 0.0033)
)
If I understand your question correctly, you would need to group by
PortfolioandDateand create a new column that calculates the variance of theWgtcolumn.I hope this helps!