genRandomFuns: Generate multiple versions of a function with randomly chosen...

View source: R/param_tuning.R

genRandomFunsR Documentation

Generate multiple versions of a function with randomly chosen parameters

Description

Portfolio functions usually contain some parameters that can be tuned. This function creates multiple versions of a function with randomly chosen parameters. After backtesting those portfolios, the plotting function plotPerformanceVsParams can be used to show the performance vs parameters.

Usage

genRandomFuns(portfolio_fun, params_grid, name = "portfolio", N_funs = NULL)

Arguments

portfolio_fun

Portfolio function with parameters unspecified.

params_grid

Named list containing for each parameter the possible values it can take.

name

String with the name of the portfolio function.

N_funs

Number of functions to be generated.

Author(s)

Daniel P. Palomar and Rui Zhou

See Also

plotPerformanceVsParams

Examples

library(portfolioBacktest)

# define GMVP with parameters "delay", "lookback", and "regularize"
GMVP_portfolio_fun <- function(dataset, ...) {
  prices <- tail(lag(dataset$adjusted, delay), lookback)
  X <- diff(log(prices))[-1]
  Sigma <- cov(X)
  if (regularize)
    Sigma <- Sigma + 0.1 * mean(diag(Sigma)) * diag(ncol(Sigma))
  # design GMVP
  w <- solve(Sigma, rep(1, ncol(Sigma)))
  return(w/sum(w))
}

# generate the functions with random parameters
portfolio_list <- genRandomFuns(portfolio_fun = GMVP_portfolio_fun, 
                                params_grid = list(lookback = c(100, 120, 140, 160),
                                                   delay = c(0, 5, 10, 15, 20),
                                                   regularize = c(FALSE, TRUE)),
                                name = "GMVP", 
                                N_funs = 40)
names(portfolio_list)
portfolio_list[[1]]
rlang::env_print(portfolio_list[[1]])
rlang::fn_env(portfolio_list[[1]])$lookback
rlang::fn_env(portfolio_list[[1]])$delay
rlang::fn_env(portfolio_list[[1]])$regularize


dppalomar/portfolioBacktest documentation built on April 27, 2022, 3:27 a.m.