Excel Solver: Nonlinear Least Squares Equivalent in C #

I am working on a forms application that reports based on observable data. Before developing the application, Excel spreadsheets were used on the excel solver side to perform non-linear least squares regression. I left university a long time ago, and in the end I can write it, I doubt that the client wants to wait a few months until I come up with a suitable replacement for excel sovler.

So my question is: Is there a C # equivalent for the non-linear least squares reversal function in Excel? I observed data and some initial assumptions based on observed data, so input is not a problem. Hell, even the name of the equation used would be a good starting point.

I looked at alglib, but I'm not sure which one would be appropriate.

Thanks in advance.

+4
source share
3 answers

So, I ended up biting a bullet and going with alglib. The lsfit package eventually did what I wanted, although it took me pretty darn, as it is not readable by a non-mathematician.

I will keep it here so that anyone who has the same problem can find it.

+1
source

You can do this using the Iridium MathDotNet library.

Here is an example of a C # class:

using MathNet.Numerics.LinearAlgebra; namespace StackOverflow.Examples { public class PolynomialRegression { readonly Vector _xData; readonly Vector _yData; readonly Vector _coef; readonly int _order; public PolynomialRegression(Vector xData, Vector yData, int order) { if (xData.Length != yData.Length) { throw new IndexOutOfRangeException(); } _xData = xData; _yData = yData; _order = order; var n = xData.Length; var a = new Matrix(n, order + 1); for (var i = 0; i < n; i++) a.SetRowVector(VandermondeRow(xData[i]), i); // Least Squares |y=A(x)*c| ... tr(A)*y = tr(A)*A*c ... inv(tr(A)*A)*tr(A)*y = c // http://en.wikipedia.org/wiki/Total_least_squares var at = Matrix.Transpose(a); var y2 = new Matrix(yData, n); _coef = (at * a).Solve(at * y2).GetColumnVector(0); } Vector VandermondeRow(double x) { var row = new double[_order + 1]; for (var i = 0; i <= _order; i++) row[i] = Math.Pow(x, i); return new Vector(row); } public double Fit(double x) { return Vector.ScalarProduct(VandermondeRow(x), _coef); } public int Order { get { return _order; } } public Vector Coefficients { get { return _coef; } } public Vector XData { get { return _xData; } } public Vector YData { get { return _yData; } } } } 

And here is a usage example:

  var xVector = new Vector(new double[] { 1, 2, 3, 4, 5 }); var yVector = new Vector(new double[] { 10, 20, 30, 40, 50 }); var order = 2; _poly = new PolynomialRegression(xVector, yVector, order); 
+4
source

One option is to simply use Excel from your C # program. In this way, the calculation is done using Excel, and your program can do everything Excel can do. See How to Automate Microsoft Excel with Microsoft Visual C # .NET

Another option is to use one of the many math libraries available, as Michael spoke about. Many libraries are available. Of course, you will want to verify that the answers you get are Excel; -)

+3
source

Source: https://habr.com/ru/post/1387444/


All Articles