import numpy as np import pandas as pd import matplotlib.pyplot as plt from sklearn.svm import SVR from sklearn.metrics import mean_absolute_error, mean_squared_error from sklearn.model_selection import GridSearchCV from sklearn.preprocessing import StandardScaler from sklearn.inspection import permutation_importance import math def mape(y_true, y_pred): return np.mean(np.abs((y_true - y_pred) / y_true)) * 100 #Load train_data = pd.read_excel('(((Long-D1_standardized_train.xlsx', engine='openpyxl' ) valid_data = pd.read_excel('(((Long-D1_standardized_valid.xlsx', engine='openpyxl') test_data = pd.read_excel('(((Long-D1_standardized_test.xlsx', engine='openpyxl') predictors = ["ok", "hk", "lk", "ck", "volk", "qavolk","notk", "tbvolk", "tqvolk", "oip", "hi", "li", "ci", "os", "hs", "ls", "cs", "vols", "qavols", "nots", "tbvols", "tqvols", "om", "hm", "lm", "cm", "oilvol30", "dxvol30", "spvol30", "gldvol30", "nqvol30", "vxvol30", "djvol30", "y10vol30", "y2vol30", "oilp30", "dxp30", "spp30", "gldp30", "nqp30", "vxp30", "djp30", "y10p30", "y2p30", "oilp30v", "dxp30v", "spp30v", "gldp30v", "nqp30v", "djp30v", "obv", "vwap10", "vwap14", "vwap26", "vwap35", "vwap51", "vwap67", "sma10", "sma14", "sma26", "sma35", "sma51", "sma67", "ema10", "ema14", "ema26", "ema35", "ema51", "ema67", "rsi10", "rsi14", "rsi26", "rsi35", "rsi51", "rsi67", "macd", "macds", "macdh", "stoks", "stods", "stokl", "stodl", "rocv10", "rocv14", "rocv26", "rocv35", "rocv51", "rocv67", "rocp10", "rocp14", "rocp26", "rocp35", "rocp51", "rocp67", "bbus2", "bbms2", "bbls2", "bbus3", "bbls3", "bbul2", "bbml2", "bbll2", "bbul3", "bbll3", "atr10", "atr14", "atr26", "atr35", "atr51", "atr67", # "bsr", # "bvol", "svol", "dt", "cvd", "lsra", "lap", "sap", "lsrg", # "lgp", "sgp", "lsrp", "lpp", "spp", "oib", "oiu" ] X_train = train_data[predictors] y_train = train_data['V'] X_valid = valid_data[predictors] y_valid = valid_data['V'] X_test = test_data[predictors] y_test = test_data['V'] #scale sc_X = StandardScaler() sc_y = StandardScaler() X_train = sc_X.fit_transform(X_train) X_valid = sc_X.transform(X_valid) X_test = sc_X.transform(X_test) y_train = sc_y.fit_transform(y_train.values.reshape(-1, 1)).ravel() y_valid = sc_y.transform(y_valid.values.reshape(-1, 1)).ravel() y_test = sc_y.transform(y_test.values.reshape(-1, 1)).ravel() #hypers tuning param_grid = {'C': [0.1, 1, 10, 100], 'gamma': [0.01, 0.001], 'epsilon': [0.000001, 0.0000001, 0.0000001], 'kernel': ['poly', 'rbf', 'sigmoid']} grid = GridSearchCV(SVR(), param_grid, scoring='neg_mean_absolute_error', cv=10) grid.fit(X_valid, y_valid) best_params = grid.best_params_ print('Best hyperparameters:', best_params) #train print("Training the model ...") svr = SVR(**best_params) svr.fit(X_train, y_train) # Predict on the test data y_pred = svr.predict(X_test) y_pred = sc_y.inverse_transform(y_pred.reshape(-1, 1)).ravel() y_test = sc_y.inverse_transform(y_test.reshape(-1, 1)).ravel() print("Evaluating the model on the test dataset...") mae = mean_absolute_error(y_test, y_pred) rmse = math.sqrt(mean_squared_error(y_test, y_pred)) mape_value = mape(y_test, y_pred) print('MAE:', mae) print('RMSE:', rmse) print('MAPE:', mape_value) # Define the window size window_size = 1 #eval print("Calculating the error metrics for the specified window size...") def calculate_errors(y_true, y_pred, window_size): mae = mean_absolute_error(y_true[:window_size], y_pred[:window_size]) rmse = np.sqrt(mean_squared_error(y_true[:window_size], y_pred[:window_size])) mape_value = mape(y_true[:window_size], y_pred[:window_size]) return mae, rmse, mape_value mae_window, rmse_window, mape_window = calculate_errors(y_test, y_pred, window_size) #selection print("Performing feature selection using Permutation Importance...") result = permutation_importance(svr, X_valid, y_valid, n_repeats=10, random_state=42) sorted_idx = result.importances_mean.argsort() #save print("Saving the results to a .txt file...") with open('SVR_predictions_1.txt', 'w') as file: file.write(f'Best hyperparameters: {best_params}\n') file.write(f'MAE: {mae}\n') file.write(f'RMSE: {rmse}\n') file.write(f'MAPE: {mape_value}\n') file.write("Windowed Test ({} steps): MAE = {}, RMSE = {}, MAPE = {}%\n".format(window_size, mae_window, rmse_window, mape_window)) file.write('\nFeature Importance:\n') for i in sorted_idx[::-1]: file.write(f'{predictors[i]}: {result.importances_mean[i]:.5f}\n') file.write('\nPredicted V values for the entire test set:\n') for value in y_pred: file.write(f'{value}\n') # Plot true values and predictions plt.figure(figsize=(12, 6)) plt.plot(y_test, label='True Values', marker='o') plt.plot(y_pred, label='Predictions', marker='x') plt.xlabel('Index') plt.ylabel('Volatility') plt.legend() plt.show() # Plot the feature importances plt.figure(figsize=(12, 6)) plt.barh(range(X_valid.shape[1]), result.importances_mean[sorted_idx], color="b", align="center") plt.yticks(range(X_valid.shape[1]), [predictors[i] for i in sorted_idx]) plt.xlabel("Permutation Importance") plt.show()