Skip to content
DeepForgeby svx
All problems
MediumTime Seriests-027

Detrend by Differencing

Problem

Detrend a series with lag-lag differencing: y[t] = x[t] - x[t-lag] for t = lag, ..., n-1. Lag 1 removes a linear trend while lag m removes a repeating seasonal pattern of length m.

Return an empty list when lag is not positive or lag is at least the series length.

Hint

Subtract the value lag positions earlier from each current value.

Test cases

case 1
in: [[1,4,9,16],1]
expected: [3,5,7]
case 2
in: [[1,4,9,16],2]
expected: [8,12]
case 3
in: [[5,5,5],1]
expected: [0,0]
case 4
in: [[1,2,3],5]
expected: []
Starter codePython
def difference_lag(series, lag):
    # Your code here
    pass

4 test cases · run your code against them in the editor