# Polyfit with Timeseries

**URL:** <https://scilab.discourse.group/t/polyfit-with-timeseries/917>\
**Category:** Data handling\
**Created:** [April 24, 2025, 8:42am UTC](https://scilab.discourse.group/t/polyfit-with-timeseries/917 "2025-04-24T08:42:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pascal](https://yyz2.discourse-cdn.com/free1/user_avatar/scilab.discourse.group/pascal/32/40_2.png) [@pascal](https://scilab.discourse.group/u/pascal)\
**Post date:** [April 24, 2025, 8:42am UTC](https://scilab.discourse.group/t/polyfit-with-timeseries/917/1 "2025-04-24T08:42:07Z")

</div>

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

 ![image](https://global.discourse-cdn.com/free1/uploads/utc/original/1X/caf538a45a3db6fb47c89a22f5427993ea97b728.png)  
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](https://scilab.discourse.group/uploads/short-url/dLh5bgyXJyxgtqCMhlGYmQbf7vJ.sci) (7.1 KB)

---

<div class="post-metadata">

**Author:** ![pascal](https://yyz2.discourse-cdn.com/free1/user_avatar/scilab.discourse.group/pascal/32/40_2.png) [@pascal](https://scilab.discourse.group/u/pascal)\
**Post date:** [April 24, 2025, 8:52am UTC](https://scilab.discourse.group/t/polyfit-with-timeseries/917/2 "2025-04-24T08:52:23Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![mottelet](https://yyz2.discourse-cdn.com/free1/user_avatar/scilab.discourse.group/mottelet/32/13_2.png) [@mottelet](https://scilab.discourse.group/u/mottelet)\
**Post date:** [April 24, 2025, 9:10am UTC](https://scilab.discourse.group/t/polyfit-with-timeseries/917/3 "2025-04-24T09:10:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pascal](https://yyz2.discourse-cdn.com/free1/user_avatar/scilab.discourse.group/pascal/32/40_2.png) [@pascal](https://scilab.discourse.group/u/pascal)\
**Post date:** [April 24, 2025, 9:49am UTC](https://scilab.discourse.group/t/polyfit-with-timeseries/917/4 "2025-04-24T09:49:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mottelet](https://yyz2.discourse-cdn.com/free1/user_avatar/scilab.discourse.group/mottelet/32/13_2.png) [@mottelet](https://scilab.discourse.group/u/mottelet)\
**Post date:** [April 24, 2025, 10:00am UTC](https://scilab.discourse.group/t/polyfit-with-timeseries/917/5 "2025-04-24T10:00:21Z")

</div>

e.g.

```auto
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.

---

<div class="post-metadata">

**Author:** ![pascal](https://yyz2.discourse-cdn.com/free1/user_avatar/scilab.discourse.group/pascal/32/40_2.png) [@pascal](https://scilab.discourse.group/u/pascal)\
**Post date:** [April 24, 2025, 12:05pm UTC](https://scilab.discourse.group/t/polyfit-with-timeseries/917/6 "2025-04-24T12:05:20Z")

</div>

yes, thank you. the result now is much better 🙂

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

M.f.G. Pascal

---

<div class="post-metadata">

**Author:** ![mottelet](https://yyz2.discourse-cdn.com/free1/user_avatar/scilab.discourse.group/mottelet/32/13_2.png) [@mottelet](https://scilab.discourse.group/u/mottelet)\
**Post date:** [April 24, 2025, 2:45pm UTC](https://scilab.discourse.group/t/polyfit-with-timeseries/917/7 "2025-04-24T14:45:43Z")

</div>

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.
