Fast Min-$\epsilon$ Segmented Regression using Constant-Time Segment Merging

Ansgar Lößer, Max Schlecht, Florian Schintke, Joel Witzke, Matthias Weidlich, Björn ScheuermannICML 2025

Introduction

Segmented regression is a vital statistical tool for modeling data that changes behavior over time—such as a robot's power consumption during different tasks, or sudden shifts in financial markets. The goal is to approximate a complex, noisy function using $k$ simpler pieces (segments) to minimize the mean squared error (MSE).

The "gold standard" approach uses dynamic programming (DP) to find mathematically optimal breakpoints. However, this optimal solution has a computational complexity of $\mathcal{O}(n^2)$, making it prohibitively slow for large datasets containing millions of points. While existing "fast" heuristics can run in $\mathcal{O}(n \log n)$ time, they often sacrifice significant accuracy. They tend to produce either too many segments or place breakpoints poorly, leading to high MSE—especially when a user needs exactly $k$ segments for qualitative analysis.

To bridge this gap, researchers have developed a new greedy algorithm for segmented regression that achieves near-optimal accuracy while scaling efficiently. By leveraging a clever matrix-based "constant-time merging" technique, the approach is up to 100x faster and 1,000x more accurate than previous state-of-the-art heuristics on large datasets.

The Core Innovation: Constant-Time Merging

The authors introduce a three-phase greedy approach that maintains high statistical efficiency without the $\mathcal{O}(n^2)$ computational price tag:

  1. Initial Placement: The algorithm starts by creating the maximum possible number of tiny segments across the dataset.
  2. Greedy Reduction: The algorithm iteratively merges the two adjacent segments that result in the smallest increase to the total error, continuing until exactly $k$ segments remain.
  3. Placement Optimization: After reaching the target $k$ segments, a final "fine-tuning" pass nudges breakpoints locally to further minimize error.
Step-by-step visualization of the greedy segment merging process.

The most significant breakthrough in this method is the constant-time trick used during the greedy reduction phase. Standard regression requires re-scanning all data points within a segment to calculate its error. The authors realized that the components of the Ordinary Least Squares (OLS) solution—specifically, the Gram matrices derived from the independent and dependent variables—are additive.

By precomputing these matrices, merging two segments simply requires adding their respective matrices together. This turns a linear-time re-calculation into a constant-time matrix addition, allowing the algorithm to evaluate merges and calculate the new segment's error in $\mathcal{O}(1)$ time relative to the number of samples ($n$).

Experimental Evaluation and Key Results

The paper evaluates the algorithm using both synthetic datasets (piecewise continuous functions with varying sample sizes and Gaussian noise) and a practical real-world dataset (a time series of CPU usage with over 70,000 samples).

The results demonstrate a massive Pareto improvement over existing methods:

  • Speed and Scalability: On datasets with more than $10^4$ samples, the new method is roughly 100x faster than the leading heuristic (Acharya et al.).
  • Accuracy: The method achieved up to 1,000x lower MSE than previous heuristics when constrained to the same number of segments. Unlike previous heuristics that often require $2k$ or $4k$ segments to achieve acceptable error rates, this method provides high accuracy with exactly $k$ segments, preserving the physical interpretability of the model.
  • Near-Optimality: On average, the error was only 3% higher than the mathematically optimal $\mathcal{O}(n^2)$ DP solution, but it reached that result in a fraction of the time.
MSE vs. Runtime comparison showing the proposed approach achieving near-optimal error at a fraction of the computational cost.

Interestingly, in high-noise scenarios, the greedy heuristic sometimes produces a model that is closer to the true underlying function than the "optimal" DP solution. This occurs because the optimal solution can overfit to the noise in the samples, whereas the greedy merging acts as a form of natural regularization.

The real-world implications are stark: on the 70,000-sample CPU dataset, the optimal dynamic programming solution took over 48 minutes to run. The authors' approach found the exact same solution in just 10 milliseconds.

Limitations and Future Work

While highly effective, the approach has a few limitations. As a greedy heuristic, it is theoretically possible for the algorithm to get "stuck" in a local optimum that is significantly worse than the global best, though experiments suggest this is rare in practice. Additionally, the current implementation does not strictly enforce continuity (where the end of one segment must perfectly meet the start of the next).

Finally, while the algorithm handles multiple independent variables, the "curse of dimensionality" still applies. As the number of dimensions increases, the gap between the heuristic and the optimal solution tends to widen if the sample size isn't sufficiently large. Future work may explore adapting the constant-time matrix trick to enforce strict continuity constraints for specialized piecewise applications.

Summary updated

BibTeX
@inproceedings{loer2025fast,
  author = {Ansgar Lößer and Max Schlecht and Florian Schintke and Joel Witzke and Matthias Weidlich and Björn Scheuermann},
  title = {{Fast Min-$\textbackslash\{\}epsilon$ Segmented Regression using Constant-Time Segment Merging}},
  booktitle = {International Conference on Machine Learning},
  year = {2025},
  url = {https://openreview.net/forum?id=w2QNIkcwWw},
}
Figure