Linear regression with constrained intercept


(Dpybus) #1

I’m performing linear regression on a physiological data set using C#.

The relevant code is:

Tuple<double, double> r = Fit.Line(XValues, YValues); - where XValues and YValues are double[].

I have good (physiological) reasons for believing that the intercept on the Y axis should be zero.

Can anyone advise me how to apply the constraint?


(Peter Vanderwaart) #2

I don’t see a way to do this directly in Fit (though there may be one). However, it’s a dead easy computation.

You want an equation y = ax for a value of a that minimizes

sum( (ax-y)^2) where the sum is taken over your values x[i], y[i] for i = 1,2,3
My calculus (check it!) gives the answer

a = sum(x[i]*y[i]) / sum (x[i]^2)


(Christoph Rüegg) #3

Thanks for the reply! I’ve just added Fit.LineThroughOrigin, which does exactly that. We may also want to add a more generic Fit.LineThroughPoint in the future.


(Dpybus) #4

I’ve just updated to version 3.20.2.0, but can’t find it in ‘Fit’ there. Do I have to install version 4.x ?


(Christoph Rüegg) #5

Ah, it is not released yet, so it will be part of the next release (likely 4.0.0-beta7, or 4.0.0 if a finally do the release). I don’t plan to backport new functionality like this to v3 (only bug fixes).


(Dpybus) #6

Thank you very much. I eagerly await!!


(Dpybus) #7

Thank you very much. Fit.LineThroughOrigin (in 4.07 beta) does exactly what I want.