This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Function to open an XLSX file with error handling | |
open_xlsx <- function(file_path) { | |
# Output the Excel file in case it exists | |
result <- tryCatch({ | |
# Read the Excel file | |
data <- read.xlsx(file_path) | |
# Set the date column as datetime type | |
data$date <- as.Date(data$date, origin = "1899-12-30") | |
# Return the data |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Set the seed to estimate the TVP-VAR-SV model | |
set.seed(2024) | |
# Estimate the TVP-VAR-SV model | |
bv <- bvar.sv.tvp(tvp_var_data, tau= 250, nf=1, nrep = 300, nburn=20) | |
# Obtain the forecasts of the model based on the mean of the posterior-distribution draws | |
forecast_ys <- rowMeans(bv$fc.ydraws[1:7,1,]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Set tickers | |
tickers <- c('MSFT', 'AAPL', 'TSLA', 'NFLX', 'META', 'AMZN','GOOGL') | |
# Set start and end dates | |
start = "1990-01-01" | |
end = "2024-08-01" | |
df <- new.env() | |
# Import the data |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library('TTR') | |
library('quantmod') | |
library('stats') | |
library('lubridate') | |
library('dplyr') | |
library('ggplot2') | |
library('forecast') | |
library('vars') | |
library('openxlsx') | |
library('bvarsv') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
install.packages('TTR') | |
install.packages('quantmod') | |
install.packages('stats') | |
install.packages('lubridate') | |
install.packages('dplyr') | |
install.packages('ggplot2') | |
install.packages('forecast') | |
install.packages('vars') | |
install.packages('openxlsx') | |
install.packages('bvarsv') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def boruta_shap_algorithm(X, y, trials=20, workers=2, significance_level=0.05, seed=2024): | |
# Set the seed | |
np.random.seed(seed) | |
# Assert that the number of samples of both data match | |
assert X.shape[0] == y.shape[0], "X and y dimensions don't coincide" | |
# Set a dictionary to save the number of hits for each feature |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# select features from n number of trials | |
def choose_features(feature_hits, TRIALS, thresh): | |
# Define the boundaries for the green zone | |
# Define the green zone threshold | |
green_zone_thresh = TRIALS - thresh | |
# Define the blue zone upper threshold | |
blue_zone_upper = green_zone_thresh | |
# Define the blue zone lower threshold | |
blue_zone_lower = thresh |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Set the minimum number of trials as a threshold number to accept an input feature as a selected feature | |
def get_tail_items(pmf, significance_level=0.05): | |
# Set total to zero | |
total = 0 | |
# Create a loop based on the probability mass function | |
for i, x in enumerate(pmf): | |
# Increment the total variable with the probability “x” of i | |
total += x | |
# If total is higher than the significance level | |
if total >= significance_level: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Import the necessary libraries | |
import scipy as sp | |
import numpy as np | |
import pandas as pd | |
import shap | |
from xgboost import XGBRFClassifier | |
from xgboost import XGBRFRegressor | |
from sklearn.preprocessing import LabelEncoder | |
from concurrent import futures |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data['y'].value_counts() |