From f4559cb8e7dea6fc2c471990978ef9b11f20beb6 Mon Sep 17 00:00:00 2001 From: NaiveCoding <71413722+ReviveCoding@users.noreply.github.com> Date: Wed, 4 Nov 2026 03:37:54 -0500 Subject: [PATCH 1/3] Clean up Python imports, annotations and collection expressions --- src/derivguard/post_validation_v1_1.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/derivguard/post_validation_v1_1.py b/src/derivguard/post_validation_v1_1.py index f18949e..d13ad3d 100644 --- a/src/derivguard/post_validation_v1_1.py +++ b/src/derivguard/post_validation_v1_1.py @@ -692,7 +692,7 @@ def _attach_heston_greek_disagreement(rows: pd.DataFrame) -> pd.DataFrame: def _block_intervals(group: pd.DataFrame, draws: int, seed: int) -> dict[str, tuple[float, float]]: dates = np.asarray(sorted(group["quote_date"].unique())) if dates.size < 2: - return {metric: (np.nan, np.nan) for metric in BOOTSTRAP_METRICS} + return dict.fromkeys(BOOTSTRAP_METRICS, (np.nan, np.nan)) blocks: dict[object, dict[str, FloatArray]] = {} for date in dates: block = group[group["quote_date"] == date] @@ -768,7 +768,7 @@ def slice_metrics(rows: pd.DataFrame, config: AnalyticsConfig) -> pd.DataFrame: intervals = ( _block_intervals(group, config.bootstrap_draws, config.seed + len(records) * 17) if result["defensible"] - else {metric: (np.nan, np.nan) for metric in BOOTSTRAP_METRICS} + else dict.fromkeys(BOOTSTRAP_METRICS, (np.nan, np.nan)) ) for metric, (low, high) in intervals.items(): result[f"{metric}_ci_low"] = low @@ -806,7 +806,7 @@ def two_dimensional_metrics(rows: pd.DataFrame, config: AnalyticsConfig) -> pd.D intervals = ( _block_intervals(group, config.bootstrap_draws, config.seed + len(records) * 29) if result["defensible"] - else {metric: (np.nan, np.nan) for metric in BOOTSTRAP_METRICS} + else dict.fromkeys(BOOTSTRAP_METRICS, (np.nan, np.nan)) ) for metric, (low, high) in intervals.items(): result[f"{metric}_ci_low"] = low From 5578f9457975bc0f47746b3dcc84ffe1b9059498 Mon Sep 17 00:00:00 2001 From: NaiveCoding <71413722+ReviveCoding@users.noreply.github.com> Date: Thu, 5 Nov 2026 06:15:16 -0500 Subject: [PATCH 2/3] Clarify local names and evaluation terminology --- src/derivguard/data/forward.py | 52 ++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/src/derivguard/data/forward.py b/src/derivguard/data/forward.py index b90ff12..59bb0e3 100644 --- a/src/derivguard/data/forward.py +++ b/src/derivguard/data/forward.py @@ -181,37 +181,47 @@ def estimate_forward_discount( spread-weighted Huber IRLS. The spread weight is inverse variance using the root-sum-square of call and put quoted spreads. """ - k = _as_vector(strikes, "strikes") - c = _as_vector(calls, "calls") - p = _as_vector(puts, "puts") - if not (len(k) == len(c) == len(p)) or len(k) < 3: + strike_prices = _as_vector(strikes, "strikes") + call_prices = _as_vector(calls, "calls") + put_prices = _as_vector(puts, "puts") + if ( + not (len(strike_prices) == len(call_prices) == len(put_prices)) + or len(strike_prices) < 3 + ): raise ValueError("at least three equally sized call/put pairs are required") - if np.any(k <= 0) or np.any(c < 0) or np.any(p < 0): + if np.any(strike_prices <= 0) or np.any(call_prices < 0) or np.any(put_prices < 0): raise ValueError("strikes must be positive and option prices nonnegative") if method not in {"F0", "F1", "F2", "F3"}: raise ValueError(f"unknown forward estimator: {method}") spread_weighted = method in {"F1", "F3"} robust = method in {"F2", "F3"} - base_weights = np.ones_like(k) + base_weights = np.ones_like(strike_prices) if spread_weighted: if call_spreads is None or put_spreads is None: raise ValueError(f"{method} requires call_spreads and put_spreads") - cs = _as_vector(call_spreads, "call_spreads") - ps = _as_vector(put_spreads, "put_spreads") - if len(cs) != len(k) or len(ps) != len(k) or np.any(cs < 0) or np.any(ps < 0): + call_spread_values = _as_vector(call_spreads, "call_spreads") + put_spread_values = _as_vector(put_spreads, "put_spreads") + if ( + len(call_spread_values) != len(strike_prices) + or len(put_spread_values) != len(strike_prices) + or np.any(call_spread_values < 0) + or np.any(put_spread_values < 0) + ): raise ValueError("spreads must be nonnegative and match pair count") - scale = np.maximum(np.hypot(cs, ps), spread_floor) + scale = np.maximum( + np.hypot(call_spread_values, put_spread_values), spread_floor + ) base_weights = 1.0 / np.square(scale) base_weights /= np.mean(base_weights) - differences = c - p + call_minus_put = call_prices - put_prices weights = base_weights.copy() - beta = _weighted_fit(k, differences, weights) + beta = _weighted_fit(strike_prices, call_minus_put, weights) converged = not robust iterations = 1 if robust: for iteration in range(1, max_iterations + 1): iterations = iteration - residual = differences - (beta[0] + beta[1] * k) + residual = call_minus_put - (beta[0] + beta[1] * strike_prices) median = np.median(residual) mad = np.median(np.abs(residual - median)) robust_scale = max(1.4826 * mad, np.finfo(np.float64).eps) @@ -219,8 +229,12 @@ def estimate_forward_discount( huber_weights = np.ones_like(standardized) outside = standardized > huber_delta huber_weights[outside] = huber_delta / standardized[outside] - new_beta = _weighted_fit(k, differences, base_weights * huber_weights) - if np.linalg.norm(new_beta - beta) <= tolerance * (1.0 + np.linalg.norm(beta)): + new_beta = _weighted_fit( + strike_prices, call_minus_put, base_weights * huber_weights + ) + if np.linalg.norm(new_beta - beta) <= tolerance * ( + 1.0 + np.linalg.norm(beta) + ): beta = new_beta weights = base_weights * huber_weights converged = True @@ -230,11 +244,13 @@ def estimate_forward_discount( intercept, slope = float(beta[0]), float(beta[1]) discount = -slope if not (0.0 < discount <= 1.5): - raise ValueError(f"parity regression produced implausible discount factor {discount}") + raise ValueError( + f"parity regression produced implausible discount factor {discount}" + ) forward = intercept / discount if forward <= 0: raise ValueError(f"parity regression produced nonpositive forward {forward}") - residual = differences - (intercept + slope * k) + residual = call_minus_put - (intercept + slope * strike_prices) return ForwardEstimate( method=method, forward=forward, @@ -243,7 +259,7 @@ def estimate_forward_discount( slope=slope, residual_rmse=float(np.sqrt(np.mean(np.square(residual)))), weighted_rmse=float(np.sqrt(np.average(np.square(residual), weights=weights))), - observations=len(k), + observations=len(strike_prices), iterations=iterations, converged=converged, ) From 8229df2530cd3fb7665c5cce26027d611afdbe49 Mon Sep 17 00:00:00 2001 From: NaiveCoding <71413722+ReviveCoding@users.noreply.github.com> Date: Thu, 5 Nov 2026 06:18:54 -0500 Subject: [PATCH 3/3] Format forward estimator names with repository Ruff settings --- src/derivguard/data/forward.py | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/src/derivguard/data/forward.py b/src/derivguard/data/forward.py index 59bb0e3..863d389 100644 --- a/src/derivguard/data/forward.py +++ b/src/derivguard/data/forward.py @@ -184,10 +184,7 @@ def estimate_forward_discount( strike_prices = _as_vector(strikes, "strikes") call_prices = _as_vector(calls, "calls") put_prices = _as_vector(puts, "puts") - if ( - not (len(strike_prices) == len(call_prices) == len(put_prices)) - or len(strike_prices) < 3 - ): + if not (len(strike_prices) == len(call_prices) == len(put_prices)) or len(strike_prices) < 3: raise ValueError("at least three equally sized call/put pairs are required") if np.any(strike_prices <= 0) or np.any(call_prices < 0) or np.any(put_prices < 0): raise ValueError("strikes must be positive and option prices nonnegative") @@ -208,9 +205,7 @@ def estimate_forward_discount( or np.any(put_spread_values < 0) ): raise ValueError("spreads must be nonnegative and match pair count") - scale = np.maximum( - np.hypot(call_spread_values, put_spread_values), spread_floor - ) + scale = np.maximum(np.hypot(call_spread_values, put_spread_values), spread_floor) base_weights = 1.0 / np.square(scale) base_weights /= np.mean(base_weights) call_minus_put = call_prices - put_prices @@ -229,12 +224,8 @@ def estimate_forward_discount( huber_weights = np.ones_like(standardized) outside = standardized > huber_delta huber_weights[outside] = huber_delta / standardized[outside] - new_beta = _weighted_fit( - strike_prices, call_minus_put, base_weights * huber_weights - ) - if np.linalg.norm(new_beta - beta) <= tolerance * ( - 1.0 + np.linalg.norm(beta) - ): + new_beta = _weighted_fit(strike_prices, call_minus_put, base_weights * huber_weights) + if np.linalg.norm(new_beta - beta) <= tolerance * (1.0 + np.linalg.norm(beta)): beta = new_beta weights = base_weights * huber_weights converged = True @@ -244,9 +235,7 @@ def estimate_forward_discount( intercept, slope = float(beta[0]), float(beta[1]) discount = -slope if not (0.0 < discount <= 1.5): - raise ValueError( - f"parity regression produced implausible discount factor {discount}" - ) + raise ValueError(f"parity regression produced implausible discount factor {discount}") forward = intercept / discount if forward <= 0: raise ValueError(f"parity regression produced nonpositive forward {forward}")