Function Repository Resource:

FractionalDTimeSeries

Source Notebook

Calculate the fractional difference of a time series

Contributed by: Frank Salvador Ygnacio Rosas

ResourceFunction["FractionalDTimeSeries"][tseries,d]

applies a fractional difference of order d to the time series tseries.

ResourceFunction["FractionalDTimeSeries"][tseries,d,tol]

uses a tolerance value of tol.

Details

ResourceFunction["FractionalDTimeSeries"][tseries,d,tol] represents a fractional difference operator of order d for the time series tseries, given a tolerance tol, as formulated by Marcos Lopez de Prado (2018).
The order d should be a positive real.
By default, the tolerance is set to 0.0001.

Examples

Basic Examples (2) 

Get a time series of closing prices and visualize it:

In[1]:=
ts = FinancialData["GE", "Close", "Jan. 1, 2010"]
Out[1]=
In[2]:=
DateListPlot[ts]
Out[2]=

Compute the half difference of the time series and visualize it:

In[3]:=
fdseries = ResourceFunction["FractionalDTimeSeries"][ts, 0.5]
Out[3]=
In[4]:=
DateListPlot[fdseries]
Out[4]=

Scope (2) 

Get a time series of closing prices:

In[5]:=
ts = FinancialData["TEVA", "Close", "Jan. 1, 2010"]
Out[5]=
In[6]:=
DateListPlot[ts]
Out[6]=

With order 1, FractionalDTimeSeries gives the usual first difference:

In[7]:=
firstDiff = ResourceFunction["FractionalDTimeSeries"][ts, 1]
Out[7]=
In[8]:=
DateListPlot[firstDiff]
Out[8]=

However, applying the half-difference twice on the original time series yields an equivalent first difference, but with fewer data points and on a slightly different scale:

