 Difference - Maple Help

TimeSeriesAnalysis

 Difference
 differencing transformation Calling Sequence Apply(Difference, timeseries) Apply(Difference(times = n, lag = k), timeseries) Unapply(Difference, forecast) Unapply(Difference(times = n, lag = k), forecast) Unapply(Difference(times = n, lag = k), forecast, origin = o) Parameters

 timeseries - n - positive integer (optional) k - positive integer (optional) forecast - TimeSeries data set, typically obtained from a forecasting method o - specification of origin ; list of Matrices Description

 • The differencing transformation takes a time series ${T}_{1},{T}_{2},{T}_{3},\mathrm{...}$ and returns the time series ${T}_{2}-{T}_{1},{T}_{3}-{T}_{2},\mathrm{...}$.
 • Apply this transformation to a time series using the Apply command. Translate differenced information such as forecasts back to the original domain by using the Unapply command.
 • By using the $\mathrm{lag}$ option, one can difference a time series with a given lag. In particular, the transformation $\mathrm{Difference}\left(\mathrm{lag}=2\right)$ transforms the time series ${T}_{1},{T}_{2},{T}_{3},\mathrm{...}$ into ${T}_{3}-{T}_{1},{T}_{4}-{T}_{2},{T}_{5}-{T}_{2},\mathrm{...}$. The default lag is 1.
 • If there are missing data points (represented in Maple by the value $\mathrm{undefined}$ in the time series), the differencing transformation skips over them while leaving the undefined value in place. For example, if ${T}_{4}$ is undefined, then the result of differencing with $\mathrm{lag}=2$ is

${T}_{3}-{T}_{1},\mathrm{undefined},{T}_{5}-{T}_{3},{T}_{6}-{T}_{2},{T}_{7}-{T}_{5},{T}_{8}-{T}_{6},\mathrm{... .}$

This is done so that Unapply can recover the data.

 • By using the $\mathrm{times}$ option, one can specify the transformation that consists of differencing multiple times. The default is $\mathrm{times}=1$. Specifying $\mathrm{times}=2$, for example, describes the transformation that takes the time series ${T}_{1},{T}_{2},{T}_{3},\mathrm{...}$ to ${T}_{1}+{T}_{3}-2{T}_{2},{T}_{2}+{T}_{4}-2{T}_{3},\mathrm{...}$.
 • Every time differencing is applied, the resulting time series has $k$ data points fewer than the input. This is reflected in the timestamps; the first timestamp is omitted from the resulting time series. In other words, the timestamp for the difference ${T}_{2}-{T}_{1}$ is taken to be the time at which ${T}_{2}$ occurred, because it is only at this time that both ${T}_{1}$ and ${T}_{2}$ are available and the first difference can be computed.
 • For the Unapply call, to transform ${T}_{2}-{T}_{1},{T}_{3}-{T}_{2},\mathrm{...}$ back into the original ${T}_{1},{T}_{2},{T}_{3},\mathrm{...}$, Maple needs to know the value ${T}_{1}$, which will be referred to as the origin. If the forecast is created using TimeSeriesAnalysis commands, then Unapply can generally find the time series object from which the original differences were taken, and use this object to infer the origin. Otherwise, or to override this choice, the origin can be specified explicitly using the $\mathrm{origin}=o$ option.

In the example above, only a single number was needed. However, if $\mathrm{forecast}$ contains $m$ data sets, then a Vector of $m$ origins is needed - one for every data set. Moreover, if $\mathrm{lag}=k$, then each data set needs $k$ independent origins; all of these are arranged in a $k$x_m_ Matrix. Finally, with $\mathrm{times}=n$, we need such a Matrix for every differencing operation that is undone.

