Skip to contents

A Rebalance Structure

Usage

rebalance_portfolio(
  .data,
  .fn,
  ...,
  .strategy = c("risk_parity", "mean_variance")
)

Arguments

.data

A tabular (non-tidy) tibble.

.fn

A function to compute the optimization strategy

...

Additional arguments to be passed to .fn.

.strategy

A character with the optimization technique to be implemented. Currently one of: risk_parity or mean_variance.

Value

A tibble.

Examples

stocks <- tibble::tibble(
  time = as.Date('2009-01-01') + 0:99,
  X    = stats::rnorm(100, 0, 1),
  Y    = stats::rnorm(100, 0, 2),
  Z    = stats::rnorm(100, 0, 4)
)

roll <- construct_rolling_infrastructure(stocks, .initial = 50)

rebal <- construct_rebalance_infrastructure(roll)

# Mean Variance Strategy
mu_sigma <- function(.data) {
  list(mu = colMeans(.data), sigma = stats::cov(.data))
}

rebalance_portfolio(rebal, mu_sigma, .strategy = "mean_variance")
#> # A tibble: 50 × 4
#>    .date      .analysis         .assessment      .optimization   
#>    <date>     <list>            <list>           <list>          
#>  1 2009-02-20 <tibble [50 × 3]> <tibble [1 × 3]> <named list [6]>
#>  2 2009-02-21 <tibble [50 × 3]> <tibble [1 × 3]> <named list [6]>
#>  3 2009-02-22 <tibble [50 × 3]> <tibble [1 × 3]> <named list [6]>
#>  4 2009-02-23 <tibble [50 × 3]> <tibble [1 × 3]> <named list [6]>
#>  5 2009-02-24 <tibble [50 × 3]> <tibble [1 × 3]> <named list [6]>
#>  6 2009-02-25 <tibble [50 × 3]> <tibble [1 × 3]> <named list [6]>
#>  7 2009-02-26 <tibble [50 × 3]> <tibble [1 × 3]> <named list [6]>
#>  8 2009-02-27 <tibble [50 × 3]> <tibble [1 × 3]> <named list [6]>
#>  9 2009-02-28 <tibble [50 × 3]> <tibble [1 × 3]> <named list [6]>
#> 10 2009-03-01 <tibble [50 × 3]> <tibble [1 × 3]> <named list [6]>
#> # … with 40 more rows

# Risk Parity Strategy
#compute_cov <- function(.data) stats::cov(as.matrix(.data))

#rebalance_portfolio(rebal, compute_cov, .strategy = "risk_parity")