
Security News
The Hidden Blast Radius of the Axios Compromise
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.
bces
Advanced tools
Python module for performing linear regression for data with measurement errors and intrinsic scatter
Python module for performing robust linear regression on (X,Y) data points with measurement errors.
The BCES fitting method is the bivariate correlated errors and intrinsic scatter (BCES) and follows the description given in Akritas & Bershady. 1996, ApJ. Some of the advantages of BCES regression compared to ordinary least squares (OLS) fitting:
The WLS (weighted least squares) method handles the case where only Y has measurement errors and X is treated as error-free. It accounts for intrinsic scatter in the data and follows Akritas & Bershady 1996, §2.3.
pip install bces
Alternatively, if you plan to modify the source then install the package with a symlink, so that changes to the source files will be immediately available:
pip install -e .
import bces.bces as BCES
a,b,aerr,berr,covab=BCES.bcesp(x,xerr,y,yerr,cov)
Arguments:
If you have no reason to believe that your measurement errors are correlated (which is usually the case), you can provide an array of zeroes as input for cov:
cov = numpy.zeros_like(x)
Output:
Each element of the arrays a, b, aerr, berr and covab correspond to the result of one of the different BCES lines: $y|x$, $x|y$, bissector and orthogonal, as detailed in the table below. Please read the original BCES paper to understand what these different lines mean.
| Element | Method | Description |
|---|---|---|
| 0 | y|x | Assumes x as the independent variable |
| 1 | x|y | Assumes y as the independent variable |
| 2 | bissector | Line that bisects the y|x and x|y. This approach is self-inconsistent, do not use this method. |
| 3 | orthogonal | Orthogonal least squares: line that minimizes orthogonal distances. Should be used when it is not clear which variable should be treated as the independent one |
By default, bcesp runs the bootstrapping in parallel.
import bces.bces as BCES
a,b,aerr,berr,covab=BCES.wls(x,y,yerr)
Arguments:
Output:
Note that unlike BCES, WLS returns scalar values (a single regression line) rather than 4-element arrays.
The wlsp method performs bootstrapping in parallel, if you need that.
Both methods return unbiased estimates of the slope and intercept, but they suit different statistical situations:
Both methods account for intrinsic scatter.
Why choose WLS over OLS? When only Y has measurement errors, prefer WLS over OLS. OLS assigns equal weight to every data point regardless of measurement uncertainty, while WLS weights each point by the inverse of its error variance so more precisely measured points have greater influence on the fit. This produces more accurate and statistically efficient estimates when data points have heteroscedastic (unequal) errors.
bces-examples.ipynb is a jupyter notebook including a practical, step-by-step example of how to use BCES to perform regression on data with uncertainties on x and y. It also illustrates how to plot the confidence band for a fit.
wls.ipynb is a jupyter notebook with examples of WLS regression, including fits with intrinsic scatter.

pytest -v -s
If you end up using this code in your paper, you are morally obliged to cite the following works
I spent considerable time writing this code, making sure it is correct and user-friendly, so I would appreciate your citation of the first paper in the above list as a token of gratitude.
If you are really happy with the code, you can buy me a coffee.
FAQs
Python module for performing linear regression for data with measurement errors and intrinsic scatter
We found that bces demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.

Research
A supply chain attack on Axios introduced a malicious dependency, plain-crypto-js@4.2.1, published minutes earlier and absent from the project’s GitHub releases.

Research
Malicious versions of the Telnyx Python SDK on PyPI delivered credential-stealing malware via a multi-stage supply chain attack.