Question

Reffering to the question: Multiple Regression with math.net

@christoph-ruegg Can you provide me an example of resolving regression using Fit.LinearMultiDim.

var xdata = new DenseMatrix(
            new double[,]{{1, 36, 66, 45, 32},
                        {1, 37, 68, 12, 2},
                        {1, 47, 64, 78, 34},
                        {1, 32, 53, 56, 32},
                        {1, 1, 101, 24, 90}});

            var ydata = new double[] { 15, 20, 25, 55, 95 };

            var y = new DenseVector(ydata);

            var p = xdata.QR().Solve(y); // Fit ?

Is there any simpler method to achieve this?

Was it helpful?

Solution

I'm not sure how this relates to the posted sample code but I can certainly show an example using Fit.LinearMultiDim. According to the inline docs this routine fits by least squares a set of multi-dimensional points of the form (x=[x1,x2,..,xk], y), organized in two arrays of the same length (one containing the x-value arrays, one the y-values), to a linear combination of arbitrary functions.

Let's say we went outdoors to N places and measured the altitude, resulting in N (x,y,z) tuples. Now we want to approximate the landscape by a simple parametric model. By visual inspection we figured that there are two plateaus that could be approximated by tanh and we choose the following linear model:

z -> p0 + p1*tanh(x) + p2*tanh(y) + p3*x + p4*x*y

...where we would like to find the best fitting p0-p4. We need at least as many points as we have linear parameters (5 in this example), but ideally have much more.

Since we map (x,y) to (z) we need to organize the tuples in two arrays:

double[][] xy = new[] { new[]{x1,y1}, new[]{x2,y2}, new[]{x3,y3}, ...  };
double[] z = new[] { z1, z2, z3, ... };

Then we can call Fit.LinearMultiDim with our model, which will return an array with the best fitting 5 parameters p0-p4:

Fit.LinearMultiDim(xy, z,
   d => 1.0,              // p0*1.0
   d => Math.Tanh(d[0]),  // p1*tanh(x)
   d => Math.Tanh(d[1]),  // p2*tanh(y)
   d => d[0],             // p3*x
   d => d[0]*d[1]);       // p4*x*y
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top