Spaces:
Sleeping
Sleeping
Update ml_engine/monte_carlo.py
Browse files- ml_engine/monte_carlo.py +43 -51
ml_engine/monte_carlo.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
# ml_engine/monte_carlo.py
|
| 2 |
-
# (V11.
|
| 3 |
-
# - Tier 1: Light Check ->
|
| 4 |
-
# - Tier 2: Advanced Simulation ->
|
| 5 |
|
| 6 |
import numpy as np
|
| 7 |
import pandas as pd
|
|
@@ -18,7 +18,7 @@ class MonteCarloEngine:
|
|
| 18 |
"""
|
| 19 |
|
| 20 |
def __init__(self):
|
| 21 |
-
print("✅ [MonteCarlo V11.
|
| 22 |
|
| 23 |
# ==============================================================================
|
| 24 |
# ⚡ TIER 1: Light Check (Applied to ALL 150 Coins)
|
|
@@ -26,23 +26,10 @@ class MonteCarloEngine:
|
|
| 26 |
def run_light_check(self, prices: List[float]) -> float:
|
| 27 |
"""
|
| 28 |
فحص سريع يطبق على الـ 150 عملة.
|
| 29 |
-
|
| 30 |
-
[التعديل المطلوب]:
|
| 31 |
-
إلغاء المنطقة الميتة تماماً.
|
| 32 |
-
أي انحراف عن 0.50 يعطي نتيجة فوراً.
|
| 33 |
-
|
| 34 |
-
Logic:
|
| 35 |
-
- Prob > 0.55 -> +0.10
|
| 36 |
-
- Prob > 0.52 -> +0.07
|
| 37 |
-
- Prob > 0.50 -> +0.03 (أي نبض إيجابي يعطي درجة)
|
| 38 |
-
- Prob < 0.50 -> -0.03 (أي نبض سلبي يخصم درجة)
|
| 39 |
-
- Prob < 0.48 -> -0.07
|
| 40 |
-
- Prob < 0.45 -> -0.10
|
| 41 |
"""
|
| 42 |
try:
|
| 43 |
-
|
| 44 |
-
if not prices or len(prices) < 20:
|
| 45 |
-
return 0.0
|
| 46 |
|
| 47 |
clean_prices = []
|
| 48 |
for p in prices:
|
|
@@ -53,7 +40,6 @@ class MonteCarloEngine:
|
|
| 53 |
|
| 54 |
if len(clean_prices) < 20: return 0.0
|
| 55 |
|
| 56 |
-
# 2. الحساب الإحصائي (Z-Score)
|
| 57 |
price_arr = np.array(clean_prices, dtype=np.float64)
|
| 58 |
log_returns = np.diff(np.log(price_arr))
|
| 59 |
|
|
@@ -62,33 +48,19 @@ class MonteCarloEngine:
|
|
| 62 |
|
| 63 |
if sigma == 0: return 0.0
|
| 64 |
|
| 65 |
-
# احتمالية الصعود P(X > 0)
|
| 66 |
prob_up = norm.cdf(mu / sigma)
|
| 67 |
|
| 68 |
-
#
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
-
# --- المنطقة الإيجابية ---
|
| 72 |
-
if prob_up >= 0.55:
|
| 73 |
-
return 0.10 # صعود قوي
|
| 74 |
-
elif prob_up >= 0.52:
|
| 75 |
-
return 0.07 # صعود متوسط
|
| 76 |
-
elif prob_up > 0.5001:
|
| 77 |
-
return 0.03 # صعود طفيف جداً (لكي لا يكون صفر)
|
| 78 |
-
|
| 79 |
-
# --- المنطقة السلبية ---
|
| 80 |
-
elif prob_up <= 0.45:
|
| 81 |
-
return -0.10 # هبوط قوي
|
| 82 |
-
elif prob_up <= 0.48:
|
| 83 |
-
return -0.07 # هبوط متوسط
|
| 84 |
-
elif prob_up < 0.4999:
|
| 85 |
-
return -0.03 # هبوط طفيف جداً (لكي لا يكون صفر)
|
| 86 |
-
|
| 87 |
-
# الحالة النادرة جداً (التعادل التام)
|
| 88 |
return 0.00
|
| 89 |
|
| 90 |
except Exception as e:
|
| 91 |
-
# print(f"⚠️ [Light MC] Error: {e}")
|
| 92 |
return 0.0
|
| 93 |
|
| 94 |
# ==============================================================================
|
|
@@ -96,8 +68,9 @@ class MonteCarloEngine:
|
|
| 96 |
# ==============================================================================
|
| 97 |
def run_advanced_simulation(self, prices: List[float], num_simulations=5000, time_horizon=24) -> float:
|
| 98 |
"""
|
| 99 |
-
|
| 100 |
-
|
|
|
|
| 101 |
"""
|
| 102 |
try:
|
| 103 |
if not prices or len(prices) < 50: return 0.0
|
|
@@ -126,20 +99,39 @@ class MonteCarloEngine:
|
|
| 126 |
|
| 127 |
# Metrics
|
| 128 |
threshold = last_price * 1.002
|
| 129 |
-
prob_profit = np.mean(final_prices > threshold)
|
| 130 |
var_95 = np.percentile(final_prices, 5)
|
| 131 |
drawdown_risk = (var_95 - last_price) / last_price
|
| 132 |
expected_return = (np.mean(final_prices) - last_price) / last_price
|
| 133 |
|
| 134 |
-
#
|
|
|
|
|
|
|
| 135 |
mc_score = 0.0
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
if
|
| 139 |
-
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
except Exception as e:
|
| 145 |
return 0.0
|
|
|
|
| 1 |
# ml_engine/monte_carlo.py
|
| 2 |
+
# (V11.8 - GEM-Architect: Dynamic Sensitivity Logic)
|
| 3 |
+
# - Tier 1: Light Check -> Keep as is (Micro-scores).
|
| 4 |
+
# - Tier 2: Advanced Simulation -> Dynamic Range (-0.10 to +0.10).
|
| 5 |
|
| 6 |
import numpy as np
|
| 7 |
import pandas as pd
|
|
|
|
| 18 |
"""
|
| 19 |
|
| 20 |
def __init__(self):
|
| 21 |
+
print("✅ [MonteCarlo V11.8] Hybrid Engine Loaded (Dynamic Sensitivity).")
|
| 22 |
|
| 23 |
# ==============================================================================
|
| 24 |
# ⚡ TIER 1: Light Check (Applied to ALL 150 Coins)
|
|
|
|
| 26 |
def run_light_check(self, prices: List[float]) -> float:
|
| 27 |
"""
|
| 28 |
فحص سريع يطبق على الـ 150 عملة.
|
| 29 |
+
(تم الإبقاء على المنطق كما هو بناء على التحديث السابق)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
"""
|
| 31 |
try:
|
| 32 |
+
if not prices or len(prices) < 20: return 0.0
|
|
|
|
|
|
|
| 33 |
|
| 34 |
clean_prices = []
|
| 35 |
for p in prices:
|
|
|
|
| 40 |
|
| 41 |
if len(clean_prices) < 20: return 0.0
|
| 42 |
|
|
|
|
| 43 |
price_arr = np.array(clean_prices, dtype=np.float64)
|
| 44 |
log_returns = np.diff(np.log(price_arr))
|
| 45 |
|
|
|
|
| 48 |
|
| 49 |
if sigma == 0: return 0.0
|
| 50 |
|
|
|
|
| 51 |
prob_up = norm.cdf(mu / sigma)
|
| 52 |
|
| 53 |
+
# سلم الحساسية (No Dead Zone)
|
| 54 |
+
if prob_up >= 0.55: return 0.10
|
| 55 |
+
elif prob_up >= 0.52: return 0.07
|
| 56 |
+
elif prob_up > 0.5001: return 0.03
|
| 57 |
+
elif prob_up <= 0.45: return -0.10
|
| 58 |
+
elif prob_up <= 0.48: return -0.07
|
| 59 |
+
elif prob_up < 0.4999: return -0.03
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
return 0.00
|
| 62 |
|
| 63 |
except Exception as e:
|
|
|
|
| 64 |
return 0.0
|
| 65 |
|
| 66 |
# ==============================================================================
|
|
|
|
| 68 |
# ==============================================================================
|
| 69 |
def run_advanced_simulation(self, prices: List[float], num_simulations=5000, time_horizon=24) -> float:
|
| 70 |
"""
|
| 71 |
+
[تعديل V11.8]: نظام تنقيط ديناميكي (Dynamic Scoring).
|
| 72 |
+
النطاق: من -0.10 (سلبي جداً) إلى +0.10 (إيجابي جداً).
|
| 73 |
+
يعتمد على قوة احتمالية الربح (Prob Profit) والعائد المتوقع (Exp Return).
|
| 74 |
"""
|
| 75 |
try:
|
| 76 |
if not prices or len(prices) < 50: return 0.0
|
|
|
|
| 99 |
|
| 100 |
# Metrics
|
| 101 |
threshold = last_price * 1.002
|
| 102 |
+
prob_profit = np.mean(final_prices > threshold) # نسبة المسارات الرابحة
|
| 103 |
var_95 = np.percentile(final_prices, 5)
|
| 104 |
drawdown_risk = (var_95 - last_price) / last_price
|
| 105 |
expected_return = (np.mean(final_prices) - last_price) / last_price
|
| 106 |
|
| 107 |
+
# ---------------------------------------------------------
|
| 108 |
+
# ⚖️ Dynamic Scoring Logic (-0.10 to +0.10)
|
| 109 |
+
# ---------------------------------------------------------
|
| 110 |
mc_score = 0.0
|
| 111 |
+
|
| 112 |
+
# 1. القوة الإيجابية (Max +0.10)
|
| 113 |
+
if prob_profit > 0.50:
|
| 114 |
+
# إضافة تدريجية بحسب قوة الاحتمالية
|
| 115 |
+
# مثال: 0.55 -> يضيف نقاط قليلة، 0.70 -> يضيف الكثير
|
| 116 |
+
strength = (prob_profit - 0.50) * 2 # Scale 0.0 to 1.0
|
| 117 |
+
mc_score += strength * 0.08 # Max 0.08
|
| 118 |
+
|
| 119 |
+
if expected_return > 0.01: # عائد متوقع ممتاز
|
| 120 |
+
mc_score += 0.02
|
| 121 |
+
|
| 122 |
+
# 2. القوة السلبية (Max -0.10)
|
| 123 |
+
elif prob_profit <= 0.50:
|
| 124 |
+
# خصم تدريجي بحسب ضعف الاحتمالية
|
| 125 |
+
weakness = (0.50 - prob_profit) * 2 # Scale 0.0 to 1.0
|
| 126 |
+
mc_score -= weakness * 0.08 # Max -0.08
|
| 127 |
|
| 128 |
+
if drawdown_risk < -0.05: # مخاطرة عالية
|
| 129 |
+
mc_score -= 0.02
|
| 130 |
+
|
| 131 |
+
# Clamp Result (لضمان عدم تجاوز الحدود المطلوبة)
|
| 132 |
+
final_mc_score = max(-0.10, min(0.10, mc_score))
|
| 133 |
+
|
| 134 |
+
return float(final_mc_score)
|
| 135 |
|
| 136 |
except Exception as e:
|
| 137 |
return 0.0
|