File size: 2,265 Bytes
0125724
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""
TradeSense AI — Conditional Replication Unit Tests
Validates:
- Untouched 20-asset Tech/Consumer cohort definition
- Cohort evaluation pipeline execution
"""

from datetime import date, timedelta
import numpy as np
import pandas as pd
import pytest

from quant.research.conditional_replication_engine import (
    ConditionalReplicationEngine,
    CohortReplicationReport,
    UNTOUCHED_TECH_CONSUMER_20,
)


def generate_synthetic_universe(tickers: list, n_rows: int = 250) -> dict:
    np.random.seed(42)
    start_date = date(2020, 1, 1)
    dates = [start_date + timedelta(days=i) for i in range(n_rows)]
    universe = {}

    for idx, tk in enumerate(tickers):
        drift = 0.0004 + (idx * 0.0001)
        vol = 0.012 + (idx * 0.002)
        rets = np.random.normal(drift, vol, n_rows)
        close = 100.0 * np.cumprod(1.0 + rets)
        high = close * 1.01
        low = close * 0.99
        open_p = (high + low) / 2.0
        vol_shares = np.random.randint(1000000, 5000000, n_rows)

        universe[tk] = pd.DataFrame({
            "session": dates,
            "Open": open_p,
            "High": high,
            "Low": low,
            "Close": close,
            "Volume": vol_shares,
        })

    return universe


class TestConditionalReplication:
    def test_cohort_definition_structure(self):
        tks = ConditionalReplicationEngine.get_cohort_tickers()
        sec_map = ConditionalReplicationEngine.get_cohort_sector_map()

        assert len(tks) == 20
        assert len(sec_map) == 20
        assert "QCOM" in tks
        assert "BKNG" in tks

    def test_evaluate_untouched_cohort_execution(self):
        tks = ["QCOM", "TXN", "AVGO", "BKNG", "ABNB", "ORLY", "SPY"]
        universe = generate_synthetic_universe(tks, n_rows=300)

        rep: CohortReplicationReport = ConditionalReplicationEngine.evaluate_untouched_cohort(
            cohort_dfs={tk: universe[tk] for tk in tks if tk != "SPY"},
            spy_df=universe["SPY"],
            horizon_sessions=5,
        )

        assert rep.n_cohort_assets == 6
        assert rep.orthogonalization_audit.n_sessions > 0
        assert rep.purged_walk_forward.total_folds > 0
        assert isinstance(rep.is_graduation_to_robust_conditional_alpha_approved, bool)