def forecast_markov(trainingdata, evaldata): n = len(evaldata) mod = mc.MarkovChain().from_data(trainingdata['count']) ids, states = mod.simulate(n, start=10) plot(f'markov_{n}.png', states, 'Forecast based on Markov Chains', evaldata['count'].values) print('Accuracy Scores for Markov Chains:') accuracy.eval_model(states, 1, evaldata['count'].values)
def forecast_sarimax(trainingdata, evaldata): n = len(evaldata) periods_in_season = 24 mod = sm.tsa.statespace.SARIMAX(trainingdata['count'].astype(float), order=(0, 0, 0), seasonal_order=(1, 0, 1, periods_in_season), enforce_stationarity=False) res = mod.fit(disp=False) print(res.summary()) predictions = res.forecast(n) print(predictions) accuracy.eval_model(predictions.values, 1, evaldata['count'].values) plot(f'sarimax/sarimax_s{periods_in_season}_{n}.png', predictions.values, 'Forecast based on the SARIMAX Model', evaldata['count'].values)
def forecast_linear_reg(trainingdata, evaldata): n = len(evaldata) to_timestamp_converter = lambda t: (t - np.datetime64('1970-01-01T00:00:00Z')) / np.timedelta64(1, 's') x = np.array([to_timestamp_converter(t) for t in trainingdata.index.values]).reshape((-1, 1)) print(x) y = np.array(trainingdata['count']) model = LinearRegression().fit(x, y) predictions = model.predict(x[:n]) plot(f'linear_reg_{n}.png', predictions, 'Forecast based on Linear Regression (OLS)', evaldata['count'].values) print('Summary') print(f'Coefficient of Determination: {model.score(x, y)}') print(f'Function: {model.coef_}t + {model.intercept_}') print('Accuracy Scores for OLS:') accuracy.eval_model(predictions, 1, evaldata['count'].values)
def forecast_neural(trainingdata, evaldata): scaler = MinMaxScaler(feature_range=(0, 1)) dataset = scaler.fit_transform(np.array(trainingdata['count'].astype(float)).reshape(-1, 1)) validationset = scaler.fit_transform(np.array(evaldata['count'].astype(float)).reshape(-1, 1)) look_back = 20 trainX, trainY = create_dataset(dataset, look_back) testX, testY = create_dataset(validationset, look_back) trainX = np.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1])) try: model = keras.models.load_model(f"lstm_network_{look_back}") print('Successfully loaded model.') except IOError: print('Creating new model.') model = Sequential() model.add(LSTM(4, input_shape=(1, look_back))) model.add(Dense(1)) model.compile(loss='mean_squared_error', optimizer='adam') model.fit(trainX, trainY, epochs=100, batch_size=1, verbose=2) model.save(f'lstm_network_{look_back}') # make predictions # trainPredict = model.predict(trainX) # testPredict = model.predict(testX) prediction_list = trainX[-1] for _ in range(len(evaldata)): x = prediction_list[-look_back:] x = x.reshape((1, 1, look_back)) out = model.predict(x)[0][0] prediction_list = np.append(prediction_list, out) prediction_list = prediction_list[look_back:] # invert predictions print(np.shape(testX)) print(testX) testPredict = scaler.inverse_transform(prediction_list.reshape(-1, 1)) testX = scaler.inverse_transform(testX) accuracy.eval_model(testPredict[:,0], 1, evaldata['count'].values) plot(f'lstm_l{look_back}_{len(testPredict[:,0])}.png', testPredict[:,0], 'Forecast based on a LSTM Neural Network', evaldata['count'].values)
def forecast_neural_timestamp(trainingdata, evaldata): scaler = MinMaxScaler(feature_range=(0, 1)) trainY = scaler.fit_transform(np.array(trainingdata['count'].astype(float)).reshape(-1, 1)) trainX = scaler.fit_transform(np.array(trainingdata.index.values.astype(float)).reshape(-1, 1)) testY = scaler.fit_transform(np.array(evaldata['count'].astype(float)).reshape(-1, 1)) testX = scaler.fit_transform(np.array(trainingdata.index.values.astype(float)).reshape(-1, 1)) try: model = keras.models.load_model(f"lstm_network_timestamp") print('Successfully loaded model.') except IOError: print('Creating new model.') model = Sequential() model.add(LSTM(4, input_shape=(1, 1))) model.add(Dense(1)) model.compile(loss='mean_squared_error', optimizer='adam') model.fit(trainX, trainY, epochs=100, batch_size=1, verbose=2) model.save(f'lstm_network_timestamp') # make predictions # trainPredict = model.predict(trainX) # testPredict = model.predict(testX) prediction_list = np.array() for x in testX: out = model.predict(x)[0][0] print(out) prediction_list = np.append(prediction_list, out) # invert predictions print(np.shape(testX)) print(testX) testPredict = scaler.inverse_transform(prediction_list.reshape(-1, 1)) testX = scaler.inverse_transform(testX) accuracy.eval_model(testPredict[:,0], 1, evaldata['count'].values) plot(f'lstm_timestamp_{len(testPredict[:,0])}.png', testPredict[:,0], 'Forecast based on a LSTM Neural Network', evaldata['count'].values)