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 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:
- Web: https://christophercrowley.com/resource-curve-fitting.html
- PDF:
Curve_fitting.pdfin this repository
implementation_section.tex is the LaTeX source for the section of those notes
documenting this code.
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.
MATLAB — Symbolic Math Toolbox.
Python — numpy 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
| 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.