{"id":10036,"library":"piecewise-regression","title":"Piecewise Regression","description":"Piecewise-regression is a Python library for performing segmented (or piecewise) regression analysis. It allows fitting models with one or more breakpoints, identifying where the relationship between independent and dependent variables changes. The current version is 1.5.0, with a moderate release cadence focused on statistical improvements and convenience features.","status":"active","version":"1.5.0","language":"en","source_language":"en","source_url":"https://github.com/chasmani/piecewise-regression","tags":["statistics","regression","segmented regression","data science","modeling"],"install":[{"cmd":"pip install piecewise-regression","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Core numerical operations and data handling.","package":"numpy"},{"reason":"Scientific computing, optimization routines for model fitting.","package":"scipy"},{"reason":"Plotting and visualization of fit results.","package":"matplotlib"},{"reason":"Statistical tests and models, particularly for p-value calculations and statistical inference.","package":"statsmodels"},{"reason":"Enhanced statistical data visualization.","package":"seaborn"}],"imports":[{"symbol":"PiecewiseRegression","correct":"from piecewise_regression import PiecewiseRegression"}],"quickstart":{"code":"import numpy as np\nfrom piecewise_regression import PiecewiseRegression\nimport matplotlib.pyplot as plt\n\n# Generate some synthetic data with a known breakpoint\nx = np.linspace(0, 100, 500)\ny = np.concatenate([\n    -0.1 * x[x <= 50] + 10,\n    0.5 * x[x > 50] - 20\n]) + np.random.normal(0, 1, 500)\n\n# Initialize and fit the model with one breakpoint\npw_fit = PiecewiseRegression(x, y, n_breakpoints=1)\npw_fit.fit()\n\n# Print a summary of the fit\npw_fit.summary()\n\n# Plot the fit (requires matplotlib to show)\nfig, ax = pw_fit.plot_fit(show=False)\nax.set_title('Piecewise Regression Fit')\nplt.show()","lang":"python","description":"This quickstart demonstrates how to generate synthetic data with a breakpoint, initialize the `PiecewiseRegression` model, fit it to the data, and then visualize the results. It highlights the core workflow from data preparation to model interpretation."},"warnings":[{"fix":"Be aware that statistical results related to breakpoint significance might differ. Refer to the v1.5.0 release notes and cited literature (Muggeo, 2008) for details on the new methodology.","message":"The Davies test for the existence of breakpoints was modified in v1.5.0 to use the Wald test statistic. This change affects the underlying methodology and may result in different p-values and statistical conclusions for breakpoint detection compared to versions prior to 1.5.0.","severity":"gotcha","affected_versions":">=1.5.0"},{"fix":"Start with a reasonable number based on domain knowledge or visual inspection of the data. Use statistical tests provided by the library (e.g., Davies test for 1 breakpoint) or information criteria (e.g., AIC, BIC) for model selection, though these are not directly implemented for breakpoint count in the library itself.","message":"Choosing the correct number of breakpoints (`n_breakpoints`) is crucial. Incorrectly specifying this parameter can lead to overfitting (too many breakpoints) or underfitting (too few), resulting in a poor model fit and misleading conclusions.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Inspect any warnings during the `fit()` process. Consider pre-processing your data, checking for outliers, or experimenting with different `n_breakpoints` values. Ensure your data distribution is suitable for linear regression segments.","message":"The optimization process for finding breakpoints and regression coefficients can sometimes fail to converge, especially with noisy data, sparse data, or poorly chosen initial parameters. This might lead to warnings or incorrect results.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Install the library using pip: `pip install piecewise-regression`. Ensure you are in the correct virtual environment if you are using one.","cause":"The piecewise-regression library is not installed or the Python environment where the script is run does not have it installed.","error":"ModuleNotFoundError: No module named 'piecewise_regression'"},{"fix":"Ensure `n_breakpoints` is a positive integer and `n_breakpoints < len(x) - 2`. For example, with 5 data points, `n_breakpoints` can be at most 2. Start with `n_breakpoints=1` for basic breakpoint detection.","cause":"The specified `n_breakpoints` is either zero, negative, or too large relative to the number of data points, making it statistically impossible or ill-defined to fit segments.","error":"ValueError: n_breakpoints must be a positive integer less than number_of_data_points - 2"},{"fix":"Add the import statement at the top of your script: `from piecewise_regression import PiecewiseRegression`.","cause":"The `PiecewiseRegression` class was used without being correctly imported from the library.","error":"NameError: name 'PiecewiseRegression' is not defined"}]}