def finalize(filename=None): dataset = Series.from_csv('Krakow_data.csv') X = dataset.values.astype('float32') history = [x for x in X] months_in_year = 12 diff = difference.difference(X, months_in_year) model = ARIMA(diff, order=(6, 0, 2)) model_fit = model.fit(trend='nc', disp=0) model_fit.save('model.pkl') np.save('model_bias.npy', [0.237574]) validation = Series.from_csv('Krakow_validation.csv') y = validation.values.astype('float32') model_fit = ARIMAResults.load('model.pkl') bias = np.load('model_bias.npy') predictions = list() yhat = float(model_fit.forecast()[0]) yhat = bias + difference.inverse_difference(history, yhat, months_in_year) predictions.append(yhat) history.append(y[0]) label_for_x = [ '2017-06', '2017-07', '2017-08', '2017-09', '2017-10', '2017-11', '2017-12', '2018-01', '2018-02', '2018-03', '2018-04', '2018-05', '2018-06', '2018-07', '2018-08' ] print('For month: {} Predicted= {} , Expected= {}'.format( label_for_x[0], str(yhat), str(y[0]))) for i in range(1, len(y)): months_in_year = 12 diff = difference.difference(history, months_in_year) model = ARIMA(diff, order=(6, 0, 2)) model_fit = model.fit(trend='nc', disp=0) yhat = model_fit.forecast()[0] yhat = bias + difference.inverse_difference(history, yhat, months_in_year) predictions.append(yhat) obs = y[i] history.append(obs) print('For month: {} Predicted= {} , Expected= {}'.format( label_for_x[i], str(yhat), str(obs))) mse = mean_squared_error(y, predictions) rmse = sqrt(mse) print('RMSE: {:0.3f}'.format(rmse)) plt.figure(figsize=(10, 7)) plt.title('Forecast for temperature in Kraków') plt.xlabel('Data') plt.ylabel('Temperature') plt.xticks(list(range(0, 15)), label_for_x) plt.xticks(rotation=45) plt.grid() plt.plot(y, label="Observational values") plt.plot(predictions, color='red', label="Foreseen values") plt.legend() plt.savefig(filename) return rmse
def differences(query): import difference as dif ans = "<NA>" getAns= dif.difference(query) if getAns: ans = [{"differences":getAns}] else: ans =[{}] return ans
def forecast(series, filename): X = series.values months_in_year = 12 differenced = np.array(difference.difference( X, months_in_year)) #compare current value with last year value # fit model model = ARIMA(differenced, order=(6, 0, 2)) #adjust model to dataset model_fit = model.fit(disp=0) # multi-step out-of-sample forecast forecast = model_fit.forecast(steps=12)[0] # invert the differenced forecast to something usable history = [x for x in X] #generate monthly data times = list(pd.date_range('2018-09-01', periods=12, freq='MS')) month = 1 for yhat in forecast: inverted = difference.inverse_difference(history, yhat, months_in_year) print('Month {:%Y-%m} Predicted temperature: {:.2f}'.format( times[month - 1], inverted)) history.append(inverted) month += 1 #plotting plt.clf() plt.figure(figsize=(10, 7)) plt.grid() plt.title( 'Multi-step out-of-sample forecast for Kraków within 12 upcoming months' ) plt.xlabel('Date') plt.ylabel('Temperature [°C]') plt.plot(times, history[len(X):], label='Average temperature' ) #we want to plot only the last 12 predicted values plt.legend() RMSE = 3.114 plt.fill_between(times, [(item - RMSE) for item in history[len(X):]], [(item + RMSE) for item in history[len(X):]], color='k', alpha=.15) plt.xticks(rotation=45) plt.yticks([item for item in range(-6, 26, 2)]) plt.savefig(filename)
def Dickey_Fuller_test(series, filename_csv, filename_plot): X = series.values X = X.astype('float32') months_in_year = 12 stationary = Series(difference.difference(X, months_in_year)) stationary.index = series.index[months_in_year:] result = adfuller(stationary) print('ADF Statistic: %f' % result[0]) print('p-value: %f' % result[1]) print('Critical Values:') for key, value in result[4].items(): print('\t%s: %.3f' % (key, value)) stationary.to_csv(filename_csv) plt.clf() plt.figure(figsize=(10, 7)) stationary.plot() plt.grid() plt.title('Seasonal differenced temperature line plot') plt.ylabel('Temperature [°C]') plt.savefig(filename_plot)
import shlex, sys, difference if __name__ == '__main__': ab = shlex.split(file(sys.argv[1]).read()) sys.stdout.write(str(difference.difference(ab[0], ab[1])))
import shlex, sys, difference if __name__ == "__main__": ab = shlex.split(file(sys.argv[1]).read()) sys.stdout.write(str(difference.difference(ab[0], ab[1])))