Stacking Weights

Calculate the weights of a stacked portfolio.

Several optimizers each give their portfolio, and a final optimizer divides the weight over those portfolios as if they were assets, judged by the returns each earns out of sample. Each optimizer has its own strengths (inverse volatility is robust, maximum diversification spreads the risk, HRP avoids inverting the covariance matrix, minimum CVaR guards the tail); stacking combines them by how they hold up rather than choosing one.

The formula is as follows:

\[w = W v,\;\; \text{with } W \text{ the } N \times K \text{ matrix whose column } k \text{ holds the weights of method } k \text{ optimized on all periods, and } v \text{ the weights the final method gives the } K \text{ portfolios}\]
  • The final method is optimized on the returns of the K portfolios. With folds, the periods are cut into that many consecutive blocks; for each block the methods are optimized on the other periods and their portfolios earn the block’s returns, so every return the final method sees is out of sample (a K-fold cross-validation without shuffling). Without folds it uses the in-sample returns R W, which favours the methods that fit the past best.

Also known as: stacking, stacked generalization, ensemble optimization.

Example · AAPL, MSFT, JNJ, XOM · 2021 to 2025

Install the Finance Toolkit with pip install financetoolkit -U, then call get_stacking_weights:

from financetoolkit import Toolkit

toolkit = Toolkit(
    ["AAPL", "MSFT", "JNJ", "XOM"],
    api_key="FINANCIAL_MODELING_PREP_KEY",
    start_date="2021-01-01",
    end_date="2025-12-31",
)

toolkit.portfolio.get_stacking_weights()[
    [
        "Weight",
        "Maximum Diversification",
        "Hierarchical Risk Parity",
        "Minimum CVaR",
    ]
]

Or, with the Finance Toolkit MCP set up, ask an AI assistant such as Claude or ChatGPT:

How would a stacked portfolio of several optimizers weight Apple, Microsoft, Johnson & Johnson and ExxonMobil, based on 2021 to 2025, and how do maximum diversification, hierarchical risk parity and minimum CVaR weight them on their own?

Which returns:

Parameters

get_stacking_weights accepts the following parameters.

