5

I'm performing simple linear regression with Math.NET.

I provided a common code sample below. Alternative to this example one can use the Fit class for simple linear regression.

What I additionally want is to specify additional constraints like a fixed y-intercept or force the fit to run throug a fixed point, e.g. (2, 2). How to achieve this in Math.NET?

var xdata = new double[] { 10, 20, 30 };
var ydata = new double[] { 15, 20, 25 };

var X = DenseMatrix.CreateFromColumns(new[] {new DenseVector(xdata.Length, 1), new DenseVector(xdata)});
var y = new DenseVector(ydata);

var p = X.QR().Solve(y);
var a = p[0];
var b = p[1];
Stefan Teitge
  • 716
  • 3
  • 10
  • 21
  • You would have to change the function class resp. mathematical model that you are fitting, changing the number of free parameters. – Lutz Lehmann Jun 16 '14 at 12:10

2 Answers2

1

You can modify your data set to reflect the constraint , and then use the standard math.Net linear regression

if (x0,y0) is the point through which the regression line must pass, fit the model y−y0=β(x−x0)+ε, i.e., a linear regression with "no intercept" on a translated data set.

see here : https://stats.stackexchange.com/questions/12484/constrained-linear-regression-through-a-specified-point

and here : http://en.wikipedia.org/wiki/Linear_least_squares_(mathematics)#Constrained_linear_least_squares

Community
  • 1
  • 1
Shachaf.Gortler
  • 5,655
  • 14
  • 43
  • 71
0

First of all, it you want to force the regression through the origin, you can use LineThroughOrigin or alternativelly LineThroughOriginFunc if what you want is the function itself.

To force the regression to have a desired intercept, I would perform a normal linear regression and get the intercept and slope (knowing these you know everything about your linear function).

With this information, you can compensate the intercept, for example: If you made your regression in which

intercept = 2

slope = 1

Then you know that your equation would be y = x + 2. If you want the same function to cross the y axis in 3 (y = x + 3), you would just need to add 1 to the intercept so that

intercept = 3

slope = 1