How do I apply portfolio weights to every day’s returns?

Multiply a days-by-stocks return matrix by a vector of weights without a loop, learn the rule NumPy uses to line up two arrays of different shapes, and add up each day’s portfolio return.

After this lesson, you can compute each stock’s contribution to a portfolio’s daily return and add the contributions up.

How do I multiply each stock by its weight on every day?

returns * weights multiplies each column of the return matrix by its stock’s weight on every day, without a loop, so AAA’s return of 0.012 on 2 March with a weight of 0.5 contributes 0.006. NumPy matches arrays of different shapes from the right, and a missing axis counts as length 1, so the three weights line up with the three stock columns and repeat for all eight days. This is called broadcasting.

The returns are three made-up stocks’ daily returns in decimals over eight days, one column per stock, as in the lesson on return matrices. The weights must be in the same order as the columns.

The following box computes the contributions of the first two days.

import numpy as np

returns = np.column_stack([
    [0.0120, -0.0085, 0.0034, -0.0151, 0.0062, 0.0097, -0.0023, 0.0045],
    [-0.0064, 0.0102, -0.0130, 0.0058, -0.0011, 0.0076, 0.0149, -0.0092],
    [0.0031, -0.0047, 0.0088, 0.0015, -0.0203, 0.0124, 0.0006, 0.0071],
])
weights = np.array([0.5, 0.3, 0.2])
contributions = returns * weights
print(returns.shape, weights.shape, contributions.shape)
print(contributions[:2].round(5))
(8, 3) (3,) (8, 3)
[[ 0.006   -0.00192  0.00062]
 [-0.00425  0.00306 -0.00094]]

What if each day has its own factor?

Eight factors, one per day, have the shape (8,), which NumPy matches against the three stock columns, so returns * factors raises a ValueError. factors[:, None] adds a column axis of length 1, which gives the shape (8, 1), so each day’s factor applies across its row.

The factors are 1 for the first four days and 2 for the last four. On a square matrix, the wrong shape gives no error and silently multiplies the wrong cells, so the shape of a factor needs a check.

The following box catches the error and applies the factors as a column.

factors = np.array([1, 1, 1, 1, 2, 2, 2, 2])
try:
    returns * factors
except ValueError as error:
    print(type(error).__name__)
print(returns[4, 0], (returns * factors[:, None])[4, 0])
ValueError
0.0062 0.0124

How do I get one portfolio return per day?

.sum(axis=1) adds each day’s contributions across the stocks, as in the lesson on axes, which gives the portfolio’s daily return. On 2 March it is \(0.5 \times 0.0120 + 0.3 \times (-0.0064) + 0.2 \times 0.0031 = 0.0047\).

The same weights every day assume that the portfolio is rebalanced to them before each day, as the lesson on drifting weights explains.

The following box computes the daily portfolio returns.

print(contributions.sum(axis=1).round(5))
[ 0.0047  -0.00213 -0.00044 -0.00551 -0.00129  0.00961  0.00344  0.00091]

Your turn

With weights of 0.4, 0.4 and 0.2, print the contributions on 9 March, row 5. You should see 0.00388 for AAA, 0.00304 for BBB and 0.00248 for CCC.

The new weights broadcast in the same way.

print((returns * np.array([0.4, 0.4, 0.2]))[5].round(5))
[0.00388 0.00304 0.00248]