skillfed

piecewise-regression

piecewise (segmented) regression in python

piecewise-regression v1.5.0 133.8K downloads/30d#11,495 on PyPI131
Permissive license MIT DORMANT released

What it is and what it does

Piecewise-regression implements Muggeo's iterative algorithm to fit piecewise linear models—straight lines with one or more breakpoints where the slope changes. You provide x and y data plus either initial breakpoint guesses or a desired number of breakpoints, and the package estimates the segment gradients, breakpoint positions, and confidence intervals for all parameters. It includes a Davies test to assess whether breakpoints exist statistically.

The package uses bootstrap restarting to escape local optima and improve the chance of finding a global solution, though convergence is not guaranteed. It also offers a ModelSelection tool based on Bayesian Information Criterion (BIC) to compare models with different numbers of breakpoints. Results can be extracted as structured data or plotted with matplotlib for visualization of the fit, breakpoints, and confidence bands.

Use it for:

  • Detect and quantify shifts in trend in time-series data, such as changes in growth rate before and after a policy intervention.
  • Identify optimal threshold points in dose-response or dose-effect studies where biological or physical response changes slope.
  • Analyze piecewise relationships in economic or environmental data where structural breaks occur at known or unknown transition points.
  • Compare competing segmented models using BIC to select the number of breakpoints that best explains the data.
  • Extract confidence intervals around estimated breakpoint positions for hypothesis testing or uncertainty quantification.

Worth the install?

AI-flagged interpretation of the facts on this page — verify before relying

Fits piecewise linear regression models to data with one or more breakpoints, estimating segment gradients and breakpoint positions with confidence intervals.

Yes, if you need to fit piecewise linear models with statistical inference. The package is well-maintained enough (last commit mid-2024), has no known vulnerabilities, and low install friction. Dormant status is not a blocker for a stable, narrowly-scoped tool. Install with caution if you require active development or frequent updates; for one-off analysis or established workflows, it is reliable.

Install

piecewise-regression on PyPI

pip

pip install piecewise-regression

uv

uv add piecewise-regression

poetry

poetry add piecewise-regression

Installing piecewise-regression

Before you install

Low install friction with a pure Python wheel and four standard scientific dependencies (numpy, scipy, matplotlib, statsmodels). Maintenance is dormant—last release was 2023-12-18 and last commit 2024-06-09—but the repository remains active and unarchived.

License in practice

MIT license is permissive; you can use, modify, and distribute this package freely in commercial and private projects with minimal restrictions.

Quickstart

pip install piecewise-regression

import piecewise_regression
import numpy as np

x = np.linspace(0, 20, 100)
y = 100 - 4*x + 2*np.maximum(x - 7, 0) + np.random.normal(size=100)
pw_fit = piecewise_regression.Fit(x, y, n_breakpoints=1)
pw_fit.summary()

Verify before relying

  • Whether the package handles edge cases (e.g., collinear data, very few observations) gracefully or requires specific data characteristics.
  • Performance characteristics on large datasets or high-dimensional breakpoint searches.
  • Whether bootstrap restarting convergence guarantees improve with specific parameter tuning beyond n_boot.

Package facts

License MIT (permissive)
Python support not specified
Install friction low — pure-Python wheel
Runtime dependencies 4 — numpy, matplotlib, scipy, statsmodels
Maintenance dormant — 970 days since the last release
Last repo commit
First released
Downloads 133,799/month — #11,495 on PyPI (30-day window, as of 2026-08-14)
Known vulnerabilities none known (OSV.dev, checked 2026-08-14)

Evidence: piecewise_regression-1.5.0-py3-none-any.whl

License :: OSI Approved :: MIT LicenseProgramming Language :: Python :: 3Programming Language :: Python :: 3.10Programming Language :: Python :: 3.7Programming Language :: Python :: 3.8Programming Language :: Python :: 3.9

Tags

piecewise linear regressionsegmented regression breakpointsfitting lines with breakpointsregression model breakpoint detectionmulti-segment linear fittingchange point regressionsegmented line fitting
regression-analysisstatistical-modelingbreakpoint-detection

More Mathematics packages