[英]Linear regression with constraints with Math.NET
I'm performing simple linear regression with Math.NET. 我正在使用Math.NET进行简单的线性回归。
I provided a common code sample below. 我在下面提供了一个通用代码示例。 Alternative to this example one can use the Fit class for simple linear regression.
作为此示例的替代,可以使用Fit类进行简单的线性回归。
What I additionally want is to specify additional constraints like a fixed y-intercept or force the fit to run throug a fixed point, eg (2, 2). 我还想要的是指定其他约束,例如固定的y轴截距或强制拟合通过固定点运行,例如(2,2)。 How to achieve this in Math.NET?
如何在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];
You can modify your data set to reflect the constraint , and then use the standard math.Net linear regression 您可以修改数据集以反映约束,然后使用标准的math.Net线性回归
if (x0,y0) is the point through which the regression line must pass, fit the model y−y0=β(x−x0)+ε, ie, a linear regression with "no intercept" on a translated data set.
if(x0,y0)是回归线必须通过的点,拟合模型y-y0 =β(x-x0)+ε,即在翻译数据集上具有“无截距”的线性回归。
see here : https://stats.stackexchange.com/questions/12484/constrained-linear-regression-through-a-specified-point 请看这里: 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 在这里: http : //en.wikipedia.org/wiki/Linear_least_squares_(mathematics)#Constrained_linear_least_squares
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. 首先,你想强制通过原点进行回归,你可以使用LineThroughOrigin或者替代LineThroughOriginFunc,如果你想要的是函数本身。
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
拦截= 2
slope = 1
斜率= 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 然后你就知道你的方程式是y = x + 2.如果你想让相同的函数在3(y = x + 3)中穿过y轴,你只需要在截距中加1,这样就可以了
intercept = 3
拦截= 3
slope = 1
斜率= 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.