The most explicit way to specify the origin argument is therefore as a list of $n$ Matrices, each of size $k$x_m_. However, several shortcuts are available:

 – A Matrix can be specified as a list or Vector of the elements needed.
 – A Matrix can be specified as the list or Vector of the elements of its first row; this row will be repeated $k$ times.
 – A Matrix can be specified as a single number, which the $kxm$ Matrix will be filled with.
 – Instead of a list of Matrices, you can specify a single $nxm$ Matrix, each row of which will be expanded into a $kxm$ by repeating it $k$ times.
 – Finally, if you specify a single number, it will be used to fill all $n$ of the $kxm$ Examples

 > $\mathrm{with}\left(\mathrm{TimeSeriesAnalysis}\right):$
 > $\mathrm{sales_numbers}≔⟨150,147,114,113,91,164,56,39,32,86⟩$
 ${\mathrm{sales_numbers}}{≔}\left[\begin{array}{c}{150}\\ {147}\\ {114}\\ {113}\\ {91}\\ {164}\\ {56}\\ {39}\\ {32}\\ {86}\end{array}\right]$ (1)
 > $\mathrm{sales}≔\mathrm{TimeSeries}\left(\mathrm{sales_numbers},\mathrm{startdate}="2010-01-01",\mathrm{frequency}="weekly",\mathrm{header}="Weekly Sales"\right)$
 ${\mathrm{sales}}{≔}\left[\begin{array}{c}{\mathrm{Time series}}\\ {\mathrm{Weekly Sales}}\\ {\mathrm{10 rows of data:}}\\ {\mathrm{2010-01-01 - 2010-03-05}}\end{array}\right]$ (2)
 > $\mathrm{GetData}\left(\mathrm{sales}\right)\left[..5\right]$
 $\left[\begin{array}{c}{150.}\\ {147.}\\ {114.}\\ {113.}\\ {91.}\end{array}\right]$ (3)

Here are the differences in sales from week to week.

 > $\mathrm{differenced}≔\mathrm{Apply}\left(\mathrm{Difference},\mathrm{sales}\right)$
 ${\mathrm{differenced}}{≔}\left[\begin{array}{c}{\mathrm{Time series}}\\ {\mathrm{Weekly Sales \left(differenced\right)}}\\ {\mathrm{9 rows of data:}}\\ {\mathrm{2010-01-08 - 2010-03-05}}\end{array}\right]$ (4)
 > $\mathrm{GetData}\left(\mathrm{differenced}\right)\left[..4\right]$
 $\left[\begin{array}{c}{-3.}\\ {-33.}\\ {-1.}\\ {-22.}\end{array}\right]$ (5)

Reconstructing the original data (except for the first row):

 > $\mathrm{original}≔\mathrm{Unapply}\left(\mathrm{Difference},\mathrm{differenced}\right)$
 ${\mathrm{original}}{≔}\left[\begin{array}{c}{\mathrm{Time series}}\\ {\mathrm{Weekly Sales}}\\ {\mathrm{9 rows of data:}}\\ {\mathrm{2010-01-08 - 2010-03-05}}\end{array}\right]$ (6)
 > $\mathrm{GetData}\left(\mathrm{original}\right)\left[..4\right]$
 $\left[\begin{array}{c}{147.}\\ {114.}\\ {113.}\\ {91.}\end{array}\right]$ (7)

The original data, differenced twice:

 > $\mathrm{differenced_twice}≔\mathrm{Apply}\left(\mathrm{Difference},\mathrm{differenced}\right)$
 ${\mathrm{differenced_twice}}{≔}\left[\begin{array}{c}{\mathrm{Time series}}\\ {\mathrm{Weekly Sales \left(differenced\right) \left(differenced\right)}}\\ {\mathrm{8 rows of data:}}\\ {\mathrm{2010-01-15 - 2010-03-05}}\end{array}\right]$ (8)
 > $\mathrm{GetData}\left(\mathrm{differenced_twice}\right)\left[..3\right]$
 $\left[\begin{array}{c}{-30.}\\ {32.}\\ {-21.}\end{array}\right]$ (9)

You can do this in one step, too. The header gets a bit nicer name in this case:

 > $\mathrm{differenced_twice_2}≔\mathrm{Apply}\left(\mathrm{Difference}\left('\mathrm{times}'=2\right),\mathrm{sales}\right)$
 ${\mathrm{differenced_twice_2}}{≔}\left[\begin{array}{c}{\mathrm{Time series}}\\ {\mathrm{Weekly Sales \left(differenced twice\right)}}\\ {\mathrm{8 rows of data:}}\\ {\mathrm{2010-01-15 - 2010-03-05}}\end{array}\right]$ (10)
 > $\mathrm{GetData}\left(\mathrm{differenced_twice_2}\right)\left[..3\right]$
 $\left[\begin{array}{c}{-30.}\\ {32.}\\ {-21.}\end{array}\right]$ (11)

