diff --git a/Chapter 11/1_Indirect_LSTM_Model.py b/Chapter 11/1_Indirect_LSTM_Model.py new file mode 100644 index 0000000..aed7995 --- /dev/null +++ b/Chapter 11/1_Indirect_LSTM_Model.py @@ -0,0 +1,64 @@ +import pandas as pd +import matplotlib.pyplot as plt +import numpy as np +from keras.models import Sequential +from keras.layers import Dense, LSTM +from master_function import import_cot_data, data_preprocessing +from master_function import plot_train_test_values, calculate_directional_accuracy +from sklearn.metrics import mean_squared_error + +# Calling the function and preprocessing the data +CAD = 'CANADIAN DOLLAR - CHICAGO MERCANTILE EXCHANGE' +data = import_cot_data(2010, 2023, CAD) +data = np.array(data.iloc[:, -1], dtype = np.float64) + +# Setting the hyperparameters +num_lags = 100 +train_test_split = 0.80 +neurons = 200 +num_epochs = 200 +batch_size = 4 + +# Creating the training and test sets +x_train, y_train, x_test, y_test = data_preprocessing(data, num_lags, train_test_split) + +# Reshape the data to 3D for LSTM input +x_train = x_train.reshape((-1, num_lags, 1)) +x_test = x_test.reshape((-1, num_lags, 1)) + +# Create the LSTM model +model = Sequential() + +# Adding a first layer +model.add(LSTM(units = neurons, input_shape = (num_lags, 1))) + +# Adding a second layer +model.add(Dense(neurons, activation = 'relu')) + +# Adding the output layer +model.add(Dense(units = 1)) + +# Compiling the model +model.compile(loss = 'mean_squared_error', optimizer = 'adam') + +# Fitting the model +model.fit(x_train, y_train, epochs = num_epochs, batch_size = batch_size) + +# Predicting in the training set for illustrative purposes +y_predicted_train = model.predict(x_train) + +# Predicting in the test set +y_predicted = model.predict(x_test) + +# Plotting +plot_train_test_values(100, 50, y_train, y_test, y_predicted) + +# Performance evaluation +print('---') +print('Directional Accuracy Train = ', round(calculate_directional_accuracy(y_predicted_train, y_train), 2), '%') +print('Directional Accuracy Test = ', round(calculate_directional_accuracy(y_predicted, y_test), 2), '%') +print('RMSE Train = ', round(np.sqrt(mean_squared_error(y_predicted_train, y_train)), 10)) +print('RMSE Test = ', round(np.sqrt(mean_squared_error(y_predicted, y_test)), 10)) +print('Correlation In-Sample Predicted/Train = ', round(np.corrcoef(np.reshape(y_predicted_train, (-1)), y_train)[0][1], 3)) +print('Correlation Out-of-Sample Predicted/Test = ', round(np.corrcoef(np.reshape(y_predicted, (-1)), np.reshape(y_test, (-1)))[0][1], 3)) +print('---') \ No newline at end of file diff --git a/Chapter 11/2_Direct_MPF_LSTM_Model.py b/Chapter 11/2_Direct_MPF_LSTM_Model.py new file mode 100644 index 0000000..60c53e9 --- /dev/null +++ b/Chapter 11/2_Direct_MPF_LSTM_Model.py @@ -0,0 +1,70 @@ +import pandas as pd +import matplotlib.pyplot as plt +import numpy as np +from keras.models import Sequential +from keras.layers import Dense, LSTM +from master_function import import_cot_data, direct_mpf +from master_function import calculate_directional_accuracy +from sklearn.metrics import mean_squared_error + +# Calling the function and preprocessing the data +CAD = 'CANADIAN DOLLAR - CHICAGO MERCANTILE EXCHANGE' +data = import_cot_data(2010, 2023, CAD) +data = np.array(data.iloc[:, -1], dtype = np.float64) + +# Setting the hyperparameters +num_lags = 100 +train_test_split = 0.80 +neurons = 400 +num_epochs = 200 +batch_size = 10 +forecast_horizon = 100 + +# Prepare the arrays +x_train, y_train, x_test, y_test = direct_mpf(data, + num_lags, + train_test_split, + forecast_horizon) + +# Reshape the data to 3D for LSTM input +x_train = x_train.reshape((-1, num_lags, 1)) +x_test = x_test.reshape((-1, num_lags, 1)) + +# Create the LSTM model +model = Sequential() + +# Adding a first layer +model.add(LSTM(units = neurons, input_shape = (num_lags, 1))) + +# Adding a second layer +model.add(Dense(neurons, activation = 'relu')) + +# Adding the output layer +model.add(Dense(units = forecast_horizon)) + +# Compiling the model +model.compile(loss = 'mean_squared_error', optimizer = 'adam') + +# Fitting the model +model.fit(x_train, y_train, epochs = num_epochs, batch_size = batch_size) + +# Predicting in the test set +y_predicted = model.predict(x_test) + +# Plotting +plt.plot(y_predicted[-1, 0:50], label = 'Predicted data', color = 'red', linewidth = 1) +plt.plot(y_test[-1, 0:50], label = 'Test data', color = 'black', linestyle = 'dashed', linewidth = 2) +plt.grid() +plt.legend() + +''' +y_predicted = np.transpose(y_predicted[-1, 0:50]) +y_test = np.transpose(y_test[-1, 0:50]) + +# Performance evaluation +print('---') +print('Directional Accuracy Test = ', round(calculate_directional_accuracy(y_predicted, y_test), 2), '%') +print('RMSE Test = ', round(np.sqrt(mean_squared_error(y_predicted, y_test)), 10)) +print('Correlation Out-of-Sample Predicted/Test = ', round(np.corrcoef(np.reshape(y_predicted, (-1)), np.reshape(y_test, (-1)))[0][1], 3)) +print('---') +''' \ No newline at end of file diff --git a/Chapter 11/3_Recursive_MPF_LSTM_Model.py b/Chapter 11/3_Recursive_MPF_LSTM_Model.py new file mode 100644 index 0000000..470a1dc --- /dev/null +++ b/Chapter 11/3_Recursive_MPF_LSTM_Model.py @@ -0,0 +1,58 @@ +import pandas as pd +import matplotlib.pyplot as plt +import numpy as np +from keras.models import Sequential +from keras.layers import Dense, LSTM +from master_function import import_cot_data, data_preprocessing, recursive_mpf +from master_function import plot_train_test_values, calculate_directional_accuracy +from sklearn.metrics import mean_squared_error + +# Calling the function and preprocessing the data +CAD = 'CANADIAN DOLLAR - CHICAGO MERCANTILE EXCHANGE' +data = import_cot_data(2010, 2023, CAD) +data = np.array(data.iloc[:, -1], dtype = np.float64) + +# Setting the hyperparameters +num_lags = 10 +train_test_split = 0.80 +neurons = 5 +num_epochs = 10 +batch_size = 20 + +# Creating the training and test sets +x_train, y_train, x_test, y_test = data_preprocessing(data, num_lags, train_test_split) + +# Reshape the data to 3D for LSTM input +x_train = x_train.reshape((-1, num_lags, 1)) +x_test = x_test.reshape((-1, num_lags, 1)) + +# Create the LSTM model +model = Sequential() + +# Adding a first layer +model.add(LSTM(units = neurons, input_shape = (num_lags, 1))) + +# Adding a second layer +model.add(Dense(neurons, activation = 'relu')) + +# Adding the output layer +model.add(Dense(units = 1)) + +# Compiling the model +model.compile(loss = 'mean_squared_error', optimizer = 'adam') + +# Fitting the model +model.fit(x_train, y_train, epochs = num_epochs, batch_size = batch_size) + +# Predicting in the test set on a recursive basis +x_test, y_predicted = recursive_mpf(x_test, y_test, num_lags, model) + +# Plotting +plot_train_test_values(100, 50, y_train, y_test, y_predicted) + +# Performance evaluation +print('---') +print('Directional Accuracy Test = ', round(calculate_directional_accuracy(y_predicted, y_test), 2), '%') +print('RMSE Test = ', round(np.sqrt(mean_squared_error(y_predicted, y_test)), 10)) +print('Correlation Out-of-Sample Predicted/Test = ', round(np.corrcoef(np.reshape(y_predicted, (-1)), np.reshape(y_test, (-1)))[0][1], 3)) +print('---') \ No newline at end of file diff --git a/Chapter 11/4_LSTM_Model_Technical_Indicators_Inputs.py b/Chapter 11/4_LSTM_Model_Technical_Indicators_Inputs.py new file mode 100644 index 0000000..336b352 --- /dev/null +++ b/Chapter 11/4_LSTM_Model_Technical_Indicators_Inputs.py @@ -0,0 +1,79 @@ +import pandas as pd +import matplotlib.pyplot as plt +import numpy as np +from keras.models import Sequential +from keras.layers import Dense, LSTM +from master_function import mass_import, rsi, ma, calculate_accuracy +from master_function import plot_train_test_values, multiple_data_preprocessing +from sklearn.metrics import mean_squared_error +from master_function import add_column, delete_column + +# Calling the function and preprocessing the data +data = mass_import(0, 'W1')[:, -1] +data = rsi(np.reshape(data, (-1, 1)), 5, 0, 1) +data = ma(data, 5, 0, 2) +data[:, 2] = data[:, 0] - data[:, 2] +data = add_column(data, 1) +for i in range(len(data)): + data[i, 3] = data[i, 0] - data[i - 1, 0] +data[:, 0] = data[:, -1] +data = delete_column(data, 3, 1) + +# Setting the hyperparameters +num_lags = 6 +train_test_split = 0.80 +neurons = 500 +num_epochs = 500 +batch_size = 200 + +x_train, y_train, x_test, y_test = multiple_data_preprocessing(data, train_test_split) + +# Reshape the data to 3D for LSTM input +x_train = x_train.reshape((-1, num_lags, 1)) +x_test = x_test.reshape((-1, num_lags, 1)) + +# Create the LSTM model +model = Sequential() + +# Adding a first layer +model.add(LSTM(units = neurons, input_shape = (num_lags, 1))) + +# Adding a second layer +model.add(Dense(neurons, activation = 'relu')) + +# Adding a third layer +model.add(Dense(neurons, activation = 'relu')) + +# Adding a fourth layer +model.add(Dense(neurons, activation = 'relu')) + +# Adding a fifth layer +model.add(Dense(neurons, activation = 'relu')) + +# Adding the output layer +model.add(Dense(units = 1)) + +# Compiling the model +model.compile(loss = 'mean_squared_error', optimizer = 'adam') + +# Fitting the model +model.fit(x_train, y_train, epochs = num_epochs, batch_size = batch_size) + +# Predicting in the training set for illustrative purposes +y_predicted_train = model.predict(x_train) + +# Predicting in the test set +y_predicted = model.predict(x_test) + +# Plotting +plot_train_test_values(100, 50, y_train, y_test, y_predicted) + +# Performance evaluation +print('---') +print('Accuracy Train = ', round(calculate_accuracy(y_predicted_train, y_train), 2), '%') +print('Accuracy Test = ', round(calculate_accuracy(y_predicted, y_test), 2), '%') +print('RMSE Train = ', round(np.sqrt(mean_squared_error(y_predicted_train, y_train)), 10)) +print('RMSE Test = ', round(np.sqrt(mean_squared_error(y_predicted, y_test)), 10)) +print('Correlation In-Sample Predicted/Train = ', round(np.corrcoef(np.reshape(y_predicted_train, (-1)), y_train)[0][1], 3)) +print('Correlation Out-of-Sample Predicted/Test = ', round(np.corrcoef(np.reshape(y_predicted, (-1)), np.reshape(y_test, (-1)))[0][1], 3)) +print('---') \ No newline at end of file diff --git a/Chapter 11/5_LSTM_Volatility_Model_Bitcoin.py b/Chapter 11/5_LSTM_Volatility_Model_Bitcoin.py new file mode 100644 index 0000000..e84aebc --- /dev/null +++ b/Chapter 11/5_LSTM_Volatility_Model_Bitcoin.py @@ -0,0 +1,78 @@ +import pandas as pd +import matplotlib.pyplot as plt +import numpy as np +from keras.models import Sequential +from keras.layers import Dense, LSTM +from master_function import import_cot_data, data_preprocessing +from master_function import plot_train_test_values, calculate_directional_accuracy +from sklearn.metrics import mean_squared_error +from master_function import add_column, delete_row, volatility + +# Calling the function and preprocessing the data +data = pd.read_excel('Bitcoin_Daily_Historical_Data.xlsx').values +data = volatility(data, 10, 0, 1) +data = data[:, -1] + +# Differencing for stationarity +data = np.diff(data) + +# Checking for stationarity +from statsmodels.tsa.stattools import adfuller +print('p-value: %f' % adfuller(data)[1]) + +# Setting the hyperparameters +num_lags = 300 +train_test_split = 0.80 +neurons = 80 +num_epochs = 100 +batch_size = 500 + +# Creating the training and test sets +x_train, y_train, x_test, y_test = data_preprocessing(data, num_lags, train_test_split) + +# Reshape the data to 3D for LSTM input +x_train = x_train.reshape((-1, num_lags, 1)) +x_test = x_test.reshape((-1, num_lags, 1)) + +# Create the LSTM model +model = Sequential() + +# Adding a first layer +model.add(LSTM(units = neurons, input_shape = (num_lags, 1))) + +# Adding a second layer +model.add(Dense(neurons, activation = 'relu')) + +# Adding a third layer +model.add(Dense(neurons, activation = 'relu')) + +# Adding a fourth layer +model.add(Dense(neurons, activation = 'relu')) + +# Adding the output layer +model.add(Dense(units = 1)) + +# Compiling the model +model.compile(loss = 'mean_squared_error', optimizer = 'adam') + +# Fitting the model +model.fit(x_train, y_train, epochs = num_epochs, batch_size = batch_size) + +# Predicting in the training set for illustrative purposes +y_predicted_train = model.predict(x_train) + +# Predicting in the test set +y_predicted = model.predict(x_test) + +# Plotting +plot_train_test_values(100, 50, y_train, y_test, y_predicted) + +# Performance evaluation +print('---') +print('Directional Accuracy Train = ', round(calculate_directional_accuracy(y_predicted_train, y_train), 2), '%') +print('Directional Accuracy Test = ', round(calculate_directional_accuracy(y_predicted, y_test), 2), '%') +print('RMSE Train = ', round(np.sqrt(mean_squared_error(y_predicted_train, y_train)), 10)) +print('RMSE Test = ', round(np.sqrt(mean_squared_error(y_predicted, y_test)), 10)) +print('Correlation In-Sample Predicted/Train = ', round(np.corrcoef(np.reshape(y_predicted_train, (-1)), y_train)[0][1], 3)) +print('Correlation Out-of-Sample Predicted/Test = ', round(np.corrcoef(np.reshape(y_predicted, (-1)), np.reshape(y_test, (-1)))[0][1], 3)) +print('---') \ No newline at end of file diff --git a/Chapter 11/6_Dynamic_Plotting_Model_LSTM.py b/Chapter 11/6_Dynamic_Plotting_Model_LSTM.py new file mode 100644 index 0000000..98f04d3 --- /dev/null +++ b/Chapter 11/6_Dynamic_Plotting_Model_LSTM.py @@ -0,0 +1,93 @@ +import pandas as pd +import matplotlib.pyplot as plt +import numpy as np +from keras.models import Sequential +from keras.layers import Dense, LSTM +from master_function import data_preprocessing, compute_diff +from master_function import calculate_directional_accuracy, update +from sklearn.metrics import mean_squared_error +import tensorflow as tf +import random + +# Import the data +data = np.reshape(pd.read_excel('ISM_PMI.xlsx').values, (-1)) +data = compute_diff(data, 1) + +# Setting the hyperparameters +num_lags = 100 +train_test_split = 0.80 +neurons = 100 +num_epochs = 10 +batch_size = 200 + +# Creating the training and test sets +x_train, y_train, x_test, y_test = data_preprocessing(data, num_lags, train_test_split) + +# Reshape the data to 3D for LSTM input +x_train = x_train.reshape((-1, num_lags, 1)) +x_test = x_test.reshape((-1, num_lags, 1)) + +# Create the LSTM model +model = Sequential() + +# Adding a first layer +model.add(LSTM(units = neurons, input_shape = (num_lags, 1))) + +# Adding a second layer +model.add(Dense(neurons, activation = 'relu')) + +# Adding a third layer +model.add(Dense(neurons, activation = 'relu')) + +# Adding the output layer +model.add(Dense(units = 1)) + +# Compiling the model +model.compile(loss = 'mean_squared_error', optimizer = 'adam') + +def update_plot(epoch, logs): + if epoch % 1 == 0: + plt.cla() + y_predicted_train = model.predict(x_train) + plt.plot(y_train[-100:], label = 'Training data', color = 'black', linewidth = 2.5) + plt.plot(y_predicted_train[-100:,], label = 'Predicted data', color = 'red', linewidth = 1) + plt.title(f'Training Epoch: {epoch}') + plt.xlabel('Time') + plt.ylabel('Value') + plt.legend() + plt.grid() + plt.savefig(str(random.randint(1, 1000))) + +# Create the dynamic plot +fig = plt.figure() + +# Train the model using the on_epoch_end callback +class PlotCallback(tf.keras.callbacks.Callback): + def on_epoch_end(self, epoch, logs=None): + update_plot(epoch, logs) + plt.pause(0.001) + +plot_callback = PlotCallback() +history = model.fit(x_train, y_train, epochs = num_epochs, batch_size = batch_size, callbacks = [plot_callback]) + +# Predicting in the training set +y_predicted_train = model.predict(x_train) + +# Predicting in the test set +y_predicted = model.predict(x_test) + +# Performance evaluation +print('---') +print('Directional Accuracy Train = ', round(calculate_directional_accuracy(y_predicted_train, y_train), 2), '%') +print('Directional Accuracy Test = ', round(calculate_directional_accuracy(y_predicted, y_test), 2), '%') +print('RMSE Train = ', round(np.sqrt(mean_squared_error(y_predicted_train, y_train)), 10)) +print('RMSE Test = ', round(np.sqrt(mean_squared_error(y_predicted, y_test)), 10)) +print('---') + +from tensorflow.keras.utils import plot_model +from IPython.display import Image + +plot_model(model, to_file = 'Architecture.png', + show_shapes = True, + show_layer_names = True) +Image('Architecture.png') diff --git a/Chapter 11/Bitcoin_Daily_Historical_Data.xlsx b/Chapter 11/Bitcoin_Daily_Historical_Data.xlsx new file mode 100644 index 0000000..4cd25b4 Binary files /dev/null and b/Chapter 11/Bitcoin_Daily_Historical_Data.xlsx differ diff --git a/Chapter 11/ISM_PMI.xlsx b/Chapter 11/ISM_PMI.xlsx new file mode 100644 index 0000000..5ba423f Binary files /dev/null and b/Chapter 11/ISM_PMI.xlsx differ