# How to “undifference” a time series variable

I need to "undifference" or "integrate" a time series variable. In its current state, it is twice-differenced (a money market, cash return proxy variable that was I(2) to achieve stationarity). I simulated the variable, along with others, to generate 100,000 observations from a fitted copula, so I have no initial reference value to start off the "undifferencing" function as in R's diffinv() function.

Here is a simplified problem to illustrate (using R):

Create an arbitrary original series:

undifferenced = c(0.5, -0.1, 0.2, 0.08, -0.02)

Difference the series:

differenced = diff(undifferenced)

Output: -0.60  0.30 -0.12 -0.10


Attempt to "undifference" the series:

diffinv(differenced)

Output: 0.00 -0.60 -0.30 -0.42 -0.52,


which is not the original series

Or if you want to remove the 0.00 to clean up:

diffinv(differenced)[-1]

Output: -0.60 -0.30 -0.42 -0.52,


which in any event is not the original series

If you "somehow" know, a priori, the initial value of the original series, the output is correct:

diffinv(fff, xi = 0.5)

Output: 0.50 -0.10  0.20  0.08 -0.02,


which IS the original series.

In my simulation, I have no way of specifying xi.

At this point, if anyone has any experience "undifferencing" correctly or could help figure out the problem, I would appreciate any help.

As an aside, for non-R users, I can export the data to work with in Excel.

@Andy @Glen_b I appreciate the help, thank you. I'm gna evaluate under the pretext that the mean of the original series is a good enough proxy to initialize the diffinv() function (in a context of simulation where you don't have an actual original value to initialize). The assumption then that the simulated differenced data is of the same quality as the original differenced data.

A problem came up though in initializing xi = mean(undifferenced) - this scalar value works on a smaller set of observations (~240 obs), but the scalar doesn't let the diffinv() function operate on the simulated series of 100,000 observations. R's help file describes xi as a "numeric vector, matrix, or time series containing the initial values for the integrals". Any ideas or reference? There's no guidance of what the length the xi vector should be and I would've thought just one scalar would suffice in kicking off the undifferencing process.

• Consider what differences you would get if you had added some constant $c$ to all the numbers in your original series (e.g. try $c=1,10,-13,\sqrt{\pi},...$). Note that the differences are the same no matter what $c$ is. Therefore, you can't recover the correct value of $c$ from the differences. You eliminated some information by taking differences - how big the original numbers were. If you don't keep at least one original data value (or some other way to get the level they were at), then the differences can't get you there. The easiest is probably to keep the first value. – Glen_b Dec 28 '14 at 0:08

I'm afraid you've already hit upon the easiest solution, which is to pick a xi, aka the constant of integration. The alternative is to phrase any question you ask about your "undifferenced" sequence such that the constant of integration doesn't matter.

The underlying problem is that "differencing" maps many sequences of values to the same sequence of differences. If $x_n$ is your sequence, then for any constant $C$, the sequence $x_n+ C$ will have the same differences.

• Hi Andy, I did consider initializing an "aribtrary" constant of integration, i.e. picking a random xi . If I do this, however, I believe I get an undifferenced series with meaningfully dissimilar characteristics to the original series. By "dissimilar characteristics", I compared the means and variances of the original series to the diffinv( aribtrary xi ) series and they were quite different. So I'm a bit hesitant to pick a xi . – GMan Dec 27 '14 at 16:06
• By "pick" I meant "pick the correct xi using information from your source data". And picking xi or asking only questions that don't care about it are mathematically the only things you can do. There's no third way. – Andy Jones Dec 27 '14 at 16:16
• Incidentally, the variances of the original series and the reconstructed series should be the same since $\text{var}(X + a) = \text{var}(X)$ – Andy Jones Dec 27 '14 at 16:17
• Yes, we have the same idea about picking xi from the original (undifferenced) source data. So, interestingly, in terms of picking the right xi, I took the mean of the undifferenced series and used it in diffinv(differenced, xi = mean(undifferenced)). The output is different to the original, the mean is different to the original, but the variance is identical to the variance of the original. – GMan Dec 27 '14 at 16:52
• Yeah. The variance is the same because $\text{var}(X + a) = \text{var}(X)$, but the mean is different because $\mathbb{E}[X + a] = \mathbb{E}[X] + a$ – Andy Jones Dec 27 '14 at 16:55

Instead of doing diff() with the actual time series data, use instead the d parameter in auto.arima function to define it. lets say your data series is val.ts and you want to do differencing only until first order to make your series stationary, then instead of using auto.arima(diff(val.ts)), do auto.arima(val.ts,d=1). I have answered it here to my actual question here Convert double differenced forecast into actual value

Since the series was twice-differenced, you need the first two values of the original source data.

You mention that it is a money market, cash return proxy variable. Shouldn't it be possible to get the source data?

In your simulation, I'm assuming you are generating 100,000 series that have similar characteristics (using the fitted copula) as the twice-differenced data? You just need to choose your xi values to "re-calibrate" your differencing series back to be similar to the original.

In your example, the 0.5 is the initial value to start at when adding each differencing step back.

If I generated 100,000 differencing series, I would set xi to 0.5 to have the same starting point as the original.