DIFF
Differences a time series.
Required Arguments
Z — Vector of length NOBSZ containing the time series. (Input)
IPER — Vector of length NDIFF containing the periods at which Z is to be differenced. (Input)
The elements of IPER must be greater than or equal to one.
IORD — Vector of length NDIFF containing the order of each difference given in IPER. (Input)
The elements of IORD must be greater than or equal to zero.
NOBSX — Number of observations in the differenced series X. (Output)
NOBSX = NOBSZ  IMISS * NLOST.
X — Vector of length NOBSX containing the differenced series. (Output)
Optional Arguments
NOBSZ — Number of observations in the time series Z. (Input)
NOBSZ must be greater than or equal to one.
Default: NOBSZ = size (Z,1).
NDIFF — Number of differences to perform. (Input)
NDIFF must be greater than or equal to one.
Default: NDIFF = size (IPER,1).
IPRINT — Printing option. (Input)
Default: IPRINT = 0.
IPRINT
Action
0
No printing is performed.
1
Prints the number of observations lost because of differencing Z, the number of observations in the differenced series X, and the differenced series X
IMISS — Missing value option. (Input)
Default: IMISS = 0.
IMISS
Action
0
Include missing values in X.
1
Exclude missing values from X.
NLOST — Number of observations lost because of differencing the time series Z. (Output)
NLOST = IPER(1) * IORD(1) +  + IPER(NDIFF ) * IORD(NDIFF).
FORTRAN 90 Interface
Generic: CALL DIFF (Z, IPER, IORD, NOBSX, X [])
Specific: The specific interface names are S_DIFF and D_DIFF.
FORTRAN 77 Interface
Single: CALL DIFF (NOBSZ, Z, NDIFF, IPER, IORD, IPRINT, IMISS, NLOST, NOBSX, X)
Double: The double precision name is DDIFF.
Description
Routine DIFF performs m = NDIFF successive backward differences of period si= IPER(i) and order di = IORD(i) for i = 1, …, m on the n = NOBSZ observations {Zt} for t = 1, 2, …, n.
Consider the backward shift operator B given by
BkZt = Ztk, for all k
Then, the backward difference operator with period s is defined by
sZt = (1 Bs)Zt = Zt Zts, s 0
Note that BsZt and sZt are defined only for t = (s + 1), …, n. Repeated differencing with period s is simply
where d 0 is the order of differencing. Note that
is defined only for t = (sd + 1), …, n.
The general difference formula used in routine DIFF is given by
 
where nL = NLOST represents the number of observations “lost” because of differencing and NaN (not a number) represents the missing value code. See the routine AMACH; in the “Machine‑Dependent Constants” section of the Reference Material. Note that nL = Σjsjdj.
A homogeneous stationary time series may be arrived at by appropriately differencing a homogeneous nonstationary time series (Box and Jenkins 1976, page 85). Preliminary application of an appropriate transformation followed by differencing of a series may enable model identification and parameter estimation in the class of homogeneous stationary autoregressive‑moving average models.
Comments
1. Workspace may be explicitly provided, if desired, by use of D2FF/DD2FF. The reference is:
CALL D2FF (NOBSZ, Z, NDIFF, IPER, IORD, IPRINT, IMISS, NLOST, NOBSX, X, XWK)
The additional argument is:
XWK — Work vector of length equal to NOBSZ.
2. A value is considered to be missing if it is not itself in the data set or if it is the result of an operation involving missing value(s). In differencing, missing values occur at the beginning of the differenced series since X(i) = Z(i Z(i  k) is not defined for k greater than or equal to i.
Example
Consider the Airline Data (Box and Jenkins 1976, page 531) consisting of the monthly total number of international airline passengers from January 1949 through December 1960. Routine DIFF is used to compute
Xt = 112Xt = (Zt Zt12) (Zt1 Zt13)
For the first invocation of DIFF with IMISS = 0, X1, X2, …, X13 are set to the missing value code (NaN) and the equation is applied for t = 14, 15, …, 24. For the second invocation of DIFF with IMISS = 1, the missing values are excluded from the output array containing the differenced series.
 
USE GDATA_INT
USE DIFF_INT
 
IMPLICIT NONE
INTEGER IPRINT, NDIFF, NOBSZ
PARAMETER (IPRINT=1, NDIFF=2, NOBSZ=24)
!
INTEGER IMISS, IORD(NDIFF), IPER(NDIFF), NCOL, NLOST, NOBSX, &
NROW
REAL X(NOBSZ), Z(144, 1)
! Periods of differencing
DATA IPER/1, 12/
! Orders of differencing
DATA IORD/1, 1/
! Airline Data
CALL GDATA (4, Z, NROW, NCOL)
! Nonseasonal and seasonal difference
! first 24 observations in Z
!
! Include missing values in result X
! USE Default IMISS = 0
CALL DIFF (Z(:, 1), IPER, IORD, NOBSX, X, NOBSZ=NOBSZ, IPRINT=IPRINT)
! Exclude missing values in result X
IMISS = 1
CALL DIFF (Z(:, 1), IPER, IORD, NOBSX, X, IPRINT=IPRINT, &
NOBSZ=NOBSZ, IMISS=IMISS)
!
END
Output
 
Output from DIFF/D2FF
 
NLOST = 13
NOBSX = 24
I Z(I) X(I)
1 112.00 NaN
2 118.00 NaN
3 132.00 NaN
4 129.00 NaN
5 121.00 NaN
6 135.00 NaN
7 148.00 NaN
8 148.00 NaN
9 136.00 NaN
10 119.00 NaN
11 104.00 NaN
12 118.00 NaN
13 115.00 NaN
14 126.00 5.000
15 141.00 1.000
16 135.00 -3.000
17 125.00 -2.000
18 149.00 10.000
19 170.00 8.000
20 170.00 0.000
21 158.00 0.000
22 133.00 -8.000
23 114.00 -4.000
24 140.00 12.000
 
Output from DIFF/D2FF
 
NLOST = 13
NOBSX = 11
 
I Z(I) X(I)
1 112.00 5.000
2 118.00 1.000
3 132.00 -3.000
4 129.00 -2.000
5 121.00 10.000
6 135.00 8.000
7 148.00 0.000
8 148.00 0.000
9 136.00 -8.00
10 119.00 -4.000
11 104.00 12.000
12 118.00
13 115.00
14 126.00
15 141.00
16 135.00
17 125.00
18 149.00
19 170.00
20 170.00
21 158.00
22 133.00
23 114.00
24 140.00