Spaces:
Configuration error
Configuration error
HeTalksInMaths
commited on
Commit
Β·
ad8f7e9
1
Parent(s):
3c1c6ff
Major improvement plan update: Nested CV + Adaptive Scoring
Browse files- Replace simple train/val/test with nested cross-validation
- Add adaptive uncertainty-aware scoring algorithm
- Comprehensive evaluation metrics (AUROC, ECE, Brier, etc.)
- Complete 7-week implementation roadmap
- Includes working code for NestedCVEvaluator class
- NEXT_STEPS_IMPROVEMENTS.md +173 -0
- togmal_improvement_plan.md +1114 -0
NEXT_STEPS_IMPROVEMENTS.md
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ToGMAL Next Steps: Adaptive Scoring & Nested CV
|
| 2 |
+
|
| 3 |
+
## Updated: 2025-10-21
|
| 4 |
+
|
| 5 |
+
This document outlines the immediate next steps to improve ToGMAL's difficulty assessment accuracy and establish a rigorous evaluation framework.
|
| 6 |
+
|
| 7 |
+
---
|
| 8 |
+
|
| 9 |
+
## π― Immediate Goals (This Week)
|
| 10 |
+
|
| 11 |
+
### 1. **Implement Adaptive Uncertainty-Aware Scoring**
|
| 12 |
+
- **Problem**: Current naive weighted average fails on low-similarity matches
|
| 13 |
+
- **Example Failure**: "Prove universe is 10,000 years old" β matched to factual recall (similarity ~0.57) β incorrectly rated LOW risk
|
| 14 |
+
- **Solution**: Add uncertainty penalties when:
|
| 15 |
+
- Max similarity < 0.7 (weak best match)
|
| 16 |
+
- High variance in k-NN similarities (diverse, unreliable matches)
|
| 17 |
+
- Low average similarity (all matches are weak)
|
| 18 |
+
- **File to modify**: `benchmark_vector_db.py::query_similar_questions()`
|
| 19 |
+
- **Expected improvement**: 5-15% AUROC gain on low-similarity cases
|
| 20 |
+
|
| 21 |
+
### 2. **Export Database for Evaluation**
|
| 22 |
+
- Add `get_all_questions_as_dataframe()` method to export 32K questions
|
| 23 |
+
- Prepare for train/val/test splitting and nested CV
|
| 24 |
+
- **File to modify**: `benchmark_vector_db.py`
|
| 25 |
+
|
| 26 |
+
### 3. **Test Adaptive Scoring**
|
| 27 |
+
- Create test script with edge cases
|
| 28 |
+
- Compare baseline vs. adaptive on known failure modes
|
| 29 |
+
- **New file**: `test_adaptive_scoring.py`
|
| 30 |
+
|
| 31 |
+
---
|
| 32 |
+
|
| 33 |
+
## π Evaluation Framework (Next 2-3 Weeks)
|
| 34 |
+
|
| 35 |
+
### Why Nested Cross-Validation?
|
| 36 |
+
|
| 37 |
+
**Problem with simple train/val/test split:**
|
| 38 |
+
- Single validation set can be lucky/unlucky (unrepresentative)
|
| 39 |
+
- Repeated "peeking" at validation during hyperparameter search causes data leakage
|
| 40 |
+
- Test set gives only ONE performance estimate (high variance)
|
| 41 |
+
|
| 42 |
+
**Nested CV advantages:**
|
| 43 |
+
- **Outer loop**: 5-fold CV for unbiased generalization estimate
|
| 44 |
+
- **Inner loop**: 3-fold grid search for hyperparameter tuning
|
| 45 |
+
- **No leakage**: Test folds never seen during tuning
|
| 46 |
+
- **Robust**: Multiple performance estimates across 5 different test sets
|
| 47 |
+
|
| 48 |
+
### Hyperparameters to Tune
|
| 49 |
+
|
| 50 |
+
```python
|
| 51 |
+
param_grid = {
|
| 52 |
+
'k_neighbors': [3, 5, 7, 10],
|
| 53 |
+
'similarity_threshold': [0.6, 0.7, 0.8],
|
| 54 |
+
'low_sim_penalty': [0.3, 0.5, 0.7],
|
| 55 |
+
'variance_penalty': [1.0, 2.0, 3.0],
|
| 56 |
+
'low_avg_penalty': [0.2, 0.4, 0.6]
|
| 57 |
+
}
|
| 58 |
+
```
|
| 59 |
+
|
| 60 |
+
### Evaluation Metrics
|
| 61 |
+
|
| 62 |
+
1. **AUROC** (primary): Discriminative ability (0.5=random, 1.0=perfect)
|
| 63 |
+
2. **FPR@TPR95**: False positive rate when catching 95% of risky prompts
|
| 64 |
+
3. **AUPR**: Area under precision-recall curve (good for imbalanced data)
|
| 65 |
+
4. **Expected Calibration Error (ECE)**: Are predicted probabilities accurate?
|
| 66 |
+
5. **Brier Score**: Overall probabilistic prediction accuracy
|
| 67 |
+
|
| 68 |
+
---
|
| 69 |
+
|
| 70 |
+
## ποΈ Implementation Phases
|
| 71 |
+
|
| 72 |
+
### Phase 1: Adaptive Scoring (This Week)
|
| 73 |
+
- [x] β 32K vector database with 20 domains, 7 benchmark sources
|
| 74 |
+
- [ ] Add `_compute_adaptive_difficulty()` method
|
| 75 |
+
- [ ] Integrate uncertainty penalties into scoring
|
| 76 |
+
- [ ] Test on known failure cases
|
| 77 |
+
- [ ] Update `togmal_mcp.py` to use adaptive scoring
|
| 78 |
+
|
| 79 |
+
### Phase 2: Data Export & Baseline (Week 2)
|
| 80 |
+
- [ ] Add `get_all_questions_as_dataframe()` export method
|
| 81 |
+
- [ ] Create simple 70/15/15 train/val/test split
|
| 82 |
+
- [ ] Run current ToGMAL (baseline) on test set
|
| 83 |
+
- [ ] Compute baseline metrics:
|
| 84 |
+
- AUROC
|
| 85 |
+
- FPR@TPR95
|
| 86 |
+
- Expected Calibration Error
|
| 87 |
+
- Brier Score
|
| 88 |
+
- [ ] Document failure modes (low similarity, cross-domain, etc.)
|
| 89 |
+
|
| 90 |
+
### Phase 3: Nested CV Implementation (Week 3)
|
| 91 |
+
- [ ] Implement `NestedCVEvaluator` class
|
| 92 |
+
- [ ] Outer CV: 5-fold stratified by (domain Γ difficulty)
|
| 93 |
+
- [ ] Inner CV: 3-fold grid search over hyperparameters
|
| 94 |
+
- [ ] Temporary vector DB creation per fold
|
| 95 |
+
- [ ] Metrics computation on each outer fold
|
| 96 |
+
|
| 97 |
+
### Phase 4: Hyperparameter Tuning (Week 4)
|
| 98 |
+
- [ ] Run full nested CV (5 outer Γ 3 inner = 15 train-test runs)
|
| 99 |
+
- [ ] Collect best hyperparameters per fold
|
| 100 |
+
- [ ] Identify most common optimal parameters
|
| 101 |
+
- [ ] Compute mean Β± std generalization performance
|
| 102 |
+
- [ ] Compare to baseline
|
| 103 |
+
|
| 104 |
+
### Phase 5: Final Model & Deployment (Week 5)
|
| 105 |
+
- [ ] Train final model on ALL 32K questions with best hyperparameters
|
| 106 |
+
- [ ] Re-index full vector database
|
| 107 |
+
- [ ] Deploy to MCP server and HTTP facade
|
| 108 |
+
- [ ] Test with Claude Desktop
|
| 109 |
+
|
| 110 |
+
### Phase 6: OOD Testing (Week 6)
|
| 111 |
+
- [ ] Create OOD test sets:
|
| 112 |
+
- **Adversarial**: "Prove false premises", jailbreaks
|
| 113 |
+
- **Domain Shift**: Creative writing, coding, real user queries
|
| 114 |
+
- **Temporal**: New benchmarks (2024+)
|
| 115 |
+
- [ ] Evaluate on each OOD set
|
| 116 |
+
- [ ] Analyze performance degradation vs. in-distribution
|
| 117 |
+
|
| 118 |
+
### Phase 7: Iteration & Documentation (Week 7)
|
| 119 |
+
- [ ] Analyze failures on OOD sets
|
| 120 |
+
- [ ] Add new heuristics for missed patterns
|
| 121 |
+
- [ ] Re-run nested CV with updated features
|
| 122 |
+
- [ ] Generate calibration plots (reliability diagrams)
|
| 123 |
+
- [ ] Write technical report
|
| 124 |
+
|
| 125 |
+
---
|
| 126 |
+
|
| 127 |
+
## π Expected Improvements
|
| 128 |
+
|
| 129 |
+
Based on OOD detection literature and nested CV best practices:
|
| 130 |
+
|
| 131 |
+
1. **Adaptive scoring**: +5-15% AUROC on low-similarity cases
|
| 132 |
+
- Baseline: ~0.75 AUROC (naive weighted average)
|
| 133 |
+
- Target: ~0.85+ AUROC (adaptive with uncertainty)
|
| 134 |
+
|
| 135 |
+
2. **Nested CV**: Honest, robust performance estimates
|
| 136 |
+
- Simple split: Single point estimate (could be lucky/unlucky)
|
| 137 |
+
- Nested CV: Mean Β± std across 5 folds
|
| 138 |
+
|
| 139 |
+
3. **Domain calibration**: -10-20% false positives
|
| 140 |
+
- Expected: FPR@TPR95 drops from ~0.25 to ~0.15
|
| 141 |
+
|
| 142 |
+
4. **Multi-signal fusion**: Better edge case detection
|
| 143 |
+
- Combine vector similarity + rule-based heuristics
|
| 144 |
+
- Improved recall on adversarial examples
|
| 145 |
+
|
| 146 |
+
5. **Calibration**: ECE < 0.05
|
| 147 |
+
- Better alignment between predicted risk and actual difficulty
|
| 148 |
+
|
| 149 |
+
---
|
| 150 |
+
|
| 151 |
+
## β
Validation Checklist (Before Production Deploy)
|
| 152 |
+
|
| 153 |
+
- [ ] Nested CV completed with no data leakage
|
| 154 |
+
- [ ] Hyperparameters tuned on inner CV folds only
|
| 155 |
+
- [ ] Generalization performance estimated on outer CV folds
|
| 156 |
+
- [ ] OOD sets tested (adversarial, domain-shift, temporal)
|
| 157 |
+
- [ ] Calibration error within acceptable range (ECE < 0.1)
|
| 158 |
+
- [ ] Failure modes documented with specific examples
|
| 159 |
+
- [ ] Ablation studies show each component contributes
|
| 160 |
+
- [ ] Performance: adaptive > baseline on all metrics
|
| 161 |
+
- [ ] Real-world testing with user queries
|
| 162 |
+
|
| 163 |
+
---
|
| 164 |
+
|
| 165 |
+
## π Quick Start Command
|
| 166 |
+
|
| 167 |
+
See `togmal_improvement_plan.md` for full implementation details including:
|
| 168 |
+
- Complete code for `NestedCVEvaluator` class
|
| 169 |
+
- Adaptive scoring implementation
|
| 170 |
+
- All evaluation metrics with examples
|
| 171 |
+
- Detailed roadmap with weekly milestones
|
| 172 |
+
|
| 173 |
+
**Next Action**: Implement adaptive scoring in `benchmark_vector_db.py` and test with edge cases.
|
togmal_improvement_plan.md
ADDED
|
@@ -0,0 +1,1114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ToGMAL Improvement Plan: Adaptive Scoring & Evaluation Framework
|
| 2 |
+
|
| 3 |
+
## Executive Summary
|
| 4 |
+
|
| 5 |
+
This plan addresses two critical gaps in togmal's current implementation:
|
| 6 |
+
1. **Naive weighted averaging fails when retrieved questions have low similarity** to the prompt
|
| 7 |
+
2. **Lack of rigorous evaluation methodology** to measure OOD detection performance
|
| 8 |
+
|
| 9 |
+
---
|
| 10 |
+
|
| 11 |
+
## Problem 1: Low-Similarity Scoring Issues
|
| 12 |
+
|
| 13 |
+
### Current Limitation
|
| 14 |
+
Your system uses a simple weighted average of difficulty scores from k-nearest neighbors, which produces unreliable risk assessments when:
|
| 15 |
+
- Maximum similarity < 0.6 (semantically distant matches)
|
| 16 |
+
- Retrieved questions span multiple unrelated domains
|
| 17 |
+
- Query is truly novel/out-of-distribution
|
| 18 |
+
|
| 19 |
+
**Example:** "Prove universe is 10,000 years old" matched to factual recall questions about Earth's age (similarity ~0.57), resulting in LOW risk despite being a "prove false premise" pattern.
|
| 20 |
+
|
| 21 |
+
### Solution: Adaptive Uncertainty-Aware Scoring
|
| 22 |
+
|
| 23 |
+
#### 1. Similarity-Based Confidence Adjustment
|
| 24 |
+
|
| 25 |
+
Implement a **confidence decay function** that increases risk when similarity is low:
|
| 26 |
+
|
| 27 |
+
```python
|
| 28 |
+
def compute_adaptive_risk(similarities, difficulties, k=5):
|
| 29 |
+
"""
|
| 30 |
+
Adjust risk score based on retrieval confidence
|
| 31 |
+
"""
|
| 32 |
+
# Base weighted score
|
| 33 |
+
weights = np.array(similarities) / sum(similarities)
|
| 34 |
+
base_score = np.dot(weights, difficulties)
|
| 35 |
+
|
| 36 |
+
# Confidence metrics
|
| 37 |
+
max_sim = max(similarities)
|
| 38 |
+
avg_sim = np.mean(similarities)
|
| 39 |
+
sim_variance = np.var(similarities)
|
| 40 |
+
|
| 41 |
+
# Uncertainty penalty - increase risk when:
|
| 42 |
+
# - Max similarity is low (< 0.7)
|
| 43 |
+
# - High variance in similarities (diverse matches)
|
| 44 |
+
# - Average similarity is low
|
| 45 |
+
|
| 46 |
+
uncertainty_penalty = 0.0
|
| 47 |
+
|
| 48 |
+
# Low maximum similarity threshold
|
| 49 |
+
if max_sim < 0.7:
|
| 50 |
+
uncertainty_penalty += (0.7 - max_sim) * 0.5
|
| 51 |
+
|
| 52 |
+
# High variance (retrieved questions are dissimilar to each other)
|
| 53 |
+
if sim_variance > 0.05:
|
| 54 |
+
uncertainty_penalty += min(sim_variance * 2, 0.3)
|
| 55 |
+
|
| 56 |
+
# Low average similarity
|
| 57 |
+
if avg_sim < 0.5:
|
| 58 |
+
uncertainty_penalty += (0.5 - avg_sim) * 0.4
|
| 59 |
+
|
| 60 |
+
# Adjusted score (higher = more risky)
|
| 61 |
+
adjusted_score = base_score + uncertainty_penalty
|
| 62 |
+
|
| 63 |
+
# Map to risk levels
|
| 64 |
+
if adjusted_score < 0.2:
|
| 65 |
+
return "MINIMAL"
|
| 66 |
+
elif adjusted_score < 0.4:
|
| 67 |
+
return "LOW"
|
| 68 |
+
elif adjusted_score < 0.6:
|
| 69 |
+
return "MODERATE"
|
| 70 |
+
elif adjusted_score < 0.8:
|
| 71 |
+
return "HIGH"
|
| 72 |
+
else:
|
| 73 |
+
return "CRITICAL"
|
| 74 |
+
```
|
| 75 |
+
|
| 76 |
+
**Key Insight:** Research shows that cosine similarity thresholds vary by domain and task. Values 0.7-0.8 are commonly recommended starting points for "relevant" matches. Below 0.6, matches become increasingly unreliable.
|
| 77 |
+
|
| 78 |
+
#### 2. Multi-Signal Fusion
|
| 79 |
+
|
| 80 |
+
Combine multiple indicators beyond just k-NN similarity:
|
| 81 |
+
|
| 82 |
+
```python
|
| 83 |
+
def compute_risk_with_fusion(prompt, knn_results, heuristics):
|
| 84 |
+
"""
|
| 85 |
+
Fuse vector similarity with rule-based heuristics
|
| 86 |
+
"""
|
| 87 |
+
# Vector-based score (from k-NN)
|
| 88 |
+
vector_score = compute_adaptive_risk(
|
| 89 |
+
knn_results['similarities'],
|
| 90 |
+
knn_results['difficulties']
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
# Rule-based heuristics (existing togmal patterns)
|
| 94 |
+
heuristic_score = heuristics.evaluate(prompt)
|
| 95 |
+
|
| 96 |
+
# Domain classifier (is this math/physics/medical?)
|
| 97 |
+
domain_confidence = classify_domain(prompt)
|
| 98 |
+
|
| 99 |
+
# Combine scores with learned weights
|
| 100 |
+
final_score = (
|
| 101 |
+
0.4 * vector_score +
|
| 102 |
+
0.4 * heuristic_score +
|
| 103 |
+
0.2 * domain_uncertainty(domain_confidence)
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
return final_score
|
| 107 |
+
```
|
| 108 |
+
|
| 109 |
+
#### 3. Threshold Calibration per Domain
|
| 110 |
+
|
| 111 |
+
Different domains need different thresholds. Implement **domain-specific calibration**:
|
| 112 |
+
|
| 113 |
+
```python
|
| 114 |
+
# Learned from validation data
|
| 115 |
+
DOMAIN_THRESHOLDS = {
|
| 116 |
+
'math': {'low': 0.65, 'moderate': 0.75, 'high': 0.85},
|
| 117 |
+
'physics': {'low': 0.60, 'moderate': 0.70, 'high': 0.80},
|
| 118 |
+
'medical': {'low': 0.70, 'moderate': 0.80, 'high': 0.90},
|
| 119 |
+
'general': {'low': 0.60, 'moderate': 0.70, 'high': 0.80}
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
def get_calibrated_threshold(domain, risk_level):
|
| 123 |
+
return DOMAIN_THRESHOLDS.get(domain, DOMAIN_THRESHOLDS['general'])[risk_level]
|
| 124 |
+
```
|
| 125 |
+
|
| 126 |
+
---
|
| 127 |
+
|
| 128 |
+
## Problem 2: Evaluation & Generalization
|
| 129 |
+
|
| 130 |
+
### Proposed Evaluation Framework: Nested Cross-Validation (Gold Standard)
|
| 131 |
+
|
| 132 |
+
#### Why Nested CV > Simple Train/Val/Test Split
|
| 133 |
+
|
| 134 |
+
**Problem with simple splits:**
|
| 135 |
+
- Single validation set can be unrepresentative (lucky/unlucky split)
|
| 136 |
+
- Repeated "peeking" at validation during hyperparameter search causes leakage
|
| 137 |
+
- Test set provides only ONE estimate of generalization (high variance)
|
| 138 |
+
|
| 139 |
+
**Nested CV advantages:**
|
| 140 |
+
- **Outer loop**: K-fold CV for unbiased generalization estimate
|
| 141 |
+
- **Inner loop**: Hyperparameter search on each training fold
|
| 142 |
+
- **No leakage**: Test folds never seen during tuning
|
| 143 |
+
- **Multiple estimates**: Robust performance across K different test sets
|
| 144 |
+
|
| 145 |
+
#### Implementation: Nested Cross-Validation
|
| 146 |
+
|
| 147 |
+
```python
|
| 148 |
+
from sklearn.model_selection import StratifiedKFold, GridSearchCV
|
| 149 |
+
import numpy as np
|
| 150 |
+
from typing import Dict, List, Any
|
| 151 |
+
|
| 152 |
+
class NestedCVEvaluator:
|
| 153 |
+
"""
|
| 154 |
+
Nested cross-validation for ToGMAL hyperparameter tuning and evaluation.
|
| 155 |
+
|
| 156 |
+
Outer CV: 5-fold stratified CV for generalization estimate
|
| 157 |
+
Inner CV: 3-fold stratified CV for hyperparameter search
|
| 158 |
+
|
| 159 |
+
This prevents data leakage from "peeking" at validation during tuning.
|
| 160 |
+
"""
|
| 161 |
+
|
| 162 |
+
def __init__(
|
| 163 |
+
self,
|
| 164 |
+
benchmark_data,
|
| 165 |
+
outer_folds: int = 5,
|
| 166 |
+
inner_folds: int = 3,
|
| 167 |
+
random_state: int = 42
|
| 168 |
+
):
|
| 169 |
+
self.data = benchmark_data
|
| 170 |
+
self.outer_folds = outer_folds
|
| 171 |
+
self.inner_folds = inner_folds
|
| 172 |
+
self.random_state = random_state
|
| 173 |
+
|
| 174 |
+
# Stratify by (domain, difficulty) to ensure balanced folds
|
| 175 |
+
self.stratify_labels = (
|
| 176 |
+
benchmark_data['domain'].astype(str) + '_' +
|
| 177 |
+
benchmark_data['difficulty_label'].astype(str)
|
| 178 |
+
)
|
| 179 |
+
|
| 180 |
+
def run_nested_cv(
|
| 181 |
+
self,
|
| 182 |
+
param_grid: Dict[str, List[Any]],
|
| 183 |
+
scoring_metric: str = 'roc_auc'
|
| 184 |
+
) -> Dict[str, Any]:
|
| 185 |
+
"""
|
| 186 |
+
Run nested cross-validation.
|
| 187 |
+
|
| 188 |
+
Args:
|
| 189 |
+
param_grid: Hyperparameters to search (e.g., {'k': [3,5,7], 'threshold': [0.6,0.7]})
|
| 190 |
+
scoring_metric: Metric for optimization (roc_auc, f1, etc.)
|
| 191 |
+
|
| 192 |
+
Returns:
|
| 193 |
+
Dictionary with:
|
| 194 |
+
- outer_scores: Generalization performance on each outer fold
|
| 195 |
+
- best_params_per_fold: Optimal hyperparameters found in each inner CV
|
| 196 |
+
- mean_test_score: Average performance across outer folds
|
| 197 |
+
- std_test_score: Standard deviation (uncertainty estimate)
|
| 198 |
+
"""
|
| 199 |
+
|
| 200 |
+
# Outer CV: For generalization estimate
|
| 201 |
+
outer_cv = StratifiedKFold(
|
| 202 |
+
n_splits=self.outer_folds,
|
| 203 |
+
shuffle=True,
|
| 204 |
+
random_state=self.random_state
|
| 205 |
+
)
|
| 206 |
+
|
| 207 |
+
outer_scores = []
|
| 208 |
+
best_params_per_fold = []
|
| 209 |
+
|
| 210 |
+
print("Starting Nested Cross-Validation...")
|
| 211 |
+
print(f"Outer CV: {self.outer_folds} folds")
|
| 212 |
+
print(f"Inner CV: {self.inner_folds} folds")
|
| 213 |
+
print(f"Param grid: {param_grid}")
|
| 214 |
+
print("="*80)
|
| 215 |
+
|
| 216 |
+
for fold_idx, (train_idx, test_idx) in enumerate(outer_cv.split(self.data, self.stratify_labels)):
|
| 217 |
+
print(f"\nOuter Fold {fold_idx + 1}/{self.outer_folds}")
|
| 218 |
+
|
| 219 |
+
# Split data for this outer fold
|
| 220 |
+
train_data = self.data.iloc[train_idx]
|
| 221 |
+
test_data = self.data.iloc[test_idx]
|
| 222 |
+
|
| 223 |
+
# Inner CV: Hyperparameter search on training data ONLY
|
| 224 |
+
inner_cv = StratifiedKFold(
|
| 225 |
+
n_splits=self.inner_folds,
|
| 226 |
+
shuffle=True,
|
| 227 |
+
random_state=self.random_state
|
| 228 |
+
)
|
| 229 |
+
|
| 230 |
+
# Run grid search on inner folds
|
| 231 |
+
best_params, best_inner_score = self._inner_grid_search(
|
| 232 |
+
train_data,
|
| 233 |
+
param_grid,
|
| 234 |
+
inner_cv,
|
| 235 |
+
scoring_metric
|
| 236 |
+
)
|
| 237 |
+
|
| 238 |
+
print(f" Inner CV best params: {best_params}")
|
| 239 |
+
print(f" Inner CV best score: {best_inner_score:.4f}")
|
| 240 |
+
|
| 241 |
+
# Build ToGMAL vector DB with ONLY training data
|
| 242 |
+
vector_db = self._build_vector_db(train_data)
|
| 243 |
+
|
| 244 |
+
# Evaluate on held-out test fold with best hyperparameters
|
| 245 |
+
test_score = self._evaluate_on_test_fold(
|
| 246 |
+
vector_db,
|
| 247 |
+
test_data,
|
| 248 |
+
best_params,
|
| 249 |
+
scoring_metric
|
| 250 |
+
)
|
| 251 |
+
|
| 252 |
+
print(f" Outer test score: {test_score:.4f}")
|
| 253 |
+
|
| 254 |
+
outer_scores.append(test_score)
|
| 255 |
+
best_params_per_fold.append(best_params)
|
| 256 |
+
|
| 257 |
+
# Aggregate results
|
| 258 |
+
mean_score = np.mean(outer_scores)
|
| 259 |
+
std_score = np.std(outer_scores)
|
| 260 |
+
|
| 261 |
+
print("\n" + "="*80)
|
| 262 |
+
print("Nested CV Results:")
|
| 263 |
+
print(f" Outer scores: {[f'{s:.4f}' for s in outer_scores]}")
|
| 264 |
+
print(f" Mean Β± Std: {mean_score:.4f} Β± {std_score:.4f}")
|
| 265 |
+
print("="*80)
|
| 266 |
+
|
| 267 |
+
return {
|
| 268 |
+
'outer_scores': outer_scores,
|
| 269 |
+
'mean_test_score': mean_score,
|
| 270 |
+
'std_test_score': std_score,
|
| 271 |
+
'best_params_per_fold': best_params_per_fold,
|
| 272 |
+
'most_common_params': self._find_most_common_params(best_params_per_fold)
|
| 273 |
+
}
|
| 274 |
+
|
| 275 |
+
def _inner_grid_search(
|
| 276 |
+
self,
|
| 277 |
+
train_data,
|
| 278 |
+
param_grid: Dict[str, List[Any]],
|
| 279 |
+
inner_cv,
|
| 280 |
+
scoring_metric: str
|
| 281 |
+
) -> tuple:
|
| 282 |
+
"""
|
| 283 |
+
Grid search over hyperparameters using inner CV folds.
|
| 284 |
+
Returns (best_params, best_score)
|
| 285 |
+
"""
|
| 286 |
+
stratify = (
|
| 287 |
+
train_data['domain'].astype(str) + '_' +
|
| 288 |
+
train_data['difficulty_label'].astype(str)
|
| 289 |
+
)
|
| 290 |
+
|
| 291 |
+
best_score = -np.inf
|
| 292 |
+
best_params = {}
|
| 293 |
+
|
| 294 |
+
# Generate all parameter combinations
|
| 295 |
+
from itertools import product
|
| 296 |
+
param_names = list(param_grid.keys())
|
| 297 |
+
param_values = list(param_grid.values())
|
| 298 |
+
|
| 299 |
+
for param_combo in product(*param_values):
|
| 300 |
+
params = dict(zip(param_names, param_combo))
|
| 301 |
+
|
| 302 |
+
# Evaluate this parameter combination on inner folds
|
| 303 |
+
fold_scores = []
|
| 304 |
+
|
| 305 |
+
for inner_train_idx, inner_val_idx in inner_cv.split(train_data, stratify):
|
| 306 |
+
inner_train = train_data.iloc[inner_train_idx]
|
| 307 |
+
inner_val = train_data.iloc[inner_val_idx]
|
| 308 |
+
|
| 309 |
+
# Build vector DB with inner training data
|
| 310 |
+
inner_db = self._build_vector_db(inner_train)
|
| 311 |
+
|
| 312 |
+
# Evaluate on inner validation
|
| 313 |
+
score = self._evaluate_on_test_fold(
|
| 314 |
+
inner_db,
|
| 315 |
+
inner_val,
|
| 316 |
+
params,
|
| 317 |
+
scoring_metric
|
| 318 |
+
)
|
| 319 |
+
fold_scores.append(score)
|
| 320 |
+
|
| 321 |
+
avg_score = np.mean(fold_scores)
|
| 322 |
+
|
| 323 |
+
if avg_score > best_score:
|
| 324 |
+
best_score = avg_score
|
| 325 |
+
best_params = params
|
| 326 |
+
|
| 327 |
+
return best_params, best_score
|
| 328 |
+
|
| 329 |
+
def _build_vector_db(self, train_data):
|
| 330 |
+
"""Build vector database from training data."""
|
| 331 |
+
from benchmark_vector_db import BenchmarkVectorDB, BenchmarkQuestion
|
| 332 |
+
from pathlib import Path
|
| 333 |
+
import tempfile
|
| 334 |
+
|
| 335 |
+
# Create temporary DB for this fold
|
| 336 |
+
temp_dir = tempfile.mkdtemp()
|
| 337 |
+
db = BenchmarkVectorDB(
|
| 338 |
+
db_path=Path(temp_dir) / "fold_db",
|
| 339 |
+
embedding_model="all-MiniLM-L6-v2"
|
| 340 |
+
)
|
| 341 |
+
|
| 342 |
+
# Convert dataframe to BenchmarkQuestion objects
|
| 343 |
+
questions = [
|
| 344 |
+
BenchmarkQuestion(
|
| 345 |
+
question_id=row['question_id'],
|
| 346 |
+
source_benchmark=row['source_benchmark'],
|
| 347 |
+
domain=row['domain'],
|
| 348 |
+
question_text=row['question_text'],
|
| 349 |
+
correct_answer=row['correct_answer'],
|
| 350 |
+
success_rate=row['success_rate'],
|
| 351 |
+
difficulty_score=row['difficulty_score'],
|
| 352 |
+
difficulty_label=row['difficulty_label']
|
| 353 |
+
)
|
| 354 |
+
for _, row in train_data.iterrows()
|
| 355 |
+
]
|
| 356 |
+
|
| 357 |
+
db.index_questions(questions)
|
| 358 |
+
return db
|
| 359 |
+
|
| 360 |
+
def _evaluate_on_test_fold(
|
| 361 |
+
self,
|
| 362 |
+
vector_db,
|
| 363 |
+
test_data,
|
| 364 |
+
params: Dict[str, Any],
|
| 365 |
+
metric: str
|
| 366 |
+
) -> float:
|
| 367 |
+
"""
|
| 368 |
+
Evaluate ToGMAL on test fold with given hyperparameters.
|
| 369 |
+
|
| 370 |
+
Args:
|
| 371 |
+
vector_db: Vector database built from training data
|
| 372 |
+
test_data: Held-out test fold
|
| 373 |
+
params: Hyperparameters (e.g., k, similarity_threshold, weights)
|
| 374 |
+
metric: Scoring metric (roc_auc, f1, etc.)
|
| 375 |
+
"""
|
| 376 |
+
from sklearn.metrics import roc_auc_score, f1_score
|
| 377 |
+
|
| 378 |
+
predictions = []
|
| 379 |
+
ground_truth = []
|
| 380 |
+
|
| 381 |
+
for _, row in test_data.iterrows():
|
| 382 |
+
# Query vector DB with test question
|
| 383 |
+
result = vector_db.query_similar_questions(
|
| 384 |
+
prompt=row['question_text'],
|
| 385 |
+
k=params.get('k_neighbors', 5)
|
| 386 |
+
)
|
| 387 |
+
|
| 388 |
+
# Apply adaptive scoring with hyperparameters
|
| 389 |
+
risk_score = self._compute_adaptive_risk(
|
| 390 |
+
result,
|
| 391 |
+
params
|
| 392 |
+
)
|
| 393 |
+
|
| 394 |
+
predictions.append(risk_score)
|
| 395 |
+
|
| 396 |
+
# Ground truth: is this question hard? (success_rate < 0.5)
|
| 397 |
+
ground_truth.append(1 if row['success_rate'] < 0.5 else 0)
|
| 398 |
+
|
| 399 |
+
# Compute metric
|
| 400 |
+
if metric == 'roc_auc':
|
| 401 |
+
return roc_auc_score(ground_truth, predictions)
|
| 402 |
+
elif metric == 'f1':
|
| 403 |
+
# Binarize predictions at 0.5 threshold
|
| 404 |
+
binary_preds = [1 if p > 0.5 else 0 for p in predictions]
|
| 405 |
+
return f1_score(ground_truth, binary_preds)
|
| 406 |
+
else:
|
| 407 |
+
raise ValueError(f"Unknown metric: {metric}")
|
| 408 |
+
|
| 409 |
+
def _compute_adaptive_risk(
|
| 410 |
+
self,
|
| 411 |
+
query_result: Dict[str, Any],
|
| 412 |
+
params: Dict[str, Any]
|
| 413 |
+
) -> float:
|
| 414 |
+
"""
|
| 415 |
+
Compute risk score with adaptive uncertainty penalties.
|
| 416 |
+
Uses hyperparameters from inner CV search.
|
| 417 |
+
"""
|
| 418 |
+
similarities = [q['similarity'] for q in query_result['similar_questions']]
|
| 419 |
+
difficulties = [q['difficulty_score'] for q in query_result['similar_questions']]
|
| 420 |
+
|
| 421 |
+
# Base weighted average
|
| 422 |
+
weights = np.array(similarities) / sum(similarities)
|
| 423 |
+
base_score = np.dot(weights, difficulties)
|
| 424 |
+
|
| 425 |
+
# Adaptive uncertainty penalties
|
| 426 |
+
max_sim = max(similarities)
|
| 427 |
+
avg_sim = np.mean(similarities)
|
| 428 |
+
sim_variance = np.var(similarities)
|
| 429 |
+
|
| 430 |
+
uncertainty_penalty = 0.0
|
| 431 |
+
|
| 432 |
+
# Low similarity threshold (configurable)
|
| 433 |
+
sim_threshold = params.get('similarity_threshold', 0.7)
|
| 434 |
+
if max_sim < sim_threshold:
|
| 435 |
+
uncertainty_penalty += (sim_threshold - max_sim) * params.get('low_sim_penalty', 0.5)
|
| 436 |
+
|
| 437 |
+
# High variance penalty
|
| 438 |
+
if sim_variance > 0.05:
|
| 439 |
+
uncertainty_penalty += min(sim_variance * params.get('variance_penalty', 2.0), 0.3)
|
| 440 |
+
|
| 441 |
+
# Low average similarity
|
| 442 |
+
if avg_sim < 0.5:
|
| 443 |
+
uncertainty_penalty += (0.5 - avg_sim) * params.get('low_avg_penalty', 0.4)
|
| 444 |
+
|
| 445 |
+
# Final score
|
| 446 |
+
adjusted_score = base_score + uncertainty_penalty
|
| 447 |
+
|
| 448 |
+
return np.clip(adjusted_score, 0.0, 1.0)
|
| 449 |
+
|
| 450 |
+
def _find_most_common_params(self, params_list: List[Dict]) -> Dict:
|
| 451 |
+
"""Find the most frequently selected hyperparameters across folds."""
|
| 452 |
+
from collections import Counter
|
| 453 |
+
|
| 454 |
+
# For each parameter, find the most common value
|
| 455 |
+
all_param_names = params_list[0].keys()
|
| 456 |
+
most_common = {}
|
| 457 |
+
|
| 458 |
+
for param_name in all_param_names:
|
| 459 |
+
values = [p[param_name] for p in params_list]
|
| 460 |
+
most_common[param_name] = Counter(values).most_common(1)[0][0]
|
| 461 |
+
|
| 462 |
+
return most_common
|
| 463 |
+
|
| 464 |
+
|
| 465 |
+
# Example usage
|
| 466 |
+
if __name__ == "__main__":
|
| 467 |
+
import pandas as pd
|
| 468 |
+
from benchmark_vector_db import BenchmarkVectorDB
|
| 469 |
+
|
| 470 |
+
# Load all benchmark questions
|
| 471 |
+
db = BenchmarkVectorDB(db_path=Path("/Users/hetalksinmaths/togmal/data/benchmark_vector_db"))
|
| 472 |
+
stats = db.get_statistics()
|
| 473 |
+
|
| 474 |
+
# Get all questions as dataframe (you'll need to implement this)
|
| 475 |
+
all_questions_df = db.get_all_questions_as_dataframe()
|
| 476 |
+
|
| 477 |
+
# Define hyperparameter search grid
|
| 478 |
+
param_grid = {
|
| 479 |
+
'k_neighbors': [3, 5, 7, 10],
|
| 480 |
+
'similarity_threshold': [0.6, 0.7, 0.8],
|
| 481 |
+
'low_sim_penalty': [0.3, 0.5, 0.7],
|
| 482 |
+
'variance_penalty': [1.0, 2.0, 3.0],
|
| 483 |
+
'low_avg_penalty': [0.2, 0.4, 0.6]
|
| 484 |
+
}
|
| 485 |
+
|
| 486 |
+
# Run nested CV
|
| 487 |
+
evaluator = NestedCVEvaluator(
|
| 488 |
+
benchmark_data=all_questions_df,
|
| 489 |
+
outer_folds=5, # 5-fold outer CV
|
| 490 |
+
inner_folds=3 # 3-fold inner CV for hyperparameter search
|
| 491 |
+
)
|
| 492 |
+
|
| 493 |
+
results = evaluator.run_nested_cv(
|
| 494 |
+
param_grid=param_grid,
|
| 495 |
+
scoring_metric='roc_auc'
|
| 496 |
+
)
|
| 497 |
+
|
| 498 |
+
print("\nFinal Results:")
|
| 499 |
+
print(f"Generalization Performance: {results['mean_test_score']:.4f} Β± {results['std_test_score']:.4f}")
|
| 500 |
+
print(f"Most Common Best Params: {results['most_common_params']}")
|
| 501 |
+
```
|
| 502 |
+
|
| 503 |
+
**Key Advantages:**
|
| 504 |
+
- **No leakage**: Each outer test fold is never seen during hyperparameter tuning
|
| 505 |
+
- **Robust estimates**: 5 different generalization scores (not just 1)
|
| 506 |
+
- **Automatic tuning**: Inner CV finds best hyperparameters for each fold
|
| 507 |
+
- **Confidence intervals**: Standard deviation tells you uncertainty in performance
|
| 508 |
+
|
| 509 |
+
#### Phase 2: Define Evaluation Metrics
|
| 510 |
+
|
| 511 |
+
Use standard **OOD detection metrics** + **calibration metrics**:
|
| 512 |
+
|
| 513 |
+
1. **AUROC** (Area Under ROC Curve)
|
| 514 |
+
- Threshold-independent
|
| 515 |
+
- Measures overall discriminative ability
|
| 516 |
+
- Gold standard for OOD detection
|
| 517 |
+
- Interpretation: Probability that a random risky prompt is ranked higher than a random safe prompt
|
| 518 |
+
|
| 519 |
+
2. **FPR@TPR95** (False Positive Rate at 95% True Positive Rate)
|
| 520 |
+
- How many safe prompts are incorrectly flagged when catching 95% of risky ones
|
| 521 |
+
- Common in safety-critical applications
|
| 522 |
+
- Lower is better (want to minimize false alarms)
|
| 523 |
+
|
| 524 |
+
3. **AUPR** (Area Under Precision-Recall Curve)
|
| 525 |
+
- Better for imbalanced datasets
|
| 526 |
+
- Useful when risky prompts are rare
|
| 527 |
+
- Focuses on positive class (risky prompts)
|
| 528 |
+
|
| 529 |
+
4. **Expected Calibration Error (ECE)**
|
| 530 |
+
- Are your risk probabilities accurate?
|
| 531 |
+
- If you say 70% risky, is it actually 70% risky?
|
| 532 |
+
- Measures gap between predicted probabilities and observed frequencies
|
| 533 |
+
|
| 534 |
+
5. **Brier Score**
|
| 535 |
+
- Measures accuracy of probabilistic predictions
|
| 536 |
+
- Lower is better
|
| 537 |
+
- Combines discrimination and calibration
|
| 538 |
+
|
| 539 |
+
```python
|
| 540 |
+
from sklearn.metrics import roc_auc_score, precision_recall_curve, auc, brier_score_loss
|
| 541 |
+
import numpy as np
|
| 542 |
+
|
| 543 |
+
def compute_fpr_at_tpr(y_true, y_pred_proba, tpr_threshold=0.95):
|
| 544 |
+
"""Compute FPR when TPR is at specified threshold."""
|
| 545 |
+
from sklearn.metrics import roc_curve
|
| 546 |
+
|
| 547 |
+
fpr, tpr, thresholds = roc_curve(y_true, y_pred_proba)
|
| 548 |
+
|
| 549 |
+
# Find index where TPR >= threshold
|
| 550 |
+
idx = np.argmax(tpr >= tpr_threshold)
|
| 551 |
+
|
| 552 |
+
return fpr[idx]
|
| 553 |
+
|
| 554 |
+
def expected_calibration_error(y_true, y_pred_proba, n_bins=10):
|
| 555 |
+
"""
|
| 556 |
+
Compute Expected Calibration Error (ECE).
|
| 557 |
+
|
| 558 |
+
Bins predictions into n_bins buckets and measures the gap between
|
| 559 |
+
predicted probability and observed frequency in each bin.
|
| 560 |
+
"""
|
| 561 |
+
bin_boundaries = np.linspace(0, 1, n_bins + 1)
|
| 562 |
+
bin_lowers = bin_boundaries[:-1]
|
| 563 |
+
bin_uppers = bin_boundaries[1:]
|
| 564 |
+
|
| 565 |
+
ece = 0.0
|
| 566 |
+
|
| 567 |
+
for bin_lower, bin_upper in zip(bin_lowers, bin_uppers):
|
| 568 |
+
# Find predictions in this bin
|
| 569 |
+
in_bin = (y_pred_proba > bin_lower) & (y_pred_proba <= bin_upper)
|
| 570 |
+
prop_in_bin = in_bin.mean()
|
| 571 |
+
|
| 572 |
+
if prop_in_bin > 0:
|
| 573 |
+
# Observed frequency in this bin
|
| 574 |
+
accuracy_in_bin = y_true[in_bin].mean()
|
| 575 |
+
# Average predicted probability in this bin
|
| 576 |
+
avg_confidence_in_bin = y_pred_proba[in_bin].mean()
|
| 577 |
+
|
| 578 |
+
# Contribution to ECE
|
| 579 |
+
ece += np.abs(avg_confidence_in_bin - accuracy_in_bin) * prop_in_bin
|
| 580 |
+
|
| 581 |
+
return ece
|
| 582 |
+
|
| 583 |
+
def evaluate_togmal(predictions, ground_truth):
|
| 584 |
+
"""
|
| 585 |
+
Comprehensive evaluation of ToGMAL performance.
|
| 586 |
+
|
| 587 |
+
Args:
|
| 588 |
+
predictions: Dict with 'risk_score' (continuous 0-1) and 'risk_level' (categorical)
|
| 589 |
+
ground_truth: Array of difficulty scores or binary labels (0=easy, 1=hard)
|
| 590 |
+
|
| 591 |
+
Returns:
|
| 592 |
+
Dictionary with all evaluation metrics
|
| 593 |
+
"""
|
| 594 |
+
# Convert ground truth to binary if needed (HIGH/CRITICAL = 1, else = 0)
|
| 595 |
+
if hasattr(ground_truth, 'success_rate'):
|
| 596 |
+
y_true = (ground_truth['success_rate'] < 0.5).astype(int)
|
| 597 |
+
else:
|
| 598 |
+
y_true = ground_truth
|
| 599 |
+
|
| 600 |
+
y_pred_proba = predictions['risk_score'] # Continuous 0-1
|
| 601 |
+
y_pred_binary = (y_pred_proba > 0.5).astype(int) # Binarized
|
| 602 |
+
|
| 603 |
+
# AUROC
|
| 604 |
+
auroc = roc_auc_score(y_true, y_pred_proba)
|
| 605 |
+
|
| 606 |
+
# FPR@TPR95
|
| 607 |
+
fpr_at_95_tpr = compute_fpr_at_tpr(y_true, y_pred_proba, tpr_threshold=0.95)
|
| 608 |
+
|
| 609 |
+
# AUPR
|
| 610 |
+
precision, recall, _ = precision_recall_curve(y_true, y_pred_proba)
|
| 611 |
+
aupr = auc(recall, precision)
|
| 612 |
+
|
| 613 |
+
# Calibration error
|
| 614 |
+
ece = expected_calibration_error(y_true, y_pred_proba, n_bins=10)
|
| 615 |
+
|
| 616 |
+
# Brier score (lower is better)
|
| 617 |
+
brier = brier_score_loss(y_true, y_pred_proba)
|
| 618 |
+
|
| 619 |
+
# Standard classification metrics (for reference)
|
| 620 |
+
from sklearn.metrics import accuracy_score, f1_score, precision_score, recall_score
|
| 621 |
+
|
| 622 |
+
accuracy = accuracy_score(y_true, y_pred_binary)
|
| 623 |
+
f1 = f1_score(y_true, y_pred_binary)
|
| 624 |
+
precision = precision_score(y_true, y_pred_binary)
|
| 625 |
+
recall = recall_score(y_true, y_pred_binary)
|
| 626 |
+
|
| 627 |
+
return {
|
| 628 |
+
# Primary OOD detection metrics
|
| 629 |
+
'AUROC': auroc,
|
| 630 |
+
'FPR@TPR95': fpr_at_95_tpr,
|
| 631 |
+
'AUPR': aupr,
|
| 632 |
+
|
| 633 |
+
# Calibration metrics
|
| 634 |
+
'ECE': ece,
|
| 635 |
+
'Brier_Score': brier,
|
| 636 |
+
|
| 637 |
+
# Standard classification (for reference)
|
| 638 |
+
'Accuracy': accuracy,
|
| 639 |
+
'F1': f1,
|
| 640 |
+
'Precision': precision,
|
| 641 |
+
'Recall': recall
|
| 642 |
+
}
|
| 643 |
+
|
| 644 |
+
def print_evaluation_report(metrics: dict):
|
| 645 |
+
"""Pretty print evaluation metrics."""
|
| 646 |
+
print("\n" + "="*80)
|
| 647 |
+
print("ToGMAL Evaluation Report")
|
| 648 |
+
print("="*80)
|
| 649 |
+
|
| 650 |
+
print("\nOOD Detection Performance:")
|
| 651 |
+
print(f" AUROC: {metrics['AUROC']:.4f} (higher is better, 0.5=random, 1.0=perfect)")
|
| 652 |
+
print(f" FPR@TPR95: {metrics['FPR@TPR95']:.4f} (lower is better, false alarm rate)")
|
| 653 |
+
print(f" AUPR: {metrics['AUPR']:.4f} (higher is better)")
|
| 654 |
+
|
| 655 |
+
print("\nCalibration:")
|
| 656 |
+
print(f" ECE: {metrics['ECE']:.4f} (lower is better, 0=perfect calibration)")
|
| 657 |
+
print(f" Brier Score: {metrics['Brier_Score']:.4f} (lower is better)")
|
| 658 |
+
|
| 659 |
+
print("\nClassification Metrics (for reference):")
|
| 660 |
+
print(f" Accuracy: {metrics['Accuracy']:.4f}")
|
| 661 |
+
print(f" F1 Score: {metrics['F1']:.4f}")
|
| 662 |
+
print(f" Precision: {metrics['Precision']:.4f}")
|
| 663 |
+
print(f" Recall: {metrics['Recall']:.4f}")
|
| 664 |
+
|
| 665 |
+
print("\n" + "="*80)
|
| 666 |
+
```
|
| 667 |
+
|
| 668 |
+
#### Phase 3: Out-of-Distribution Testing
|
| 669 |
+
|
| 670 |
+
**Critical:** Test on data that's truly OOD from your training benchmarks.
|
| 671 |
+
|
| 672 |
+
**OOD Test Sets to Create:**
|
| 673 |
+
|
| 674 |
+
1. **Temporal OOD**: New benchmark questions released after your training data cutoff
|
| 675 |
+
2. **Domain Shift**: Categories not in MMLU (e.g., creative writing prompts, coding challenges)
|
| 676 |
+
3. **Adversarial**: Hand-crafted examples designed to fool the system
|
| 677 |
+
- "Prove [false scientific claim]"
|
| 678 |
+
- Jailbreak attempts disguised as innocent questions
|
| 679 |
+
- Edge cases from your taxonomy submissions
|
| 680 |
+
|
| 681 |
+
```python
|
| 682 |
+
ood_test_sets = {
|
| 683 |
+
'adversarial_false_premises': load_false_premise_examples(),
|
| 684 |
+
'jailbreaks': load_jailbreak_attempts(),
|
| 685 |
+
'creative_writing': load_writing_prompts(),
|
| 686 |
+
'recent_benchmarks': load_benchmarks_after('2024-01'),
|
| 687 |
+
'user_submissions': load_taxonomy_entries()
|
| 688 |
+
}
|
| 689 |
+
|
| 690 |
+
# Evaluate on each OOD set
|
| 691 |
+
for name, test_data in ood_test_sets.items():
|
| 692 |
+
metrics = evaluate_togmal(model.predict(test_data), test_data.labels)
|
| 693 |
+
print(f"{name}: AUROC={metrics['AUROC']:.3f}, FPR@95={metrics['FPR@TPR95']:.3f}")
|
| 694 |
+
```
|
| 695 |
+
|
| 696 |
+
#### Phase 4: Hyperparameter Tuning Protocol
|
| 697 |
+
|
| 698 |
+
**Use validation set ONLY** - never touch test set until final evaluation.
|
| 699 |
+
|
| 700 |
+
```python
|
| 701 |
+
from sklearn.model_selection import GridSearchCV
|
| 702 |
+
|
| 703 |
+
# Parameters to tune
|
| 704 |
+
param_grid = {
|
| 705 |
+
'similarity_threshold': [0.5, 0.6, 0.7, 0.8],
|
| 706 |
+
'k_neighbors': [3, 5, 7, 10],
|
| 707 |
+
'uncertainty_penalty_weight': [0.2, 0.4, 0.6],
|
| 708 |
+
'heuristic_weight': [0.3, 0.4, 0.5],
|
| 709 |
+
'vector_weight': [0.3, 0.4, 0.5]
|
| 710 |
+
}
|
| 711 |
+
|
| 712 |
+
# Cross-validation on validation set
|
| 713 |
+
best_params = grid_search_cv(
|
| 714 |
+
togmal_model,
|
| 715 |
+
param_grid,
|
| 716 |
+
val_set,
|
| 717 |
+
metric='AUROC',
|
| 718 |
+
cv=5 # 5-fold CV within validation set
|
| 719 |
+
)
|
| 720 |
+
|
| 721 |
+
# Train final model with best params on train + val
|
| 722 |
+
final_model = train_togmal(
|
| 723 |
+
train_set + val_set,
|
| 724 |
+
params=best_params
|
| 725 |
+
)
|
| 726 |
+
|
| 727 |
+
# Evaluate ONCE on test set
|
| 728 |
+
final_metrics = evaluate_togmal(
|
| 729 |
+
final_model.predict(test_set),
|
| 730 |
+
test_set.labels
|
| 731 |
+
)
|
| 732 |
+
```
|
| 733 |
+
|
| 734 |
+
---
|
| 735 |
+
|
| 736 |
+
## Implementation Roadmap
|
| 737 |
+
|
| 738 |
+
### Phase 1: Adaptive Scoring Implementation (Week 1-2)
|
| 739 |
+
- [x] β Implement basic vector database with 32K questions
|
| 740 |
+
- [ ] Add adaptive uncertainty-aware scoring function
|
| 741 |
+
- [ ] Similarity threshold penalties
|
| 742 |
+
- [ ] Variance penalties for diverse matches
|
| 743 |
+
- [ ] Low average similarity penalties
|
| 744 |
+
- [ ] Implement domain-specific threshold calibration
|
| 745 |
+
- [ ] Add multi-signal fusion (vector + heuristics)
|
| 746 |
+
- [ ] Integrate into `benchmark_vector_db.py::query_similar_questions()`
|
| 747 |
+
|
| 748 |
+
### Phase 2: Data Export & Preparation (Week 2)
|
| 749 |
+
- [ ] Export all 32K questions from ChromaDB to pandas DataFrame
|
| 750 |
+
- [ ] Add `BenchmarkVectorDB.get_all_questions_as_dataframe()` method
|
| 751 |
+
- [ ] Include all metadata (domain, difficulty, success_rate, etc.)
|
| 752 |
+
- [ ] Verify stratification labels (domain Γ difficulty)
|
| 753 |
+
- [ ] Create initial train/val/test split (simple 70/15/15) for baseline
|
| 754 |
+
- [ ] Document dataset statistics per split
|
| 755 |
+
|
| 756 |
+
### Phase 3: Nested CV Framework (Week 3)
|
| 757 |
+
- [ ] Implement `NestedCVEvaluator` class
|
| 758 |
+
- [ ] Outer CV loop (5-fold stratified)
|
| 759 |
+
- [ ] Inner CV loop (3-fold grid search)
|
| 760 |
+
- [ ] Temporary vector DB creation per fold
|
| 761 |
+
- [ ] Define hyperparameter search grid
|
| 762 |
+
- `k_neighbors`: [3, 5, 7, 10]
|
| 763 |
+
- `similarity_threshold`: [0.6, 0.7, 0.8]
|
| 764 |
+
- `low_sim_penalty`: [0.3, 0.5, 0.7]
|
| 765 |
+
- `variance_penalty`: [1.0, 2.0, 3.0]
|
| 766 |
+
- `low_avg_penalty`: [0.2, 0.4, 0.6]
|
| 767 |
+
- [ ] Implement evaluation metrics (AUROC, FPR@TPR95, ECE)
|
| 768 |
+
|
| 769 |
+
### Phase 4: Baseline Evaluation (Week 3-4)
|
| 770 |
+
- [ ] Run current ToGMAL (naive weighted average) on simple split
|
| 771 |
+
- [ ] Compute baseline metrics:
|
| 772 |
+
- [ ] AUROC on test set
|
| 773 |
+
- [ ] FPR@TPR95
|
| 774 |
+
- [ ] Expected Calibration Error
|
| 775 |
+
- [ ] Brier Score
|
| 776 |
+
- [ ] Analyze failure modes:
|
| 777 |
+
- [ ] Low similarity cases (max_sim < 0.6)
|
| 778 |
+
- [ ] High variance matches
|
| 779 |
+
- [ ] Cross-domain queries
|
| 780 |
+
- [ ] Document baseline performance for comparison
|
| 781 |
+
|
| 782 |
+
### Phase 5: Nested CV Hyperparameter Tuning (Week 4-5)
|
| 783 |
+
- [ ] Run full nested CV (5 outer Γ 3 inner = 15 train-test runs)
|
| 784 |
+
- [ ] Track computational cost (time per fold)
|
| 785 |
+
- [ ] Collect best hyperparameters per outer fold
|
| 786 |
+
- [ ] Identify most common optimal parameters
|
| 787 |
+
- [ ] Compute mean Β± std generalization performance
|
| 788 |
+
|
| 789 |
+
### Phase 6: Final Model Training (Week 5)
|
| 790 |
+
- [ ] Train final model on ALL 32K questions with best hyperparameters
|
| 791 |
+
- [ ] Re-index full vector database
|
| 792 |
+
- [ ] Update `togmal_mcp.py` to use adaptive scoring
|
| 793 |
+
- [ ] Deploy to MCP server and HTTP facade
|
| 794 |
+
|
| 795 |
+
### Phase 7: OOD Testing (Week 6)
|
| 796 |
+
- [ ] Create OOD test sets:
|
| 797 |
+
- [ ] **Adversarial**: Hand-crafted edge cases
|
| 798 |
+
- "Prove [false scientific claim]"
|
| 799 |
+
- Jailbreak attempts disguised as questions
|
| 800 |
+
- Taxonomy submissions from users
|
| 801 |
+
- [ ] **Domain Shift**: Categories not in MMLU
|
| 802 |
+
- Creative writing prompts
|
| 803 |
+
- Code generation tasks
|
| 804 |
+
- Real-world user queries
|
| 805 |
+
- [ ] **Temporal OOD**: New benchmarks (2024+)
|
| 806 |
+
- SimpleQA (if available)
|
| 807 |
+
- Latest MMLU updates
|
| 808 |
+
- [ ] Evaluate on each OOD set
|
| 809 |
+
- [ ] Analyze degradation vs. in-distribution performance
|
| 810 |
+
|
| 811 |
+
### Phase 8: Iteration & Documentation (Week 7)
|
| 812 |
+
- [ ] Analyze failures on OOD sets
|
| 813 |
+
- [ ] Add new heuristics for missed patterns
|
| 814 |
+
- [ ] Re-run nested CV with updated features
|
| 815 |
+
- [ ] Generate calibration plots (reliability diagrams)
|
| 816 |
+
- [ ] Write technical report:
|
| 817 |
+
- [ ] Methodology (nested CV protocol)
|
| 818 |
+
- [ ] Results (baseline vs. adaptive)
|
| 819 |
+
- [ ] Ablation studies (each penalty component)
|
| 820 |
+
- [ ] OOD generalization analysis
|
| 821 |
+
- [ ] Failure mode documentation
|
| 822 |
+
|
| 823 |
+
---
|
| 824 |
+
|
| 825 |
+
## Expected Improvements
|
| 826 |
+
|
| 827 |
+
Based on OOD detection literature and nested CV best practices:
|
| 828 |
+
|
| 829 |
+
1. **Adaptive scoring** should improve AUROC by 5-15% on low-similarity cases
|
| 830 |
+
- Baseline: ~0.75 AUROC (naive weighted average)
|
| 831 |
+
- Target: ~0.85+ AUROC (adaptive with uncertainty)
|
| 832 |
+
|
| 833 |
+
2. **Nested CV** will give honest performance estimates
|
| 834 |
+
- Simple train/test: Single point estimate (could be lucky/unlucky)
|
| 835 |
+
- Nested CV: Mean Β± std across 5 folds (robust estimate)
|
| 836 |
+
|
| 837 |
+
3. **Domain calibration** should reduce false positives by 10-20%
|
| 838 |
+
- Expected: FPR@TPR95 drops from ~0.25 to ~0.15
|
| 839 |
+
|
| 840 |
+
4. **Multi-signal fusion** should catch edge cases like "prove false premise"
|
| 841 |
+
- Combine vector similarity + rule-based heuristics
|
| 842 |
+
- Expected: Improved recall on adversarial examples
|
| 843 |
+
|
| 844 |
+
5. **Calibration improvements**
|
| 845 |
+
- Expected Calibration Error (ECE) < 0.05
|
| 846 |
+
- Better alignment between predicted risk and actual difficulty
|
| 847 |
+
|
| 848 |
+
---
|
| 849 |
+
|
| 850 |
+
## Validation Checklist
|
| 851 |
+
|
| 852 |
+
Before deploying to production:
|
| 853 |
+
- β Nested CV completed with no data leakage
|
| 854 |
+
- β Hyperparameters tuned on inner CV folds only
|
| 855 |
+
- β Generalization performance estimated on outer CV folds
|
| 856 |
+
- β OOD sets tested (adversarial, domain-shift, temporal)
|
| 857 |
+
- β Calibration error measured and within acceptable range (ECE < 0.1)
|
| 858 |
+
- β Failure modes documented with specific examples
|
| 859 |
+
- β Ablation studies show each component contributes positively
|
| 860 |
+
- β Performance comparison: adaptive > baseline on all metrics
|
| 861 |
+
- β Real-world testing with user queries from taxonomy submissions
|
| 862 |
+
|
| 863 |
+
---
|
| 864 |
+
|
| 865 |
+
## Key References
|
| 866 |
+
|
| 867 |
+
1. **Similarity Thresholds**: Cosine similarity 0.7-0.8 recommended as starting point for "relevant" matches; lower values increasingly unreliable
|
| 868 |
+
2. **OOD Metrics**: AUROC, FPR@TPR95 are standard; conformal prediction provides probabilistic guarantees
|
| 869 |
+
3. **Adaptive Methods**: Uncertainty-aware thresholds outperform fixed thresholds in retrieval tasks
|
| 870 |
+
4. **Holdout Validation**: 60-20-20 or 70-15-15 splits common; stratification by domain/difficulty essential
|
| 871 |
+
5. **Calibration**: Expected Calibration Error (ECE) measures if predicted probabilities match observed frequencies
|
| 872 |
+
6. **Nested CV**: Gold standard for hyperparameter tuning; prevents leakage from repeated validation peeking
|
| 873 |
+
7. **Stratified K-Fold**: Maintains class distribution across folds; essential for imbalanced datasets
|
| 874 |
+
|
| 875 |
+
---
|
| 876 |
+
|
| 877 |
+
## Quick Start: Immediate Implementation
|
| 878 |
+
|
| 879 |
+
### Step 1: Add Adaptive Scoring to `benchmark_vector_db.py` (Today)
|
| 880 |
+
|
| 881 |
+
Replace the naive weighted average in `query_similar_questions()` with adaptive uncertainty-aware scoring:
|
| 882 |
+
|
| 883 |
+
```python
|
| 884 |
+
def query_similar_questions(
|
| 885 |
+
self,
|
| 886 |
+
prompt: str,
|
| 887 |
+
k: int = 5,
|
| 888 |
+
domain_filter: Optional[str] = None,
|
| 889 |
+
# NEW: Adaptive scoring parameters
|
| 890 |
+
similarity_threshold: float = 0.7,
|
| 891 |
+
low_sim_penalty: float = 0.5,
|
| 892 |
+
variance_penalty: float = 2.0,
|
| 893 |
+
low_avg_penalty: float = 0.4
|
| 894 |
+
) -> Dict[str, Any]:
|
| 895 |
+
"""Find k most similar benchmark questions with adaptive uncertainty penalties."""
|
| 896 |
+
|
| 897 |
+
# ... existing code to query ChromaDB ...
|
| 898 |
+
|
| 899 |
+
# Extract similarities and difficulty scores
|
| 900 |
+
similarities = []
|
| 901 |
+
difficulty_scores = []
|
| 902 |
+
success_rates = []
|
| 903 |
+
|
| 904 |
+
for i in range(len(results['ids'][0])):
|
| 905 |
+
metadata = results['metadatas'][0][i]
|
| 906 |
+
distance = results['distances'][0][i]
|
| 907 |
+
|
| 908 |
+
# Convert L2 distance to cosine similarity
|
| 909 |
+
similarity = max(0, 1 - (distance ** 2) / 2)
|
| 910 |
+
|
| 911 |
+
similarities.append(similarity)
|
| 912 |
+
difficulty_scores.append(metadata['difficulty_score'])
|
| 913 |
+
success_rates.append(metadata['success_rate'])
|
| 914 |
+
|
| 915 |
+
# IMPROVED: Adaptive uncertainty-aware scoring
|
| 916 |
+
weighted_difficulty = self._compute_adaptive_difficulty(
|
| 917 |
+
similarities=similarities,
|
| 918 |
+
difficulty_scores=difficulty_scores,
|
| 919 |
+
similarity_threshold=similarity_threshold,
|
| 920 |
+
low_sim_penalty=low_sim_penalty,
|
| 921 |
+
variance_penalty=variance_penalty,
|
| 922 |
+
low_avg_penalty=low_avg_penalty
|
| 923 |
+
)
|
| 924 |
+
|
| 925 |
+
# ... rest of existing code ...
|
| 926 |
+
|
| 927 |
+
def _compute_adaptive_difficulty(
|
| 928 |
+
self,
|
| 929 |
+
similarities: List[float],
|
| 930 |
+
difficulty_scores: List[float],
|
| 931 |
+
similarity_threshold: float = 0.7,
|
| 932 |
+
low_sim_penalty: float = 0.5,
|
| 933 |
+
variance_penalty: float = 2.0,
|
| 934 |
+
low_avg_penalty: float = 0.4
|
| 935 |
+
) -> float:
|
| 936 |
+
"""
|
| 937 |
+
Compute difficulty score with adaptive uncertainty penalties.
|
| 938 |
+
|
| 939 |
+
Key insight: When retrieved questions have low similarity to the prompt,
|
| 940 |
+
we should INCREASE the risk estimate because we're extrapolating.
|
| 941 |
+
|
| 942 |
+
Args:
|
| 943 |
+
similarities: Cosine similarities of k-NN results
|
| 944 |
+
difficulty_scores: Difficulty scores (1 - success_rate) of k-NN results
|
| 945 |
+
similarity_threshold: Below this, apply low similarity penalty (default: 0.7)
|
| 946 |
+
low_sim_penalty: Weight for low similarity penalty (default: 0.5)
|
| 947 |
+
variance_penalty: Weight for high variance penalty (default: 2.0)
|
| 948 |
+
low_avg_penalty: Weight for low average similarity penalty (default: 0.4)
|
| 949 |
+
|
| 950 |
+
Returns:
|
| 951 |
+
Adjusted difficulty score (0.0 to 1.0, higher = more risky)
|
| 952 |
+
"""
|
| 953 |
+
import numpy as np
|
| 954 |
+
|
| 955 |
+
# Base weighted average (original approach)
|
| 956 |
+
weights = np.array(similarities) / sum(similarities)
|
| 957 |
+
base_score = np.dot(weights, difficulty_scores)
|
| 958 |
+
|
| 959 |
+
# Compute uncertainty indicators
|
| 960 |
+
max_sim = max(similarities)
|
| 961 |
+
avg_sim = np.mean(similarities)
|
| 962 |
+
sim_variance = np.var(similarities)
|
| 963 |
+
|
| 964 |
+
# Initialize uncertainty penalty
|
| 965 |
+
uncertainty_penalty = 0.0
|
| 966 |
+
|
| 967 |
+
# Penalty 1: Low maximum similarity
|
| 968 |
+
# If best match is weak, we're likely OOD
|
| 969 |
+
if max_sim < similarity_threshold:
|
| 970 |
+
penalty = (similarity_threshold - max_sim) * low_sim_penalty
|
| 971 |
+
uncertainty_penalty += penalty
|
| 972 |
+
logger.debug(f"Low max similarity penalty: {penalty:.3f} (max_sim={max_sim:.3f})")
|
| 973 |
+
|
| 974 |
+
# Penalty 2: High variance in similarities
|
| 975 |
+
# If k-NN results are very dissimilar to each other, matches are unreliable
|
| 976 |
+
variance_threshold = 0.05
|
| 977 |
+
if sim_variance > variance_threshold:
|
| 978 |
+
penalty = min(sim_variance * variance_penalty, 0.3) # Cap at 0.3
|
| 979 |
+
uncertainty_penalty += penalty
|
| 980 |
+
logger.debug(f"High variance penalty: {penalty:.3f} (variance={sim_variance:.3f})")
|
| 981 |
+
|
| 982 |
+
# Penalty 3: Low average similarity
|
| 983 |
+
# If ALL matches are weak, we're definitely OOD
|
| 984 |
+
avg_threshold = 0.5
|
| 985 |
+
if avg_sim < avg_threshold:
|
| 986 |
+
penalty = (avg_threshold - avg_sim) * low_avg_penalty
|
| 987 |
+
uncertainty_penalty += penalty
|
| 988 |
+
logger.debug(f"Low avg similarity penalty: {penalty:.3f} (avg_sim={avg_sim:.3f})")
|
| 989 |
+
|
| 990 |
+
# Final adjusted score
|
| 991 |
+
adjusted_score = base_score + uncertainty_penalty
|
| 992 |
+
|
| 993 |
+
# Clip to [0, 1] range
|
| 994 |
+
adjusted_score = np.clip(adjusted_score, 0.0, 1.0)
|
| 995 |
+
|
| 996 |
+
logger.info(
|
| 997 |
+
f"Adaptive scoring: base={base_score:.3f}, penalty={uncertainty_penalty:.3f}, "
|
| 998 |
+
f"adjusted={adjusted_score:.3f}"
|
| 999 |
+
)
|
| 1000 |
+
|
| 1001 |
+
return adjusted_score
|
| 1002 |
+
```
|
| 1003 |
+
|
| 1004 |
+
**Why this helps:**
|
| 1005 |
+
- **"Prove universe is 10,000 years old" example**: max_sim=0.57 triggers low similarity penalty β risk increases from MODERATE to HIGH
|
| 1006 |
+
- **Unrelated k-NN matches**: High variance β additional penalty β correctly flags as uncertain
|
| 1007 |
+
- **Novel domains**: Low average similarity across all matches β strong penalty β CRITICAL risk
|
| 1008 |
+
|
| 1009 |
+
### Step 2: Export Database for Evaluation (This Week)
|
| 1010 |
+
|
| 1011 |
+
Add method to export all questions as DataFrame for nested CV:
|
| 1012 |
+
|
| 1013 |
+
```python
|
| 1014 |
+
def get_all_questions_as_dataframe(self) -> 'pd.DataFrame':
|
| 1015 |
+
"""
|
| 1016 |
+
Export all questions from ChromaDB as a pandas DataFrame.
|
| 1017 |
+
Used for train/val/test splitting and nested CV.
|
| 1018 |
+
|
| 1019 |
+
Returns:
|
| 1020 |
+
DataFrame with columns:
|
| 1021 |
+
- question_id, source_benchmark, domain, question_text,
|
| 1022 |
+
- correct_answer, success_rate, difficulty_score, difficulty_label
|
| 1023 |
+
"""
|
| 1024 |
+
import pandas as pd
|
| 1025 |
+
|
| 1026 |
+
count = self.collection.count()
|
| 1027 |
+
logger.info(f"Exporting {count} questions from vector database...")
|
| 1028 |
+
|
| 1029 |
+
# Get all questions from ChromaDB
|
| 1030 |
+
all_data = self.collection.get(
|
| 1031 |
+
limit=count,
|
| 1032 |
+
include=["metadatas", "documents"]
|
| 1033 |
+
)
|
| 1034 |
+
|
| 1035 |
+
# Convert to DataFrame
|
| 1036 |
+
rows = []
|
| 1037 |
+
for i, qid in enumerate(all_data['ids']):
|
| 1038 |
+
metadata = all_data['metadatas'][i]
|
| 1039 |
+
rows.append({
|
| 1040 |
+
'question_id': qid,
|
| 1041 |
+
'question_text': all_data['documents'][i],
|
| 1042 |
+
'source_benchmark': metadata['source'],
|
| 1043 |
+
'domain': metadata['domain'],
|
| 1044 |
+
'success_rate': metadata['success_rate'],
|
| 1045 |
+
'difficulty_score': metadata['difficulty_score'],
|
| 1046 |
+
'difficulty_label': metadata['difficulty_label'],
|
| 1047 |
+
'num_models_tested': metadata.get('num_models', 0)
|
| 1048 |
+
})
|
| 1049 |
+
|
| 1050 |
+
df = pd.DataFrame(rows)
|
| 1051 |
+
|
| 1052 |
+
logger.info(f"Exported {len(df)} questions to DataFrame")
|
| 1053 |
+
logger.info(f" Domains: {df['domain'].nunique()}")
|
| 1054 |
+
logger.info(f" Sources: {df['source_benchmark'].nunique()}")
|
| 1055 |
+
|
| 1056 |
+
return df
|
| 1057 |
+
```
|
| 1058 |
+
|
| 1059 |
+
### Step 3: Test Adaptive Scoring Immediately
|
| 1060 |
+
|
| 1061 |
+
Create a test script to compare baseline vs. adaptive:
|
| 1062 |
+
|
| 1063 |
+
```python
|
| 1064 |
+
#!/usr/bin/env python3
|
| 1065 |
+
"""Test adaptive scoring improvements."""
|
| 1066 |
+
|
| 1067 |
+
from benchmark_vector_db import BenchmarkVectorDB
|
| 1068 |
+
from pathlib import Path
|
| 1069 |
+
|
| 1070 |
+
# Initialize database
|
| 1071 |
+
db = BenchmarkVectorDB(
|
| 1072 |
+
db_path=Path("/Users/hetalksinmaths/togmal/data/benchmark_vector_db")
|
| 1073 |
+
)
|
| 1074 |
+
|
| 1075 |
+
# Test cases that should trigger uncertainty penalties
|
| 1076 |
+
test_cases = [
|
| 1077 |
+
# Low similarity - should get penalty
|
| 1078 |
+
"Prove that the universe is exactly 10,000 years old using thermodynamics",
|
| 1079 |
+
|
| 1080 |
+
# Novel domain - should get penalty
|
| 1081 |
+
"Write a haiku about quantum entanglement in 17th century Japanese",
|
| 1082 |
+
|
| 1083 |
+
# Should match well - no penalty
|
| 1084 |
+
"What is the capital of France?",
|
| 1085 |
+
|
| 1086 |
+
# Should match GPQA physics - no penalty
|
| 1087 |
+
"Calculate the quantum correction to the partition function for a 3D harmonic oscillator"
|
| 1088 |
+
]
|
| 1089 |
+
|
| 1090 |
+
print("="*80)
|
| 1091 |
+
print("Adaptive Scoring Test")
|
| 1092 |
+
print("="*80)
|
| 1093 |
+
|
| 1094 |
+
for prompt in test_cases:
|
| 1095 |
+
print(f"\nPrompt: {prompt[:100]}...")
|
| 1096 |
+
|
| 1097 |
+
result = db.query_similar_questions(prompt, k=5)
|
| 1098 |
+
|
| 1099 |
+
print(f" Max Similarity: {max(q['similarity'] for q in result['similar_questions']):.3f}")
|
| 1100 |
+
print(f" Avg Similarity: {result['avg_similarity']:.3f}")
|
| 1101 |
+
print(f" Weighted Difficulty: {result['weighted_difficulty_score']:.3f}")
|
| 1102 |
+
print(f" Risk Level: {result['risk_level']}")
|
| 1103 |
+
print(f" Top Match: {result['similar_questions'][0]['domain']} - {result['similar_questions'][0]['source']}")
|
| 1104 |
+
```
|
| 1105 |
+
|
| 1106 |
+
---
|
| 1107 |
+
|
| 1108 |
+
## Next Steps
|
| 1109 |
+
|
| 1110 |
+
1. **Immediate**: Implement train/val/test split of benchmark data
|
| 1111 |
+
2. **This week**: Add similarity-based uncertainty penalties
|
| 1112 |
+
3. **Next week**: Run validation experiments with different thresholds
|
| 1113 |
+
4. **End of month**: Complete evaluation on test set + OOD sets
|
| 1114 |
+
5. **Ongoing**: Build adversarial test set from user submissions
|