AppliedMultiple choice
Shapes in a returns matrix · Part 2 of 3
R is a NumPy float64 array of daily returns with shape (250, 40): 250 days by 40 assets. Broadcasting aligns shapes from the right, and a dimension of size 1 stretches to match the other array.
You want to subtract each day’s cross-sectional mean, the average across the 40 assets, from that day’s returns. Which line does it?
- A
R - R.mean(axis=1) - B
R - R.mean(axis=1, keepdims=True) - C
R - R.mean(axis=0), which broadcasts cleanly and so must be the intended one - D
R - R.mean()
The worked solution is in Premium
The answer, the full working and the one idea to take away – for this and all 1,322 questions in the bank. Answer it in practice and your working is marked, with a known mistake named when you make one.
Learn the method
More python and data for quants questions
- You add an array of shape (100, 5) to one of shape (5,). What happens?Foundation
- How much memory does a 5,000 × 5,000 float64 NumPy array use, in megabytes?Foundation
- How many elements does the result of adding NumPy arrays of shapes (3, 1) and (1, 4) have?Foundation
- What is the length of np.arange(10).reshape(2, 5).sum(axis=0)?Foundation
- You slice a[::2] and then modify the result. What happens to a?Applied
- What is the value in np.array([127], dtype=np.int8) + 1?Applied