"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(quantlib): handle NaN in estimation window for Corrado and Cowan event study tests
santhrealf80e66af8a02
diff --git a/agent/src/quantlib/eventstudy.py b/agent/src/quantlib/eventstudy.pyindex afac3dfbe..be00368ba 100644--- a/agent/src/quantlib/eventstudy.py+++ b/agent/src/quantlib/eventstudy.py@@ -419,8 +419,11 @@ def event_study(car_se = float(np.sqrt(np.sum(daily_se**2)))standardised = car / car_se if car_se > 0 else float("nan")- est_residuals = asset_estimation.to_numpy(dtype=float) - (- fit.alpha + fit.beta * market_estimation.to_numpy(dtype=float)+ asset_est_arr = asset_estimation.to_numpy(dtype=float)+ market_est_arr = market_estimation.to_numpy(dtype=float)+ keep_est = np.isfinite(asset_est_arr) & np.isfinite(market_est_arr)+ est_residuals = asset_est_arr[keep_est] - (+ fit.alpha + fit.beta * market_est_arr[keep_est])outcomes.append(@@ -523,7 +526,12 @@ def event_study(rank_dev_est_list.append(ranks_est)rank_dev_evt_list.append(ranks_evt)- if n_events > 1 and all(len(r) == len(rank_dev_est_list[0]) for r in rank_dev_est_list):+ if (+ n_events > 1+ and rank_dev_est_list+ and len(rank_dev_est_list[0]) > 0+ and all(len(r) == len(rank_dev_est_list[0]) for r in rank_dev_est_list)+ ):est_rank_matrix = np.vstack(rank_dev_est_list) # (n_events, T_est)mean_est_ranks = est_rank_matrix.mean(axis=0) # mean rank dev per estimation days_k = float(np.sqrt(np.mean(mean_est_ranks**2)))diff --git a/agent/tests/quantlib/test_eventstudy.py b/agent/tests/quantlib/test_eventstudy.pyindex c0e7e50c8..9de48c314 100644--- a/agent/tests/quantlib/test_eventstudy.py+++ b/agent/tests/quantlib/test_eventstudy.py@@ -414,3 +414,14 @@ def test_corrado_rank_and_cowan_sign_single_event_safe_nan():assert np.isnan(result.cowan_sign_z)assert np.isnan(result.cowan_sign_p_value)assert 0.0 <= result.positive_car_fraction <= 1.0+++def test_corrado_rank_and_cowan_sign_tolerate_nan_in_estimation_period():+ returns, market = _panel(seed=803)+ event_day = returns.index[250]+ # Insert scattered NaNs in the pre-event estimation window+ returns.iloc[50, 0] = np.nan+ market.iloc[60] = np.nan+ events = [(s, event_day) for s in returns.columns]+ result = event_study(returns, market, events, event_window=(0, 2))+ assert np.isfinite(result.positive_car_fraction)