ParameterTypeDescriptionExample
methodsstr | list[str] | NoneThe optimizers to combine, a list or a comma-separated string, each with its default settings. Options are: “minimum_variance” (Minimum Variance), “maximum_sharpe” (Maximum Sharpe), “risk_parity” (Risk Parity), “inverse_volatility” (Inverse Volatility), “maximum_diversification” (Maximum Diversification), “hierarchical_risk_parity” (Hierarchical Risk Parity), “hierarchical_equal_risk_contribution” (Hierarchical Equal Risk Contribution), “nested_clustered_optimization” (Nested Clustered Optimization), “schur_complementary” (Schur Complementary), “minimum_cvar” (Minimum CVaR), “minimum_drawdown” (Minimum Drawdown), “minimum_mad” (Minimum MAD), “minimum_semideviation” (Minimum Semideviation), “equal_weight” (Equal Weight). Defaults to None, which uses “inverse_volatility”, “maximum_diversification”, “hierarchical_risk_parity” and “minimum_cvar”.None, which uses "inverse_volatility", "maximum_diversification", "hierarchical_risk_parity" and "minimum_cvar"
final_methodstrThe optimizer that weights the methods’ portfolios. Options are: “minimum_variance” (Minimum Variance), “maximum_sharpe” (Maximum Sharpe), “risk_parity” (Risk Parity), “inverse_volatility” (Inverse Volatility), “maximum_diversification” (Maximum Diversification), “hierarchical_risk_parity” (Hierarchical Risk Parity), “hierarchical_equal_risk_contribution” (Hierarchical Equal Risk Contribution), “nested_clustered_optimization” (Nested Clustered Optimization), “schur_complementary” (Schur Complementary), “minimum_cvar” (Minimum CVaR), “minimum_drawdown” (Minimum Drawdown), “minimum_mad” (Minimum MAD), “minimum_semideviation” (Minimum Semideviation), “equal_weight” (Equal Weight). Defaults to “minimum_variance”."minimum_variance"
foldsint | NoneThe number of consecutive blocks of the cross-validation, at least 2, or None to use the in-sample returns. Defaults to 5.5
periodstrThe frequency of the returns. Options are: “daily” (daily returns), “weekly” (weekly returns), “monthly” (monthly returns), “quarterly” (quarterly returns), “yearly” (yearly returns). Defaults to “daily”."weekly"
covariancestrThe estimator of the covariance matrix of the methods that use one (see get_minimum_variance_weights). Defaults to. Options are: “sample” (the sample covariance, ddof=1), “ledoit_wolf” (shrunk towards the scaled identity, Ledoit and Wolf, 2004), “ledoit_wolf_constant_correlation” (shrunk towards the average correlation, Ledoit and Wolf, 2004), “ledoit_wolf_single_factor” (shrunk towards the covariance that the benchmark as a single factor implies, Ledoit and Wolf, 2003), “oas” (Oracle Approximating Shrinkage, Chen et al., 2010), “shrunk” (a fixed 10% towards the scaled identity), “semicovariance” (the co-movement below a zero return, Estrada, 2008), “exponential” (exponentially weighted, recent periods weigh most, RiskMetrics), “denoised” (the eigenvalues of the correlation matrix that random returns could produce averaged, Laloux et al., 2000), “denoised_spectral” (those eigenvalues set to zero), “denoised_shrink” (the noise correlations shrunk towards their diagonal, López de Prado, 2020), “gerber” (co-movements beyond half a standard deviation, Gerber et al., 2022), “factor” (the covariance of the Fama and French five factor model with OLS loadings, Fama and French, 1993), “minimum_covariance_determinant” (the reweighted covariance of the half of the periods with the smallest covariance determinant, robust to outliers, Rousseeuw and Van Driessen, 1999), “jlogo” (the sparse inverse covariance on the TMFG planar graph of the correlations, Barfuss et al., 2016), “geodesic” (10% towards the scaled identity along the geodesic of the positive definite matrices, Bhatia, 2007), “regime_adjusted_ew” (exponentially weighted with a half-life of 40 periods, rescaled by the recent realized against predicted risk, Paleologo, 2025), “nercome” (eigenvectors from one part of the periods and the variances along them from the other, averaged over random splits, Lam, 2016), “ccc_garch” (the next-period covariance of GARCH(1, 1) volatilities with constant correlations, Bollerslev, 1990), “mm” (the robust MM-estimator, Maronna et al., 2019), “rocke” (Rocke’s robust S-estimator, Maronna et al., 2019). Defaults to “sample”."sample"
covariance_spanintThe span in periods of the exponentially weighted covariance; the weight of a period halves after about span / 2.9 periods. Defaults to 180.180
expected_returnsstrThe estimator of the expected returns of “maximum_sharpe” (see get_maximum_sharpe_weights). Defaults to. Options are: “mean” (the historical average), “geometric” (the compounded average), “exponential” (the exponentially weighted average, recent periods weigh most), “exponential_geometric” (that average compounded over a year), “capm” (the returns the Capital Asset Pricing Model implies from each asset’s beta to the benchmark, Sharpe, 1964), “bayes_stein” (the averages shrunk towards that of the minimum variance portfolio, Jorion, 1986), “james_stein” (the averages shrunk towards their grand mean, James and Stein, 1961), “bodnar_okhrin_parolya” (the averages and their grand mean combined with optimal weights, Bodnar, Okhrin and Parolya, 2019). Defaults to “mean”."mean"
expected_returns_spanintThe span in periods of the exponentially weighted expected returns. Defaults to 180.180
include_benchmarkboolWhether to include the benchmark as an asset. Defaults to False.True
roundingint | NoneThe number of decimals to round the results to. Defaults to None, which uses the Toolkit’s rounding.2
show_columnsstr | list[str] | NoneThe columns to keep, a name or a list of names, matched on every column level, or the rows when no column has those names (such as the line items of a financial statement). Names that do not exist are logged, and when none exist the result is returned in full. Defaults to None, which keeps every column.None, which keeps every column

References

  • Paper Wolpert, D. H. (1992). “Stacked Generalization.” Neural Networks, 5(2), 241-259.
  • Library Referenced against skfolio’s StackingOptimization
Share