File size: 12,122 Bytes
226ac39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
"""
Model Explanation Module

Provides reasoning about model behavior, performance, and interpretability.

KEY RULES:
- βœ… Accepts: Model metrics, predictions, feature importances
- ❌ NO: Raw model objects, training loops
- βœ… Returns: Explanations of WHY model behaves as it does
- ❌ NO: Model selection, hyperparameter choices

Use Cases:
1. Explain model performance metrics
2. Interpret feature importances
3. Diagnose model failures
4. Suggest model debugging steps

Example:
    from reasoning.model_explanation import explain_model_performance
    
    metrics = {
        "accuracy": 0.95,
        "precision": 0.92,
        "recall": 0.88,
        "confusion_matrix": [[800, 50], [100, 50]]
    }
    
    explanation = explain_model_performance(metrics, "classification")
    # Returns: "Your model has high accuracy but low recall..."
"""

from typing import Dict, Any, List, Optional
from . import get_reasoner


def explain_model_performance(
    metrics: Dict[str, Any],
    task_type: str,
    baseline_metrics: Optional[Dict[str, Any]] = None
) -> Dict[str, Any]:
    """
    Explain model performance metrics in plain English.
    
    Args:
        metrics: Performance metrics (accuracy, precision, recall, etc.)
        task_type: 'classification' or 'regression'
        baseline_metrics: Optional baseline to compare against
        
    Returns:
        {
            "summary": str,                    # Overall assessment
            "strengths": List[str],            # What model does well
            "weaknesses": List[str],           # What model struggles with
            "confusion_analysis": str,         # Confusion matrix interpretation
            "next_steps": List[str]            # Suggested improvements
        }
    """
    reasoner = get_reasoner()
    
    comparison = ""
    if baseline_metrics:
        comparison = f"\n**Baseline Metrics (for comparison):**\n{baseline_metrics}"
    
    prompt = f"""Analyze these model performance metrics:

**Task Type:** {task_type}

**Metrics:**
{metrics}{comparison}

Provide:
1. Overall performance summary (good/bad/acceptable)
2. Strengths (what model does well)
3. Weaknesses (where model struggles)
4. Confusion matrix analysis (if classification)
5. Next steps for improvement

Be specific and actionable. If performance is poor, suggest why."""
    
    system_prompt = """You are a model interpretation expert.
Explain performance metrics in terms business users understand.
Focus on actionable insights, not just numbers."""
    
    schema = {
        "summary": "string - Overall assessment",
        "strengths": ["array of strengths"],
        "weaknesses": ["array of weaknesses"],
        "confusion_analysis": "string - Confusion matrix explanation",
        "next_steps": ["array of improvement suggestions"]
    }
    
    return reasoner.reason_structured(prompt, schema, system_prompt)


def interpret_feature_importance(
    feature_importances: Dict[str, float],
    top_n: int = 10,
    domain: Optional[str] = None
) -> Dict[str, Any]:
    """
    Interpret feature importance scores and explain what they mean.
    
    Args:
        feature_importances: {feature_name: importance_score}
        top_n: Number of top features to focus on
        domain: Optional domain context
        
    Returns:
        {
            "top_features": List[str],         # Most important features
            "interpretation": str,             # What importances mean
            "surprising_features": List[str],  # Unexpectedly important/unimportant
            "feature_relationships": str,      # How features might interact
            "recommendations": List[str]       # What to investigate further
        }
    """
    reasoner = get_reasoner()
    
    # Sort by importance
    sorted_features = sorted(
        feature_importances.items(),
        key=lambda x: x[1],
        reverse=True
    )[:top_n]
    
    domain_context = f"\nDomain: {domain}" if domain else ""
    
    prompt = f"""Interpret these feature importance scores:

**Top {top_n} Most Important Features:**
{dict(sorted_features)}

**All Features:**
{feature_importances}{domain_context}

Explain:
1. What these importances tell us about the model
2. Which features are surprisingly important/unimportant
3. Potential feature interactions or relationships
4. What to investigate further
5. Whether importances make intuitive sense

Be specific about WHY certain features might be important."""
    
    system_prompt = """You are a model interpretability expert.
Explain feature importances in domain terms, not just statistical terms.
Point out surprising or counterintuitive results."""
    
    schema = {
        "top_features": ["array of most important features"],
        "interpretation": "string - What importances mean overall",
        "surprising_features": ["array of unexpected results"],
        "feature_relationships": "string - How features might interact",
        "recommendations": ["array of investigation suggestions"]
    }
    
    return reasoner.reason_structured(prompt, schema, system_prompt)


