Created
August 6, 2019 17:48
-
-
Save marcopeix/ee23ca16ad9516e5feb0de8b77748e9e 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
#Set initial values and some bounds | |
ps = range(0, 5) | |
d = 1 | |
qs = range(0, 5) | |
Ps = range(0, 5) | |
D = 1 | |
Qs = range(0, 5) | |
s = 5 | |
#Create a list with all possible combinations of parameters | |
parameters = product(ps, qs, Ps, Qs) | |
parameters_list = list(parameters) | |
len(parameters_list) | |
# Train many SARIMA models to find the best set of parameters | |
def optimize_SARIMA(parameters_list, d, D, s): | |
""" | |
Return dataframe with parameters and corresponding AIC | |
parameters_list - list with (p, q, P, Q) tuples | |
d - integration order | |
D - seasonal integration order | |
s - length of season | |
""" | |
results = [] | |
best_aic = float('inf') | |
for param in tqdm_notebook(parameters_list): | |
try: model = sm.tsa.statespace.SARIMAX(data.CLOSE, order=(param[0], d, param[1]), | |
seasonal_order=(param[2], D, param[3], s)).fit(disp=-1) | |
except: | |
continue | |
aic = model.aic | |
#Save best model, AIC and parameters | |
if aic < best_aic: | |
best_model = model | |
best_aic = aic | |
best_param = param | |
results.append([param, model.aic]) | |
result_table = pd.DataFrame(results) | |
result_table.columns = ['parameters', 'aic'] | |
#Sort in ascending order, lower AIC is better | |
result_table = result_table.sort_values(by='aic', ascending=True).reset_index(drop=True) | |
return result_table | |
result_table = optimize_SARIMA(parameters_list, d, D, s) | |
#Set parameters that give the lowest AIC (Akaike Information Criteria) | |
p, q, P, Q = result_table.parameters[0] | |
best_model = sm.tsa.statespace.SARIMAX(data.CLOSE, order=(p, d, q), | |
seasonal_order=(P, D, Q, s)).fit(disp=-1) | |
print(best_model.summary()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment