Chronology Current Month Current Thread Current Date
[Year List] [Month List (current year)] [Date Index] [Thread Index] [Thread Prev] [Thread Next] [Date Prev] [Date Next]

Re: [Phys-L] curve fitting



On 3/6/25 09:13, stefan jeglinski wrote in part:

2. The most common linear least squares derivation assumes that the variance is in the vert axis with no variance in the horz.

Here's a slightly more constructive version of my previous answer.
You can deal with that issue by using *total least squares*
(instead of "ordinary least squares").

That's still a maximum-likelihood i.e. maximum_a_priori method,
and as such it is still conceptually unsound, but at least it's
not stupid about the uncertainties on the x-values.

Consider the following example: interchanging x <-> y causes
the x-intercept to turn into the y-intercept, and causes
the slope to turn into 1/slope ... which is the answer that
any non-stupid 15-year-old with a transparent ruler would get.

x =

   1
   2
   3

y =

   3.0000
   4.3000
   5.0000

checkit(x,y)
xint = -2.1702
yint = 2.1371
slope = 0.9848
slpinv = 1.0155

checkit(y,x)
xint = 2.1371
yint = -2.1702
slope = 1.0155
slpinv = 0.9848

////////////////////

Wikipedia has a derivation of the math, and even a
runnable function:
  https://en.wikipedia.org/wiki/Total_least_squares

My code, including the test harness, is here:
  https://av8n.com/physics/total-least-squares-demo.octave

If you download the file, you should able to just say
  octave total-least-squares-demo.octave
and get the results quoted above.

//////////////////

Gnu Octave is similar to other scientific programming systems,
so you could probably translate the code to Matlab or to c++
(using armadillo or eigen3) ... more or less verbatim.