# How can I remove a stochastic trend from a time series

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?

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

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:

UnitRootTest[Values[ts]]
(* 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:

DateListPlot[Differences[TimeSeries[ts]]]


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

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


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

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.

Import["https://raw.githubusercontent.com/antononcube/MathematicaForPrediction/master/MonadicProgramming/MonadicQuantileRegression.m"]

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


Here is the data "de-trended":

(qrObj⟹QRMonTakeValue)[0.5]

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


Here is the "trend" function:

FullSimplify[(qrObj⟹QRMonTakeRegressionFunctions)[0.5][x]]


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 *)