TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Differencing

Time Series
Easy

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]\Delta x[t] = x[t] - x[t-1]Δ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.

Return a list with length equal to the input length minus order.

Loading visualization...

Examples

Input: series = [1, 3, 6, 10, 15], order = 1

Output: [2, 3, 4, 5]

Explanation: Each output is the current value minus the previous value.

Input: series = [1, 3, 6, 10, 15], order = 2

Output: [1, 1, 1]

Hint 1

Copy the series, then repeat first-order differencing order times.

Hint 2

Replace the working list with adjacent differences after each round.

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
Lag FeaturesPercent ChangeAutocorrelationCumulative ReturnsSimple Moving Average

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: number

You must run your code first.
PrevNext

Differencing

Time Series
Easy

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]\Delta x[t] = x[t] - x[t-1]Δ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.

Return a list with length equal to the input length minus order.

Loading visualization...

Examples

Input: series = [1, 3, 6, 10, 15], order = 1

Output: [2, 3, 4, 5]

Explanation: Each output is the current value minus the previous value.

Input: series = [1, 3, 6, 10, 15], order = 2

Output: [1, 1, 1]

Hint 1

Copy the series, then repeat first-order differencing order times.

Hint 2

Replace the working list with adjacent differences after each round.

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
Lag FeaturesPercent ChangeAutocorrelationCumulative ReturnsSimple Moving Average

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: number

You must run your code first.