# Shall we use log(diff(x)) or diff(log(x))?

I am starting to learn time series and when detrending I always end up with the same doubt...

Generally, I use diff() for, let's say there is an upward trend like inflation... and I use log() to stabilise the variance.

My question is: given time series x, is there a rule when we shall do

log(diff(x))


diff(log(x))


(code is in R language).

• I'm pretty sure you'd want log first, as the diff could be negative – MikeP Dec 2 at 13:30
• Note: diff is fine for getting rid of stochastic trends, but not of deterministic ones. In the latter case, it introduces a unit-root moving average component (something you do not want). – Richard Hardy Dec 2 at 13:53

Like always it depends on what you want to do.

## diff(log(x))

diff(log(x)) calculates relative changes. This also takes care of exponential trends. For example, you would use this to detrend the stock price development of Google. According to the logarithms laws: $$log(a) - log(b) = log(a/b)$$

all.equal(log(3) - log(5), log(3/5))


This means, instead of using the absolute difference for detrending you are using the relative change. As a bonus differences calculated using the natural logarithm can also be interpreted as a precentage change. For more information I recommend:

Cole, T. J., & Altman, D. G. (2017). Statistics Notes: Percentage differences, symmetry, and natural logarithms. BMJ, 358(August), j3683. https://doi.org/10.1136/bmj.j3683

## log(diff(x))

On the other hand log(diff(x)) calculates the absolute differences before the logarithm is applied. If you calculate a trend using this method, the trend would be more outlier resistant (but this also applies to diff(log(x))). This is helpful if there are a small number of big jumps in the time-series. Beware this method would potentially break your analyses when the difference is 0 or negative. (in R: log(0) = -Inf or log(-1) = NaN)

In my opinion diff(log(x)) is the better default choice. While there probably is a use-case for log(diff(x)), it's quite hard to think of one.