hrp
Builds a hierarchical risk parity portfolio
Usageโ
hrp [-cd {pearson,spearman,abs_pearson,abs_spearman,distance,mutual_info,tail}] [-cv {hist,ewma1,ewma2,ledoit,oas,shrunk,gl,jlogo,fixed,spectral,shrink}] [-rm RISK-MEASURE] [-as CVAR_SIMULATIONS_LOSSES] [-b CVAR_SIGNIFICANCE] [-bs CVAR_SIMULATIONS_GAINS] [-lk LINKAGE] [-k AMOUNT_CLUSTERS] [-mk MAX_CLUSTERS] [-bi {KN,FD,SC,HGR}] [-at ALPHA_TAIL] [-lo LEAF_ORDER] [-de SMOOTHING_FACTOR_EWMA] [-mt METHOD] [-ct CATEGORIES] [-p PERIOD] [-s START_PERIOD] [-e END_PERIOD] [-lr] [--freq {d,w,m}] [-mn MAX_NAN] [-th THRESHOLD_VALUE] [-r RISK_FREE] [-a SIGNIFICANCE_LEVEL] [-v LONG_ALLOCATION] [--name NAME]
Parametersโ
Name | Parameter | Description | Default | Optional | Choices |
---|---|---|---|---|---|
co_dependence | -cd --codependence | The codependence or similarity matrix used to build the distance metric and clusters. Possible values are: 'pearson': pearson correlation matrix 'spearman': spearman correlation matrix 'abs_pearson': absolute value of pearson correlation matrix 'abs_spearman': absolute value of spearman correlation matrix 'distance': distance correlation matrix 'mutual_info': mutual information codependence matrix 'tail': tail index codependence matrix | pearson | True | pearson, spearman, abs_pearson, abs_spearman, distance, mutual_info, tail |
covariance | -cv --covariance | Method used to estimate covariance matrix. Possible values are 'hist': historical method 'ewma1': exponential weighted moving average with adjust=True 'ewma2': exponential weighted moving average with adjust=False 'ledoit': Ledoit and Wolf shrinkage method 'oas': oracle shrinkage method 'shrunk': scikit-learn shrunk method 'gl': graphical lasso method 'jlogo': j-logo covariance 'fixed': takes average of eigenvalues above max Marchenko Pastour limit 'spectral': makes zero eigenvalues above max Marchenko Pastour limit 'shrink': Lopez de Prado's book shrinkage method | hist | True | hist, ewma1, ewma2, ledoit, oas, shrunk, gl, jlogo, fixed, spectral, shrink |
risk_measure | -rm --risk-measure | Risk measure used to optimize the portfolio. Possible values are: 'MV' : Variance 'MAD' : Mean Absolute Deviation 'GMD' : Gini Mean Difference 'MSV' : Semi Variance (Variance of negative returns) 'FLPM' : First Lower Partial Moment 'SLPM' : Second Lower Partial Moment 'VaR' : Value at Risk 'CVaR' : Conditional Value at Risk 'TG' : Tail Gini 'EVaR' : Entropic Value at Risk 'WR' : Worst Realization 'RG' : Range 'CVRG' : CVaR Range 'TGRG' : Tail Gini Range 'ADD' : Average Drawdown of uncompounded returns 'UCI' : Ulcer Index of uncompounded returns 'DaR' : Drawdown at Risk of uncompounded returns 'CDaR' : Conditional Drawdown at Risk of uncompounded returns 'EDaR' : Entropic Drawdown at Risk of uncompounded returns 'MDD' : Maximum Drawdown of uncompounded returns 'ADD_Rel' : Average Drawdown of compounded returns 'UCI_Rel' : Ulcer Index of compounded returns 'DaR_Rel' : Drawdown at Risk of compounded returns 'CDaR_Rel' : Conditional Drawdown at Risk of compounded returns 'EDaR_Rel' : Entropic Drawdown at Risk of compounded returns 'MDD_Rel' : Maximum Drawdown of compounded returns | MV | True | MV, MAD, GMD, MSV, VaR, CVaR, TG, EVaR, RG, CVRG, TGRG, WR, FLPM, SLPM, MDD, ADD, DaR, CDaR, EDaR, UCI, MDD_Rel, ADD_Rel, DaR_Rel, CDaR_Rel, EDaR_Rel, UCI_Rel |
cvar_simulations_losses | -as --a-sim | Number of CVaRs used to approximate Tail Gini of losses. The default is 100 | 100 | True | None |
cvar_significance | -b --beta | Significance level of CVaR and Tail Gini of gains. If empty it duplicates alpha | None | True | None |
cvar_simulations_gains | -bs --b-sim | Number of CVaRs used to approximate Tail Gini of gains. If empty it duplicates a_sim value | None | True | None |
linkage | -lk --linkage | Linkage method of hierarchical clustering | single | True | single, complete, average, weighted, centroid, median, ward, dbht |
amount_clusters | -k | Number of clusters specified in advance | None | True | None |
max_clusters | -mk --max-k | Max number of clusters used by the two difference gap statistic to find the optimal number of clusters. If k is empty this value is used | 10 | True | None |
amount_bins | -bi --bins-info | Number of bins used to calculate the variation of information | KN | True | KN, FD, SC, HGR |
alpha_tail | -at --alpha-tail | Significance level for lower tail dependence index, only used when when codependence value is 'tail' | 0.05 | True | None |
leaf_order | -lo --leaf-order | Indicates if the cluster are ordered so that the distance between successive leaves is minimal | True | True | None |
smoothing_factor_ewma | -de --d-ewma | Smoothing factor for ewma estimators | 0.94 | True | None |
nan_fill_method | -mt --method | Method used to fill nan values in time series, by default time. Possible values are: 'linear': linear interpolation 'time': linear interpolation based on time index 'nearest': use nearest value to replace nan values 'zero': spline of zeroth order 'slinear': spline of first order 'quadratic': spline of second order 'cubic': spline of third order 'barycentric': builds a polynomial that pass for all points | time | True | linear, time, nearest, zero, slinear, quadratic, cubic, barycentric |
categories | -ct --categories | Show selected categories | True | None | |
historic_period | -p --period | Period to get yfinance data from. Possible frequency strings are: 'd': means days, for example '252d' means 252 days 'w': means weeks, for example '52w' means 52 weeks 'mo': means months, for example '12mo' means 12 months 'y': means years, for example '1y' means 1 year 'ytd': downloads data from beginning of year to today 'max': downloads all data available for each asset | 3y | True | 1d, 5d, 1mo, 3mo, 6mo, 1y, 2y, 3y, 5y, 10y, ytd, max |
start_period | -s --start | Start date to get yfinance data from. Must be in 'YYYY-MM-DD' format | True | None | |
end_period | -e --end | End date to get yfinance data from. Must be in 'YYYY-MM-DD' format | True | None | |
log_returns | -lr --log-returns | If use logarithmic or arithmetic returns to calculate returns | False | True | None |
return_frequency | --freq | Frequency used to calculate returns. Possible values are: 'd': for daily returns 'w': for weekly returns 'm': for monthly returns | d | True | d, w, m |
max_nan | -mn --maxnan | Max percentage of nan values accepted per asset to be considered in the optimization process | 0.05 | True | None |
threshold_value | -th --threshold | Value used to replace outliers that are higher to threshold in absolute value | 0.3 | True | None |
risk_free | -r --risk-free-rate | Risk-free rate of borrowing/lending. The period of the risk-free rate must be annual | 0.04739 | True | None |
significance_level | -a --alpha | Significance level of CVaR, EVaR, CDaR and EDaR | 0.05 | True | None |
long_allocation | -v --value | Amount to allocate to portfolio | 1 | True | None |
name | --name | Save portfolio with personalized or default name | _HRP0 | True | None |
Examplesโ
2022 Apr 05, 14:20 (๐ฆ) /portfolio/po/ $ hrp
[3 Years] Hierarchical risk parity portfolio using pearson codependence,
single linkage and volatility as risk measure
Weights
โโโโโโโโณโโโโโโโโโโ
โ โ Value โ
โกโโโโโโโโโโโโโโโโโฉ
โ AAPL โ 13.74 % โ
โโโโโโโโผโโโโโโโโโโค
โ AMZN โ 17.97 % โ
โโโโโโโโผโโโโโโโโโโค
โ BA โ 5.74 % โ
โโโโโโโโผโโโโโโโโโโค
โ FB โ 10.29 % โ
โโโโโโโโผโโโโโโโโโโค
โ MSFT โ 18.28 % โ
โโโโโโโโผโโโโโโโโโโค
โ T โ 27.57 % โ
โโโโโโโโผโโโโโโโโโโค
โ TSLA โ 6.37 % โ
โโโโโโโโดโโโโโโโโโโ
Annual (by 252) expected return: 28.03%
Annual (by โ252) volatility: 25.35%
Sharpe ratio: 1.0982