oximedia-recommend 0.1.8

Content recommendation engine for media libraries
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
//! Offline evaluation metrics for recommendation quality.
//!
//! Provides standard information retrieval metrics for evaluating the quality
//! of recommendation lists against ground truth relevant items:
//!
//! - **Precision@K**: fraction of recommended items that are relevant
//! - **Recall@K**: fraction of relevant items that are recommended
//! - **NDCG@K**: Normalised Discounted Cumulative Gain
//! - **MAP**: Mean Average Precision
//! - **Hit Rate@K**: whether at least one relevant item appears in top-K
//! - **MRR**: Mean Reciprocal Rank

use std::collections::HashSet;

/// A single evaluation instance: a list of recommended item IDs and
/// the set of ground-truth relevant item IDs for one user/query.
#[derive(Debug, Clone)]
pub struct EvalInstance {
    /// Ordered list of recommended item IDs (most relevant first).
    pub recommended: Vec<String>,
    /// Set of ground-truth relevant item IDs.
    pub relevant: HashSet<String>,
}

impl EvalInstance {
    /// Create a new evaluation instance.
    pub fn new(recommended: Vec<String>, relevant: HashSet<String>) -> Self {
        Self {
            recommended,
            relevant,
        }
    }

    /// Convenience: create from slices of &str.
    pub fn from_strs(recommended: &[&str], relevant: &[&str]) -> Self {
        Self {
            recommended: recommended.iter().map(|s| s.to_string()).collect(),
            relevant: relevant.iter().map(|s| s.to_string()).collect(),
        }
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Per-instance metrics
// ─────────────────────────────────────────────────────────────────────────────

/// Compute Precision@K for a single instance.
///
/// Precision@K = |{recommended ∩ relevant}| / K
#[must_use]
pub fn precision_at_k(instance: &EvalInstance, k: usize) -> f64 {
    if k == 0 {
        return 0.0;
    }
    let top_k = instance.recommended.iter().take(k);
    let hits = top_k
        .filter(|item| instance.relevant.contains(*item))
        .count();
    hits as f64 / k as f64
}

/// Compute Recall@K for a single instance.
///
/// Recall@K = |{recommended ∩ relevant}| / |relevant|
#[must_use]
pub fn recall_at_k(instance: &EvalInstance, k: usize) -> f64 {
    if instance.relevant.is_empty() {
        return 0.0;
    }
    let top_k = instance.recommended.iter().take(k);
    let hits = top_k
        .filter(|item| instance.relevant.contains(*item))
        .count();
    hits as f64 / instance.relevant.len() as f64
}

/// Compute NDCG@K (Normalised Discounted Cumulative Gain) for a single instance.
///
/// Uses binary relevance: rel(i) = 1 if item is relevant, 0 otherwise.
///
/// DCG@K = Σ_{i=1}^{K} rel(i) / log2(i + 1)
/// IDCG@K = DCG of the ideal ranking (all relevant items first)
/// NDCG@K = DCG@K / IDCG@K
#[must_use]
pub fn ndcg_at_k(instance: &EvalInstance, k: usize) -> f64 {
    if instance.relevant.is_empty() || k == 0 {
        return 0.0;
    }

    // DCG
    let mut dcg = 0.0;
    for (i, item) in instance.recommended.iter().take(k).enumerate() {
        if instance.relevant.contains(item) {
            dcg += 1.0 / ((i as f64) + 2.0).log2();
        }
    }

    // IDCG: best possible DCG with min(|relevant|, k) relevant items at top
    let ideal_count = instance.relevant.len().min(k);
    let mut idcg = 0.0;
    for i in 0..ideal_count {
        idcg += 1.0 / ((i as f64) + 2.0).log2();
    }

    if idcg < f64::EPSILON {
        return 0.0;
    }

    dcg / idcg
}

/// Compute Average Precision for a single instance.
///
/// AP = (1/|relevant|) * Σ_{k=1}^{n} (Precision@k * rel(k))
#[must_use]
pub fn average_precision(instance: &EvalInstance) -> f64 {
    if instance.relevant.is_empty() {
        return 0.0;
    }

    let mut hits = 0;
    let mut sum_precision = 0.0;

    for (i, item) in instance.recommended.iter().enumerate() {
        if instance.relevant.contains(item) {
            hits += 1;
            sum_precision += hits as f64 / (i + 1) as f64;
        }
    }

    sum_precision / instance.relevant.len() as f64
}

/// Compute Hit Rate@K for a single instance (1.0 if any relevant item in top-K, else 0.0).
#[must_use]
pub fn hit_rate_at_k(instance: &EvalInstance, k: usize) -> f64 {
    let has_hit = instance
        .recommended
        .iter()
        .take(k)
        .any(|item| instance.relevant.contains(item));
    if has_hit {
        1.0
    } else {
        0.0
    }
}

/// Compute Reciprocal Rank for a single instance.
///
/// RR = 1 / rank of first relevant item (0.0 if none found).
#[must_use]
pub fn reciprocal_rank(instance: &EvalInstance) -> f64 {
    for (i, item) in instance.recommended.iter().enumerate() {
        if instance.relevant.contains(item) {
            return 1.0 / (i + 1) as f64;
        }
    }
    0.0
}

// ─────────────────────────────────────────────────────────────────────────────
// Aggregated (mean) metrics over multiple instances
// ─────────────────────────────────────────────────────────────────────────────

/// Compute Mean Average Precision (MAP) over multiple instances.
#[must_use]
pub fn mean_average_precision(instances: &[EvalInstance]) -> f64 {
    if instances.is_empty() {
        return 0.0;
    }
    let total: f64 = instances.iter().map(average_precision).sum();
    total / instances.len() as f64
}

/// Compute mean Precision@K over multiple instances.
#[must_use]
pub fn mean_precision_at_k(instances: &[EvalInstance], k: usize) -> f64 {
    if instances.is_empty() {
        return 0.0;
    }
    let total: f64 = instances.iter().map(|inst| precision_at_k(inst, k)).sum();
    total / instances.len() as f64
}

/// Compute mean Recall@K over multiple instances.
#[must_use]
pub fn mean_recall_at_k(instances: &[EvalInstance], k: usize) -> f64 {
    if instances.is_empty() {
        return 0.0;
    }
    let total: f64 = instances.iter().map(|inst| recall_at_k(inst, k)).sum();
    total / instances.len() as f64
}

/// Compute mean NDCG@K over multiple instances.
#[must_use]
pub fn mean_ndcg_at_k(instances: &[EvalInstance], k: usize) -> f64 {
    if instances.is_empty() {
        return 0.0;
    }
    let total: f64 = instances.iter().map(|inst| ndcg_at_k(inst, k)).sum();
    total / instances.len() as f64
}

/// Compute Mean Reciprocal Rank (MRR) over multiple instances.
#[must_use]
pub fn mean_reciprocal_rank(instances: &[EvalInstance]) -> f64 {
    if instances.is_empty() {
        return 0.0;
    }
    let total: f64 = instances.iter().map(reciprocal_rank).sum();
    total / instances.len() as f64
}

/// Compute mean Hit Rate@K over multiple instances.
#[must_use]
pub fn mean_hit_rate_at_k(instances: &[EvalInstance], k: usize) -> f64 {
    if instances.is_empty() {
        return 0.0;
    }
    let total: f64 = instances.iter().map(|inst| hit_rate_at_k(inst, k)).sum();
    total / instances.len() as f64
}

// ─────────────────────────────────────────────────────────────────────────────
// Evaluation report
// ─────────────────────────────────────────────────────────────────────────────

/// Comprehensive evaluation report.
#[derive(Debug, Clone)]
pub struct EvaluationReport {
    /// Precision@K for various K values.
    pub precision: Vec<(usize, f64)>,
    /// Recall@K for various K values.
    pub recall: Vec<(usize, f64)>,
    /// NDCG@K for various K values.
    pub ndcg: Vec<(usize, f64)>,
    /// Mean Average Precision.
    pub map: f64,
    /// Mean Reciprocal Rank.
    pub mrr: f64,
    /// Hit Rate@K for various K values.
    pub hit_rate: Vec<(usize, f64)>,
    /// Number of evaluation instances.
    pub num_instances: usize,
}

/// Generate a full evaluation report over multiple instances.
///
/// Computes all metrics at k = 1, 3, 5, 10, 20.
#[must_use]
pub fn generate_report(instances: &[EvalInstance]) -> EvaluationReport {
    let k_values = [1, 3, 5, 10, 20];

    let precision: Vec<(usize, f64)> = k_values
        .iter()
        .map(|&k| (k, mean_precision_at_k(instances, k)))
        .collect();

    let recall: Vec<(usize, f64)> = k_values
        .iter()
        .map(|&k| (k, mean_recall_at_k(instances, k)))
        .collect();

    let ndcg: Vec<(usize, f64)> = k_values
        .iter()
        .map(|&k| (k, mean_ndcg_at_k(instances, k)))
        .collect();

    let hit_rate: Vec<(usize, f64)> = k_values
        .iter()
        .map(|&k| (k, mean_hit_rate_at_k(instances, k)))
        .collect();

    EvaluationReport {
        precision,
        recall,
        ndcg,
        map: mean_average_precision(instances),
        mrr: mean_reciprocal_rank(instances),
        hit_rate,
        num_instances: instances.len(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn make_instance(rec: &[&str], rel: &[&str]) -> EvalInstance {
        EvalInstance::from_strs(rec, rel)
    }

    // ---- Precision@K ----

    #[test]
    fn test_precision_at_k_perfect() {
        let inst = make_instance(&["a", "b", "c"], &["a", "b", "c"]);
        assert!((precision_at_k(&inst, 3) - 1.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_precision_at_k_half() {
        let inst = make_instance(&["a", "x", "b", "y"], &["a", "b"]);
        assert!((precision_at_k(&inst, 4) - 0.5).abs() < f64::EPSILON);
    }

    #[test]
    fn test_precision_at_k_zero() {
        let inst = make_instance(&["x", "y", "z"], &["a", "b"]);
        assert!((precision_at_k(&inst, 3)).abs() < f64::EPSILON);
    }

    #[test]
    fn test_precision_at_k_zero_k() {
        let inst = make_instance(&["a"], &["a"]);
        assert!((precision_at_k(&inst, 0)).abs() < f64::EPSILON);
    }

    // ---- Recall@K ----

    #[test]
    fn test_recall_at_k_perfect() {
        let inst = make_instance(&["a", "b"], &["a", "b"]);
        assert!((recall_at_k(&inst, 2) - 1.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_recall_at_k_partial() {
        let inst = make_instance(&["a", "x"], &["a", "b"]);
        assert!((recall_at_k(&inst, 2) - 0.5).abs() < f64::EPSILON);
    }

    #[test]
    fn test_recall_at_k_empty_relevant() {
        let inst = make_instance(&["a"], &[]);
        assert!((recall_at_k(&inst, 1)).abs() < f64::EPSILON);
    }

    // ---- NDCG@K ----

    #[test]
    fn test_ndcg_perfect_ranking() {
        let inst = make_instance(&["a", "b", "c"], &["a", "b", "c"]);
        assert!((ndcg_at_k(&inst, 3) - 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_ndcg_worst_ranking() {
        // All relevant items are outside top-K
        let inst = make_instance(&["x", "y", "z", "a", "b"], &["a", "b"]);
        assert!((ndcg_at_k(&inst, 3)).abs() < f64::EPSILON);
    }

    #[test]
    fn test_ndcg_partial_ranking() {
        // Relevant item at position 2 (0-indexed)
        let inst = make_instance(&["x", "y", "a"], &["a"]);
        let ndcg = ndcg_at_k(&inst, 3);
        // DCG = 1/log2(4) = 0.5; IDCG = 1/log2(2) = 1.0 → NDCG = 0.5
        assert!((ndcg - 0.5).abs() < 1e-10);
    }

    #[test]
    fn test_ndcg_empty() {
        let inst = make_instance(&[], &["a"]);
        assert!((ndcg_at_k(&inst, 5)).abs() < f64::EPSILON);
    }

    // ---- Average Precision ----

    #[test]
    fn test_average_precision_perfect() {
        let inst = make_instance(&["a", "b", "c"], &["a", "b", "c"]);
        // AP = (1/3)(1/1 + 2/2 + 3/3) = 1.0
        assert!((average_precision(&inst) - 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_average_precision_one_relevant() {
        let inst = make_instance(&["x", "y", "a"], &["a"]);
        // AP = (1/1)(1/3) = 1/3
        assert!((average_precision(&inst) - 1.0 / 3.0).abs() < 1e-10);
    }

    #[test]
    fn test_average_precision_empty_relevant() {
        let inst = make_instance(&["a", "b"], &[]);
        assert!((average_precision(&inst)).abs() < f64::EPSILON);
    }

    // ---- Hit Rate@K ----

    #[test]
    fn test_hit_rate_hit() {
        let inst = make_instance(&["x", "a", "y"], &["a"]);
        assert!((hit_rate_at_k(&inst, 3) - 1.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_hit_rate_miss() {
        let inst = make_instance(&["x", "y", "z"], &["a"]);
        assert!((hit_rate_at_k(&inst, 3)).abs() < f64::EPSILON);
    }

    // ---- Reciprocal Rank ----

    #[test]
    fn test_reciprocal_rank_first() {
        let inst = make_instance(&["a", "b", "c"], &["a"]);
        assert!((reciprocal_rank(&inst) - 1.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_reciprocal_rank_third() {
        let inst = make_instance(&["x", "y", "a"], &["a"]);
        assert!((reciprocal_rank(&inst) - 1.0 / 3.0).abs() < 1e-10);
    }

    #[test]
    fn test_reciprocal_rank_none() {
        let inst = make_instance(&["x", "y", "z"], &["a"]);
        assert!((reciprocal_rank(&inst)).abs() < f64::EPSILON);
    }

    // ---- Mean metrics ----

    #[test]
    fn test_mean_average_precision() {
        let instances = vec![
            make_instance(&["a", "b", "c"], &["a", "b", "c"]),
            make_instance(&["x", "y", "a"], &["a"]),
        ];
        let map = mean_average_precision(&instances);
        // User1 AP = 1.0, User2 AP = 1/3 → MAP = 2/3
        assert!((map - 2.0 / 3.0).abs() < 1e-10);
    }

    #[test]
    fn test_mean_precision_at_k() {
        let instances = vec![
            make_instance(&["a", "x"], &["a"]),
            make_instance(&["a", "b"], &["a", "b"]),
        ];
        // P@2 user1 = 0.5, P@2 user2 = 1.0 → mean = 0.75
        assert!((mean_precision_at_k(&instances, 2) - 0.75).abs() < 1e-10);
    }

    #[test]
    fn test_mean_reciprocal_rank() {
        let instances = vec![
            make_instance(&["a", "b"], &["a"]), // RR = 1
            make_instance(&["x", "a"], &["a"]), // RR = 0.5
        ];
        assert!((mean_reciprocal_rank(&instances) - 0.75).abs() < 1e-10);
    }

    #[test]
    fn test_mean_metrics_empty() {
        let instances: Vec<EvalInstance> = vec![];
        assert!((mean_average_precision(&instances)).abs() < f64::EPSILON);
        assert!((mean_precision_at_k(&instances, 5)).abs() < f64::EPSILON);
        assert!((mean_recall_at_k(&instances, 5)).abs() < f64::EPSILON);
        assert!((mean_ndcg_at_k(&instances, 5)).abs() < f64::EPSILON);
        assert!((mean_reciprocal_rank(&instances)).abs() < f64::EPSILON);
        assert!((mean_hit_rate_at_k(&instances, 5)).abs() < f64::EPSILON);
    }

    // ---- Report ----

    #[test]
    fn test_generate_report() {
        let instances = vec![
            make_instance(&["a", "b", "c", "d", "e"], &["a", "c"]),
            make_instance(&["x", "y", "a", "b", "c"], &["a", "b"]),
        ];
        let report = generate_report(&instances);
        assert_eq!(report.num_instances, 2);
        assert!(!report.precision.is_empty());
        assert!(!report.recall.is_empty());
        assert!(!report.ndcg.is_empty());
        assert!(!report.hit_rate.is_empty());
        assert!(report.map >= 0.0);
        assert!(report.mrr >= 0.0);
    }

    #[test]
    fn test_generate_report_empty() {
        let report = generate_report(&[]);
        assert_eq!(report.num_instances, 0);
        assert!((report.map).abs() < f64::EPSILON);
    }

    #[test]
    fn test_eval_instance_new() {
        let rec = vec!["a".to_string(), "b".to_string()];
        let mut rel = HashSet::new();
        rel.insert("a".to_string());
        let inst = EvalInstance::new(rec, rel);
        assert_eq!(inst.recommended.len(), 2);
        assert_eq!(inst.relevant.len(), 1);
    }
}