Polyfit with Timeseries

Hi Scilab TEAM
i have a problem with making polyfit on timeseries


for making a polyfit i have to make the dates into a number (unix timebase) then i have the polyfit values that are extrvagant high, so only linear regression is valid.
How can i maket it more suitable to the given datas?
SNB.sci (7.1 KB)

Actally i thought if i offset first to day one make polyfit and then add the offset.
But is it then correct? or should it be on the number of points?

Hello,

When dealing with polynomial approximation, even more when high order is used (6 in your case, which is BTW insane to me), you should normalize the independent variable (let us call it t) as much as possible. For example if the values of t in your dataset (t_1,\dots,t_n) are in [a,b] (in your case you can take a=t_1, b=t_n) you should use the change of variable

s=\frac{t-a}{b-a},

then try to fit the (s_i,y_i)_{i=1,\dots,n} dataset instead of (t_i,y_i)_{i=1,\dots,n}. This is mathematically equivalent, because if you fit a polynomial q(s) vs. (s_i,y_i)_{i=1,\dots,n} then the polynomial p(t) fitting (t_i,y_i)_{i=1,\dots,n} is

p(t) = q\left(\frac{t-a}{b-a}\right),

however, this is not numerically equivalent, as you already experienced it. So you should always normalize the independent variable, at least when its magnitude is large.

S.

Thank you, for this brief advise :slight_smile:
Could it be implemented in the function?

e.g.

t = Kerninflation(Inflation(i)).date.date;
s = (t-t(1))/(t($)-t(1));
Kerninflation(Inflation(i)).poly=polyfit(s,Kerninflation(Inflation(i)).value,6);

and when using polyval for a given date use the same transformation (evaluate the polynomial on the normalized date).

S.

yes, thank you. the result now is much better :slight_smile:

could this be implemented in polyfit and polyvar in the near future?

M.f.G. Pascal

No, in the case where the scaling is necessary, forming and returning p(t) computed from q(s) would destroy the benefit of initial scaling. Scaling the data is the user’s task.

S.