I have a matrix H of size s×d with holdings of s stocks across d days. H shows how many shares of each stock is in my portfolio on each day. The number of shares can change from day to day due to trading. I also have a matrix D of size s×d with the dividend each stock paid on each day. Most of the entries in D is zero. I want to 1) calculate the total dividends received on each day across all stocks, 2) calculate the total dividends received for each stock across all days, 3) calculate the total dividends received across all stocks and days.
This is all fairly straightforward to do with a spreadsheet of course. What I would like to know is how this is done correctly in linear algebra.
I know that if I want a 1×n result I need to multiply a 1×d matrix with a d×n matrix, and I think there are some manipulations that can be done to the matrices H and D to get to each of the 3 cases but I am currently clueless as to what those might be...
For example (some R programming):
H = matrix(c(100,100,110,0,200,200), nrow = 2, byrow=TRUE)
D = matrix(c(0, 0, 5, 3, 0, 0), nrow = 2, byrow = TRUE)
Here stock 1 paid a dividend of 5 on day 3 when I held 110 shares so my total dividend is 550. Stock 2 paid a dividend on day 1 but I did not hold any shares so my total dividend is 0.
The daily returns is an element-by-element multiplication. In
MATLABthis isR=H.*DThe sum by stock is given by the diagonals of
$$T = H D^\top$$
Going down the diagonal, the sum of the 1st stock is 550 and for the second stock 0.
The sum by day is given by the diagonals of
$$U = H^\top D$$
Going down the diagonal, the sum for the 1st day is 0, the 2nd day 0 and the 3rd day 550.
The total sum is
$$S = {\rm trace}(T) = {\rm trace}(U)$$