The procedure is more or less the same as in 2 dimensional spaces. Only the setting up of the input data differs. The input data consisting of a feature vector X = (x1, x2, x3,…,xn) and a result y must be set up into an equation using a dedicated function of all input features for each factor c. So instead of
In my sample implementation I use a second degree polynomial for both of 2 input features. So my equation looks like:
With this equation I have to set up the input matrix equation
My sample data consists of 99 data sets. So I get a 99x5 matrix for C (the x and x2 values) and a solution vector with 99 entries (the y values).
Similar to the procedure in the 2 dimensional algorithm, this matrix equation I multiplied by the transposed matrix of C like:
To have a clear algorithm, I use a separate matrix to hold the f(x) values. The data itself I keep in a class “data” that contains the collection “values” of double arrays. So the first step is to fill the matrix by the f(x) values like:
fOfX = new double[samples, features];
double[] tempD;
for (i = 0; i < samples; i++)
{
tempD = data.values.ElementAt(i);
fOfX[i, 0] = tempD[0];
fOfX[i, 1] = tempD[0] * tempD[0];
fOfX[i, 2] = tempD[1];
fOfX[i, 3] = tempD[1] * tempD[1];
fOfX[i, 4] = 1.0;
}
double[] tempD;
for (i = 0; i < samples; i++)
{
tempD = data.values.ElementAt(i);
fOfX[i, 0] = tempD[0];
fOfX[i, 1] = tempD[0] * tempD[0];
fOfX[i, 2] = tempD[1];
fOfX[i, 3] = tempD[1] * tempD[1];
fOfX[i, 4] = 1.0;
}
The main matrix is filled like:
for (i = 0; i < features; i++)
{
for (j = 0; j < features; j++)
{
for (k = 0; k < samples; k++)
m[j, i] = m[j, i] + fOfX[k, i] * fOfX[k, j];
}
for (k = 0; k < samples; k++)
y[i] = y[i] + data.values.ElementAt(k)[2] * fOfX[k, i];
}
{
for (j = 0; j < features; j++)
{
for (k = 0; k < samples; k++)
m[j, i] = m[j, i] + fOfX[k, i] * fOfX[k, j];
}
for (k = 0; k < samples; k++)
y[i] = y[i] + data.values.ElementAt(k)[2] * fOfX[k, i];
}
TGivens givens = new TGivens(features, m);
givens.RunGivens();
To generate the data points. Additionally I add a random value between -0.05 and +0.05
The data is stored in the data file cata.csv which is included in the project.
The goal is to approximate these data points and to compute the minimum of this function.
With my sample data I get the result
0.001805%
To find the minimum of the approximation the function can be written as:
And the minimum is there where x – x0 = 0 and y – y0 = 0.
Everything a bit rearranged:
and we just computed
A parameter comparison shows that
and
In numbers:
X0 = 3.0012
Y0 = 2.0009
And these values inserted in the above equation for z2 gives
Z0 = 8.9982
which this is the minimum value of the approximated function.
Compared with the Gradient descend. using the method of the least squares is far quicker as it computes the result immediately without having to iterate and it is even a bit more accurate.
The method of the least squares has been developed by Carl Friedrich Gauss at the age of 18. That was in 1795, without a computer, without social media. Not even with electric light…unbelievable