Risk Parity in Python: Equal Risk Contribution Done Right

Risk parity in one paragraph
Risk parity is an accounting idea before it is an investment idea. As Asness, Frazzini, and Pedersen put it, the instruction is: “Be diversified, but be diversified by risk not by dollars.” The term “risk parity” was coined by Edward Qian at PanAgora, and the first risk parity fund was Bridgewater’s “All Weather,” launched in 1996, according to the riskParityPortfolio vignette.
The motivation is practical: a conventional 60/40 portfolio can be dominated by equity risk because stocks are far more volatile than bonds. Equal dollars is not equal risk. A risk-balanced portfolio therefore has lower expected return unless levered, and the authors describe risk parity investors as “applying leverage to the risk-balanced portfolio.”
The formal definition of ERC
Cluster Risk Parity defines equal risk contribution as “the risk-balanced portfolio such that the total risk contribution is the same for all assets in the portfolio.” To make that precise, let marginal risk contribution be MCTR_i(w) = ∂σ(w)/∂w_i and total risk contribution be TCTR_i(w) = w_i · MCTR_i(w).
The riskParityPortfolio vignette gives the Euler decomposition:
σ(w) = Σ_i w_i (Σw)_i / √(w′Σw) = Σ_i RC_i
where
RC_i = w_i (Σw)_i / √(w′Σw)
Normalizing by portfolio risk gives relative risk contributions:
RRC_i = w_i (Σw)_i / (w′Σw)
and
Σ RRC_i = 1
The ERC condition can be written in equivalent ways:
TCTR_i = TCTR_jfor alli, jw_i(Σw)_i = w_j(Σw)_jfor alli, jRRC_i = 1/Nfor equal risk budgets
The same source calls ERC “a middle-ground alternative” to 1/N and minimum variance, “balanced in risk and in weights.” But it is also “highly dependent upon the structure of the universe of assets considered.”
Why inverse volatility is not ERC
The naive "diag" formulation in riskParityPortfolio is the inverse-volatility portfolio: weight each asset by the reciprocal of its standalone volatility. It uses only variances, not covariance cross-terms.
ERC depends on the whole covariance matrix through (Σw)_i, which includes each asset’s correlations with every other asset. Inverse volatility can satisfy the equal-risk equations only when those cross-terms happen to line up, such as in an equal-correlation structure. It is not “wrong” as a heuristic; it simply fails the ERC condition when correlations are not equal.
The orchestrator run shows the difference on synthetic data. On the same 6-asset covariance, the ERC solver produced max|RRC − 1/n| = 1.5e-13, while the inverse-volatility portfolio left max|RRC − 1/n| = 0.1217.
No closed form: solving ERC numerically
The equations above do not have a usable analytic solution. The Choi & Chen paper states plainly that “solving portfolio weights must resort to numerical methods as the analytic solution is not available.”
The common convex reformulation, associated with Spinu, is:
min 0.5·w′Σw − Σ b_i ln(w_i)
Its first-order condition is:
w_i(Σw)_i = b_i
With b_i = 1/n, this is equal risk contribution. The CCD solver lineage includes Spinu (2013) and Griveau-Billion, Richard & Roncalli (2013), as documented in the riskParityPortfolio vignette. Choi & Chen report an improved CCD solver approximately 3× faster than the original CCD, saving more than 40% of iterations.
A numpy coordinate-descent ERC solver you can read
This is a solver correctness check on synthetic data — NOT a market backtest. It supports no performance statement. The covariance is generated from np.random.default_rng(7) as A @ A.T / n with diag(uniform(0.02, 0.09)) and n = 6.
The CCD update used below is:
w_i = (−c_i + √(c_i^2 + 4σ_ii b_i)) / (2σ_ii)
with
c_i = Σ_{j≠i} σ_ij w_j
import numpy as np
def erc_weights(Sigma, budgets=None, tol=1e-12, max_iter=10000):
Sigma = np.asarray(Sigma, dtype=float)
n = Sigma.shape[0]
if budgets is None:
budgets = np.full(n, 1.0 / n)
else:
budgets = np.asarray(budgets, dtype=float)
budgets = budgets / budgets.sum()
w = 1.0 / np.sqrt(np.diag(Sigma))
for it in range(1, max_iter + 1):
w_old = w.copy()
for i in range(n):
c_i = float(Sigma[i] @ w - Sigma[i, i] * w[i])
sigma_ii = Sigma[i, i]
w[i] = (-c_i + np.sqrt(c_i * c_i + 4.0 * sigma_ii * budgets[i])) / (2.0 * sigma_ii)
if np.max(np.abs(w - w_old)) < tol:
break
w = w / np.sum(w)
return w, it
def risk_contributions(w, Sigma):
sigma_w = Sigma @ w
variance = w @ sigma_w
return (w * sigma_w) / variance
# Solver correctness check on synthetic data only.
# This is not a market backtest and supports no performance statement.
rng = np.random.default_rng(7)
n = 6
A = rng.normal(size=(n, n))
Sigma = A @ A.T / n + np.diag(rng.uniform(0.02, 0.09, size=n))
Sigma = (Sigma + Sigma.T) / 2.0
w, iterations = erc_weights(Sigma)
rrc = risk_contributions(w, Sigma)
max_dev = np.max(np.abs(rrc - 1 / n))
print("weights:", np.round(w, 6).tolist())
print("sum(w):", np.round(np.sum(w), 1))
if max_dev < 1e-12:
print("RRC = 1/6 exactly for all six assets")
else:
print("RRC:", np.round(rrc, 12))
print(f"max deviation: {max_dev:.1e}")
print("iterations:", iterations)
On the orchestrator run, the printed output was exactly:
weights: [0.145629, 0.304743, 0.308484, 0.060623, 0.070318, 0.110203]
sum(w): 1.0
RRC = 1/6 exactly for all six assets
max deviation: 1.5e-13
iterations: 25
The solver reaches equal risk contributions to the reported tolerance on the synthetic matrix. The inverse-volatility portfolio on the same matrix leaves the 0.1217 gap quoted above.
ERC with Riskfolio-Lib
For production work, you can use the Riskfolio-Lib 7.4 API. The documented method is:
Portfolio.rp_optimization(model='Classic', rm='MV', rf=0, b=None, b_f=None, hist=True)
with model ∈ {'Classic','FM'} and b = "The vector of risk constraints per asset. The default is 1/n (number of assets)." The same API page also documents Portfolio.rrp_optimization, the relaxed risk parity model, whose version parameter is described as the relaxed risk parity model version “proposed in” Richard & Roncalli (the underlying method follows Gambeta & Kwon, 2020).
The snippet below uses only documented parameters and does not add expected-return inputs, risk-free rates used in a computation, or constraints.
# Assuming `port` is a Riskfolio-Lib Portfolio object constructed from a returns DataFrame.
# Uses only documented parameters from the Riskfolio-Lib 7.4 API.
w_erc = port.rp_optimization(
model="Classic",
rm="MV",
b=None,
hist=True,
)
# b=None uses the documented default:
# "The vector of risk constraints per asset. The default is 1/n (number of assets)."
Beyond ERC: HRP, HERC, NCO, and formulations
The riskParityPortfolio vignette distinguishes three formulations: the naive "diag" formulation, a vanilla convex formulation, and a general nonconvex formulation. The "diag" formulation is inverse volatility; the convex formulation is the Spinu form used above.
The Riskfolio-Lib hierarchical-clustering docs document a separate class for the clustering family: HRP, HERC and NCO, each with its own objective set. These are different optimization objectives from ERC; HRP is not automatically equal risk contribution unless the objective explicitly targets ERC.
# Assuming `hc_port` is an HCPortfolio object.
# Uses only documented parameters from the Riskfolio-Lib 7.4 API.
w_hrp = hc_port.optimization(
model="HRP",
codependence="pearson",
obj="MinRisk",
rm="MV",
l=2,
)
w_herc = hc_port.optimization(
model="HERC",
codependence="pearson",
obj="MinRisk",
rm="MV",
l=2,
)
w_nco = hc_port.optimization(
model="NCO",
codependence="pearson",
obj="MinRisk",
rm="MV",
l=2,
)
| Method | Uses expected returns? | Uses covariance off-diagonals? | Target risk contribution | Rebalance behaviour | Main failure mode |
|---|---|---|---|---|---|
1/N |
No | No | None | Equal dollar weights | Risk concentrates in volatile assets |
| Inverse-vol | No | No, only variances | None | Volatility-scaled dollar weights | Ignores correlation cross-terms |
| ERC | No | Yes | Equal RRC by construction |
Numerical solve; changes with Σ |
Sensitive to covariance structure and duplication |
| HRP | No for the documented MinRisk call shown |
Indirectly, through correlation/clustering structure | Not equal RC unless ERC objective is targeted | Hierarchical allocation | Does not automatically satisfy the ERC equations |
What the evidence says: leverage aversion and the risk premium
The Leverage Aversion Risk Parity paper frames the historical case for risk-budgeting portfolios. The paper’s framing is that risk parity “outperformed the market over the last century.” But the same paper also states that risk-balanced portfolios have lower expected return unless levered, and that risk parity investors “apply leverage to the risk-balanced portfolio.”
The numbers below are from Table 2, Panel A and Panel B, comparing Risk Parity minus Value Weighted over the stated samples.
| Panel | Sample | Excess Return | Alpha | Volatility | Sharpe | Skewness | Excess Kurtosis |
|---|---|---|---|---|---|---|---|
| A | stocks & bonds, 1926–2010 | 4.15 (t=2.95) | 5.50 (t=4.30) | 12.69 | 0.33 | −0.79 | 8.30 |
| B | global stocks, bonds, credit, commodities, 1973–2010 | 1.84 (t=1.43) | 3.03 (t=2.52) | 7.52 | 0.24 | 0.31 | 2.51 |
Both panels are reported with T-bills as the risk-free rate. These are historical sample statements, not a semiconductor backtest and not a per-ticker claim.
The duplication problem and cluster risk parity
Cluster Risk Parity notes that ERC is not duplication invariant. Minimum variance is duplication invariant, but ERC is “biased toward assets with multiple representations.”
The Roncalli & Weisang example shows why. With 5 equity and 5 bond indices, the solution is “well balanced between equity and bond in terms of risk.” With 7 equity and 3 bond indices, equity risk becomes 70% of total portfolio risk, “a solution very unbalanced between equity and bond risks.”
The fix is Cluster Risk Parity (Varadi/Kapler): 3 steps — partition into clusters, ERC within clusters, ERC across clusters.
Honest limitations in a concentrated AI-silicon universe
The riskParityPortfolio vignette quotes a critique of mean-variance optimization: it considers “only … the risk of the portfolio as a whole”, ignores risk diversification, and is “highly sensitive to the estimation errors in the parameters.” The same vignette describes the risk-parity design as “less sensitive to parameter estimation errors” than mean-variance, though ERC still requires an estimate of Σ.
In a concentrated AI-silicon universe, the practical risks are not exotic. They are the standard quantitative ones:
- covariance regime shifts can change what “equal risk” means;
- duplication bias can overweight assets with multiple representations;
- a risk-balanced portfolio can tilt toward lower-volatility assets and may require leverage to reach a target portfolio risk;
- the AFP paper explicitly ties the 60/40 critique to equity risk dominance, not to a semiconductor-specific edge;
- the Riskfolio-Lib API documents tools, but it does not remove the need to define a universe carefully.
What the numbers don’t tell you (yet)
The ten tickers this blog tracks — NVDA, AMD, AVGO, TSM, ASML, MU, INTC, QCOM, TXN, ARM — are framing only. The sources cited here contain no per-ticker volatility, correlation, or return figures, and no backtest on those ten names was run.
FAQ
Is inverse volatility the same as risk parity?
No. riskParityPortfolio states the naive "diag" formulation is the inverse-volatility portfolio. It uses standalone volatility and ignores covariance cross-terms, so it generally does not satisfy the ERC condition.
Do I need expected returns to compute ERC?
No. The Spinu convex form and the Riskfolio-Lib API documented rp_optimization call rely on covariance and risk budgets, not expected returns.
Why can’t I just write down the weights?
Because Choi & Chen state that “solving portfolio weights must resort to numerical methods as the analytic solution is not available.”
Does HRP guarantee equal risk contributions?
No. The Riskfolio-Lib API documents HRP as a model in HCPortfolio.optimization, but equal risk contribution is a separate ERC condition. It is only guaranteed if the optimization explicitly targets ERC.
Why does the universe matter so much?
Cluster Risk Parity says ERC is highly dependent on universe structure and is not duplication invariant. Adding duplicate representations can change the risk balance.
Is this a buy signal for semiconductors?
No. This guide contains no per-ticker market statistics and no AI-silicon backtest. The only executed code is a synthetic solver correctness check.
The Bottom Line
Risk parity is an equation, not a slogan. Inverse volatility is a useful heuristic when correlations are benign, but it is not equal risk contribution. If you want ERC, solve the covariance-based condition numerically, verify it with a small numpy solver, and use documented tools such as Riskfolio-Lib when you need production features. The AFP evidence supports the risk-budgeting logic historically, but it also makes clear that leverage is often required to reach a target risk level. For a concentrated AI-silicon universe, the sources do not provide the per-ticker numbers needed to make a performance claim.
How This Guide Was Built
This post is based on six verified sources: Cluster Risk Parity, the riskParityPortfolio vignette, Choi & Chen, the Riskfolio-Lib 7.4 API and its hierarchical-clustering docs, and Leverage Aversion Risk Parity. The numpy code was executed on a synthetic covariance generated with np.random.default_rng(7); it is a solver correctness check, not a market backtest, and the printed output in this post is the actual output of the code as published. No per-ticker statistics for NVDA, AMD, AVGO, TSM, ASML, MU, INTC, QCOM, TXN, or ARM were used, and no performance conclusion is drawn about the AI-silicon universe.


