# Extrapolation of Time Series in Python

I have the following plot in time series and would like to extrapolate it to derive a value in X[n+20], for instance. I tried poly fitting it but extrapolation does come out correct that way. Any proposed approaches?

The data comes from blood glucose after a meal overtime, it usually starts at some value, say 90mg/dl at mealtime, peaks to 150mg/dl, then decreases down to some value which can be less than the intial 90mg/dl. Since we have several post-meal signals which all start at different values, we aligned them by substracting the offset so that we can derive a general curve fit for them all, of course it will only be an approximation and that is fine. x is time.

\begin{array}{cc} 0 & 0. \\ 5 & 4.3 \\ 10 & 12.7 \\ 15 & 22.1 \\ 20 & 32.6 \\ 25 & 41. \\ 30 & 47.8 \\ 35 & 50. \\ 40 & 51.9 \\ 45 & 52. \\ 50 & 51.2 \\ 55 & 45.4 \\ 60 & 43.4 \\ 65 & 39.9 \\ 70 & 40.2 \\ 75 & 35.4 \\ 80 & 30.2 \\ 85 & 27.9 \\ 90 & 25. \\ 95 & 19.6 \\ 100 & 15.8 \\ 105 & 13.9 \\ 110 & 9.1 \\ 115 & 4. \\ 120 & 1.6 \\ 125 & -1.4 \\ 130 & -4. \\ 135 & -5.6 \\ 140 & -6.8 \\ 145 & -7.7 \\ 150 & -8.6 \\ 155 & -10.4 \\ 160 & -10.9 \\ 165 & -10.9 \\ 170 & -10.2 \\ 175 & -10.2 \\ 180 & -10.3 \\ 185 & -10.7 \\ \end{array}

What you are doing is . Judging from what you write in the comments, forecasting this series is not truly a statistical problem, but a biological one. Best to model the dynamics giving rise to this curve and then extrapolate these out.

From a purely statistical/forecasting standpoint, the best you can probably do is the so-called naive or random-walk forecast: project the last observation out in a flat line. If you want to, use the smoothed value.

(And even if you do use a nice complex model, it makes sense to compare its predictions out-of-sample to a simple benchmark like the naive forecast. You find surprisingly often that the simple benchmark outperforms a more complex approach.)

• Yup, a biological problem sometimes modeled as a feedback loop, which is why I did what I did above.
– Carl
Mar 30, 2021 at 7:21

Old Ans. The Y data takes negative values. You could try fitting $Y+11.0$ or more generally $Y+Y_{min}$ with a gamma distribution which may also need an offset. Without more information, it is guesswork. Either show tabular data or forget getting a good answer, not clear enough.

After comments, edits and more information. OK, you can try a less than critically damped sine wave.

Here, for example is $$A \exp (-\lambda t) \sin (\omega t+\theta )$$ where $\{A\to -98.0396,\lambda \to 0.0126426,\omega \to 0.0259994,\theta \to 3.08326\}$

This is the result of assuming that glucose is a less than critically damped negative feedback network moderated by insulin. Caution, this is a quick and dirty answer and attention should be paid to residual and error types, whether or not the data is proportional and myriad other factors.

For example, you might be better served by getting rid of the {0,0} first point, and let the algorithm decide at what time, $t$, the first root of the equation is zero, as this may not be when you think it occurs. Why? If you think about it for a moment, the zero concentration first root is implied by each and every data entry already, so that entering {0,0} is redundant, arbitrary and misleading. Moreover, the feedback loop may not occur in linear time, but in time to some power. Doing those things we obtain

$$A \exp \left(-\lambda x^B\right) \sin \left(\omega x^B+\theta \right)\;,$$

where $\{A\to -104.526,B\to 0.936079,\lambda \to 0.0178241,\omega \to -0.0362561,\theta \to 0.14761\}$

Where the start time is 4.07133 min, and the correlation between model and data is $r=0.99783$. If you want to find any particular predicted concentration, just input that time in the formula.

Keep in mind that this is just an ordinary least squares model, that it is quick and dirty, that I have not examined the original data and its error structure, and that I prefer biological models where $r^2\geq0.999+$ unless there is some outstanding reason (e.g., demonstrated to be noise) why it cannot be that precise. Many of my colleagues are happy when they get $r\geq0.8$, however, that is all too often due to a lack of proper modelling, and not because of noise. In this data, the precision of the concentration data is sometimes only one or two decimal places, and a subtraction (which doubles the error) has been performed. Without better data, it would be hard to exceed the $r^2=0.995665$ found. That is, in this case, the last 1/2% explained fraction would be harder to obtain. Sorry, I use Mathematica, not Python. If you want the Mathematica code, let me know.

• Ymin is not set cuz it is gradually decreasing overtime, slowly but still, so there is not a set offset. For instance, 11 that you suggested is ok up to that point above, but few minutes later it'll be some other value, say 12 or 13. Since you asked for tabular data, an example of one signal data points, Y = [0.0, 4.3, 12.7, 22.1, 32.6, 41.0, 47.8, 50.0, 51.9, 52.0, 51.2, 45.4, 43.4, 39.9, 40.2, 35.4, 30.2, 27.9, 25.0, 19.6, 15.8, 13.9, 9.1, 4.0, 1.6, -1.4, -4.0, -5.6, -6.8, -7.7, -8.6, -10.4, -10.9, -10.9, -10.2, -10.2, -10.3, -10.7], while X = [0, 5, 10, .....,], increments of 5mins. Oct 15, 2017 at 2:21
• Need more information. Why does overtime start off at zero, rise to a peak then decrease below zero? How can overtime be less than zero? Really, without rules of the game, there is no predicting anything, and certainly one cannot use polynomials for prediction, it would be very rare that they forecast anything.
– Carl
Oct 15, 2017 at 3:24
• Good point. Ok, I guess the only way to answer your question is to know a little about the data. So the data comes from blood glucose after a meal overtime, it usually starts at some value, say 90mg/dl at mealtime, peaks to 150mg/dl, then decreases down to some value which can be less than the intial 90mg/dl. Since we have several post-meal signals which all start at different values, we aligned them by substracting the offset so that we can derive a general curve fit for them all, of course it will only be an approximation and that is fine. Oct 15, 2017 at 3:41
• In this type of modeling problems, many variables are involved.. and we usually use complex models to extract best model, using ODE for instance. Here, we are attempting to simplify the model to X and Y and attempt to model that nonlinearity, mainly because the other variables are not provided. Hope that clarifies a bit the problem definition. Oct 15, 2017 at 3:43