def diagnose_model_failure(
    failure_description: str,
    model_type: str,
    metrics: Dict[str, Any],
    sample_predictions: Optional[List[Dict]] = None
) -> Dict[str, Any]:
    """
    Diagnose why a model is failing and suggest fixes.
    
    Args:
        failure_description: Description of the problem
            Example: "Model predicts all positives" or "Poor performance on test set"
        model_type: Model algorithm used
        metrics: Current performance metrics
        sample_predictions: Optional sample of predictions vs actuals
        
    Returns:
        {
            "diagnosis": str,              # What's likely wrong
            "root_causes": List[str],      # Possible root causes
            "debugging_steps": List[str],  # How to investigate
            "potential_fixes": List[str]   # Suggested solutions
        }
    """
    reasoner = get_reasoner()
    
    samples = ""
    if sample_predictions:
        samples = f"\n**Sample Predictions:**\n{sample_predictions[:10]}"
    
    prompt = f"""Diagnose this model failure:

**Problem:** {failure_description}

**Model Type:** {model_type}

**Current Metrics:**
{metrics}{samples}

Provide:
1. Diagnosis of what's likely wrong
2. Possible root causes
3. Debugging steps to take
4. Potential fixes to try

Be specific and prioritize most likely causes."""
    
    system_prompt = """You are a model debugging expert.
Provide systematic diagnostic steps, not just guesses.
Prioritize most common failure modes first."""
    
    schema = {
        "diagnosis": "string - What's likely wrong",
        "root_causes": ["array of possible causes"],
        "debugging_steps": ["array of investigation steps"],
        "potential_fixes": ["array of solutions to try"]
    }
    
    return reasoner.reason_structured(prompt, schema, system_prompt)


def explain_prediction(
    prediction: Any,
    feature_values: Dict[str, Any],
    feature_contributions: Optional[Dict[str, float]] = None,
    model_type: str = "unknown"
) -> str:
    """
    Explain a single prediction in plain English.
    
    Args:
        prediction: Model's prediction
        feature_values: Feature values for this prediction
        feature_contributions: Optional SHAP values or contributions
        model_type: Type of model
        
    Returns:
        Natural language explanation of the prediction
    """
    reasoner = get_reasoner()
    
    contributions = ""
    if feature_contributions:
        contributions = f"\n**Feature Contributions:**\n{feature_contributions}"
    
    prompt = f"""Explain this model prediction in simple terms:

**Prediction:** {prediction}

**Input Features:**
{feature_values}{contributions}

**Model Type:** {model_type}

Explain:
- What the model predicted
- Which features most influenced the prediction
- Why this prediction makes sense (or doesn't)
- How confident we should be in this prediction

Make it understandable to non-technical users."""
    
    system_prompt = """You are explaining model predictions to business users.
Use plain English, avoid jargon, focus on the 'why' behind predictions."""
    
    return reasoner.reason(prompt, system_prompt, temperature=0.1)


def compare_models(
    model1_metrics: Dict[str, Any],
    model2_metrics: Dict[str, Any],
    model1_name: str = "Model A",
    model2_name: str = "Model B",
    business_context: Optional[str] = None
) -> Dict[str, Any]:
    """
    Compare two models and recommend which to use.
    
    Args:
        model1_metrics: Metrics for first model
        model2_metrics: Metrics for second model
        model1_name: Name/description of first model
        model2_name: Name/description of second model
        business_context: Optional business requirements
            Example: "Need high recall, false negatives are costly"
        
    Returns:
        {
            "winner": str,                     # Which model is better
            "comparison": str,                 # Detailed comparison
            "tradeoffs": List[str],            # Key tradeoffs
            "recommendation": str,             # Final recommendation
            "context_considerations": str      # Business context factors
        }
    """
    reasoner = get_reasoner()
    
    context = ""
    if business_context:
        context = f"\n**Business Context:**\n{business_context}"
    
    prompt = f"""Compare these two models:

**{model1_name} Metrics:**
{model1_metrics}

**{model2_name} Metrics:**
{model2_metrics}{context}

Determine:
1. Which model is objectively better (if any)
2. Key differences and tradeoffs
3. Which model to choose given business context
4. When you might choose the "worse" model

Consider accuracy, precision, recall, training time, interpretability, etc."""
    
    system_prompt = """You are a model selection expert.
Don't just pick the highest accuracy - consider tradeoffs and business needs.
Sometimes a simpler or faster model is better."""
    
    schema = {
        "winner": "string - Which model is better overall",
        "comparison": "string - Detailed comparison",
        "tradeoffs": ["array of key tradeoffs"],
        "recommendation": "string - Final recommendation with reasoning",
        "context_considerations": "string - How business context affects choice"
    }
    
    return reasoner.reason_structured(prompt, schema, system_prompt)


def explain_overfitting(
    train_metrics: Dict[str, float],
    test_metrics: Dict[str, float],
    model_complexity: Optional[str] = None
) -> Dict[str, Any]:
    """
    Detect and explain overfitting (or underfitting).
    
    Args:
        train_metrics: Training set metrics
        test_metrics: Test set metrics
        model_complexity: Optional description of model complexity
        
    Returns:
        {
            "diagnosis": str,              # Overfitting/underfitting/good_fit
            "severity": str,               # Low/medium/high
            "explanation": str,            # Why this is happening
            "solutions": List[str]         # How to fix it
        }
    """
    reasoner = get_reasoner()
    
    prompt = f"""Analyze these train vs test metrics for overfitting:

**Training Metrics:**
{train_metrics}

**Test Metrics:**
{test_metrics}

**Model Complexity:** {model_complexity or 'Unknown'}

Determine:
1. Whether model is overfitting, underfitting, or well-fitted
2. Severity of the problem
3. Why this is happening
4. Specific solutions to try

Be specific about the gap between train and test performance."""
    
    system_prompt = """You are a model diagnostics expert.
Explain overfitting in practical terms and provide actionable solutions."""
    
    schema = {
        "diagnosis": "string - overfitting/underfitting/good_fit",
        "severity": "string - low/medium/high",
        "explanation": "string - Why this is happening",
        "solutions": ["array of specific fixes to try"]
    }
    
    return reasoner.reason_structured(prompt, schema, system_prompt)