Skip to content

Instantly share code, notes, and snippets.

@AVJdataminer
Last active May 20, 2020 17:47
Show Gist options
  • Save AVJdataminer/425ebc0b421c398d5e59e5254f01700b to your computer and use it in GitHub Desktop.
Save AVJdataminer/425ebc0b421c398d5e59e5254f01700b to your computer and use it in GitHub Desktop.
# Make a function to find the MSE of a single ARIMA model
def evaluate_arima_model(data, arima_order):
# Needs to be an integer because it is later used as an index.
split=int(len(data) * 0.8)
train, test = data[0:split], data[split:len(data)]
past=[x for x in train]
# make predictions
predictions = list()
for i in range(len(test)):#timestep-wise comparison between test data and one-step prediction ARIMA model.
model = ARIMA(past, order=arima_order)
model_fit = model.fit(disp=0)
future = model_fit.forecast()[0]
predictions.append(future)
past.append(test[i])
# calculate out of sample error
error = mean_squared_error(test, predictions)
return error
# Make a function to evaluate different ARIMA models with several different p, d, and q values.
def evaluate_models(dataset, p_values, d_values, q_values):
best_score, best_cfg = float("inf"), None
for p in p_values:
for d in d_values:
for q in q_values:
order = (p,d,q)
try:
mse = evaluate_arima_model(dataset, order)
if mse < best_score:
best_score, best_cfg = mse, order
print('ARIMA%s MSE=%.3f' % (order,mse))
except:
continue
return print('Best ARIMA%s MSE=%.3f' % (best_cfg, best_score))
# Now, we choose a couple of values to try for each parameter.
p_values = [x for x in range(0, 4)]
d_values = [x for x in range(0, 1)]
q_values = [x for x in range(15, 20)]
# Finally, we can find the optimum ARIMA model for our data.
# Nb. this can take a while...!
import warnings
warnings.filterwarnings("ignore")
y_log = np.log(y)
evaluate_models(y_log, p_values, d_values, q_values)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment