Differencing
Differencing
Differencing is a transformation that converts a non-stationary time series into a stationary one by computing the change between consecutive observations. First-order differencing removes linear trends, second-order removes quadratic trends, and so on. It is a key preprocessing step for ARIMA models which require stationarity.
Given a time series and a differencing order d, apply d rounds of first-order differencing.
Algorithm
First-order differencing computes:
Δx[t]=x[t]−x[t−1]For order d, apply this operation d times. Each round reduces the length by 1, so the output has length n - d.
Examples
Input:
series = [1, 3, 6, 10, 15], order = 1
Output:
[2, 3, 4, 5]
First differences: 3-1=2, 6-3=3, 10-6=4, 15-10=5. The increasing differences show an accelerating trend.
Input:
series = [1, 3, 6, 10, 15], order = 2
Output:
[1, 1, 1]
First differences: [2, 3, 4, 5]. Second differences: [1, 1, 1]. The constant second difference confirms the original series follows a quadratic pattern.
Hint 1
Start with a copy of the series. Apply first-order differencing in a loop 'order' times. Each iteration creates a new list: [result[i] - result[i-1] for i in range(1, len(result))].
Hint 2
After each round of differencing, the list gets shorter by 1. After d rounds, the output has length n - d. Make sure to update the working list each iteration.
Requirements
- Apply first-order differencing (subtract previous element) d times
- Each round of differencing reduces the series length by 1
- The output has length n - order
- Return a list of numbers
Constraints
- series has at least order + 1 elements
- order >= 1
- Return a list of numbers
- Time limit: 300 ms
Try Similar Problems
Log in to take notes on this problem
Accepts: array
Accepts: number
Differencing
Differencing
Differencing is a transformation that converts a non-stationary time series into a stationary one by computing the change between consecutive observations. First-order differencing removes linear trends, second-order removes quadratic trends, and so on. It is a key preprocessing step for ARIMA models which require stationarity.
Given a time series and a differencing order d, apply d rounds of first-order differencing.
Algorithm
First-order differencing computes:
Δx[t]=x[t]−x[t−1]For order d, apply this operation d times. Each round reduces the length by 1, so the output has length n - d.
Examples
Input:
series = [1, 3, 6, 10, 15], order = 1
Output:
[2, 3, 4, 5]
First differences: 3-1=2, 6-3=3, 10-6=4, 15-10=5. The increasing differences show an accelerating trend.
Input:
series = [1, 3, 6, 10, 15], order = 2
Output:
[1, 1, 1]
First differences: [2, 3, 4, 5]. Second differences: [1, 1, 1]. The constant second difference confirms the original series follows a quadratic pattern.
Hint 1
Start with a copy of the series. Apply first-order differencing in a loop 'order' times. Each iteration creates a new list: [result[i] - result[i-1] for i in range(1, len(result))].
Hint 2
After each round of differencing, the list gets shorter by 1. After d rounds, the output has length n - d. Make sure to update the working list each iteration.
Requirements
- Apply first-order differencing (subtract previous element) d times
- Each round of differencing reduces the series length by 1
- The output has length n - order
- Return a list of numbers
Constraints
- series has at least order + 1 elements
- order >= 1
- Return a list of numbers
- Time limit: 300 ms
Try Similar Problems
Log in to take notes on this problem
Accepts: array
Accepts: number