"""High-level Signal Analyzer & Event Study runners with write-before-return trial logging.""" from __future__ import annotations from typing import Any, Mapping import polars as pl from .calculations import analyze_signal_panel, compute_event_study from .ledger import ( ClusterDsrSummary, HorizonBucket, RecordedTrial, TrialLedgerClient, UniverseMarket, canonical_expression_hash, ) def run_signal_study( *, panel: pl.DataFrame, ledger: TrialLedgerClient, principal_id: str = "operator", universe_market: UniverseMarket, horizon_bucket: HorizonBucket, horizon_label: str, expression: str, params: Mapping[str, Any], vault_cutoff_date: str, n_quantiles: int = 5, min_cross_section: int = 10, ledger_series: str = "rank_ic", ) -> dict[str, Any]: """Runs Signal Analyzer under the Vault fence, writes to `trial_ledger`, and computes cluster DSR.""" analysis = analyze_signal_panel( panel, vault_cutoff_date=vault_cutoff_date, n_quantiles=n_quantiles, min_cross_section=min_cross_section, ) dates: list[str] = analysis["dates"] # type: ignore[assignment] if len(dates) < 2: return {"status": "insufficient_data", "analysis": analysis, "trial": None, "dsr": None} series: list[float] = ( analysis["rank_ic_series"] if ledger_series == "rank_ic" else analysis["spread_series"] ) # type: ignore[assignment] expr_hash = canonical_expression_hash( expression=expression, params=params, universe=universe_market, sample_start=dates[0], sample_end=dates[-1], horizon=horizon_label, ) recorded: RecordedTrial = ledger.record_trial( principal_id=principal_id, stage="signal_ic", universe_market=universe_market, horizon_bucket=horizon_bucket, expression_hash=expr_hash, sample_start=dates[0], sample_end=dates[-1], vault_cutoff_date=vault_cutoff_date, daily_series=series, ) dsr: ClusterDsrSummary = ledger.cluster_deflated_sharpe( trial_id=recorded.trial_id, observation_dates_by_trial_id={recorded.trial_id: dates}, ) return { "status": "ok", "analysis": analysis, "trial": recorded, "dsr": dsr, } def run_event_study_trial( *, bars_df: pl.DataFrame, events_df: pl.DataFrame, ledger: TrialLedgerClient, principal_id: str = "operator", universe_market: UniverseMarket, horizon_bucket: HorizonBucket, expression: str, params: Mapping[str, Any], vault_cutoff_date: str, pre_days: int = 5, post_days: int = 10, min_events: int = 5, ) -> dict[str, Any]: """Runs Event Study under the Vault fence, writes to `trial_ledger`, and computes cluster DSR.""" study = compute_event_study( bars_df, events_df, vault_cutoff_date=vault_cutoff_date, pre_days=pre_days, post_days=post_days, min_events=min_events, ) if study.get("status") != "ok": return {"status": "insufficient_data", "study": study, "trial": None, "dsr": None} dates: list[str] = study["clustered_dates"] # type: ignore[assignment] series: list[float] = study["mean_cars"] # type: ignore[assignment] expr_hash = canonical_expression_hash( expression=expression, params=params, universe=universe_market, sample_start=dates[0], sample_end=dates[-1], horizon=f"event_-{pre_days}_+{post_days}d", ) recorded = ledger.record_trial( principal_id=principal_id, stage="event_study", universe_market=universe_market, horizon_bucket=horizon_bucket, expression_hash=expr_hash, sample_start=dates[0], sample_end=dates[-1], vault_cutoff_date=vault_cutoff_date, daily_series=series, ) dsr = ledger.cluster_deflated_sharpe( trial_id=recorded.trial_id, observation_dates_by_trial_id={recorded.trial_id: dates}, ) return { "status": "ok", "study": study, "trial": recorded, "dsr": dsr, }