Learn R Programming

PortfolioTesteR (version 0.1.4)

roll_fit_predict_seq: Rolling fit/predict for sequence models (flattened steps-by-p features)

Description

Rolling fit/predict for sequence models (flattened steps-by-p features)

Usage

roll_fit_predict_seq(
  features_list,
  labels,
  steps = 26L,
  horizon = 4L,
  fit_fn,
  predict_fn,
  is_periods = 156L,
  oos_periods = 4L,
  step = 4L,
  group = c("pooled", "per_symbol", "per_group"),
  group_map = NULL,
  normalize = c("none", "zscore", "minmax"),
  min_train_samples = 50L,
  na_action = c("omit", "zero")
)

Value

wide panel of scores.

Arguments

features_list

list of panels to be stacked over steps history.

labels

future-return panel aligned to the features.

steps

int; lookback length (e.g., 26).

horizon

int; label horizon (e.g., 4).

fit_fn

function (X, y) -> model trained on in-sample stacked rows.

predict_fn

function (model, Xnew) -> numeric scores.

is_periods, oos_periods, step

ints; in-sample length, out-of-sample length, and step size for the rolling window.

group

one of "pooled", "per_symbol", "per_group".

group_map

optional data.frame(Symbol, Group) if group = "per_group".

normalize

"none", "zscore", or "minmax" applied using IS data only.

min_train_samples

Optional minimum IS samples required to fit; if not met, skip fit.

na_action

"omit" or "zero" for feature NA handling.

Examples

Run this code
# \donttest{
data(sample_prices_weekly); data(sample_prices_daily)
mom <- panel_lag(calc_momentum(sample_prices_weekly, 12), 1)
vol <- panel_lag(align_to_timeframe(
  calc_rolling_volatility(sample_prices_daily, 20),
  sample_prices_weekly$Date, "forward_fill"), 1)
Y <- make_labels(sample_prices_weekly, horizon = 4, type = "log")
fit_lm  <- function(X,y){ Xc <- cbind(1,X); list(coef=stats::lm.fit(Xc,y)$coefficients) }
pred_lm <- function(m,X){ as.numeric(cbind(1,X) %*% m$coef) }
S <- roll_fit_predict_seq(list(mom=mom, vol=vol), Y,
                          steps = 26, horizon = 4,
                          fit_fn = fit_lm, predict_fn = pred_lm,
                          is_periods = 104, oos_periods = 4, step = 4)
head(S)
# }

Run the code above in your browser using DataLab