So we have just defined the error, a function of the fit.
But to be honest, for our algorithm, we don't need the error function itself.
So this was just a practice to define the error function.
What we need instead is the gradient of the error function.
So basically the partial derivatives with respect to our coefficients.
And the reason is our algorithm will work as follows.
We start from some value a zero, which will, for example, be this array here and it will give us
an incorrect fit.
This, we could find out by calculating the error of its value, and it will be a very large number,
much larger than this one, because this one is actually the value that we got for the correct parameters.
And what will happen then is we will loop and in the loop.
We will update our values, our parameters, our coefficients, and we will update them in a way so
that the error function will be decreased.
So it will get smaller and smaller.
And to do this, there exists several reasonable methods and we will later in this course discuss something
called Monte Carlo algorithm.
This is something we could use here.
We would exploit just randomness to decrease the error.
But what I want to show you here is a very typical method that is used very often.
It's called gradient descent or gradient descent methods.
So the thing is, maybe you know this from mathematics when you have a function that depends on several
arguments say, for example, on X and Y, then you can draw something like height map, like when you
have a map, when you go hiking and you have a map that shows you a mountain.
And the idea on this map is that you can draw at every point.
The so-called gradient and the gradient when you're calculated will always point along the direction
of the largest slope.
So when you have the steepest way.
So this will be the fastest way to climb up the mountain.
So this will be a vector, and the components of this vector will be the partial derivatives.
So I think you have heard of this, but if not, it's not a big deal.
We are not really here for the math.
I just give you the math and we are here for the programming, of course.
So the idea would be to calculate the gradient of the error function.
This is what is written down here.
So this is called the novella operator.
And I wrote Down the gradient consists out of elements that are all these partial derivatives of this
error function.
So we have to calculate the derivative with respect to a key.
So there will be several terms of this are a function here.
So we will derive this one respect to a key where the acts are in the function f and then we have this
vector.
There's Nublar acting on the error function and this will be the steepest increase of the error.
So to decrease it, we have to go along the opposite direction.
And this is the whole idea.
We take out parameters a zero, calculate the gradient, go along the opposite direction and update
our parameters.
And then we do it again.
We calculate once again the gradient and we loop over this process several times, and this should bring
us down to the minimum value for the error itself.
And these will then be our final parameters.
So as I wrote, the gradient consists of several elements, which are these partial derivatives, and
what we have to do is we have to calculate the derivative of this function with respect to A.K.
So as I said, these acres are only in the F.
So first of all, we have to calculate this derivative for using the chain rule, and the outer derivative
would be two times this function here.
And of course, the sun sign.
And then we just have to multiply by the inner derivative.
So we have two times the sum of this difference, and then we have the inner derivative, which would
be minus.
And then the narrative of this one would be the river to f with respect to a K.
And if you remember, I will scroll up here a bit to the definition of our model function.
This was the function, and if we derive this with respect to a K, we only get a single term from the
sum and this would be X to the power of K, of course.
So we have now.
Here, the yeah x to the power of K.
And since we have to some sign over the data points, then it also has the Index II, which corresponds
to the data points.
So basically, we just have to calculate our program this term here, which will be one component of
the gradient.
And then the the individual components just differ by the key where key goes from zero to three in our
case.
So it's always the same.
It just differs here by to the power of zero, one, two and three.
So let's go ahead and do this.
We will just right, define our own fit gradient and we have a function f.
We have four coefficients and we have our data and f is to fit to fit function.
The so this is the same thing as here.
So we use to set exactly the same parameters or the same arguments as in the error function.
So now what we have to do is we have to sum up over some terms so we can, of course, take again the
loop over the four.
So we're using a formal, for example, and then looping over not over the error, but looping over
the the gradient or over the partial derivatives.
But I want what I want to do here instead is I want to show you something else.
I want to show you how you can do this even in a single line of code.
And this is because we have to return here, not a single value, but a vector.
So as I said, this one is just a single component of this gradient.
So our actual return will be an array, so it will be and p dot parade.
And will be some court, and then we write for K in range.
Length of coefficients.
So in our case, this will just be a vector with four components.
But since I want to do it here in a very general way where you could also later on say we use a polynomial
of six order, we have to write it this way.
So now just to make it look a bit nicer, we can, for example, write it like this.
And I think know maybe this and then it should work.
And now here we have the actual code from here.
So we write minus two.
This one, we can also write here, but we can also write it here.
It doesn't matter.
Simply write minus two.
And then we just have to say.
Some over these expressions here and now, it would be very tedious to do this using a loop.
So what I do instead is I will sum up over an array, so I write and prod some over and don't pray that
the array will be some values.
And then the index runs for I in range length.
Peter and once again, remember, if you want to have the number of data points, you have to take the
length of data zero and not length of data.
Otherwise, this will just be two.
And no, it's 21.
And now we have to sum.
And now we just have to write this one.
So I write
data.
One Croma eye, which will be the Y component of the IWF data point mines have off date zero comma I
comma coefficients.
And then we multiply by this one day to zero.
Come on, I took the power off key and I think this should be it.
Let's see what happens if we call this our own.
So once again, problem here.
So our fit gradient polynomial.
Come up a zero.
Come on data, um, polynomial, of course, or function is not called polynomial, it's called polynomial
model.
And you see, it is indeed a function that returns an array with four numbers.
So, yeah, at least in terms of the shape of the solution, it looks good.
So this is here, our solution or our gradient of the error function at this particular point.
And we have just programmed here a vector gradient.
So the Knobloch acting on the error function and every component is given by this expression where we
have to change here, the key value when we go to a different component.
And in the next step, we will use this function and use it over and over again to update the value
of a.