Online No-Intercept Linear Regression
Learn this problemProblem statement
You are fitting a no-intercept linear-regression model y = kx to observations that arrive in batches. The x-values and y-values are provided as matching matrices xBatches and yBatches; row i contains the paired observations in batch i.
Process the batches in order. Maintain the cumulative values
numerator = sum(x * y)denominator = sum(x^2)
over every observation seen so far. After each complete batch, the current slope is k = numerator / denominator.
Return an array containing the cumulative slope after every batch, in input order. Update the two running sums incrementally; do not rescan observations from earlier batches.
Function
onlineNoInterceptSlopes(xBatches: double[][], yBatches: double[][]) β double[]Examples
Example 1
xBatches = [[1.0,2.0],[3.0]]yBatches = [[2.0,4.0],[9.0]]return = [2.0,2.642857142857143]After the first batch, the running numerator is 1 * 2 + 2 * 4 = 10 and the denominator is 1^2 + 2^2 = 5, so the slope is 2. The second batch adds 27 to the numerator and 9 to the denominator, giving 37 / 14.
Example 2
xBatches = [[-2.0,1.0],[0.0,4.0]]yBatches = [[4.0,1.0],[5.0,8.0]]return = [-1.4,1.1904761904761905]The first batch gives (-2 * 4 + 1 * 1) / ((-2)^2 + 1^2) = -7 / 5 = -1.4. The sample with x = 0 changes neither running sum. After adding (4, 8), the cumulative slope is 25 / 21.
Constraints
1 <= xBatches.length = yBatches.length- Every batch is non-empty, and
xBatches[i].length = yBatches[i].length. - The total number of observations across all batches is at most
10^5. - All coordinates are finite double-precision values.
- After every batch, the cumulative
sum(x^2)is positive. - Answers within an absolute or relative error of
10^-6are accepted.
More Two Sigma problems
- Balanced Split String with WildcardsOA Β· Seen Jul 2026
- Closest ColorOA Β· Seen Jul 2026
- Linear InterpolatorOA Β· Seen Jun 2026
- Piecewise Linear Interpolation and ExtrapolationOA Β· Seen May 2026
- Sewer Drainage PartitionOA Β· Seen Mar 2026
- Calculate y/x using PatchOA Β· Seen Nov 2025
- Nums That Are Divisible by N πSeen Mar 2024
- Replacing Val πSeen Mar 2024