2017-05-31 55 views
0

我使用的是statsmodels.tsa.arima_model.ARIMA,我在插入算法之前對內生變量進行了平方根變換。該機型採用的1階差分:如何撤消平方根變換變量的差分(滯後階數1)? [ARIMA()]

model = ARIMA(sj_sqrt, order=(2, 1, 0)) 

擬合模型,並抓住了預測之後,我想提出的預測回到原來的形式與原數據進行比較。但是,我似乎無法正確轉換它們。

複製此問題的一個簡單的版本,這裏是一些代碼:

#original data: 
test = pd.Series([1,1,1,50,1,1,1,1,1,1,1,1,40,1,1,2,1,1,1,1,1]) 

#sqrt transformed data: 
test_sqrt = np.sqrt(test) 

#sqrt differenced data: 
test_sqrt_diff = test_sqrt.diff(periods=1) 

#undo differencing: 
test_sqrt_2 = cumsum(test_sqrt_diff) 

#undo transformations: 
test_2 = test_sqrt_2 ** 2 

f, axarr = plt.subplots(5, sharex=True, sharey=True) 
axarr[0].set_title('original data:') 
axarr[0].plot(test) 
axarr[1].set_title('sqrt transformed data:') 
axarr[1].plot(test_sqrt) 
axarr[2].set_title('sqrt differenced data:') 
axarr[2].plot(test_sqrt_diff) 
axarr[3].set_title('differencing undone with .cumsum():') 
axarr[3].plot(test_sqrt_2) 
axarr[4].set_title('transformation undone by squaring:') 
axarr[4].plot(test_2) 
f.set_size_inches(5, 12) 

您可以從該非差,未轉換的數據是不太相同的規模圖表中看到。 test[3]回報50,並test_2[3]回報36.857864376269056enter image description here

回答

0

解決方案:

## original 
x = np.array([1,1,1,50,1,1,1,1,1,1,1,1,40,1,1,2,1,1,1,1,1]) 

## sqrt 
x_sq = np.sqrt(x) 

## diff 
d_sq = np.diff(x_sq,n=1) 

## Only works when d = 1 
def diffinv(d,i): 
    inv = np.insert(d,0,i) 
    inv = np.cumsum(inv) 
    return inv 

## inv diff 
y_sq = diffinv(d_sq,x_sq[0]) 

## Check inv diff 
(y_sq==x_sq).all()