Skip to content

Commit 01587de

Browse files
committed
new stock experiment and bug fixes
1 parent 9ddfb26 commit 01587de

File tree

4 files changed

+36
-3
lines changed

4 files changed

+36
-3
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Generated by roxygen2: do not edit by hand
22

33
export(main_prep_and_run_motor)
4+
export(main_prep_and_run_stock)
45
export(main_prep_and_run_synth)
56
export(mtDBN)
67
export(treeSc)

R/mtDBN.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,15 +326,16 @@ mtDBN <- R6::R6Class("mtDBN",
326326

327327
# Operate the M5 smoothing up to the root node
328328
node <- private$tree_sc$classify_inst(instance)
329-
330-
#while(is.environment(node$p_node)){
329+
330+
#while(is.environment(node$p_node)){ # In case an smoothing model is built per split point, which is very costly
331+
if(is.environment(node$p_node)){
331332
parent_model <- private$models[[as.character(node$p_node$name)]]
332333
preds_p <- private$exact_prediction_step(parent_model, vars_pred,
333334
private$as_named_vector(instance[1, .SD, .SDcols = c(vars_ev, prov_ev)]))
334335
preds$mu_p <- Map(function(x,y){(node$n * x + k * y) / (node$n + k)}, preds$mu_p, preds_p$mu_p)
335336
#preds$mu_p <- Map(function(x,y){(x + y) / (2)}, preds$mu_p, preds_p$mu_p)
336337
node <- node$p_node
337-
#}
338+
}
338339

339340
return(preds)
340341
}

R/stock_exp.R

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#' @export
2+
main_prep_and_run_stock <- function(){
3+
dt <- data.table::fread("./dataset/TWII_1y.csv") # 0.5 secs between rows
4+
id_var <- "Date" # We will crossvalidate based on the months
5+
6+
dt[, Date := unlist(strsplit(as.character(dt$Date), "-[0-9]*$"))] # We leave only the year and month of the dates for making CV groups
7+
dt[, Volume := as.numeric(Volume)]
8+
setnames(dt, "Adj Close", "Adj_Close")
9+
obj_vars <- "Open_t_0"
10+
obj_var_univ <- "Open"
11+
obj_var_multiv <- c("Open", "Volume")
12+
prov_ev <- NULL
13+
# max_min_vals <- max_min_norm(dt, names(dt)[-1])
14+
dt[, Adj_Close := NULL] # Exactly the same as Close
15+
16+
full_exp_run(dt = dt, id_var = id_var, obj_vars = obj_vars,
17+
obj_var_univ = obj_var_univ, obj_var_multiv = obj_var_multiv,
18+
prov_ev = prov_ev, res_file = "full_run_stock_results.txt",
19+
mae_file = "full_run_stock_mae.csv", mape_file = "full_run_stock_mape.csv", pred_len = 1,
20+
fold_len = 2, seed = 42, size = 3, method = "psoho", n_it = 100, min_ind = 50)
21+
}

R/utils.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,16 @@ mape <- function(orig, pred){
223223
return((100/length(orig)) * sum(abs((orig - pred) / orig)))
224224
}
225225

226+
max_min_norm <- function(dt, cols = names(dt)){
227+
min_vals <- dt[, sapply(.SD, min), .SDcols = cols]
228+
max_vals <- dt[, sapply(.SD, max), .SDcols = cols]
229+
230+
for(v in cols)
231+
dt[, (v) := (get(v) - min_vals[v]) / (max_vals[v] - min_vals[v])]
232+
233+
return(list(max_vals = max_vals, min_vals = min_vals))
234+
}
235+
226236
# Experiment pipeline functions
227237

228238
forecast_cycle_intervals_single <- function(f_dt_test, model_fit, id_var, size, obj_vars, prov_ev, pred_len){

0 commit comments

Comments
 (0)