So you see, we are almost at the end of our notebook here, because the only thing left to do is we
have to loop over the error of its gradient function over and over again and then update our values
to do the actual fitting of our model function.
So let's do it.
I will define here the number of iterations to be maybe 1000.
I think we need quite a few of those several thousands, but let's start with one thousand.
And then we must define some update rate and to be honest, looking at these arrows here.
So I know already that these are the correct set of parameters.
So even if it's correct, the gradient will never be zero, which is due to the random numbers that
we have added.
So the fit is never really perfect, so we will always have some error on some gradient.
So these will be the smallest gradients that we will encounter.
So if these are the smallest gradients and such values shouldn't really affect the A0 at all.
So we need a value of age that is much smaller than the reciprocal of these values, so much smaller
than this one.
So I don't know.
Maybe I will add just here to more zero, so we will have to test and see if this works or not.
And then we will start with some values for our eight.
And as I said previously, we will of course not use a zero because, yeah, that would be stupid,
because then we would have to perfect fit right away.
We have to start from some random values.
We could, of course, guess something just first of all, fit this function by I and then improve the
fit by using this algorithm.
But here I want to do them in a very brute force way and just say, OK, I used two times and p dot
random dot around four minus one.
So this means a is now a vector.
It has four components for the four coefficients, and all the values are in the range of minus one
and one.
And they are randomly selected.
For example, we can check this are now the starting points for our eight coefficients.
And when I run this once again, of course, then the starting parameters are different.
All right.
So now the only thing that we have to do is you have to write a loop for I in range iterations.
Now, the only thing that we have to do is we have to update the values for eight eight is the old eight.
And then I told you that we will go along the opposite direction of the gradient and we will multiply
by the step with.
And then we will just update this with this error.
And now, of course, we have to use our parameters and not the A0.
And I think this is already it.
So when we run this, then we should end up with some parameters.
And so we could, for example, say here at the starting point, print A and then at the end print a
again.
So you see the A values have been updated.
All right.
So we do not really know if these are good values yet, but we could check this by comparing a with
a zero because we know that these zeros are the good values.
And it doesn't look good yet.
So probably we have to increase the numbers of iterations.
Let's go with 10000.
So you see, it takes a moment now,
and this time at least the last two coefficients fit quite well.
And maybe this is a bit surprising for you now.
But the last two coefficients are the most important ones because they determine the highest order polynomial
terms.
So this would be the coefficient for X to the power of three.
This one four x squared.
And of course, these will determine the shape of the function, especially for large X values.
So it's really, really good that we see that these values are quite close to the actual values.
So maybe just go to 100K.
So you see here it runs for a few seconds now.
And maybe if your PC is slower than mine, this could take quite a few seconds.
Let's see what's happening here.
Actually, I could leave this here because we print the values anyway, so this time, let's see.
Um.
Yeah, I didn't really get much better, but I would say, let's just look at the plots because it's
hard to say just by comparing the parameters.
So even though these are the parameters that we have used for creating the data points, they do not
necessarily have to be the perfect parameters for the fit because we have added this random noise.
So I would say, let's just go ahead and plot the function.
So let me copy this one.
And what we will do is we will use here a plot, so we will use people, we will plot the actual line
and we will plot through, of course, the function with our fitted coefficients.
And then in the background, we will use a scatter point as scatterplot and sorry, using the data points.
Will use data zero.
Common data one.
And you see, it looks really good.
So even though, as I said, the parameters do not agree perfectly.
If it looks good and this could be, for example, for the following reason here this is the zero with
all the terms, and this is just a shift in my direction.
So actually, our fit is shifted by 18 units to the top compared to the function that we have used to
create the data.
But the linear term is also quite different.
So maybe this compensates the whole thing.
So what we could do is we could also add here the other fit or not the other fit, but the function
that we have used to create the data and.
Now, I'm a bit surprised.
I just stopped the recording because I was surprised that the orange function that we have just plotted
doesn't really reproduce our data points, so something was off here.
I went back to the very start of this section.
So where we have started with the interpolation and generated the data, I have seen that here.
These are actually the coefficients and the first coefficient is actually 15.
And for some reason, I redefined here, let me search for it.
Here in three point 2.3.1, a redefined a zero to be minus two for some reason, so that was, of course,
wrong.
So let's updated to 15, and then I was, let's say let's rerun the whole notebook, our restyled run
all.
And now, of course, it takes a few seconds, few seconds.
Actually, it's very fast, except for the very last thing, because here we will have to loop 100000
times.
This takes a while.
And then we can compare the plots and we can compare to coefficients this time in an accurate manner.
So sorry for having made this mistake.
I hope this wasn't too confusing, but this is sometimes how it goes.
You make mistakes, and the good thing or good methods to finding these mistakes is always plotting.
I think because I have seen the orange function was way too low, so I noticed there had to be something
wrong here and now everything looks good.
You see, the orange function was the one that we have used for creating this data, and then we have
added the noise on top of it.
And the black function is the one that we have received from the fitting.
So it could really be that due to the random data that we have added that the black function is actually
better than the orange one, and we could even verify this by calculating the error.
So this time we can use our Arrow Fit function so that it isn't useless.
We can just go ahead and calculate here.
Print error fits polynomial model, a zero data and then a data.
So, first of all, will be our fit.
Second run will be a zero.
The data are the function we have used initially.
And do you really see our fit data has a lower error.
So really, at least for the error that we have to find our fit is better than the function that we
have started with.
So this is maybe a bit counterintuitive and a bit unexpected.
So for me, it's also unexpected, but it makes sense because due to the randomly added points here,
it can happen that the ideal function for fitting this data is different than the function that we have
started out with.
And so now if we compare two coefficients, they are a bit different.
But you see, it makes sense because our fit is actually the better fit to the data.
I think this is a very good closing remark and really the ultimate proof that our fit works very, very
well and that this method is very useful.
And so comparing this result with these blind methods, we have now really fitted our data with a model
function.
And this is really what you want to do in many physical problems where you have some mechanism that
follows some physical law and then you measure some experimental data and then you do not always want
to have the perfect fit, which would be a splain function.
It would be the mathematically ideal fit.
But do you want to have the physically optimal fit so you want to fit it to the physical model?
And therefore, for example, you disregard these thermal fluctuation effects.
So this is at least for many scientific problems, the better way to go to really think first about
it, what would be our model function and then fit this model of function to your data?
So in our case, we have used here a polynomial, and that's for our model function.
We have made it really general, so you could now go ahead and test with a higher order polynomial.
But please be warned, this can often leads to problems because the higher order you go into polynomial,
the higher these terms will get and the higher the error will get, and you will soon run to some memory
issues.
But in general, it should work to you to consider also higher order polynomials.
And of course, also you could use a completely different model function.
You could use an exponential function sine function, anything that you want, basically.
And this will be the only thing that you have to update.
Of course, you have to be careful here a bit with the parameters of the coefficient so that you still
call them and define them as an array.
But if you do this, then you have to, of course, also take care a bit of the error function.
But the general idea in the finding the error function and then the finding the error of its gradient
function and then looping over and over again so that we can update the value of a this will all remain
the same no matter which function you use for the fit.