Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linear regression with constraints with Math.NET

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];
like image 819
Stefan Teitge Avatar asked Nov 10 '22 06:11

Stefan Teitge


1 Answers

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

like image 86
Shachaf.Gortler Avatar answered Nov 14 '22 22:11

Shachaf.Gortler