Created
September 16, 2021 16:01
-
-
Save h3ik0th/8296ceb3d3700f771024f9341e99cb3b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Are Tyrion and Cersei stationary processes? | |
# ADF and KPSS tests: | |
def ADF_test(x): | |
adf_result = adfuller(x.values) | |
t_stat, p_value, _, _, critical_values, _ = adf_result | |
print(f'ADF statistic: {t_stat:.2f}') | |
#for key, value in critical_values.items(): | |
# print('critical values:') | |
# print(f' {key}, {value:.2f}') | |
print(f'AFD: p-value: {p_value:.2f}') | |
# ADF null hypothesis: There is a unit root and the series is non-stationary. | |
print("ADF: non-stationary - unit root" if p_value >= ALPHA else "ADF: stationary or only difference-stationary") | |
boo_ADF = (p_value < ALPHA) # True: (difference-)stationary | |
return boo_ADF | |
def KPSS_test(x): | |
kpss_result = kpss(x.values) | |
t_stat, p_value, _, critical_values = kpss_result | |
print(f'KPSS statistic: {t_stat:.2f}') | |
# for key, value in critical_values.items(): | |
# print('critical Values:') | |
# print(f' {key}, {value:.2f}') | |
print(f'KPSS: p-value: {p_value:.2f}') | |
# KPSS null hypothesis: The series is at least trend-stationary. | |
print("KPSS: non-stationary - unit root") if p_value < ALPHA else print("KPSS: stationary or only trend-stationary") | |
boo_KPSS = (p_value >= ALPHA) # True: (trend-)stationary) | |
return boo_KPSS | |
# combine the ADF and KPSS results and derive the recommendation | |
def is_Stationary(x): | |
boo_ADF = ADF_test(x) | |
boo_KPSS = KPSS_test(x) | |
n_diffscore = boo_KPSS *10 + boo_ADF*1 # combine the KPSS and ADF result in a single score with 4 possible values: 0,1,10,11 | |
diff_eval = EvalDiff(n_diffscore) # get the recommendation statement | |
print(diff_eval) | |
return diff_eval | |
# translate the test score into a recommendation statement | |
def EvalDiff(diffscore): | |
# 4 possible combinations of KPSS and ADF test results | |
ADF1_KPSS1 = "Stationary: ADF and KPSS in agreement." | |
ADF0_KPSS1 = """ADF finds a unit root; but KPSS finds that the series is stationary | |
around a deterministic trend. Thus, the series is only trend-stationary. Difference it (or de-trend it by other methods).""" | |
ADF1_KPSS0 = """ADF does not find a unit root; but KPSS finds that it is non-stationary. | |
Thus, the series is only difference-stationary. Difference it.""" | |
ADF0_KPSS0 = "Not stationary. KPSS and ADF in agreement. Difference it." | |
switcher={ | |
11:ADF1_KPSS1, | |
10:ADF0_KPSS1, | |
1:ADF1_KPSS0, | |
0:ADF0_KPSS0 | |
} | |
return switcher.get(diffscore, "invalid") | |
# the following pmdarima method returns the recommended order of differencing, based on the larger result of ADF or KPSS | |
def nDiff(x): | |
n_adf = ndiffs(x, max_d=4, test="adf") | |
n_kpss = ndiffs(x, max_d=4, test="kpss") | |
n_diff = max(n_adf, n_kpss) | |
return n_diff |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment