def test_non_seasonal_arima3(self): ts_data = self.getData4() f_name='non_seasonal_arima3.pmml' model = ARIMA(ts_data,order=(1, 0, 1)) result = model.fit(trend = 'c', method = 'css-mle') StatsmodelsToPmml(result, f_name, description="A test model") self.assertEqual(os.path.isfile(f_name),True)
def test_non_seasonal_arima1(self): ts_data = self.getData4() f_name='non_seasonal_arima1.pmml' model = ARIMA(ts_data,order=(9, 2, 0)) result = model.fit(trend = 'c', method = 'css-mle') StatsmodelsToPmml(result, f_name, model_name="arima_920") self.assertEqual(os.path.isfile(f_name),True)
def test_non_seasonal_arima8(self): ts_data = self.statsmodels_data_helper.get_non_seasonal_data() f_name = 'non_seasonal_arima8.pmml' model = ARIMA(ts_data, order=(5, 1, 2)) result = model.fit(trend='c', method='mle') ArimaToPMML(result, f_name, conf_int=[80, 95]) self.assertEqual(self.schema.is_valid(f_name), True)
def test_non_seasonal_arima2(self): ts_data = self.getData4() f_name = 'non_seasonal_arima2.pmml' model = ARIMA(ts_data, order=(9, 2, 3)) result = model.fit(trend='nc', method='css-mle') ArimaToPMML(result, f_name, description="A test model") self.assertEqual(os.path.isfile(f_name), True)
def test_non_seasonal_arima1(self): ts_data = self.statsmodels_data_helper.get_non_seasonal_data() f_name = 'non_seasonal_arima1.pmml' model = ARIMA(ts_data, order=(9, 2, 0)) result = model.fit(trend='c', method='css-mle') ArimaToPMML(result, f_name) self.assertEqual(self.schema.is_valid(f_name), True)
def test_non_seasonal_arima7(self): ts_data = self.statsmodels_data_helper.get_non_seasonal_data() f_name = 'non_seasonal_arima7.pmml' model = ARIMA(ts_data, order=(5, 1, 2)) result = model.fit(trend='nc', method='mle') StatsmodelsToPmml(result, f_name) self.assertEqual(self.schema.is_valid(f_name), True)
def predict_by_samples(data, test_data, train_length, num_sample, output_steps, lag_order=1, if_sample=True): warnings.filterwarnings("ignore") test_num, data_dim = test_data.shape if not if_sample: num_sample = data_dim #widgets = ['Train: ', Percentage(), ' ', Bar('-'), ' ', ETA()] #pbar = ProgressBar(widgets=widgets, maxval=test_data.shape[0]-output_steps).start() error_all = [] index_all = np.zeros([test_num - output_steps, num_sample], dtype=np.int32) valid_num = np.zeros(test_num - output_steps, dtype=np.int32) real = np.zeros([test_num - output_steps, num_sample]) predict = np.zeros([test_num - output_steps, num_sample]) # samples = np.arange(data_dim) # for t in range(test_num - output_steps): #pbar.update(t) if t % 100 == 0: print(t) error_index = [] if if_sample: samples = np.random.randint(data_dim, size=num_sample) for r in range(num_sample): # t: which time slot # i: which station i = samples[r] train_df = data[t:train_length + t, i] try: results = ARIMA(train_df, order=(lag_order, 0, 1)).fit(trend='nc', disp=0) except: error_index.append(r) continue #pre, _, _ = results.forecast(output_steps) #pre = results.predict(train_length, train_length+output_steps) pre = results.predict(train_length, train_length) test_real = test_data[t:t + output_steps, i] real[t, r] = test_real #print(pre) predict[t, r] = pre index_all[t] = samples error_all.append(error_index) valid_num[t] = num_sample - len(error_index) #pbar.finish() return real, predict, index_all, error_all, valid_num
def train(self, array_X, array_Y): self.train_X = array_X self.train_Y = array_Y array = numpy.concatenate((numpy.array([array_Y]).T, array_X), axis=1) idx = pd.date_range('20130101', periods=48000) #xxx= pd.DataFrame(data=numpy.array([array_Y]),index=idx) xxx = pd.DataFrame(data=array, index=idx) model = ARIMA(endog=xxx, order=(0, 1, 1)) fit = model.fit() res = fit.fittedvalues.values[:, 0] res = numpy.hstack((res[0], res)) return res
def arima(time_serie, Npt, order, simple_ticker, fit: bool, save: bool, load: bool, path): filename = path+'/Resources/TunnedModels/' + simple_ticker + '_arima_2020_07_31.pickle' if fit: arima_fit = ARIMA(time_serie.values.astype('float'), order).fit(disp=False) if save: # save the model to disk pickle.dump(arima_fit, open(filename, 'wb')) elif load: # load the model from disk arima_fit = pickle.load(open(filename, 'rb')) else: print("Error: fit and load parameters are both false.") sys.exit(-1) forecast_arima = [x for x in arima_fit.forecast(Npt)[0]] # [0]= predições return pd.Series(forecast_arima)
def predict(self, test_X, test_Y): predictions = numpy.empty(0) array_train = numpy.concatenate( (numpy.array([self.train_Y]).T, self.train_X), axis=1) array_test = numpy.concatenate((numpy.array([test_Y]).T, test_X), axis=1) for t in range(0, test_Y.shape[0]): array = numpy.vstack((array_train, array_test[:t])) model = ARIMA(endog=pd.DataFrame(data=array)) fit = model.fit() lag = fit.k_ar pred = fit.forecast(array[-lag:], 1)[0] predictions = numpy.append(predictions, pred[0]) return predictions
def build_model(self): """Build ARIMA model with 3 hyperparameters See https://en.wikipedia.org/wiki/Autoregressive_integrated_moving_average for detailed information """ self.p = self.para['p'] self.d = self.para['d'] self.q = self.para['q'] self.model = ARIMA(self.train, order=(self.p, self.d, self.q), exog=self.exog_train)