Unapplying differencing twice leads to the original data (minus the first two rows).

 > $\mathrm{original_2}≔\mathrm{Unapply}\left(\mathrm{Difference}\left('\mathrm{times}'=2\right),\mathrm{differenced_twice}\right)$
 ${\mathrm{original_2}}{≔}\left[\begin{array}{c}{\mathrm{Time series}}\\ {\mathrm{Weekly Sales}}\\ {\mathrm{8 rows of data:}}\\ {\mathrm{2010-01-15 - 2010-03-05}}\end{array}\right]$ (12)
 > $\mathrm{GetData}\left(\mathrm{original_2}\right)\left[..3\right]$
 $\left[\begin{array}{c}{114.}\\ {113.}\\ {91.}\end{array}\right]$ (13)
 > $\mathrm{original_3}≔\mathrm{Unapply}\left(\mathrm{Difference}\left('\mathrm{times}'=2\right),\mathrm{differenced_twice_2}\right)$
 ${\mathrm{original_3}}{≔}\left[\begin{array}{c}{\mathrm{Time series}}\\ {\mathrm{Weekly Sales}}\\ {\mathrm{8 rows of data:}}\\ {\mathrm{2010-01-15 - 2010-03-05}}\end{array}\right]$ (14)
 > $\mathrm{GetData}\left(\mathrm{original_3}\right)\left[..3\right]$
 $\left[\begin{array}{c}{114.}\\ {113.}\\ {91.}\end{array}\right]$ (15)

You can also perform two operations of unapplying differencing once, to either $\mathrm{differenced_twice}$ or $\mathrm{differenced_twice_2}$.

 > $\mathrm{intermediate}≔\mathrm{Unapply}\left(\mathrm{Difference},\mathrm{differenced_twice}\right)$
 ${\mathrm{intermediate}}{≔}\left[\begin{array}{c}{\mathrm{Time series}}\\ {\mathrm{Weekly Sales \left(differenced\right)}}\\ {\mathrm{8 rows of data:}}\\ {\mathrm{2010-01-15 - 2010-03-05}}\end{array}\right]$ (16)
 > $\mathrm{GetData}\left(\mathrm{intermediate}\right)\left[..3\right]$
 $\left[\begin{array}{c}{-33.}\\ {-1.}\\ {-22.}\end{array}\right]$ (17)
 > $\mathrm{original_4}≔\mathrm{Unapply}\left(\mathrm{Difference},\mathrm{intermediate}\right)$
 ${\mathrm{original_4}}{≔}\left[\begin{array}{c}{\mathrm{Time series}}\\ {\mathrm{Weekly Sales}}\\ {\mathrm{8 rows of data:}}\\ {\mathrm{2010-01-15 - 2010-03-05}}\end{array}\right]$ (18)
 > $\mathrm{GetData}\left(\mathrm{original_4}\right)\left[..3\right]$
 $\left[\begin{array}{c}{114.}\\ {113.}\\ {91.}\end{array}\right]$ (19)
 > $\mathrm{intermediate_2}≔\mathrm{Unapply}\left(\mathrm{Difference},\mathrm{differenced_twice_2}\right)$
 ${\mathrm{intermediate_2}}{≔}\left[\begin{array}{c}{\mathrm{Time series}}\\ {\mathrm{Weekly Sales \left(differenced\right)}}\\ {\mathrm{8 rows of data:}}\\ {\mathrm{2010-01-15 - 2010-03-05}}\end{array}\right]$ (20)
 > $\mathrm{GetData}\left(\mathrm{intermediate_2}\right)\left[..3\right]$
 $\left[\begin{array}{c}{-33.}\\ {-1.}\\ {-22.}\end{array}\right]$ (21)
 > $\mathrm{original_5}≔\mathrm{Unapply}\left(\mathrm{Difference},\mathrm{intermediate_2}\right)$
 ${\mathrm{original_5}}{≔}\left[\begin{array}{c}{\mathrm{Time series}}\\ {\mathrm{Weekly Sales}}\\ {\mathrm{8 rows of data:}}\\ {\mathrm{2010-01-15 - 2010-03-05}}\end{array}\right]$ (22)
 > $\mathrm{GetData}\left(\mathrm{original_5}\right)\left[..3\right]$
 $\left[\begin{array}{c}{114.}\\ {113.}\\ {91.}\end{array}\right]$ (23)

