File size: 9,109 Bytes
7a658e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
73
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
"""
Enhanced Model Evaluation Script
Includes comprehensive metrics, drift detection, and performance monitoring
"""
import os
import sys
import pandas as pd
import numpy as np
import pickle
import json
from datetime import datetime
from pathlib import Path
from sklearn.metrics import (
    accuracy_score, precision_score, recall_score, f1_score,
    confusion_matrix, roc_curve, auc, classification_report
)
import matplotlib.pyplot as plt
import yaml

# Add parent directory to path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))

from src.monitoring.drift_detector import DriftDetector
from src.monitoring.metrics_collector import MetricsCollector
from src.monitoring.monitoring_service import MonitoringService


def load_params():
    """Load parameters from params.yaml"""
    with open("params.yaml", "r") as f:
        return yaml.safe_load(f)


def evaluate_model_comprehensive(model_path: str, strategy_type: str, 
                                test_data: pd.DataFrame, 
                                monitoring_service: MonitoringService):
    """
    Comprehensive model evaluation
    
    Args:
        model_path: Path to model file
        strategy_type: TOP or BOTTOM
        test_data: Test dataset
        monitoring_service: Monitoring service instance
    """
    print(f"\n{'='*60}")
    print(f"Evaluating {strategy_type} Strategy Model")
    print(f"{'='*60}\n")
    
    # Load model
    if not os.path.exists(model_path):
        print(f"Error: Model not found at {model_path}")
        return None
    
    with open(model_path, "rb") as f:
        model = pickle.load(f)
    
    # Prepare features
    features = ["sma_10", "sma_20", "rsi", "volatility", "price_position"]
    X_test = test_data[features].fillna(0)
    
    # Create labels
    if strategy_type == "TOP":
        y_test = ((test_data["price_position"] > 70) & 
                 (test_data["rsi"] > 50) & (test_data["rsi"] < 70)).astype(int)
    else:  # BOTTOM
        y_test = ((test_data["price_position"] < 30) & 
                 (test_data["rsi"] < 30)).astype(int)
    
    # Predictions
    y_pred = model.predict(X_test)
    try:
        y_proba = model.predict_proba(X_test)[:, 1]
    except:
        y_proba = None
    
    # Basic metrics
    accuracy = accuracy_score(y_test, y_pred)
    precision = precision_score(y_test, y_pred, zero_division=0)
    recall = recall_score(y_test, y_pred, zero_division=0)
    f1 = f1_score(y_test, y_pred, zero_division=0)
    
    print(f"πŸ“Š Basic Metrics:")
    print(f"  Accuracy:  {accuracy:.4f}")
    print(f"  Precision: {precision:.4f}")
    print(f"  Recall:    {recall:.4f}")
    print(f"  F1 Score:  {f1:.4f}")
    
    # Classification report
    print(f"\nπŸ“‹ Classification Report:")
    print(classification_report(y_test, y_pred, 
                                target_names=['HOLD', 'BUY'], 
                                zero_division=0))
    
    # Confusion Matrix
    cm = confusion_matrix(y_test, y_pred)
    print(f"\nπŸ”’ Confusion Matrix:")
    print(f"  {'':>10} Predicted HOLD  Predicted BUY")
    print(f"  Actual HOLD   {cm[0,0]:>6}        {cm[0,1]:>6}")
    print(f"  Actual BUY    {cm[1,0]:>6}        {cm[1,1]:>6}")
    
    # ROC Curve (if probabilities available)
    roc_auc = None
    if y_proba is not None and len(np.unique(y_test)) > 1:
        try:
            fpr, tpr, _ = roc_curve(y_test, y_proba)
            roc_auc = auc(fpr, tpr)
            print(f"\nπŸ“ˆ ROC AUC Score: {roc_auc:.4f}")
        except:
            pass
    
    # Drift Detection
    print(f"\nπŸ” Drift Detection:")
    drift_result = monitoring_service.drift_detector.detect_drift(
        test_data[features]
    )
    
    if drift_result.get("drift_detected"):
        print(f"  ⚠️  DRIFT DETECTED!")
        for feature, drift_info in drift_result.get("feature_drifts", {}).items():
            if drift_info.get("drift_detected"):
                print(f"    - {feature}: p-value = {drift_info['p_value']:.4f}")
    else:
        print(f"  βœ… No significant drift detected")
    
    # Create plots
    os.makedirs("plots", exist_ok=True)
    
    # Confusion Matrix Plot
    plt.figure(figsize=(8, 6))
    plt.imshow(cm, interpolation='nearest', cmap=plt.cm.Blues)
    plt.title(f'Confusion Matrix - {strategy_type} Strategy')
    plt.colorbar()
    tick_marks = np.arange(2)
    plt.xticks(tick_marks, ['HOLD', 'BUY'])
    plt.yticks(tick_marks, ['HOLD', 'BUY'])
    plt.ylabel('True label')
    plt.xlabel('Predicted label')
    thresh = cm.max() / 2.
    for i, j in np.ndindex(cm.shape):
        plt.text(j, i, format(cm[i, j], 'd'),
                horizontalalignment="center",
                color="white" if cm[i, j] > thresh else "black")
    plt.tight_layout()
    plt.savefig(f"plots/confusion_matrix_{strategy_type.lower()}.png")
    plt.close()
    
    # ROC Curve Plot
    if roc_auc is not None:
        plt.figure(figsize=(8, 6))
        plt.plot(fpr, tpr, color='darkorange', lw=2,
                label=f'ROC curve (AUC = {roc_auc:.2f})')
        plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')
        plt.xlim([0.0, 1.0])
        plt.ylim([0.0, 1.05])
        plt.xlabel('False Positive Rate')
        plt.ylabel('True Positive Rate')
        plt.title(f'ROC Curve - {strategy_type} Strategy')
        plt.legend(loc="lower right")
        plt.tight_layout()
        plt.savefig(f"plots/roc_curve_{strategy_type.lower()}.png")
        plt.close()
    
    # Compile results
    results = {
        "model_path": model_path,
        "strategy_type": strategy_type,
        "evaluation_date": datetime.now().isoformat(),
        "metrics": {
            "accuracy": float(accuracy),
            "precision": float(precision),
            "recall": float(recall),
            "f1_score": float(f1),
            "roc_auc": float(roc_auc) if roc_auc else None
        },
        "confusion_matrix": cm.tolist(),
        "sample_size": {
            "total": int(len(y_test)),
            "positive": int(y_test.sum()),
            "negative": int(len(y_test) - y_test.sum())
        },
        "drift_detection": drift_result
    }
    
    return results


