Problem · Array

Cumulative No-Intercept Regression Betas

Learn this problem
MediumCitadel logoCitadelFULLTIMEPHONE SCREEN

Problem statement

You receive two equally shaped matrices, xRows and yRows. Each row contains returns for the same p paired asset columns. The positive integer array batchSizes partitions the rows into consecutive batches: the first value gives the number of rows in batch 0, the second gives the number in batch 1, and so on. The values sum to the total number of rows.

Process batches in order. For each asset column j, pair every observed x value with the corresponding y value. After adding batch i, compute the cumulative no-intercept regression slope

beta[j] = sum(x * y) / sum(x * x),

where both sums use every paired observation from batches 0 through i, inclusive.

Return one row of p slopes after every batch prefix. Answers are compared with absolute or relative tolerance 10^-6.

Function

cumulativeNoInterceptBetas(xRows: double[][], yRows: double[][], batchSizes: int[]) → double[][]

Examples

Example 1

xRows = [[1.0,2.0],[2.0,1.0],[3.0,2.0]]yRows = [[2.0,1.0],[4.0,3.0],[6.0,2.0]]batchSizes = [2,1]return = [[2.0,1.0],[2.0,1.0]]

After the first batch, asset 0 has slope (1*2 + 2*4) / (1^2 + 2^2) = 2, while asset 1 has slope (2*1 + 1*3) / (2^2 + 1^2) = 1. The second batch preserves both ratios.

Example 2

xRows = [[1.0],[2.0],[1.0]]yRows = [[1.0],[2.0],[3.0]]batchSizes = [2,1]return = [[1.0],[1.3333333333333333]]

The first prefix has numerator and denominator 5. After the next pair, the numerator becomes 8 and the denominator becomes 6.

Constraints

  • 1 <= xRows.length = yRows.length <= 200000
  • 1 <= batchSizes.length <= 200
  • Every batch size is positive, and their sum equals xRows.length.
  • 1 <= p <= 100, and every row has exactly p columns.
  • Every value is finite and has absolute value at most 10^6.
  • For every asset and every batch prefix, cumulative sum(x*x) is positive.

More Citadel problems

drafts saved locally
public double[][] cumulativeNoInterceptBetas(double[][] xRows, double[][] yRows, int[] batchSizes) {
    // Write your solution here.
}
xRows[[1.0,2.0],[2.0,1.0],[3.0,2.0]]
yRows[[2.0,1.0],[4.0,3.0],[6.0,2.0]]
batchSizes[2,1]
expected[[2.0,1.0],[2.0,1.0]]
checking account