+-
c# – 使用Math.NET约束的线性回归
我正在使用Math.NET进行简单的线性回归.

我在下面提供了一个通用代码示例.作为此示例的替代,可以使用Fit类进行简单的线性回归.

我还想要的是指定额外的约束,例如固定的y轴截距或强制拟合通过固定点运行,例如, (2,2).如何在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];
最佳答案
您可以修改数据集以反映约束,然后使用标准的math.Net线性回归

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.

见这里:https://stats.stackexchange.com/questions/12484/constrained-linear-regression-through-a-specified-point

在这里:http://en.wikipedia.org/wiki/Linear_least_squares_(mathematics)#Constrained_linear_least_squares

点击查看更多相关文章

转载注明原文:c# – 使用Math.NET约束的线性回归 - 乐贴网