def main():
    """Main evaluation function"""
    params = load_params()
    
    # Load test data
    test_data_path = "data/processed/indicators.parquet"
    if not os.path.exists(test_data_path):
        print(f"Error: Test data not found at {test_data_path}")
        print("Please run prepare_data.py first")
        return
    
    test_data = pd.read_parquet(test_data_path)
    test_data = test_data.dropna(subset=["rsi", "sma_10", "sma_20"])
    
    print(f"πŸ“Š Loaded {len(test_data)} test samples")
    
    # Initialize monitoring service
    monitoring_service = MonitoringService(
        drift_threshold=params["mlops"]["monitoring"]["drift_threshold"]
    )
    
    # Initialize reference baseline (use first 50% as reference)
    split_idx = len(test_data) // 2
    reference_data = test_data.iloc[:split_idx]
    monitoring_service.initialize_reference_baseline(
        reference_data[["sma_10", "sma_20", "rsi", "volatility", "price_position"]]
    )
    
    # Use second half for testing
    test_data = test_data.iloc[split_idx:]
    print(f"πŸ“Š Using {len(test_data)} samples for testing")
    
    os.makedirs("metrics", exist_ok=True)
    
    all_results = {}
    
    # Evaluate both strategies
    for strategy_type in ["TOP", "BOTTOM"]:
        model_path = f"models/{strategy_type.lower()}_strategy_model.pkl"
        
        if os.path.exists(model_path):
            results = evaluate_model_comprehensive(
                model_path, strategy_type, test_data, monitoring_service
            )
            
            if results:
                all_results[strategy_type] = results
                
                # Record metrics
                monitoring_service.metrics_collector.record_model_metrics(
                    f"{strategy_type.lower()}_strategy_model",
                    results["metrics"]
                )
        else:
            print(f"\n⚠️  Model not found: {model_path}")
            print(f"   Skipping {strategy_type} strategy evaluation")
    
    # Save comprehensive results
    with open("metrics/comprehensive_evaluation.json", "w") as f:
        json.dump(all_results, f, indent=2)
    
    # Health report
    print(f"\n{'='*60}")
    print(f"System Health Report")
    print(f"{'='*60}\n")
    
    health_report = monitoring_service.get_health_report()
    print(f"Status: {health_report['status']}")
    print(f"Metrics:")
    for key, value in health_report["metrics"].items():
        print(f"  {key}: {value}")
    
    # Save health report
    with open("metrics/health_report.json", "w") as f:
        json.dump(health_report, f, indent=2)
    
    print(f"\nβœ… Evaluation complete!")
    print(f"πŸ“ Results saved to:")
    print(f"   - metrics/comprehensive_evaluation.json")
    print(f"   - metrics/health_report.json")
    print(f"   - plots/ (confusion matrices and ROC curves)")


if __name__ == "__main__":
    main()