select_top_k_scores_by_group: Select top-k symbols per group by score

View source: R/ml.R

select_top_k_scores_by_groupR Documentation

Select top-k symbols per group by score

Description

For each date, choose the top k symbols within each group based on a score panel. Returns a logical selection panel aligned to the input.

Usage

select_top_k_scores_by_group(scores, k, group_map, max_per_group = 3L)

Arguments

scores

Wide score panel (Date + symbols).

k

Positive integer: number of symbols to select per group.

group_map

Named character vector or 2-column data.frame (symbol, group) mapping symbols to groups.

max_per_group

Integer cap per group (default 3L).

Details

  • Group membership comes from group_map (symbol -> group).

  • Selection is computed independently by group on each date.

  • Ties follow the ordering implied by order(..., method = "radix").

Value

Logical selection panel (Date + symbols) where TRUE marks selected symbols.

Examples

set.seed(42)
scores <- data.frame(
  Date = as.Date("2020-01-01") + 0:1,
  A = runif(2), B = runif(2), C = runif(2), D = runif(2), E = runif(2), F = runif(2)
)
gmap <- data.frame(Symbol = c("A","B","C","D","E","F"),
                   Group  = c("G1","G1","G2","G2","G3","G3"))
sel <- select_top_k_scores_by_group(scores, k = 4, group_map = gmap, max_per_group = 2)
head(sel)

PortfolioTesteR documentation built on Nov. 5, 2025, 5:23 p.m.