|
| 1 | +-- ============================================================================ |
| 2 | +-- AWARE ML Explainability Schema |
| 3 | +-- ============================================================================ |
| 4 | +-- Stores ML model metadata, feature importance, tier boundaries, and training history |
| 5 | +-- for transparency, monitoring, and debugging. |
| 6 | +-- ============================================================================ |
| 7 | + |
| 8 | +-- ---------------------------------------------------------------------------- |
| 9 | +-- Feature Importance |
| 10 | +-- ---------------------------------------------------------------------------- |
| 11 | +-- Stores feature importance scores from XGBoost and other models. |
| 12 | +-- Updated after each training run. |
| 13 | + |
| 14 | +CREATE TABLE IF NOT EXISTS polybot.aware_ml_feature_importance ( |
| 15 | + feature_name String, |
| 16 | + importance_score Float32, |
| 17 | + importance_rank UInt16, |
| 18 | + model_version LowCardinality(String), |
| 19 | + importance_type LowCardinality(String) DEFAULT 'weight', -- 'weight', 'gain', 'cover' |
| 20 | + calculated_at DateTime64(3) DEFAULT now64(3) |
| 21 | +) |
| 22 | +ENGINE = ReplacingMergeTree(calculated_at) |
| 23 | +ORDER BY (model_version, feature_name) |
| 24 | +SETTINGS index_granularity = 8192; |
| 25 | + |
| 26 | + |
| 27 | +-- ---------------------------------------------------------------------------- |
| 28 | +-- Tier Boundaries |
| 29 | +-- ---------------------------------------------------------------------------- |
| 30 | +-- Documents the score ranges for each tier (BRONZE, SILVER, GOLD, DIAMOND). |
| 31 | +-- Used for explainability and consistency checks. |
| 32 | + |
| 33 | +CREATE TABLE IF NOT EXISTS polybot.aware_ml_tier_boundaries ( |
| 34 | + tier_name LowCardinality(String), -- BRONZE, SILVER, GOLD, DIAMOND |
| 35 | + tier_order UInt8, -- 1, 2, 3, 4 for sorting |
| 36 | + score_min Float32, |
| 37 | + score_max Float32, |
| 38 | + confidence_threshold Float32 DEFAULT 0.5, |
| 39 | + description String, |
| 40 | + model_version LowCardinality(String), |
| 41 | + updated_at DateTime64(3) DEFAULT now64(3) |
| 42 | +) |
| 43 | +ENGINE = ReplacingMergeTree(updated_at) |
| 44 | +ORDER BY (model_version, tier_order) |
| 45 | +SETTINGS index_granularity = 8192; |
| 46 | + |
| 47 | + |
| 48 | +-- ---------------------------------------------------------------------------- |
| 49 | +-- Training Runs |
| 50 | +-- ---------------------------------------------------------------------------- |
| 51 | +-- Logs each model training run for tracking and rollback capability. |
| 52 | + |
| 53 | +CREATE TABLE IF NOT EXISTS polybot.aware_ml_training_runs ( |
| 54 | + run_id UUID DEFAULT generateUUIDv4(), |
| 55 | + model_version LowCardinality(String), |
| 56 | + started_at DateTime64(3), |
| 57 | + completed_at DateTime64(3), |
| 58 | + duration_seconds UInt32, |
| 59 | + status LowCardinality(String) DEFAULT 'running', -- 'running', 'success', 'failed', 'rolled_back' |
| 60 | + |
| 61 | + -- Training data stats |
| 62 | + n_traders UInt32, |
| 63 | + n_trades UInt32, |
| 64 | + train_split_ratio Float32, |
| 65 | + |
| 66 | + -- Final metrics |
| 67 | + tier_accuracy Float32, |
| 68 | + sharpe_mae Float32, |
| 69 | + val_loss Float32, |
| 70 | + |
| 71 | + -- Trigger info |
| 72 | + trigger_reason LowCardinality(String), -- 'scheduled', 'drift', 'manual' |
| 73 | + triggered_by String DEFAULT 'system', |
| 74 | + |
| 75 | + -- Hyperparameters (JSON blob) |
| 76 | + hyperparameters String, |
| 77 | + |
| 78 | + -- Notes |
| 79 | + notes String DEFAULT '' |
| 80 | +) |
| 81 | +ENGINE = MergeTree() |
| 82 | +ORDER BY (started_at) |
| 83 | +PARTITION BY toYYYYMM(started_at) |
| 84 | +SETTINGS index_granularity = 8192; |
| 85 | + |
| 86 | + |
| 87 | +-- ---------------------------------------------------------------------------- |
| 88 | +-- Drift Reports |
| 89 | +-- ---------------------------------------------------------------------------- |
| 90 | +-- Stores drift detection results for monitoring. |
| 91 | + |
| 92 | +CREATE TABLE IF NOT EXISTS polybot.aware_ml_drift_reports ( |
| 93 | + report_id UUID DEFAULT generateUUIDv4(), |
| 94 | + checked_at DateTime64(3) DEFAULT now64(3), |
| 95 | + |
| 96 | + -- Summary |
| 97 | + alert_level LowCardinality(String), -- 'normal', 'warning', 'critical' |
| 98 | + drift_ratio Float32, -- Fraction of features that drifted |
| 99 | + n_features UInt16, |
| 100 | + n_drifted UInt16, |
| 101 | + |
| 102 | + -- Baseline info |
| 103 | + baseline_date DateTime64(3), |
| 104 | + n_samples_baseline UInt32, |
| 105 | + n_samples_current UInt32, |
| 106 | + |
| 107 | + -- Action taken |
| 108 | + retrain_triggered UInt8 DEFAULT 0, |
| 109 | + retrain_reason String DEFAULT '', |
| 110 | + |
| 111 | + -- Detailed results (JSON array) |
| 112 | + feature_results String |
| 113 | +) |
| 114 | +ENGINE = MergeTree() |
| 115 | +ORDER BY (checked_at) |
| 116 | +PARTITION BY toYYYYMM(checked_at) |
| 117 | +SETTINGS index_granularity = 8192; |
| 118 | + |
| 119 | + |
| 120 | +-- ============================================================================ |
| 121 | +-- Seed Data: Default Tier Boundaries |
| 122 | +-- ============================================================================ |
| 123 | + |
| 124 | +INSERT INTO polybot.aware_ml_tier_boundaries |
| 125 | +(tier_name, tier_order, score_min, score_max, confidence_threshold, description, model_version) |
| 126 | +VALUES |
| 127 | +('BRONZE', 1, 0.0, 49.9, 0.3, 'Entry-level traders. May have limited history or inconsistent performance.', 'ensemble_v1'), |
| 128 | +('SILVER', 2, 50.0, 69.9, 0.4, 'Developing traders. Show some edge but not consistently profitable.', 'ensemble_v1'), |
| 129 | +('GOLD', 3, 70.0, 89.9, 0.5, 'Skilled traders. Consistent profitability with good risk management.', 'ensemble_v1'), |
| 130 | +('DIAMOND', 4, 90.0, 100.0, 0.7, 'Elite traders. Top performers with exceptional track records.', 'ensemble_v1'); |
| 131 | + |
| 132 | + |
| 133 | +-- ============================================================================ |
| 134 | +-- Views |
| 135 | +-- ============================================================================ |
| 136 | + |
| 137 | +-- Latest feature importance |
| 138 | +CREATE VIEW IF NOT EXISTS polybot.v_ml_feature_importance_latest AS |
| 139 | +SELECT |
| 140 | + feature_name, |
| 141 | + importance_score, |
| 142 | + importance_rank, |
| 143 | + model_version, |
| 144 | + importance_type |
| 145 | +FROM polybot.aware_ml_feature_importance FINAL |
| 146 | +WHERE model_version = ( |
| 147 | + SELECT model_version |
| 148 | + FROM polybot.aware_ml_feature_importance |
| 149 | + ORDER BY calculated_at DESC |
| 150 | + LIMIT 1 |
| 151 | +) |
| 152 | +ORDER BY importance_rank; |
| 153 | + |
| 154 | + |
| 155 | +-- Latest training run |
| 156 | +CREATE VIEW IF NOT EXISTS polybot.v_ml_training_latest AS |
| 157 | +SELECT * |
| 158 | +FROM polybot.aware_ml_training_runs |
| 159 | +WHERE status = 'success' |
| 160 | +ORDER BY completed_at DESC |
| 161 | +LIMIT 1; |
| 162 | + |
| 163 | + |
| 164 | +-- Drift trend (last 30 days) |
| 165 | +CREATE VIEW IF NOT EXISTS polybot.v_ml_drift_trend AS |
| 166 | +SELECT |
| 167 | + toDate(checked_at) AS date, |
| 168 | + avg(drift_ratio) AS avg_drift_ratio, |
| 169 | + max(drift_ratio) AS max_drift_ratio, |
| 170 | + countIf(alert_level = 'warning') AS warning_count, |
| 171 | + countIf(alert_level = 'critical') AS critical_count, |
| 172 | + countIf(retrain_triggered = 1) AS retrains_triggered |
| 173 | +FROM polybot.aware_ml_drift_reports |
| 174 | +WHERE checked_at >= now() - INTERVAL 30 DAY |
| 175 | +GROUP BY date |
| 176 | +ORDER BY date; |
0 commit comments