Spaces:
Paused
Paused
Update learning_hub/statistical_analyzer.py
Browse files
learning_hub/statistical_analyzer.py
CHANGED
|
@@ -1,11 +1,13 @@
|
|
| 1 |
# learning_hub/statistical_analyzer.py
|
| 2 |
-
# (V13.
|
| 3 |
|
| 4 |
import json
|
| 5 |
import asyncio
|
| 6 |
import traceback
|
| 7 |
from datetime import datetime
|
| 8 |
from collections import deque
|
|
|
|
|
|
|
| 9 |
|
| 10 |
class StatisticalAnalyzer:
|
| 11 |
def __init__(self, r2_service, data_manager):
|
|
@@ -20,7 +22,7 @@ class StatisticalAnalyzer:
|
|
| 20 |
"monte_carlo": deque(maxlen=self.window_size)
|
| 21 |
}
|
| 22 |
|
| 23 |
-
# الأوزان الحالية
|
| 24 |
self.current_weights = {
|
| 25 |
"titan": 0.5,
|
| 26 |
"patterns": 0.4,
|
|
@@ -31,12 +33,12 @@ class StatisticalAnalyzer:
|
|
| 31 |
print("✅ Learning Hub: Statistical Analyzer (Live Tuner) loaded")
|
| 32 |
|
| 33 |
async def initialize(self):
|
| 34 |
-
#
|
| 35 |
self.initialized = True
|
| 36 |
|
| 37 |
async def update_live_performance(self, trade_object: Dict[str, Any]):
|
| 38 |
"""
|
| 39 |
-
تحديث سجلات الأداء اللحظية وتعديل الأوزان.
|
| 40 |
"""
|
| 41 |
try:
|
| 42 |
pnl = trade_object.get('pnl_percent', 0)
|
|
@@ -48,57 +50,76 @@ class StatisticalAnalyzer:
|
|
| 48 |
# 1. تقييم كل مكون على حدة
|
| 49 |
# هل كان المكون يتوقع الصعود (>0.6)؟ وهل نجح؟
|
| 50 |
|
| 51 |
-
# Titan
|
| 52 |
t_score = comps.get('titan_score', 0.5)
|
| 53 |
t_correct = 1 if (t_score > 0.6 and is_win) or (t_score < 0.4 and not is_win) else 0
|
| 54 |
self.history["titan"].append(t_correct)
|
| 55 |
|
| 56 |
-
# Patterns
|
| 57 |
p_score = comps.get('patterns_score', 0.5)
|
| 58 |
p_correct = 1 if (p_score > 0.6 and is_win) or (p_score < 0.4 and not is_win) else 0
|
| 59 |
self.history["patterns"].append(p_correct)
|
| 60 |
|
| 61 |
-
# Monte Carlo
|
| 62 |
m_score = comps.get('mc_score', 0.5)
|
| 63 |
m_correct = 1 if (m_score > 0.6 and is_win) or (m_score < 0.4 and not is_win) else 0
|
| 64 |
self.history["monte_carlo"].append(m_correct)
|
| 65 |
|
| 66 |
-
# 2. إعادة حساب الأوزان (فقط إذا
|
| 67 |
if len(self.history["titan"]) >= 5:
|
| 68 |
await self._rebalance_weights()
|
| 69 |
|
| 70 |
except Exception as e:
|
| 71 |
-
print(f"❌ [
|
|
|
|
| 72 |
|
| 73 |
async def _rebalance_weights(self):
|
| 74 |
"""إعادة توزيع الأوزان بناءً على الدقة الحديثة"""
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# learning_hub/statistical_analyzer.py
|
| 2 |
+
# (V13.1 - GEM-Architect: Real-Time Weight Adaptation - Fixed Imports)
|
| 3 |
|
| 4 |
import json
|
| 5 |
import asyncio
|
| 6 |
import traceback
|
| 7 |
from datetime import datetime
|
| 8 |
from collections import deque
|
| 9 |
+
# ✅ [FIX] إضافة استيراد الأنواع المفقودة
|
| 10 |
+
from typing import Dict, Any
|
| 11 |
|
| 12 |
class StatisticalAnalyzer:
|
| 13 |
def __init__(self, r2_service, data_manager):
|
|
|
|
| 22 |
"monte_carlo": deque(maxlen=self.window_size)
|
| 23 |
}
|
| 24 |
|
| 25 |
+
# الأوزان الحالية (الافتراضية)
|
| 26 |
self.current_weights = {
|
| 27 |
"titan": 0.5,
|
| 28 |
"patterns": 0.4,
|
|
|
|
| 33 |
print("✅ Learning Hub: Statistical Analyzer (Live Tuner) loaded")
|
| 34 |
|
| 35 |
async def initialize(self):
|
| 36 |
+
# في النسخ المستقبلية، يمكننا تحميل آخر أوزان محفوظة من R2 هنا
|
| 37 |
self.initialized = True
|
| 38 |
|
| 39 |
async def update_live_performance(self, trade_object: Dict[str, Any]):
|
| 40 |
"""
|
| 41 |
+
تحديث سجلات الأداء اللحظية وتعديل الأوزان فوراً.
|
| 42 |
"""
|
| 43 |
try:
|
| 44 |
pnl = trade_object.get('pnl_percent', 0)
|
|
|
|
| 50 |
# 1. تقييم كل مكون على حدة
|
| 51 |
# هل كان المكون يتوقع الصعود (>0.6)؟ وهل نجح؟
|
| 52 |
|
| 53 |
+
# Titan Check
|
| 54 |
t_score = comps.get('titan_score', 0.5)
|
| 55 |
t_correct = 1 if (t_score > 0.6 and is_win) or (t_score < 0.4 and not is_win) else 0
|
| 56 |
self.history["titan"].append(t_correct)
|
| 57 |
|
| 58 |
+
# Patterns Check
|
| 59 |
p_score = comps.get('patterns_score', 0.5)
|
| 60 |
p_correct = 1 if (p_score > 0.6 and is_win) or (p_score < 0.4 and not is_win) else 0
|
| 61 |
self.history["patterns"].append(p_correct)
|
| 62 |
|
| 63 |
+
# Monte Carlo Check
|
| 64 |
m_score = comps.get('mc_score', 0.5)
|
| 65 |
m_correct = 1 if (m_score > 0.6 and is_win) or (m_score < 0.4 and not is_win) else 0
|
| 66 |
self.history["monte_carlo"].append(m_correct)
|
| 67 |
|
| 68 |
+
# 2. إعادة حساب الأوزان (فقط إذا تجمعت عينة كافية >= 5)
|
| 69 |
if len(self.history["titan"]) >= 5:
|
| 70 |
await self._rebalance_weights()
|
| 71 |
|
| 72 |
except Exception as e:
|
| 73 |
+
print(f"❌ [StatsAnalyzer] Update failed: {e}")
|
| 74 |
+
traceback.print_exc()
|
| 75 |
|
| 76 |
async def _rebalance_weights(self):
|
| 77 |
"""إعادة توزيع الأوزان بناءً على الدقة الحديثة"""
|
| 78 |
+
try:
|
| 79 |
+
# حساب المتوسط (Accuracy) لكل مكون
|
| 80 |
+
acc_t = sum(self.history["titan"]) / len(self.history["titan"])
|
| 81 |
+
acc_p = sum(self.history["patterns"]) / len(self.history["patterns"])
|
| 82 |
+
acc_m = sum(self.history["monte_carlo"]) / len(self.history["monte_carlo"])
|
| 83 |
+
|
| 84 |
+
# منع الأوزان الصفرية (Smoothing)
|
| 85 |
+
acc_t = max(acc_t, 0.1)
|
| 86 |
+
acc_p = max(acc_p, 0.1)
|
| 87 |
+
acc_m = max(acc_m, 0.1)
|
| 88 |
+
|
| 89 |
+
total = acc_t + acc_p + acc_m
|
| 90 |
+
|
| 91 |
+
# الأوزان الجديدة المقترحة
|
| 92 |
+
new_w = {
|
| 93 |
+
"titan": acc_t / total,
|
| 94 |
+
"patterns": acc_p / total,
|
| 95 |
+
"monte_carlo": acc_m / total
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
# تطبيق التغيير بنعومة (Smoothing): 90% قديم + 10% جديد
|
| 99 |
+
# هذا يمنع التقلب الشديد في الأوزان مع كل صفقة
|
| 100 |
+
self.current_weights = {
|
| 101 |
+
k: (self.current_weights[k] * 0.9) + (new_w[k] * 0.1)
|
| 102 |
+
for k in new_w
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
# (اختياري) طباعة التعديل للمراقبة
|
| 106 |
+
# print(f"⚖️ [Stats] Weights Adapted: Titan={self.current_weights['titan']:.2f}")
|
| 107 |
|
| 108 |
+
except Exception as e:
|
| 109 |
+
print(f"❌ [StatsAnalyzer] Rebalance failed: {e}")
|
| 110 |
+
|
| 111 |
+
def get_live_weights(self) -> Dict[str, float]:
|
| 112 |
+
"""إرجاع الأوزان الحالية لاستخدامها في المعالج"""
|
| 113 |
+
return self.current_weights
|
| 114 |
+
|
| 115 |
+
async def save_weights_to_r2(self):
|
| 116 |
+
"""حفظ الأوزان الحالية في R2 (للاستعادة عند إعادة التشغيل)"""
|
| 117 |
+
try:
|
| 118 |
+
data = {
|
| 119 |
+
"weights": self.current_weights,
|
| 120 |
+
"last_updated": datetime.now().isoformat()
|
| 121 |
+
}
|
| 122 |
+
# نفترض وجود دالة upload_json_async في r2_service كما أضفناها سابقاً
|
| 123 |
+
await self.r2_service.upload_json_async(data, "learning_dynamic_weights.json")
|
| 124 |
+
except Exception:
|
| 125 |
+
pass
|