Skip to content

Instantly share code, notes, and snippets.

@thistleknot
Last active December 2, 2022 02:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thistleknot/29fd85ec48468b509f836b8cbf24763c to your computer and use it in GitHub Desktop.
Save thistleknot/29fd85ec48468b509f836b8cbf24763c to your computer and use it in GitHub Desktop.
seasonal and non seasonal undifferencing
def undiff(data, seasonal, nonseasonal, xi):
print("you have to know what xi for which use case you are going to use")
#nonseasonal
if(nonseasonal!=0 and seasonal==0):
temp = np.concatenate([np.array(xi),np.array(data)])
temp_ = diff_inv(temp,1,nonseasonal)
return(temp_[-len(data):])
#seasonal
if(seasonal!=0 and nonseasonal == 0):
temp = np.concatenate([np.array(xi),np.array(data)])
temp_ = diff_inv(temp,season,1)
return(temp_[-len(data):])
#both
if(seasonal==1 and nonseasonal == 1):
temp_ = np.concatenate([np.array(xi),np.array(data)])
initial_seasonal_delta = xi.iloc[season]-xi.iloc[0]
s_diffed_cat = np.concatenate([[np.array(initial_seasonal_delta)],np.array(data.dropna())])
ns_undiffed = diff_inv(s_diffed_cat,1,1)[-len(temp_):]
ns_diffed_cat = np.concatenate([np.array(xi[1:(season+1)]),np.array(ns_undiffed)])
s_undiffed = diff_inv(ns_diffed_cat,season,1)[-len(ns_undiffed):]
return(s_undiffed[-len(data):])
def difference(data, seasonal, nonseasonal):
if seasonal > 0:
return(data.diff(periods=season).diff(nonseasonal))
elif season > 0:
return(data.diff(nonseasonal))
else:
return(data)
#non seasonal
#undiff(temp_test['target'].dropna(), 0, nonseasonal,[raw_int[chosen].loc[temp_train['target'].index[-1]]])
#test
#undiff(temp_test[chosen],seasonal,nonseasonal,raw_int[chosen].loc[temp_train['target'].index[-1:]])
#train
#train_prior_date = raw_int[chosen].index[np.argwhere(data_final.index==temp_train['target'].index[0]).ravel()[0]-nonseasonal]
#train_xi=[raw_int[chosen].loc[train_prior_date]]
#undiff(temp_train['target'], seasonal, nonseasonal,xi)
#seasonal
#undiff(raw_int[chosen].diff(periods=4).dropna(),1,0,raw_int[chosen][0:season])
#non seasonal and seasonal
#undiff(raw_int[chosen].diff(periods=4).diff().dropna(),seasonal,nonseasonal,raw_int[chosen][0:season+nonseasonal])
@thistleknot
Copy link
Author

thistleknot commented Dec 2, 2022

diff_inv([*np.log(weighted_portfolio).values[0:2],*np.log(weighted_portfolio).diff(2).dropna().values],1,2)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment