Skip to contents

ledgr_align_weights() turns the numeric output of an estimator or solver into ledgr_weights() identified by the instruments of the intact history frame or matrix it was estimated from, so no column names have to be rebuilt by hand. The solver output comes first, so it pipes: solution |> ledgr_align_weights(history) |> ledgr_target_rebalance(ctx). An unnamed vector is read in the order of the result's included instruments, which is the column order of ledgr_history_matrix(); a vector named by those instruments, in any order, is matched by name. A one-column or one-row matrix is read as a vector. A length that differs from the number of included instruments, names that differ from them (wrong, duplicated or missing names) or a wider matrix refuse with ledgr_weight_alignment_mismatch rather than guessing.

Usage

ledgr_align_weights(solution, history)

Arguments

solution

A finite numeric vector, or a one-column or one-row numeric matrix, with one weight per included instrument. A missing or non-finite weight refuses with ledgr_invalid_strategy_type, as ledgr_weights() does.

history

The intact history frame or matrix the solution was estimated from.

Value

A ledgr_weights object named by the included instrument IDs.

Details

Only the included estimation sample receives a weight. Instruments the result excluded are listed by ledgr_history_info(); see that page and ledgr_target_rebalance() for what happens to a held excluded instrument.

Articles

Reading causal history: vignette("causal-history", package = "ledgr") system.file("doc", "causal-history.html", package = "ledgr")

Examples

if (FALSE) { # \dontrun{
strategy <- function(ctx, params) {
  returns <- ctx |>
    ledgr_history_matrix(feature = "return_1", lookback = 60L,
                         min_instruments = 3L)
  x <- returns
  w <- 1 / apply(x, 2L, stats::sd)
  unname(w / sum(w)) |>
    ledgr_align_weights(returns) |>
    ledgr_target_rebalance(ctx, equity_fraction = 0.95)
}
} # }