Skip to content

Pin the fit weight convention with an analytic weighted-least-squares test #370

@rozyczko

Description

@rozyczko

Summary

_prepare_fit_arrays returns weights = 1/sigma and passes them as weights=dy to easy_science_multi_fitter.fit (src/easyreflectometry/fitting.py:122, 254). The internal chi^2 helper (fitting.py:138) consistently treats the same array as sigma-denominators, but whether the EasyScience core fitter expects 1/sigma, sigma, or 1/sigma^2 is not pinned by any numerical test. This is the single most load-bearing assumption in the fitting stack: if the convention is wrong, every fitted parameter uncertainty (and the chi^2-based model comparison) is wrong.

Suggested fix

Add a test that fits a straight line (or single-layer reflectivity) with known heteroscedastic errors and asserts both the parameter values and the parameter covariance against the analytic WLS solution. If the convention turns out mismatched, fix _prepare_fit_arrays accordingly.

Related observations in the same area (can be folded in or split):

  • mcmc_sample (fitting.py:358-437) silently applies the Mighell transform (default hybrid) to zero-variance points feeding the Bayesian likelihood — Mighell is a chi-square bias correction, not a likelihood; at minimum warn (the least-squares path does).
  • DataSet1D.ye defaults to np.zeros_like(x) (data/data_store.py:108), i.e. zero variance, which the fitter then masks/transforms wholesale; "no uncertainties supplied" deserves a loud error.

Found during deep code review (DEEP_ANALYSIS.md §4.10).

Metadata

Metadata

Assignees

No one assigned

    Labels

    [priority] highShould be prioritized soon[scope] bugBug report or fix (major.minor.PATCH)
    No fields configured for issues without a type.

    Projects

    Status
    Bugs

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions