ruvector-data-framework 0.3.0

Core discovery framework for RuVector dataset integrations - find hidden patterns in massive datasets using vector memory, graph structures, and dynamic min-cut algorithms
Documentation
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
//! Optimized Discovery Benchmark
//!
//! Compares baseline vs optimized engine performance using realistic
//! data from climate, finance, and research domains.
//!
//! Run: cargo run --example optimized_benchmark -p ruvector-data-framework --features parallel

use std::collections::HashMap;
use std::time::{Duration, Instant};
use chrono::{Utc, Duration as ChronoDuration};
use rand::{Rng, SeedableRng};
use rand::rngs::StdRng;

use ruvector_data_framework::ruvector_native::{
    NativeDiscoveryEngine, NativeEngineConfig, Domain, SemanticVector,
};
use ruvector_data_framework::optimized::{
    OptimizedDiscoveryEngine, OptimizedConfig, simd_cosine_similarity,
};

fn main() {
    println!("╔══════════════════════════════════════════════════════════════╗");
    println!("║         RuVector Discovery Engine Benchmark                   ║");
    println!("║    Baseline vs Optimized (SIMD + Parallel + Statistical)     ║");
    println!("╚══════════════════════════════════════════════════════════════╝\n");

    // Generate realistic test data
    let data = generate_multi_domain_data();
    println!("📊 Generated {} vectors across 3 domains\n", data.len());

    // Run benchmarks
    let baseline_results = benchmark_baseline(&data);
    let optimized_results = benchmark_optimized(&data);

    // Print comparison
    print_comparison(&baseline_results, &optimized_results);

    // Run SIMD microbenchmark
    simd_microbenchmark();

    // Run discovery quality benchmark
    discovery_quality_benchmark(&data);

    println!("\n✅ Benchmark complete");
}

/// Generate realistic multi-domain data
fn generate_multi_domain_data() -> Vec<SemanticVector> {
    let mut rng = StdRng::seed_from_u64(42);
    let mut vectors = Vec::with_capacity(500);

    // Climate data - temperature, precipitation, pressure patterns
    let climate_topics = [
        "temperature_anomaly", "precipitation_index", "drought_severity",
        "ocean_heat_content", "arctic_sea_ice", "atmospheric_co2",
        "el_nino_index", "atlantic_oscillation", "monsoon_intensity",
        "wildfire_risk", "flood_probability", "hurricane_potential",
    ];

    for (i, topic) in climate_topics.iter().enumerate() {
        for month in 0..12 {
            let embedding = generate_climate_embedding(&mut rng, i, month);
            vectors.push(SemanticVector {
                id: format!("climate_{}_{}", topic, month),
                embedding,
                domain: Domain::Climate,
                timestamp: Utc::now() - ChronoDuration::days((11 - month as i64) * 30),
                metadata: {
                    let mut m = HashMap::new();
                    m.insert("topic".to_string(), topic.to_string());
                    m.insert("month".to_string(), month.to_string());
                    m
                },
            });
        }
    }

    // Financial data - sector performance, market indicators
    let finance_sectors = [
        "energy_sector", "utilities_sector", "agriculture_commodities",
        "insurance_sector", "real_estate", "transportation",
        "consumer_staples", "materials_sector",
    ];

    for (i, sector) in finance_sectors.iter().enumerate() {
        for quarter in 0..8 {
            let embedding = generate_finance_embedding(&mut rng, i, quarter);
            vectors.push(SemanticVector {
                id: format!("finance_{}_{}", sector, quarter),
                embedding,
                domain: Domain::Finance,
                timestamp: Utc::now() - ChronoDuration::days((7 - quarter as i64) * 90),
                metadata: {
                    let mut m = HashMap::new();
                    m.insert("sector".to_string(), sector.to_string());
                    m.insert("quarter".to_string(), quarter.to_string());
                    m
                },
            });
        }
    }

    // Research data - papers on climate-finance connections
    let research_topics = [
        "climate_risk_pricing", "stranded_assets", "carbon_markets",
        "physical_risk_modeling", "transition_risk", "climate_disclosure",
        "green_bonds", "sustainable_finance",
    ];

    for (i, topic) in research_topics.iter().enumerate() {
        for year in 0..5 {
            let embedding = generate_research_embedding(&mut rng, i, year);
            vectors.push(SemanticVector {
                id: format!("research_{}_{}", topic, 2020 + year),
                embedding,
                domain: Domain::Research,
                timestamp: Utc::now() - ChronoDuration::days((4 - year as i64) * 365),
                metadata: {
                    let mut m = HashMap::new();
                    m.insert("topic".to_string(), topic.to_string());
                    m.insert("year".to_string(), (2020 + year).to_string());
                    m
                },
            });
        }
    }

    vectors
}

/// Generate climate-like embedding with topic/temporal structure
fn generate_climate_embedding(rng: &mut StdRng, topic_id: usize, time_id: usize) -> Vec<f32> {
    let dim = 128;
    let mut embedding = vec![0.0_f32; dim];

    // Base topic signature
    for i in 0..dim {
        embedding[i] = rng.gen::<f32>() * 0.1;
    }

    // Topic-specific cluster
    let topic_start = (topic_id * 10) % dim;
    for i in 0..10 {
        embedding[(topic_start + i) % dim] += 0.5 + rng.gen::<f32>() * 0.3;
    }

    // Seasonal pattern (affects climate similarity)
    let season = time_id % 4;
    let season_start = 80 + season * 10;
    for i in 0..10 {
        embedding[(season_start + i) % dim] += 0.3 + rng.gen::<f32>() * 0.2;
    }

    // Cross-domain bridge: climate topics 0-2 correlate with finance
    if topic_id < 3 {
        // Add finance-like signature
        for i in 40..50 {
            embedding[i] += 0.3;
        }
    }

    normalize_embedding(&mut embedding);
    embedding
}

/// Generate finance-like embedding
fn generate_finance_embedding(rng: &mut StdRng, sector_id: usize, time_id: usize) -> Vec<f32> {
    let dim = 128;
    let mut embedding = vec![0.0_f32; dim];

    for i in 0..dim {
        embedding[i] = rng.gen::<f32>() * 0.1;
    }

    // Sector cluster
    let sector_start = 40 + (sector_id * 8) % 40;
    for i in 0..8 {
        embedding[(sector_start + i) % dim] += 0.5 + rng.gen::<f32>() * 0.3;
    }

    // Temporal trend
    let trend_strength = time_id as f32 / 8.0;
    for i in 100..110 {
        embedding[i] += trend_strength * 0.2;
    }

    // Cross-domain: energy/utilities correlate with climate
    if sector_id < 2 {
        // Climate-like signature
        for i in 0..10 {
            embedding[i] += 0.35;
        }
    }

    normalize_embedding(&mut embedding);
    embedding
}

/// Generate research-like embedding
fn generate_research_embedding(rng: &mut StdRng, topic_id: usize, year_id: usize) -> Vec<f32> {
    let dim = 128;
    let mut embedding = vec![0.0_f32; dim];

    for i in 0..dim {
        embedding[i] = rng.gen::<f32>() * 0.1;
    }

    // Research topic cluster
    let topic_start = 10 + (topic_id * 12) % 60;
    for i in 0..12 {
        embedding[(topic_start + i) % dim] += 0.5 + rng.gen::<f32>() * 0.2;
    }

    // Bridge to both climate and finance
    // Climate connection
    for i in 0..8 {
        embedding[i] += 0.25;
    }
    // Finance connection
    for i in 45..53 {
        embedding[i] += 0.25;
    }

    // Recent papers have evolved vocabulary
    let recency = year_id as f32 / 5.0;
    for i in 115..125 {
        embedding[i] += recency * 0.3;
    }

    normalize_embedding(&mut embedding);
    embedding
}

fn normalize_embedding(embedding: &mut [f32]) {
    let norm: f32 = embedding.iter().map(|x| x * x).sum::<f32>().sqrt();
    if norm > 0.0 {
        for x in embedding.iter_mut() {
            *x /= norm;
        }
    }
}

/// Benchmark results
#[derive(Debug)]
struct BenchmarkResults {
    name: String,
    vector_add_time: Duration,
    coherence_time: Duration,
    pattern_detection_time: Duration,
    total_time: Duration,
    edges_created: usize,
    patterns_found: usize,
    cross_domain_edges: usize,
}

/// Benchmark the baseline engine
fn benchmark_baseline(data: &[SemanticVector]) -> BenchmarkResults {
    println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
    println!("📈 Running Baseline Engine Benchmark...\n");

    let config = NativeEngineConfig {
        similarity_threshold: 0.55,
        mincut_sensitivity: 0.10,
        cross_domain: true,
        ..Default::default()
    };

    let mut engine = NativeDiscoveryEngine::new(config);
    let total_start = Instant::now();

    // Add vectors
    let add_start = Instant::now();
    for vector in data {
        engine.add_vector(vector.clone());
    }
    let vector_add_time = add_start.elapsed();
    println!("   Vector insertion: {:?}", vector_add_time);

    // Compute coherence
    let coherence_start = Instant::now();
    let snapshot = engine.compute_coherence();
    let coherence_time = coherence_start.elapsed();
    println!("   Coherence computation: {:?}", coherence_time);
    println!("   Min-cut value: {:.4}", snapshot.mincut_value);

    // Pattern detection
    let pattern_start = Instant::now();
    let patterns = engine.detect_patterns();
    let pattern_detection_time = pattern_start.elapsed();
    println!("   Pattern detection: {:?}", pattern_detection_time);

    let total_time = total_start.elapsed();
    let stats = engine.stats();

    println!("\n   Results:");
    println!("   - Edges: {}", stats.total_edges);
    println!("   - Cross-domain edges: {}", stats.cross_domain_edges);
    println!("   - Patterns found: {}", patterns.len());

    BenchmarkResults {
        name: "Baseline".to_string(),
        vector_add_time,
        coherence_time,
        pattern_detection_time,
        total_time,
        edges_created: stats.total_edges,
        patterns_found: patterns.len(),
        cross_domain_edges: stats.cross_domain_edges,
    }
}

/// Benchmark the optimized engine
fn benchmark_optimized(data: &[SemanticVector]) -> BenchmarkResults {
    println!("\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
    println!("🚀 Running Optimized Engine Benchmark...\n");

    let config = OptimizedConfig {
        similarity_threshold: 0.55,
        mincut_sensitivity: 0.10,
        cross_domain: true,
        use_simd: true,
        batch_size: 128,
        significance_threshold: 0.05,
        causality_lookback: 8,
        causality_min_correlation: 0.5,
        ..Default::default()
    };

    let mut engine = OptimizedDiscoveryEngine::new(config);
    let total_start = Instant::now();

    // Batch add vectors
    let add_start = Instant::now();
    #[cfg(feature = "parallel")]
    {
        engine.add_vectors_batch(data.to_vec());
    }
    #[cfg(not(feature = "parallel"))]
    {
        for vector in data {
            engine.add_vector(vector.clone());
        }
    }
    let vector_add_time = add_start.elapsed();
    println!("   Vector insertion (batch): {:?}", vector_add_time);

    // Compute coherence with caching
    let coherence_start = Instant::now();
    let snapshot = engine.compute_coherence();
    let coherence_time = coherence_start.elapsed();
    println!("   Coherence computation: {:?}", coherence_time);
    println!("   Min-cut value: {:.4}", snapshot.mincut_value);

    // Pattern detection with significance
    let pattern_start = Instant::now();
    let patterns = engine.detect_patterns_with_significance();
    let pattern_detection_time = pattern_start.elapsed();
    println!("   Pattern detection (w/ stats): {:?}", pattern_detection_time);

    let total_time = total_start.elapsed();
    let stats = engine.stats();
    let metrics = engine.metrics();

    println!("\n   Results:");
    println!("   - Edges: {}", stats.total_edges);
    println!("   - Cross-domain edges: {}", stats.cross_domain_edges);
    println!("   - Patterns found: {}", patterns.len());
    println!("   - Significant patterns: {}", patterns.iter().filter(|p| p.is_significant).count());
    println!("   - Vector comparisons: {}", stats.total_comparisons);

    // Show significant patterns
    let significant: Vec<_> = patterns.iter().filter(|p| p.is_significant).collect();
    if !significant.is_empty() {
        println!("\n   📊 Significant Patterns (p < 0.05):");
        for pattern in significant.iter().take(5) {
            println!("{} (p={:.4}, effect={:.3})",
                pattern.pattern.description,
                pattern.p_value,
                pattern.effect_size
            );
        }
    }

    BenchmarkResults {
        name: "Optimized".to_string(),
        vector_add_time,
        coherence_time,
        pattern_detection_time,
        total_time,
        edges_created: stats.total_edges,
        patterns_found: patterns.len(),
        cross_domain_edges: stats.cross_domain_edges,
    }
}

/// Print comparison of results
fn print_comparison(baseline: &BenchmarkResults, optimized: &BenchmarkResults) {
    println!("\n╔══════════════════════════════════════════════════════════════╗");
    println!("║                    Performance Comparison                     ║");
    println!("╚══════════════════════════════════════════════════════════════╝\n");

    let speedup = |base: Duration, opt: Duration| -> f64 {
        base.as_secs_f64() / opt.as_secs_f64().max(0.0001)
    };

    println!("   ┌─────────────────────┬─────────────┬─────────────┬──────────┐");
    println!("   │ Operation           │ Baseline    │ Optimized   │ Speedup  │");
    println!("   ├─────────────────────┼─────────────┼─────────────┼──────────┤");

    println!("   │ Vector Insertion    │ {:>9.2}ms │ {:>9.2}ms │ {:>6.2}x  │",
        baseline.vector_add_time.as_secs_f64() * 1000.0,
        optimized.vector_add_time.as_secs_f64() * 1000.0,
        speedup(baseline.vector_add_time, optimized.vector_add_time)
    );

    println!("   │ Coherence Compute   │ {:>9.2}ms │ {:>9.2}ms │ {:>6.2}x  │",
        baseline.coherence_time.as_secs_f64() * 1000.0,
        optimized.coherence_time.as_secs_f64() * 1000.0,
        speedup(baseline.coherence_time, optimized.coherence_time)
    );

    println!("   │ Pattern Detection   │ {:>9.2}ms │ {:>9.2}ms │ {:>6.2}x  │",
        baseline.pattern_detection_time.as_secs_f64() * 1000.0,
        optimized.pattern_detection_time.as_secs_f64() * 1000.0,
        speedup(baseline.pattern_detection_time, optimized.pattern_detection_time)
    );

    println!("   ├─────────────────────┼─────────────┼─────────────┼──────────┤");
    println!("   │ TOTAL               │ {:>9.2}ms │ {:>9.2}ms │ {:>6.2}x  │",
        baseline.total_time.as_secs_f64() * 1000.0,
        optimized.total_time.as_secs_f64() * 1000.0,
        speedup(baseline.total_time, optimized.total_time)
    );
    println!("   └─────────────────────┴─────────────┴─────────────┴──────────┘");

    println!("\n   Quality Metrics:");
    println!("   - Edges created: {}{} (same algorithm)",
        baseline.edges_created, optimized.edges_created);
    println!("   - Cross-domain: {}{}",
        baseline.cross_domain_edges, optimized.cross_domain_edges);
    println!("   - Patterns: {}{} (+ statistical filtering)",
        baseline.patterns_found, optimized.patterns_found);
}

/// SIMD microbenchmark
fn simd_microbenchmark() {
    println!("\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
    println!("⚡ SIMD Vector Operations Microbenchmark\n");

    let mut rng = StdRng::seed_from_u64(123);
    let dim = 128;
    let iterations = 100_000;

    // Generate test vectors
    let vectors: Vec<Vec<f32>> = (0..100)
        .map(|_| {
            let mut v: Vec<f32> = (0..dim).map(|_| rng.gen()).collect();
            let norm: f32 = v.iter().map(|x| x * x).sum::<f32>().sqrt();
            for x in &mut v {
                *x /= norm;
            }
            v
        })
        .collect();

    // Benchmark SIMD cosine
    let start = Instant::now();
    let mut sum = 0.0_f32;
    for i in 0..iterations {
        let a = &vectors[i % 100];
        let b = &vectors[(i + 1) % 100];
        sum += simd_cosine_similarity(a, b);
    }
    let simd_time = start.elapsed();

    // Benchmark standard cosine
    let start = Instant::now();
    let mut sum2 = 0.0_f32;
    for i in 0..iterations {
        let a = &vectors[i % 100];
        let b = &vectors[(i + 1) % 100];
        sum2 += standard_cosine(a, b);
    }
    let std_time = start.elapsed();

    println!("   {} cosine similarity operations on {}-dim vectors:\n", iterations, dim);
    println!("   SIMD version:     {:>8.2}ms ({:.2} M ops/sec)",
        simd_time.as_secs_f64() * 1000.0,
        iterations as f64 / simd_time.as_secs_f64() / 1_000_000.0
    );
    println!("   Standard version: {:>8.2}ms ({:.2} M ops/sec)",
        std_time.as_secs_f64() * 1000.0,
        iterations as f64 / std_time.as_secs_f64() / 1_000_000.0
    );
    println!("   Speedup: {:.2}x", std_time.as_secs_f64() / simd_time.as_secs_f64());
    println!("   (checksum: {:.4}, {:.4})", sum, sum2);
}

fn standard_cosine(a: &[f32], b: &[f32]) -> f32 {
    let dot: f32 = a.iter().zip(b.iter()).map(|(x, y)| x * y).sum();
    let norm_a: f32 = a.iter().map(|x| x * x).sum::<f32>().sqrt();
    let norm_b: f32 = b.iter().map(|x| x * x).sum::<f32>().sqrt();
    dot / (norm_a * norm_b)
}

/// Discovery quality benchmark
fn discovery_quality_benchmark(data: &[SemanticVector]) {
    println!("\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
    println!("🔍 Discovery Quality Analysis\n");

    let config = OptimizedConfig {
        similarity_threshold: 0.55,
        cross_domain: true,
        significance_threshold: 0.05,
        causality_lookback: 8,
        causality_min_correlation: 0.5,
        ..Default::default()
    };

    let mut engine = OptimizedDiscoveryEngine::new(config);

    // Add data in temporal batches to detect patterns
    let batch_size = data.len() / 4;
    let mut all_patterns = Vec::new();

    for (batch_idx, batch) in data.chunks(batch_size).enumerate() {
        #[cfg(feature = "parallel")]
        {
            engine.add_vectors_batch(batch.to_vec());
        }
        #[cfg(not(feature = "parallel"))]
        {
            for v in batch {
                engine.add_vector(v.clone());
            }
        }

        let patterns = engine.detect_patterns_with_significance();
        all_patterns.extend(patterns);

        println!("   Batch {} ({} vectors): {} patterns detected",
            batch_idx + 1, batch.len(), all_patterns.len());
    }

    // Analyze cross-domain discoveries
    let stats = engine.stats();

    println!("\n   Cross-Domain Analysis:");
    println!("   ─────────────────────────");
    println!("   Climate nodes:  {}", stats.domain_counts.get(&Domain::Climate).unwrap_or(&0));
    println!("   Finance nodes:  {}", stats.domain_counts.get(&Domain::Finance).unwrap_or(&0));
    println!("   Research nodes: {}", stats.domain_counts.get(&Domain::Research).unwrap_or(&0));
    println!("   Cross-domain edges: {} ({:.1}% of total)",
        stats.cross_domain_edges,
        100.0 * stats.cross_domain_edges as f64 / stats.total_edges.max(1) as f64
    );

    // Domain coherence
    println!("\n   Domain Coherence Scores:");
    if let Some(coh) = engine.domain_coherence(Domain::Climate) {
        println!("   Climate:  {:.3}", coh);
    }
    if let Some(coh) = engine.domain_coherence(Domain::Finance) {
        println!("   Finance:  {:.3}", coh);
    }
    if let Some(coh) = engine.domain_coherence(Domain::Research) {
        println!("   Research: {:.3}", coh);
    }

    // Show discovered cross-domain bridges
    let bridges: Vec<_> = all_patterns.iter()
        .filter(|p| !p.pattern.cross_domain_links.is_empty())
        .collect();

    if !bridges.is_empty() {
        println!("\n   🌉 Cross-Domain Bridges Found: {}", bridges.len());
        for bridge in bridges.iter().take(3) {
            for link in &bridge.pattern.cross_domain_links {
                println!("      {:?}{:?} (strength: {:.3}, type: {})",
                    link.source_domain,
                    link.target_domain,
                    link.link_strength,
                    link.link_type
                );
            }
        }
    }

    // Causality patterns
    let causality: Vec<_> = all_patterns.iter()
        .filter(|p| matches!(p.pattern.pattern_type, ruvector_data_framework::ruvector_native::PatternType::Cascade))
        .collect();

    if !causality.is_empty() {
        println!("\n   🔗 Temporal Causality Patterns: {}", causality.len());
        for pattern in causality.iter().take(3) {
            println!("      {} (p={:.4})", pattern.pattern.description, pattern.p_value);
        }
    }
}