Today or Tomorrow? Understanding Intertemporal Consumption and Savings
This article is inspired by a project I was working on, where, after the second differencing, the data was still not stationary, and that means further differencing could lead to the loss of the relationship between variables.
Introduction
Time series data is often said to be nonstationary most of the time because of naturally occurring patterns, which implies that the data does not have a constant mean or variance over time.
Non stationarity refers to the varying statistical properties of data. When this nature of data and use this data for regression, this can lead to spurious regression and therefore make our regression results invalid. This is where differencing and detrending come in to “fix” the data, however they do not do the the same thing. The choice is made between the two based on the nature of the data and the question at hand.
Differencing
In simple statistical terms this refers to removing the unit root.
Differencing refers to subtracting a variable from its own past variables.
-
Lets say we are working with a time series dataset of GDP. When we get the first different of GDP we not only make the series stationary we also get the GDP growth.
-
Lets see an example of how differencing is done in python.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
### First import the necessary libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# We then simulate a non-stationary time series (trend + noise)
np.random.seed(42)
n = 100
time = np.arange(n)
trend = 0.5 * time # deterministic upward trend
noise = np.random.normal(0, 2, n) # random noise
series = trend + noise
# Put in pandas Series
data = pd.Series(series, index=pd.RangeIndex(start=1, stop=n+1), name="Y_t")
# Apply first differencing
diff_data = data.diff().dropna()
# Plot original vs differenced
plt.figure(figsize=(12,5))
plt.subplot(1,2,1)
plt.plot(data, label="Original series (Y_t)")
plt.title("Non-Stationary Series")
plt.xlabel("Time")
plt.ylabel("Value")
plt.legend()
plt.subplot(1,2,2)
plt.plot(diff_data, color="orange", label="Differenced series (ΔY_t)")
plt.title("First Difference")
plt.xlabel("Time")
plt.ylabel("Value")
plt.legend()
plt.tight_layout()
plt.show()
You will get the plot below showing a stationary and a non stationary timeseries plot
-
On the left side, we have the original time series which seems to have an upward trend (non-stationary).
-
On the right we have a differenced series where the unit root has been removed, the series no longer has a trend and fluctuates around zero, which makes it almost stationary.
When to difference:
-
When the data have a stochastic trend (a random walk). e.g macroeconomic datasets stock prices,and exchange rates.
-
You’re interested in short-term changes rather than long-run levels.
One major draw back of differencing is that it eliminates long-run information therefore one may not really get accurate results when testing for long-run relationships.If you difference GDP for example, you lose the trend that reflects actual economic growth.
Detrending
Detrending means fitting and subtracting a smooth function (like a linear or quadratic trend) from the data:
the second term is now the stationary term
When to detrend:
-
When the data trend is deterministic (e.g., GDP has a predictable long-run growth path).
-
You care about long-run growth vs short-run fluctuations.
Example: removing a time trend from productivity data to study business cycle dynamics.
Detrending has a disadvantage. It assumes the trend is predictable and stable. If the series has a unit root, detrending won’t solve the problem.
Detrending removes a deterministic trend (like a linear growth path) from a series.
Here is an illustration of detrending on simulated data
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.signal import detrend
# Simulate GDP-like data (linear trend + noise)
np.random.seed(42)
n = 100
time = np.arange(n)
trend = 0.5 * time
noise = np.random.normal(0, 2, n)
series = trend + noise
# Put in pandas Series
data = pd.Series(series, index=pd.RangeIndex(start=1, stop=n+1), name="Y_t")
# Apply detrending (remove linear trend)
detrended = detrend(series)
# Plot original vs detrended
plt.figure(figsize=(12,5))
plt.subplot(1,2,1)
plt.plot(data, label="Original series (Y_t)")
plt.title("Non-Stationary Series with Trend")
plt.xlabel("Time")
plt.ylabel("Value")
plt.legend()
plt.subplot(1,2,2)
plt.plot(detrended, color="green", label="Detrended series (u_t)")
plt.title("Detrended Series")
plt.xlabel("Time")
plt.ylabel("Value")
plt.legend()
plt.tight_layout()
plt.show()
The output of the above code is graphically represented below
-
The original series on the left has a clear upward deterministic trend.
-
The detrended series on the right oscillates around zero after removing the linear growth path.
Conclusion
Both differencing and detrending are powerful tools for handling non-stationary economic data, but they serve different purposes. Differencing helps when a series behaves like a random walk, while detrending is more useful when the data follow a predictable long-run growth path. The choice ultimately depends on both the statistical properties of your data and the economic question you want to answer.
In practice, economists often experiment with both approaches, checking residuals and stationarity tests to guide their decision. By understanding when to difference and when to detrend, you not only avoid spurious regressions but also preserve the right kind of information for your analysis—whether it’s short-term changes, long-run cycles, or policy impacts.
The next time you work with GDP, inflation, or financial series, remember: the right transformation can make the difference between misleading noise and meaningful economic insight.