If you expect a biweekly pattern, you can difference with lag = 2.

 > $\mathrm{differenced_lag2}≔\mathrm{Apply}\left(\mathrm{Difference}\left(\mathrm{lag}=2\right),\mathrm{sales}\right)$
 ${\mathrm{differenced_lag2}}{≔}\left[\begin{array}{c}{\mathrm{Time series}}\\ {\mathrm{Weekly Sales \left(differenced\right)}}\\ {\mathrm{8 rows of data:}}\\ {\mathrm{2010-01-15 - 2010-03-05}}\end{array}\right]$ (24)
 > $\mathrm{GetData}\left(\mathrm{differenced_lag2}\right)\left[..3\right]$
 $\left[\begin{array}{c}{-36.}\\ {-34.}\\ {-23.}\end{array}\right]$ (25)

If one of the data points were missing, you can still do the transformation and back-transformation.

 > $\mathrm{sales_numbers}\left[5\right]≔\mathrm{undefined}:$$\mathrm{sales_numbers}$
 $\left[\begin{array}{c}{150}\\ {147}\\ {114}\\ {113}\\ {\mathrm{undefined}}\\ {164}\\ {56}\\ {39}\\ {32}\\ {86}\end{array}\right]$ (26)
 > $\mathrm{sales_missing}≔\mathrm{TimeSeries}\left(\mathrm{sales_numbers},\mathrm{startdate}="2010-01-01",\mathrm{frequency}="weekly",\mathrm{header}="Weekly Sales"\right)$
 ${\mathrm{sales_missing}}{≔}\left[\begin{array}{c}{\mathrm{Time series}}\\ {\mathrm{Weekly Sales}}\\ {\mathrm{10 rows of data:}}\\ {\mathrm{2010-01-01 - 2010-03-05}}\end{array}\right]$ (27)
 > $\mathrm{differenced_lag2_missing}≔\mathrm{Apply}\left(\mathrm{Difference}\left(\mathrm{lag}=2\right),\mathrm{sales_missing}\right)$
 ${\mathrm{differenced_lag2_missing}}{≔}\left[\begin{array}{c}{\mathrm{Time series}}\\ {\mathrm{Weekly Sales \left(differenced\right)}}\\ {\mathrm{8 rows of data:}}\\ {\mathrm{2010-01-15 - 2010-03-05}}\end{array}\right]$ (28)
 > $\mathrm{GetData}\left(\mathrm{differenced_lag2_missing}\right)$
 $\left[\begin{array}{c}{-36.}\\ {-34.}\\ {Float}{}\left({\mathrm{undefined}}\right)\\ {51.}\\ {-58.}\\ {-125.}\\ {-24.}\\ {47.}\end{array}\right]$ (29)
 > $\mathrm{GetHeaders}\left(\mathrm{differenced_lag2_missing}\right)$
 $\left[{"Weekly Sales \left(differenced\right)"}\right]$ (30) Specifying the $\mathrm{origin}$ option

 • Consider the following time series.
 > $\mathrm{ts}≔\mathrm{TimeSeries}\left(\left[\left[\mathrm{seq}\left(3..-3,-1\right)\right],\left[\mathrm{seq}\left(3..-9,-2\right)\right]\right]\right)$
 ${\mathrm{ts}}{≔}\left[\begin{array}{c}{\mathrm{Time series}}\\ {\mathrm{data set 1, data set 2}}\\ {\mathrm{7 rows of data:}}\\ {\mathrm{2014 - 2020}}\end{array}\right]$ (31)
 > $\mathrm{GetData}\left(\mathrm{ts}\right)$
 $\left[\begin{array}{cc}{3.}& {3.}\\ {2.}& {1.}\\ {1.}& {-1.}\\ {0.}& {-3.}\\ {-1.}& {-5.}\\ {-2.}& {-7.}\\ {-3.}& {-9.}\end{array}\right]$ (32)
 • If you try to unapply a differencing operation, Maple complains that it cannot find the origin.
 > $\mathrm{Unapply}\left(\mathrm{Difference},\mathrm{ts}\right)$
 • With $\mathrm{times}=1$ and $\mathrm{lag}=1$ (the default), you need to specify a list of one $1$x_2_ Matrix.
 > $\mathrm{result1}≔\mathrm{Unapply}\left(\mathrm{Difference},\mathrm{ts},'\mathrm{origin}'=\left[\mathrm{Matrix}\left(1,2,\left[2,3\right]\right)\right]\right)$
 ${\mathrm{result1}}{≔}\left[\begin{array}{c}{\mathrm{Time series}}\\ {\mathrm{data set 1, data set 2}}\\ {\mathrm{7 rows of data:}}\\ {\mathrm{2014 - 2020}}\end{array}\right]$ (33)
 > $\mathrm{GetData}\left(\mathrm{result1}\right)$
 $\left[\begin{array}{cc}{5.}& {6.}\\ {7.}& {7.}\\ {8.}& {6.}\\ {8.}& {3.}\\ {7.}& {-2.}\\ {5.}& {-9.}\\ {2.}& {-18.}\end{array}\right]$ (34)
 • This can also be specified as just the list $\left[2,3\right]$.
 > $\mathrm{result2}≔\mathrm{Unapply}\left(\mathrm{Difference},\mathrm{ts},'\mathrm{origin}'=\left[2,3\right]\right)$
 ${\mathrm{result2}}{≔}\left[\begin{array}{c}{\mathrm{Time series}}\\ {\mathrm{data set 1, data set 2}}\\ {\mathrm{7 rows of data:}}\\ {\mathrm{2014 - 2020}}\end{array}\right]$ (35)
 > $\mathrm{GetData}\left(\mathrm{result2}\right)$
 $\left[\begin{array}{cc}{5.}& {6.}\\ {7.}& {7.}\\ {8.}& {6.}\\ {8.}& {3.}\\ {7.}& {-2.}\\ {5.}& {-9.}\\ {2.}& {-18.}\end{array}\right]$ (36)
 • If you set $\mathrm{lag}=3$, you need to specify values for three previous data points for each data set. You can give a list with a single Matrix - or just specify the Matrix directly.
 > $\mathrm{result3}≔\mathrm{Unapply}\left(\mathrm{Difference}\left(\mathrm{lag}=3\right),\mathrm{ts},'\mathrm{origin}'=⟨⟨1,2,4⟩|⟨3,-2,-1⟩⟩\right)$
 ${\mathrm{result3}}{≔}\left[\begin{array}{c}{\mathrm{Time series}}\\ {\mathrm{data set 1, data set 2}}\\ {\mathrm{7 rows of data:}}\\ {\mathrm{2014 - 2020}}\end{array}\right]$ (37)
 > $\mathrm{GetData}\left(\mathrm{result3}\right)$
 $\left[\begin{array}{cc}{4.}& {6.}\\ {4.}& {-1.}\\ {5.}& {-2.}\\ {4.}& {3.}\\ {3.}& {-6.}\\ {3.}& {-9.}\\ {1.}& {-6.}\end{array}\right]$ (38)
 • If you set $\mathrm{lag}=3$ and $\mathrm{times}=2$, then you need to specify all these values for both times that you unapply differencing. The two matrices need to be specified in a list, in the order in which they will be used.
 > $\mathrm{result4}≔\mathrm{Unapply}\left(\mathrm{Difference}\left(\mathrm{lag}=3,\mathrm{times}=2\right),\mathrm{ts},'\mathrm{origin}'=\left[⟨⟨1,2,4⟩|⟨3,-2,-1⟩⟩,⟨⟨2,0,-3⟩|⟨0,0,1⟩⟩\right]\right)$
 ${\mathrm{result4}}{≔}\left[\begin{array}{c}{\mathrm{Time series}}\\ {\mathrm{data set 1, data set 2}}\\ {\mathrm{7 rows of data:}}\\ {\mathrm{2014 - 2020}}\end{array}\right]$ (39)
 > $\mathrm{GetData}\left(\mathrm{result4}\right)$
 $\left[\begin{array}{cc}{6.}& {6.}\\ {4.}& {-1.}\\ {2.}& {-1.}\\ {10.}& {9.}\\ {7.}& {-7.}\\ {5.}& {-10.}\\ {11.}& {3.}\end{array}\right]$ (40) Compatibility

 • The TimeSeriesAnalysis[Difference] command was introduced in Maple 18.