Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement