Skip to content

Instantly share code, notes, and snippets.

@dpbac
Created June 22, 2021 18:11
Show Gist options
  • Select an option

  • Save dpbac/9f2387d9069dfddb7dbf1b7cfabfb3b2 to your computer and use it in GitHub Desktop.

Select an option

Save dpbac/9f2387d9069dfddb7dbf1b7cfabfb3b2 to your computer and use it in GitHub Desktop.
def obtain_adf_kpss_results(timeseries, max_d):
""" Build dataframe with ADF statistics and p-value for time series after applying difference on time series
Args:
time_series (df): Dataframe of univariate time series
max_d (int): Max value of how many times apply difference
Returns:
Dataframe showing values of ADF statistics and p when applying ADF test after applying d times
differencing on a time-series.
"""
results=[]
for idx in range(max_d):
adf_result = adfuller(timeseries, autolag='AIC')
kpss_result = kpss(timeseries, regression='c', nlags="auto")
timeseries = timeseries.diff().dropna()
if adf_result[1] <=0.05:
adf_stationary = True
else:
adf_stationary = False
if kpss_result[1] <=0.05:
kpss_stationary = False
else:
kpss_stationary = True
stationary = adf_stationary & kpss_stationary
results.append((idx,adf_result[1], kpss_result[1],adf_stationary,kpss_stationary, stationary))
# Construct DataFrame
results_df = pd.DataFrame(results, columns=['d','adf_stats','p-value', 'is_adf_stationary','is_kpss_stationary','is_stationary' ])
return results_df
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment