Bird
Raised Fist0
ML Pythonml~20 mins

Stationarity and differencing in ML Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Stationarity Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Why is stationarity important in time series analysis?

Which of the following best explains why stationarity is important when analyzing time series data?

AStationarity guarantees the time series has a linear trend that can be easily predicted.
BStationarity means the data has no missing values, which is essential for model training.
CStationarity ensures the statistical properties like mean and variance do not change over time, making modeling and forecasting more reliable.
DStationarity means the time series data is always increasing, which simplifies analysis.
Attempts:
2 left
💡 Hint

Think about why consistent behavior in data helps in making predictions.

Predict Output
intermediate
1:30remaining
Output of differencing a time series

Given the time series data [3, 5, 8, 12, 18], what is the result of applying first-order differencing?

ML Python
data = [3, 5, 8, 12, 18]
differenced = [data[i] - data[i-1] for i in range(1, len(data))]
print(differenced)
A[1, 2, 3, 4]
B[3, 5, 8, 12, 18]
C[5, 8, 12, 18]
D[2, 3, 4, 6]
Attempts:
2 left
💡 Hint

Subtract each value from the previous one.

Model Choice
advanced
2:00remaining
Choosing a model for non-stationary data

You have a time series with a clear upward trend and non-constant variance. Which model approach is best suited before forecasting?

AApply differencing to make the series stationary, then use an ARIMA model.
BUse a linear regression model directly on the raw data without transformation.
CApply PCA to reduce dimensionality before forecasting.
DUse K-means clustering to group similar time points.
Attempts:
2 left
💡 Hint

Think about how to handle trends and changing variance before modeling.

Metrics
advanced
1:30remaining
Evaluating stationarity with statistical tests

Which metric or test is commonly used to check if a time series is stationary?

AAugmented Dickey-Fuller (ADF) test
BConfusion Matrix
CMean Squared Error (MSE)
DSilhouette Score
Attempts:
2 left
💡 Hint

Look for a test that checks for unit roots in time series.

🔧 Debug
expert
2:00remaining
Identifying the error in differencing code

What error will this code produce when trying to difference a time series?

data = [10, 15, 20]
diff = [data[i] - data[i+1] for i in range(len(data)-1)]
print(diff)
AIndexError because it tries to access an element outside the list range.
BProduces incorrect differencing values because it subtracts the next value from the current instead of the current from the previous.
CSyntaxError due to incorrect list comprehension syntax.
DRuns correctly and outputs [5, 5].
Attempts:
2 left
💡 Hint

Check the order of subtraction and the indices used.

Practice

(1/5)
1. What does it mean when a time series is stationary?
easy
A. It has missing values that need to be filled
B. It has a clear upward or downward trend
C. It contains seasonal patterns repeating over fixed intervals
D. Its statistical properties like mean and variance do not change over time

Solution

  1. Step 1: Understand stationarity definition

    Stationarity means the data's mean, variance, and other statistics stay constant over time.
  2. Step 2: Compare options to definition

    Only Its statistical properties like mean and variance do not change over time describes constant statistical properties; others describe trends, seasonality, or missing data.
  3. Final Answer:

    Its statistical properties like mean and variance do not change over time -> Option D
  4. Quick Check:

    Stationary = constant mean/variance [OK]
Hint: Stationary means stats don't change over time [OK]
Common Mistakes:
  • Confusing stationarity with trend presence
  • Thinking seasonality means stationarity
  • Assuming missing data affects stationarity
2. Which Python code correctly applies first-order differencing to a pandas Series data?
easy
A. data.dropna()
B. data.diff(1)
C. data.cumsum()
D. data.shift(1)

Solution

  1. Step 1: Recall differencing method in pandas

    The diff(1) method calculates the difference between current and previous values, performing first-order differencing.
  2. Step 2: Check other options

    shift(1) shifts data, cumsum() sums cumulatively, and dropna() removes missing values, none perform differencing.
  3. Final Answer:

    data.diff(1) -> Option B
  4. Quick Check:

    First difference = diff(1) [OK]
Hint: Use diff(1) for first-order differencing in pandas [OK]
Common Mistakes:
  • Using shift instead of diff for differencing
  • Confusing cumulative sum with differencing
  • Dropping NaNs instead of differencing
3. Given this code snippet:
import pandas as pd
series = pd.Series([10, 12, 15, 20, 25])
diff_series = series.diff(1).dropna()
print(diff_series.tolist())

What is the output?
medium
A. [0, 2, 3, 5, 5]
B. [10, 12, 15, 20, 25]
C. [2.0, 3.0, 5.0, 5.0]
D. [nan, 2, 3, 5, 5]

Solution

  1. Step 1: Calculate first differences

    Differences: 12-10=2, 15-12=3, 20-15=5, 25-20=5.
  2. Step 2: Drop NaN and print list

    The first difference is NaN, dropped by dropna(), so output is [2.0, 3.0, 5.0, 5.0].
  3. Final Answer:

    [2.0, 3.0, 5.0, 5.0] -> Option C
  4. Quick Check:

    Diff values = [2.0,3.0,5.0,5.0] [OK]
Hint: First diff drops first NaN, output is differences list [OK]
Common Mistakes:
  • Including NaN in output list
  • Printing original series instead of differences
  • Confusing shift with diff output
4. You applied first-order differencing to a time series but it still shows a trend. What is the likely issue?
medium
A. The series needs second-order differencing to remove the trend
B. You should use cumulative sum instead of differencing
C. The series is already stationary and differencing added noise
D. You forgot to normalize the data before differencing

Solution

  1. Step 1: Understand differencing orders

    First-order differencing removes linear trends; if trend remains, higher order differencing may be needed.
  2. Step 2: Evaluate other options

    Cumulative sum adds trend, normalization doesn't remove trend, and differencing adding noise means series was not stationary before.
  3. Final Answer:

    The series needs second-order differencing to remove the trend -> Option A
  4. Quick Check:

    Trend remains -> try second differencing [OK]
Hint: If trend remains, increase differencing order [OK]
Common Mistakes:
  • Using cumulative sum instead of differencing
  • Assuming normalization removes trend
  • Stopping at first differencing without checking stationarity
5. You have a monthly sales time series with a yearly seasonal pattern and an upward trend. Which differencing approach should you apply to make it stationary?
hard
A. Apply first-order differencing followed by seasonal differencing with lag 12
B. Apply only first-order differencing
C. Apply only seasonal differencing with lag 12
D. Apply logarithm transformation without differencing

Solution

  1. Step 1: Identify components to remove

    The series has both trend and yearly seasonality, so both need to be removed for stationarity.
  2. Step 2: Choose differencing methods

    First-order differencing removes trend; seasonal differencing with lag 12 removes yearly seasonality.
  3. Step 3: Combine differencing steps

    Applying first-order differencing then seasonal differencing is the correct approach to achieve stationarity.
  4. Final Answer:

    Apply first-order differencing followed by seasonal differencing with lag 12 -> Option A
  5. Quick Check:

    Trend + seasonality -> first + seasonal differencing [OK]
Hint: Remove trend then seasonality with two differencing steps [OK]
Common Mistakes:
  • Applying only one differencing type ignoring trend or seasonality
  • Using log transform alone to fix non-stationarity
  • Confusing seasonal lag with differencing order