Learn R Programming

PortfolioTesteR (version 0.1.4)

weight_by_hrp: Hierarchical Risk Parity Weighting

Description

Calculates portfolio weights using Hierarchical Risk Parity (HRP) methodology. HRP combines hierarchical clustering with risk-based allocation to create diversified portfolios that don't rely on unstable correlation matrix inversions.

Usage

weight_by_hrp(
  selected_df,
  prices_df,
  lookback_periods = 252,
  cluster_method = "ward.D2",
  distance_method = "euclidean",
  min_periods = 60,
  use_correlation = FALSE
)

Value

Weight matrix with same dates as selected_df

Arguments

selected_df

Binary selection matrix (data.frame with Date column)

prices_df

Price data for covariance calculation (typically daily) Returns are calculated internally from prices

lookback_periods

Number of periods for covariance estimation (default: 252)

cluster_method

Clustering linkage method (default: "ward.D2")

distance_method

Distance measure for clustering (default: "euclidean")

min_periods

Minimum periods required for calculation (default: 60)

use_correlation

If TRUE, cluster on correlation instead of covariance

Details

The HRP algorithm:

  1. Calculate returns from input prices

  2. Compute covariance matrix from returns

  3. Cluster assets based on distance matrix

  4. Apply recursive bisection with inverse variance weighting

  5. Results in naturally diversified portfolio without matrix inversion

The function accepts price data and calculates returns internally, matching the pattern of other library functions like calc_momentum().

Examples

Run this code
data("sample_prices_daily")
data("sample_prices_weekly")
# Create a selection first
momentum <- calc_momentum(sample_prices_weekly, lookback = 12)
selected <- filter_top_n(momentum, n = 10)

# Using daily prices for risk calculation
weights <- weight_by_hrp(selected, sample_prices_daily, lookback_periods = 252)

# Using correlation-based clustering
weights <- weight_by_hrp(selected, sample_prices_daily, use_correlation = TRUE)

Run the code above in your browser using DataLab