Skip to content

Commit 5ba5440

Browse files
committed
Add missing _normalize_weights helper for ERC solver
1 parent 9f6ef72 commit 5ba5440

1 file changed

Lines changed: 6 additions & 5 deletions

File tree

risk_engine/market.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88

99
# -------- Core helpers --------
10+
def _normalize_weights(w: np.ndarray) -> np.ndarray:
11+
"""Normalize weights to sum to 1 (safe if sum is 0)."""
12+
s = float(np.sum(w))
13+
return (w / s) if s != 0 else w
14+
1015
def portfolio_returns(returns: pd.DataFrame, weights: np.ndarray) -> pd.Series:
1116
"""Project multivariate return matrix onto portfolio weights."""
1217
return pd.Series(returns.values @ weights, index=returns.index, name="r_p")
@@ -121,7 +126,6 @@ def backtest_var_historical(returns: pd.DataFrame, weights: np.ndarray,
121126
"kupiec_pvalue": pval,
122127
}
123128

124-
import numpy as np
125129

126130
def _cov_shrink(cov: np.ndarray, lam: float = 0.01) -> np.ndarray:
127131
"""
@@ -324,7 +328,7 @@ def backtest_fhs_var(
324328
"hit_rate": hit_rate,
325329
}
326330

327-
_N = NormalDist()
331+
328332

329333
def normalize_weights(w: np.ndarray) -> np.ndarray:
330334
s = float(np.sum(w))
@@ -388,9 +392,6 @@ def incremental_var_normal(
388392

389393
# ---------- Risk Parity (Equal Risk Contribution) ----------
390394

391-
def _normalize_weights(w: np.ndarray) -> np.ndarray:
392-
s = float(np.sum(w))
393-
return (w / s) if s != 0 else w
394395

395396
def erc_weights_from_cov(
396397
cov: np.ndarray,

0 commit comments

Comments
 (0)