0
0
ML Pythonml~10 mins

Stationarity and differencing in ML Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to difference the time series data once.

ML Python
differenced_series = original_series[1]
Drag options to blanks, or click blank then click option'
A.mean()
B.diff()
C.sum()
D.plot()
Attempts:
3 left
💡 Hint
Common Mistakes
Using .mean() instead of .diff()
Using .sum() which adds values rather than differencing
Trying to plot instead of transforming the data
2fill in blank
medium

Complete the code to check if the time series is stationary using the Augmented Dickey-Fuller test.

ML Python
from statsmodels.tsa.stattools import adfuller
result = adfuller(time_series)
print('p-value:', result[1])
Drag options to blanks, or click blank then click option'
A[1]
B[0]
C[4]
D[5]
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 0 which is the test statistic, not p-value
Using index 4 or 5 which are not valid for p-value
3fill in blank
hard

Fix the error in the code to difference the series twice.

ML Python
diff2_series = original_series[1].diff()
Drag options to blanks, or click blank then click option'
A.diff(1)
B.diff(2)
C.diff().diff()
D.diff()
Attempts:
3 left
💡 Hint
Common Mistakes
Using .diff(2) which is not the same as differencing twice
Using .diff(1) which only differences once
4fill in blank
hard

Complete the code to difference the series once, drop NaNs, and print the p-value from the ADF test.

ML Python
differenced = original_series.diff()[1]
result = adfuller(differenced)
print('p-value:', result[2])
Drag options to blanks, or click blank then click option'
A.dropna()
B[1]
C[0]
D.values
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting .dropna(), causing errors in ADF test
Using [0] (test statistic) instead of [1] (p-value)
5fill in blank
hard

Fill all three blanks to extract the test statistic and p-value from the ADF test on differenced series, and reference critical values.

ML Python
adf_result = adfuller(differenced_series[1])
test_stat = adf_result[2]
p_value = adf_result[3]
critical_vals = adf_result[4]
Drag options to blanks, or click blank then click option'
A.dropna()
B[0]
C[1]
D[4]
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting .dropna()
Mixing up indices (e.g., using [4] for p-value)
Using wrong tuple positions for stat and p-value