I am having some troubles with removing a stochastic trend from a time series.

I am carrying out a study on the US debt to GDP ratio. I noted that there's a smooth stochastic trend in the series.

My dataset is made up of the yearly ratio from 1988 to 2017 (31 points).

How can I remove the trend?

  • 3
    $\begingroup$ Please can you give us the data so that we have some idea of your problem? $\endgroup$
    – Hugh
    Feb 15, 2019 at 22:55
  • $\begingroup$ how do you know the trend is stochastic? $\endgroup$
    – user42582
    Feb 15, 2019 at 23:20
  • 1
    $\begingroup$ Perhaps fit a line to the data and then subtract the values out. $\endgroup$
    – bill s
    Feb 15, 2019 at 23:47
  • $\begingroup$ For removing a stochastic trend, you can use differentiation $\endgroup$ Feb 16, 2019 at 12:54

1 Answer 1


I am having some troubles with removing a stochastic trend from a time series on Mathematica.

It is not clear to me what exactly "removing a stochastic trend" means.

  • If we assume "difference-stationary" time series (and making the time-series stationary for further analysis) then time-series differencing can be easily applied (in Mathematica.)

  • If we assume "trend-stationary" time series then using curve fitting we find both the trend and the trend-data differences.

I strongly suspect OP wants the former.

Getting the data

I downloaded the data described by OP from https://fred.stlouisfed.org/series/GFDGDPA188S . (That web page is titled "Gross Federal Debt as Percent of Gross Domestic Product (GFDGDPA188S)".)

ts = Rest@Import["~/Downloads/GFDGDPA188S-from-1988.csv"];

ts[[All, 1]] = 
  AbsoluteTime /@ 
   Map[DateList[{#, {"Year", "-", "Month", "-", "Day"}}] &, ts[[All, 1]]];

It is instructive to use Unit Root Test result for this time series:

(* 0.861226 *)

(It is very likely that ts is a unit root time series.)

Assuming "difference-stationary" time series

First we can "de-trend" using time-series differences:


enter image description here

Further, 2nd order differences over the logarithm values might be better.

DateListPlot[Differences[TimeSeriesMap[Log, ts], 2]]

enter image description here

It looks better especially if the full available data is taken:

enter image description here

Here are the results with Unit Root Test (we de-trended it since the p-value estimates are really small):

UnitRootTest[Values[Differences[TimeSeriesMap[Log, ts], 2]]] 
(* 0.0000124983 *)

UnitRootTest[Values[Differences[ts, 2]]]
(* 2.03878*10^-6 *)

Assuming "trend stationary" time-series

Another type of "de-trending" is to fit a curve and take the differences of the data points with that curve. Below I use the QRMon package.


qrObj =
   QRMonQuantileRegression[2, 0.5, InterpolationOrder -> 3]⟹
   QRMonDateListPlot[PlotTheme -> "Detailed", ImageSize -> Medium]⟹
   QRMonErrorPlots["RelativeErrors" -> False, PlotTheme -> "Detailed", "DateListPlot" -> True, ImageSize -> Medium]⟹
   QRMonErrors["RelativeErrors" -> False];

enter image description here

Here is the data "de-trended":


(* {{2776982400, 6.16547}, {2808604800, -2.84217*10^-14},...} *)

Here is the "trend" function:


enter image description here

Obviously one can experiment with different interpolation order, number of knots, or function basis. (Here B-splines are used.)

Here are the results with Unit Root Test (we de-trended it well enough):

UnitRootTest[errs[[All, 2]]]
(* 0.0139582 *)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.