Skip to content

Latest commit

 

History

9 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Curve fitting with uncertainty

Weighted least squares (WLS) and total least squares (TLS) regression that carries a full uncertainty budget through to the calibration function — implemented twice, once in MATLAB and once in Python.

Both build the design matrix analytically from a symbolic model string, so you write the model the way you'd write it on paper:

modelStr = 'A + B*log(x) + D*(log(x))^3';   % Steinhart-Hart
params   = ["A", "B", "D"];
result   = LinearRegression(R, 1./T, uR, uOneOverT, modelStr, params);
model_str = 'A + B*log(x) + D*(log(x))^3'   # ^ auto-converted to **
params    = ['A', 'B', 'D']
result    = linear_regression(R, 1.0/T, uR, uOneOverT, model_str, params)

Pass an uncertainty in y only and you get weighted least squares; pass uncertainties in both x and y and you get total least squares, solved by iteratively reweighted least squares.

The write-up

The derivations behind all of this — ordinary and weighted least squares, total least squares, and how to turn a fit into a defensible uncertainty statement — are written up in full:

implementation_section.tex is the LaTeX source for the section of those notes documenting this code.

What you get back

Beyond the coefficients and their covariance, the result carries the pieces you actually report: the per-point fit uncertainty, the reduced chi-squared, and single-number summaries of the calibration uncertainty under two different treatments of excess scatter.

Chi-squared inflation follows an inflate-only rule,

covBeta_inf = max(1, chi2r) * covBeta

so unmodelled scatter can widen the uncertainty bands but never shrink them below what the input uncertainties support.

Requirements

MATLAB — Symbolic Math Toolbox.

Pythonnumpy and sympy; matplotlib only if you want the diagnostic figure (it is imported lazily, so the module works without it).

pip install numpy sympy matplotlib

Files

File What it is
LinearRegression.m MATLAB implementation
LinearRegression_Example.m MATLAB example — thermistor calibration
linear_regression.py Python implementation
linear_regression_example.py Python example — same data
test_linear_regression.py Python test suite
ReadInDataFromCalAndDoFits.m Example of driving the fit from a spreadsheet
BuildDesignMatrix.m, TLS_regression.m, WLS_regression.m Earlier standalone pieces, kept for reference
Curve_fitting.pdf The notes

ReadInDataFromCalAndDoFits.m expects a workbook under ./data/; point it at your own.

About

Weighted and total least-squares curve fitting with a full uncertainty budget — symbolic model strings, MATLAB and Python.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages