I just released a major new version of BCES, the Python package for linear regression with measurement errors. In version 2.0, the big addition is weighted least squares (WLS) regression.

<h2 id=whats-new>What’s new</h2><p>BCES has handled the case where both X and Y have measurement errors since version 1.0, using the bivariate correlated errors and intrinsic scatter method from Akritas & Bershady (1996). But there’s a common situation in astronomy where only Y has significant measurement errors—think luminosity vs. redshift, or flux vs. some well-determined physical parameter. For that case, the full BCES machinery is overkill.</p>

Version 2.0 adds WLS regression following Section 2.3 of the same Akritas & Bershady (1996) paper. It does three things that ordinary least squares doesn't:

  1. Weights by measurement precision. Each data point is weighted by the inverse of its error variance, so your best measurements drive the fit rather than being treated equally with noisy ones.
  2. Accounts for intrinsic scatter. The method estimates the intrinsic variance in the population and folds it into the weights, so your error bars on the slope and intercept reflect the true scatter, not just measurement noise.
  3. Returns unbiased estimates of the slope and intercept, just like BCES.

<h2 id=when-to-use-wls-vs-bces>When to use WLS vs. BCES</h2><p>The rule is simple:</p>

Both methods handle intrinsic scatter. If you've been using ordinary least squares for the Y-errors-only case, WLS will give you statistically better estimates—especially when your error bars vary across the sample.

Usage

Follows the same pattern as BCES:

import bces.bces as BCES

# WLS fit (only Y has errors)
a, b, aerr, berr, covab = BCES.wls(x, y, yerr)

# With parallel bootstrapping
a, b, aerr, berr, covab = BCES.wlsp(x, y, yerr, nsim=10000)

Note that unlike BCES, WLS returns scalars (a single regression line) rather than arrays of four different regression types.

<h2 id=getting-started>Getting started</h2><div class="highlight"><pre>pip install –upgrade bces </pre></div>

I put together a Jupyter notebook with WLS examples, including fits with intrinsic scatter. The BCES examples notebook is still there for the bivariate case.

If you use this in a paper, please cite Nemmen et al. 2012, Science, 338, 1445 and the original Akritas & Bershady 1996, ApJ, 470, 706.