Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
494 views
in Technique[技术] by (71.8m points)

Python Time Series has been differenced, how do I undifference to make the values normal again

I'm working on a time series, it has a index as dates and a values field. I used these 2 lines to difference the data.

df['value2'] = (df['value'] - df.value.rolling(window=12).mean()) / df.value.rolling(window=12).std()
df['value3'] = df['value2'] - df['value2'].shift(12)

This made my dataset stationary, so i'm happy to continue using this.

Now I have ran some analysis from this and now I have values which I'm trying to undifference.

If my result dataset is saved in df_results, how do I make these normal again (undifference them). Is there a way to reverse the transformations?

** SOLUTION **

I figured out a way to reverse the differencing on the dataset.

# DIFFERENCING    
df['stp1'] = (df['cpi'] - df.cpi.rolling(window=12).mean()) 
df['stp2'] = df['stp1'] / df.cpi.rolling(window=12).std()
df['stp3'] = df['stp2'] - df['stp2'].shift(12)


# INVERSE DIFFERENCING
df['stp3r'] = df['stp3'] + df['stp2'].shift(12)
df['stp2r'] = df['stp3r'] * df.cpi.rolling(window=12).std()
df['stp1r'] = (df['stp2r'] + df.cpi.rolling(window=12).mean()) 

In order to apply this to a forecasted dataset I followed a very similar way. In this the only variable changed is 'wmar' which where the differenced forecast is saved, the last data field 'fcast3' is where the reversed differenced forecast exists:

df['fcast'] = wmar + df['stp2'].shift(12)
df['fcast2'] = df['fcast'] * df.cpi.rolling(window=12).std()
df['fcast3'] = (df['fcast2'] + df.cpi.rolling(window=12).mean()) 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...