File size: 10,950 Bytes
9b1c753 |
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 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 |
# Risk Discovery Methods - Comparison Implementation
## Overview
Implemented 3 additional risk discovery methods beyond the original K-Means clustering to enable comprehensive comparison of different approaches.
---
## π― Implemented Methods (4 Total)
### 1. β
K-Means Clustering (Original)
**File**: `risk_discovery.py`
**Status**: Already existed
**Characteristics**:
- Fast and scalable
- Creates spherical clusters
- Clear, non-overlapping boundaries
- Requires predefined number of clusters
**Best For**:
- Large datasets (10K+ clauses)
- When you need consistent, fast results
- Clear risk categorization needed
---
### 2. β
LDA Topic Modeling (NEW)
**File**: `risk_discovery_alternatives.py`
**Class**: `TopicModelingRiskDiscovery`
**Characteristics**:
- Probabilistic approach
- Clauses can belong to multiple topics
- Discovers interpretable topic-word distributions
- Slower than K-Means
**Advantages**:
- β
Natural handling of overlapping risks
- β
Provides probability distribution per clause
- β
Topic words are highly interpretable
- β
Works well with legal text (multi-thematic)
**Disadvantages**:
- β Requires tuning (alpha, beta parameters)
- β Slower training (20 iterations)
- β Less clear boundaries than K-Means
**Parameters**:
- `n_topics`: Number of topics to discover (default: 7)
- `doc_topic_prior`: Alpha - document-topic density (default: 0.1)
- `topic_word_prior`: Beta - topic-word density (default: 0.01)
**Quality Metrics**:
- Perplexity (lower is better)
- Log-likelihood (higher is better)
- Topic diversity (entropy)
**Example Output**:
```python
{
'topic_id': 0,
'topic_name': 'Topic_LIABILITY',
'top_words': ['liability', 'damages', 'indemnify', 'loss', 'liable'],
'word_weights': [0.045, 0.038, 0.031, ...],
'clause_count': 2847,
'proportion': 0.284
}
```
---
### 3. β
Hierarchical Clustering (NEW)
**File**: `risk_discovery_alternatives.py`
**Class**: `HierarchicalRiskDiscovery`
**Characteristics**:
- Builds hierarchy of clusters (dendrogram)
- Can be cut at different levels
- Agglomerative (bottom-up) approach
- Deterministic results
**Advantages**:
- β
Discovers nested risk structure
- β
No need to specify clusters upfront
- β
Deterministic (same results every run)
- β
Can explore different granularities
**Disadvantages**:
- β Slower for large datasets (O(nΒ²) complexity)
- β Memory intensive
- β Cannot scale to 100K+ clauses easily
**Parameters**:
- `n_clusters`: Number of clusters to form (default: 7)
- `linkage`: Linkage criterion - 'ward', 'average', 'complete', 'single'
**Quality Metrics**:
- Silhouette score (-1 to 1, higher is better)
- Average cluster size
- Cluster balance
**Example Output**:
```python
{
'cluster_id': 2,
'cluster_name': 'RISK_INDEMNITY',
'top_terms': ['indemnify', 'hold', 'harmless', 'defend', 'claims'],
'term_scores': [0.234, 0.198, 0.176, ...],
'clause_count': 1543,
'proportion': 0.154
}
```
---
### 4. β
DBSCAN (Density-Based) (NEW)
**File**: `risk_discovery_alternatives.py`
**Class**: `DensityBasedRiskDiscovery`
**Characteristics**:
- Density-based clustering
- Discovers clusters of arbitrary shapes
- Identifies outliers/noise automatically
- No need to specify number of clusters
**Advantages**:
- β
Finds clusters of any shape
- β
Identifies outliers (rare risks)
- β
Robust to noise
- β
No cluster number required
**Disadvantages**:
- β Sensitive to eps parameter
- β Struggles with varying density
- β May produce many small clusters
- β Requires careful tuning
**Parameters**:
- `eps`: Maximum distance between samples (default: 0.5)
- `min_samples`: Minimum samples in neighborhood (default: 5)
- `auto_tune`: Automatically tune eps (default: True)
**Quality Metrics**:
- Number of clusters found
- Outlier ratio (% of noise points)
- Average cluster size
**Special Feature**: **Outlier Detection**
- Identifies clauses that don't fit any pattern
- Useful for finding rare/unique risks
- Can flag unusual contract clauses
**Example Output**:
```python
{
'cluster_id': 3,
'cluster_name': 'RISK_PAYMENT_C3',
'top_terms': ['payment', 'fee', 'invoice', 'dollar', 'cost'],
'clause_count': 892,
'is_core_cluster': True
}
# Plus outliers
{
'n_outliers': 234,
'outlier_ratio': 0.023,
'outlier_clauses': ['Unusual clause text...']
}
```
---
## π Comparison Features
### Automated Comparison Script
**File**: `compare_risk_discovery.py`
**What It Does**:
1. Loads CUAD dataset (or generates sample data)
2. Runs all 4 methods on the same clauses
3. Measures execution time for each
4. Compares quality metrics
5. Analyzes pattern diversity
6. Generates comprehensive report
**Usage**:
```bash
python compare_risk_discovery.py
```
**Output Files**:
- `risk_discovery_comparison_report.txt` - Human-readable report
- `risk_discovery_comparison_results.json` - Detailed results with metrics
**Report Includes**:
- Summary table with timing and quality
- Detailed analysis per method
- Pattern diversity metrics
- Top discovered patterns
- Recommendations for each method
---
## π¬ Comparison Metrics
### Performance Metrics
1. **Execution Time**: Time to discover patterns
2. **Clauses/Second**: Processing throughput
3. **Scalability**: How method handles large datasets
### Quality Metrics
1. **Silhouette Score** (Hierarchical):
- Range: -1 to 1
- Higher is better
- Measures cluster cohesion
2. **Perplexity** (LDA):
- Lower is better
- Measures model fit to data
3. **Outlier Ratio** (DBSCAN):
- % of clauses marked as outliers
- Indicates rare pattern detection
### Diversity Metrics
1. **Pattern Balance**: How evenly clauses distributed
2. **Pattern Size Variance**: Consistency of cluster sizes
3. **Topic Diversity** (LDA): Entropy of word distributions
---
## π― Method Selection Guide
### Choose **K-Means** when:
- β
You have large datasets (10K+ clauses)
- β
You need fast, consistent results
- β
You want clear, non-overlapping categories
- β
You can specify number of risks upfront
### Choose **LDA** when:
- β
Clauses may have multiple risk types
- β
You need probability distributions
- β
Interpretability of topics is crucial
- β
You can afford slower processing
### Choose **Hierarchical** when:
- β
You want to explore risk hierarchies
- β
Dataset is moderate size (<10K clauses)
- β
You need deterministic results
- β
You want to analyze at multiple granularities
### Choose **DBSCAN** when:
- β
You need to identify outliers/rare risks
- β
Cluster shapes are not spherical
- β
You don't know number of clusters
- β
You want automatic noise handling
---
## π Performance Comparison (Estimated)
Based on 5,000 clauses:
| Method | Time | Scalability | Quality | Interpretability |
|---------------|---------|-------------|---------|------------------|
| K-Means | 5-10s | Excellent | Good | Good |
| LDA | 30-60s | Good | Good | Excellent |
| Hierarchical | 15-30s | Moderate | Good | Good |
| DBSCAN | 10-20s | Good | Variable| Good |
---
## π§ Integration with Training Pipeline
### Option 1: Use Single Method
```python
# In trainer.py
from risk_discovery import UnsupervisedRiskDiscovery # Original
# Or choose alternative
from risk_discovery_alternatives import TopicModelingRiskDiscovery
risk_discovery = TopicModelingRiskDiscovery(n_topics=7)
```
### Option 2: Compare Before Training
```bash
# Run comparison first
python compare_risk_discovery.py
# Review results
cat risk_discovery_comparison_report.txt
# Choose best method for your data
python train.py # Will use chosen method
```
### Option 3: Ensemble Approach (Advanced)
```python
# Use multiple methods and combine
from risk_discovery import UnsupervisedRiskDiscovery
from risk_discovery_alternatives import TopicModelingRiskDiscovery
kmeans = UnsupervisedRiskDiscovery(n_clusters=7)
lda = TopicModelingRiskDiscovery(n_topics=7)
kmeans_labels = kmeans.discover_risk_patterns(clauses)
lda_labels = lda.discover_risk_patterns(clauses)
# Combine using voting or averaging
ensemble_labels = combine_predictions(kmeans_labels, lda_labels)
```
---
## π Code Statistics
### New Implementation
- **New File**: `risk_discovery_alternatives.py` (570 lines)
- **New File**: `compare_risk_discovery.py` (450 lines)
- **Total New Code**: ~1,020 lines
- **New Classes**: 3 (TopicModelingRiskDiscovery, HierarchicalRiskDiscovery, DensityBasedRiskDiscovery)
- **New Methods**: 20+
### Dependencies Required
All methods use existing dependencies:
- `sklearn.decomposition.LatentDirichletAllocation` (LDA)
- `sklearn.cluster.AgglomerativeClustering` (Hierarchical)
- `sklearn.cluster.DBSCAN` (Density-based)
- Already in `requirements.txt`
---
## π§ͺ Testing the Comparison
### Quick Test (Sample Data)
```bash
# Uses synthetic sample clauses
python compare_risk_discovery.py
```
### Full Test (CUAD Dataset)
```bash
# Requires CUAD dataset at dataset/CUAD_v1/CUAD_v1.json
python compare_risk_discovery.py
```
### Expected Output
```
π¬ RISK DISCOVERY METHOD COMPARISON
Loading CUAD dataset...
β
Loaded 5000 clauses for comparison
METHOD 1: K-Means Clustering
Execution time: 8.34 seconds
β
7 clusters found
METHOD 2: LDA Topic Modeling
Execution time: 45.67 seconds
Perplexity: 1234.56
β
7 topics found
METHOD 3: Hierarchical Clustering
Execution time: 23.12 seconds
Silhouette Score: 0.234
β
7 clusters found
METHOD 4: DBSCAN
Execution time: 12.45 seconds
β
9 clusters found, 123 outliers
π COMPARISON SUMMARY
[Detailed comparison table...]
πΎ Report saved to: risk_discovery_comparison_report.txt
β
Detailed results saved to: risk_discovery_comparison_results.json
```
---
## π Summary
### What Was Added
1. β
**3 new risk discovery methods** (LDA, Hierarchical, DBSCAN)
2. β
**Automated comparison framework**
3. β
**Comprehensive evaluation metrics**
4. β
**Method selection guidelines**
5. β
**Integration ready for training pipeline**
### What You Can Do Now
1. **Compare Methods**: Run `compare_risk_discovery.py`
2. **Choose Best Method**: Based on your data characteristics
3. **Train with Alternative**: Swap in any method for training
4. **Ensemble Learning**: Combine multiple methods (advanced)
### Next Steps
1. Run comparison on your CUAD dataset
2. Review the generated report
3. Choose the method that best fits your needs:
- **Fast & Scalable** β K-Means
- **Overlapping Risks** β LDA
- **Risk Hierarchy** β Hierarchical
- **Outlier Detection** β DBSCAN
4. Update `trainer.py` to use chosen method
5. Train Legal-BERT with optimized risk discovery
---
**Status**: β
**COMPLETE AND READY FOR TESTING**
**Files**: `risk_discovery_alternatives.py`, `compare_risk_discovery.py`
**Lines Added**: ~1,020 lines of production code
|