implement_portfolio_sort: Implement Portfolio Sort

View source: R/implement_portfolio_sort.R

implement_portfolio_sortR Documentation

Implement Portfolio Sort

Description

A convenience wrapper that combines sample construction filtering and portfolio return computation into a single call. Equivalent to calling filter_sorting_data() followed by compute_portfolio_returns().

Usage

implement_portfolio_sort(
  data,
  sorting_variables,
  sorting_method,
  rebalancing_month = NULL,
  portfolio_sort_options,
  breakpoint_function_main = compute_breakpoints,
  breakpoint_function_secondary = compute_breakpoints,
  min_portfolio_size = 1L,
  cap_weight = 0.8,
  data_options = NULL,
  quiet = FALSE
)

Arguments

data

A data frame containing the stock-level panel data.

sorting_variables

A character vector of one or two column names to sort portfolios on.

sorting_method

A string specifying the sorting method: "univariate", "bivariate-dependent", or "bivariate-independent".

rebalancing_month

An optional integer specifying the month in which portfolios are rebalanced annually. NULL (the default) means monthly rebalancing.

portfolio_sort_options

A list of class tidyfinance_portfolio_sort_options created by portfolio_sort_options(), bundling filter and breakpoint specifications. The arguments accepted by portfolio_sort_options() include

  • filter_options A list of class tidyfinance_filter_options created by filter_options(), or NULL (the default, which applies no filters). Options include exclude_financials, exclude_utilities, min_stock_price, min_size_quantile, min_listing_age, exclude_negative_book_equity, and exclude_negative_earnings.

  • breakpoint_options_main A list of class tidyfinance_breakpoint_options created by breakpoint_options(), specifying breakpoints for the primary sorting variable. Options include n_portfolios, percentiles, breakpoints_exchanges, smooth_bunching, and breakpoints_min_size_threshold.

  • breakpoint_options_secondary A list of class tidyfinance_breakpoint_options created by breakpoint_options(), specifying breakpoints for the secondary sorting variable, or NULL (the default) for univariate sorts. Options are the same as for breakpoint_options_main.

breakpoint_function_main

The function used to compute breakpoints for the main sorting variable. Defaults to compute_breakpoints().

breakpoint_function_secondary

The function used to compute breakpoints for the secondary sorting variable. Defaults to compute_breakpoints().

min_portfolio_size

An integer specifying the minimum number of firms in the reported portfolio cross-section on a given date. Defaults to 1L (at least one observation per reported portfolio). For univariate sorts that is firms per portfolio-date; for bivariate sorts that is firms per main-portfolio-date summed across the secondary buckets. Cross-sections below the threshold have their returns set to NA. Set to 0L to deactivate the check. See compute_portfolio_returns().

cap_weight

A numeric between 0 and 1 specifying the quantile at which portfolio weights are capped for the capped value-weighted return. Defaults to 0.8.

data_options

A list of class tidyfinance_data_options (created via data_options()) specifying column name mappings. All elements are forwarded to filter_sorting_data() and compute_portfolio_returns(). Uses data_options() default if NULL: "id" = "permno", "date" = "date", "exchange" = "exchange", "mktcap_lag" = "mktcap_lag", "ret_excess" = "ret_excess", "siccd" = "siccd", "price" = "prc_adj", "listing_age" = "listing_age", "be" = "be", and "earnings" = "ib".

quiet

A logical indicating whether informational messages should be suppressed. Defaults to FALSE.

Value

A data frame of portfolio returns as returned by compute_portfolio_returns().

See Also

Other portfolio functions: assign_portfolio(), breakpoint_options(), compute_breakpoints(), compute_long_short_returns(), compute_portfolio_returns(), data_options(), filter_options(), filter_sorting_data(), portfolio_sort_options()

Examples


set.seed(123)
data <- data.frame(
  permno = 1:500,
  date = rep(
    seq.Date(
      from = as.Date("2020-01-01"),
      by = "month",
      length.out = 100
    ),
    each = 10
  ),
  mktcap_lag = runif(500, 100, 1000),
  ret_excess = rnorm(500),
  prc_adj = runif(500, 0.5, 50),
  size = runif(500, 50, 150)
)
implement_portfolio_sort(
  data = data,
  sorting_variables = "size",
  sorting_method = "univariate",
  portfolio_sort_options = portfolio_sort_options(
    filter_options = filter_options(
      min_stock_price = 1
    ),
    breakpoint_options_main = breakpoint_options(n_portfolios = 5)
  )
)



tidyfinance documentation built on June 1, 2026, 1:06 a.m.