In[9]:=
doublefdseries = Nest[ResourceFunction["FractionalDTimeSeries"][#, 0.5] &, ts, 2]
Out[9]=
In[10]:=
DateListPlot[doublefdseries]
Out[10]=

Get a time series of closing prices:

In[11]:=
ts = FinancialData["TEVA", "Close", "Jan. 1, 2010"]
Out[11]=

Apply a fractional difference of order 0.3531 to the original time series with the default tolerance:

In[12]:=
fdseries = ResourceFunction["FractionalDTimeSeries"][ts, 0.3531, 0.0001]
Out[12]=
In[13]:=
DateListPlot[fdseries]
Out[13]=

For the same time series, a fractional difference of order 0.3531 and a larger tolerance will generate a time series with more data points:

In[14]:=
fdseries = ResourceFunction["FractionalDTimeSeries"][ts, 0.3531, 0.01]
Out[14]=
In[15]:=
DateListPlot[fdseries]
Out[15]=

In contrast, a fractional difference of the same order 0.3531, but with a smaller tolerance, will generate a time series with fewer data points:

In[16]:=
fdseries = ResourceFunction["FractionalDTimeSeries"][ts, 0.3531, 0.00001]
Out[16]=
In[17]:=
DateListPlot[fdseries]
Out[17]=

Applications (3) 

Simulate an ARIMAProcess with linear trend and apply a fractional difference of order 0.5:

In[18]:=
BlockRandom[SeedRandom[170]; sample = RandomFunction[ARIMAProcess[{-0.1}, 1, {0.2}, 0.1], {1, 1*^4}]];
{ListLinePlot[sample], ListLinePlot[
  ResourceFunction["FractionalDTimeSeries"][sample, 0.5]["Values"]]}
Out[19]=

Simulate an FractionalBrownianMotionProcess with Hurst index 0.65 and apply a fractional difference of order 0.45 to it:

In[20]:=
BlockRandom[SeedRandom[150]; sample = RandomFunction[
    FractionalBrownianMotionProcess[0.65], {0, 100, 0.01}]];
{ListLinePlot[sample], ListLinePlot[
  ResourceFunction["FractionalDTimeSeries"][sample, 0.45]["Values"]]}
Out[21]=

Get a time series of the frequencies of the word "Peru" in typical published text, and apply a fractional difference of order 0.35 and a tolerance of 0.01 to it:

In[22]:=
sample = WordFrequencyData["Peru", "TimeSeries"];
{DateListPlot[sample], DateListPlot[
  ResourceFunction["FractionalDTimeSeries"][sample, 0.35, 0.01]]}
Out[23]=

Possible Issues (4) 

FractionalDTimeSeries requires the order to be a positive real number. Negative orders (integration) are currently not implemented:

In[24]:=
ResourceFunction["FractionalDTimeSeries"][
 FinancialData["F", "Close", "Jan. 1, 2000"], -0.5]
Out[24]=

FractionalDTimeSeries requires the tolerance to be positive:

In[25]:=
ResourceFunction["FractionalDTimeSeries"][
 FinancialData["F", "Close", "Jan. 1, 2000"], 0.5, -0.0001]
Out[25]=

If the tolerance is too small, no transformation will be performed:

In[26]:=
ResourceFunction["FractionalDTimeSeries"][
 FinancialData["F", "Close", "Jan. 1, 2000"], 0.5, 0.0000001]
Out[26]=

If the tolerance is set too high, the transformed time series will be essentially the same as the original one, but in a different scale:

In[27]:=
ts = FinancialData["F", "Close", "Jan. 1, 2000"];
fdshightol = ResourceFunction["FractionalDTimeSeries"][ts, 0.5, 0.15]
Out[28]=
In[29]:=
Row[
 {ListLinePlot[ts, ImageSize -> 350, PlotLabel -> Style["Original Time Series | Data points: " <> ToString@(Length@(ts["Values"])), Darker@Black, 10, Bold]], ListLinePlot[fdshightol, ImageSize -> 350, PlotLabel -> Style["FractionalDTimeSeries with d=0.5 and tol= 0.15 | Data points: " <> ToString@(Length@(fdshightol["Values"])),
     Darker@Black, 10, Bold]]}]
Out[29]=

Neat Examples (4) 

A logarithmically-transformed time series:

In[30]:=
ts = Log[
  QuantityMagnitude[FinancialData["GE", "Close", "Jan. 1, 2000"]]]
Out[30]=
In[31]:=
DateListPlot[ts]
Out[31]=

Find the relationship between the statistical value of a UnitRootTest and the CorrelationFunction of a transformed time series, by using several orders between 0 and 1:

In[32]:=
baseDs = Range[0, 1, 0.005];
setFSeries = Table[{d, ResourceFunction["FractionalDTimeSeries"][ts, d]}, {d, baseDs}];

Apply a UnitRootTest (red axis) on each differentiated time series to see up from which level of d is possible to reject H0. Also, compute its CorrelationFunction at lag 1 (blue axis) to compare:

In[33]:=
statACFDataPlot = {#[[1]], CorrelationFunction[#[[2]], 1], UnitRootTest@#[[2]]} & /@ setFSeries;

Plot the results:

In[34]:=
corrPlot = ListLinePlot[
   {#[[1]], #[[2]]} & /@ statACFDataPlot, ImagePadding -> {{50, 50}, {45, 2}},
   PlotStyle -> Blue, Frame -> {True, True, False, False},
   FrameStyle -> {Automatic, Blue, Automatic, Automatic}, FrameTicks -> {None, All, None, None},
   PlotLabel -> Style["Autocorrelation vs UnitRootTest P-value | NYSE:GE Time Series", Black, 13, Bold],
   FrameLabel -> {{Style["AC(1)", Bold], None}, {Style["d*", 11, Bold], None}},
   ImageSize -> 500];
statPlot = ListLinePlot[
   {#[[1]], #[[3]]} & /@ statACFDataPlot, ImagePadding -> {{50, 50}, {45, 2}},
   PlotStyle -> Red,
   Frame -> {False, False, False, True},
   FrameTicks -> {{None, All}, {None, None}},
   FrameStyle -> {Automatic, Automatic, Automatic, Red},
   FrameLabel -> {{None, Style["STAT", Bold]}, {None, None}},
   PlotLabel -> Style[" "],
   ImageSize -> 500,
   GridLines -> {None, {0.05}},
   GridLinesStyle -> Directive[AbsoluteThickness[3/2], Red, Dashed],
   Epilog -> {Text[
      Style["Possible to reject H0 (pval<0.05)", 9, Bold], Scaled[{0.015, 0.125}], {-1, 0}]}
   ];
In[35]:=
Overlay[{corrPlot, statPlot}]
Out[35]=

Publisher

Frank Salvador Ygnacio Rosas

Version History

  • 1.0.0 – 01 August 2022

Source Metadata

Related Resources

License Information