fynch 0.1.3

Differentiable sorting and ranking: PAVA, Fenchel-Young losses, and O(n log n) FastSoftSort
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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
//! Fenchel-Young losses: a unified framework for prediction functions and losses.
//!
//! # The Framework
//!
//! Given a regularizer Ω defined over a domain (like the probability simplex),
//! the Fenchel-Young loss is:
//!
//! ```text
//! L_Ω(θ; y) = Ω*(θ) - ⟨θ, y⟩ + Ω(y)
//! ```
//!
//! where Ω* is the Fenchel conjugate (convex conjugate) of Ω:
//!
//! ```text
//! Ω*(θ) = max_p { ⟨θ, p⟩ - Ω(p) }
//! ```
//!
//! The prediction function (argmax of the above) is:
//!
//! ```text
//! ŷ_Ω(θ) = ∇Ω*(θ) = argmax_p { ⟨θ, p⟩ - Ω(p) }
//! ```
//!
//! # Why This Matters
//!
//! Different regularizers give different behaviors:
//!
//! | Regularizer | Prediction | Loss | Sparsity |
//! |-------------|------------|------|----------|
//! | Shannon negentropy | softmax | cross-entropy | Dense |
//! | Squared L2 (1/2‖·‖²) | sparsemax | sparsemax loss | Sparse |
//! | Tsallis α-entropy | α-entmax | entmax loss | Tunable |
//!
//! # Mathematical Foundation
//!
//! The key insight is **Moreau's decomposition**: for a proper closed convex Ω,
//!
//! ```text
//! θ = ∇Ω(ŷ_Ω(θ)) + ∇Ω*(θ) × (for Ω = 1/2‖·‖²)
//! ```
//!
//! This means the prediction ŷ_Ω and the loss L_Ω are intimately connected.
//!
//! # References
//!
//! - Blondel, Martins, Niculae (2020). "Learning with Fenchel-Young Losses" (JMLR 21)
//! - Martins, Treviso, et al. (2022). "Sparse Continuous Distributions and FY Losses"
//! - Blondel, Teboul, et al. (2020). "Fast Differentiable Sorting and Ranking"

// Note: This module doesn't use crate-level Error/Result,
// preferring to return NaN/empty for invalid inputs (matching softmax conventions)

// ============================================================================
// Regularizers (Ω)
// ============================================================================

/// Trait for regularization functions Ω.
///
/// A regularizer Ω defines:
/// 1. The regularization value Ω(p)
/// 2. The prediction map ŷ_Ω(θ) = argmax_p { ⟨θ,p⟩ - Ω(p) }
/// 3. The conjugate Ω*(θ) = max_p { ⟨θ,p⟩ - Ω(p) }
pub trait Regularizer {
    /// Compute Ω(p) for a probability distribution p.
    fn omega(&self, p: &[f64]) -> f64;

    /// Compute the prediction map ŷ_Ω(θ) = ∇Ω*(θ).
    fn predict(&self, theta: &[f64]) -> Vec<f64>;

    /// Compute the conjugate Ω*(θ).
    fn conjugate(&self, theta: &[f64]) -> f64;

    /// Compute the Fenchel-Young loss L_Ω(θ; y).
    fn loss(&self, theta: &[f64], y: &[f64]) -> f64 {
        if theta.len() != y.len() {
            return f64::NAN;
        }
        let inner: f64 = theta.iter().zip(y).map(|(t, yi)| t * yi).sum();
        self.conjugate(theta) - inner + self.omega(y)
    }
}

// ============================================================================
// Shannon Negentropy → Softmax + Cross-Entropy
// ============================================================================

/// Shannon negentropy regularizer.
///
/// ```text
/// Ω(p) = Σ pᵢ log pᵢ  (negative entropy)
/// ```
///
/// This gives:
/// - Prediction: softmax(θ)
/// - Loss: cross-entropy
/// - Conjugate: log-sum-exp(θ)
///
/// # Example
///
/// ```rust
/// use fynch::fenchel::{Shannon, Regularizer};
///
/// let shannon = Shannon;
/// let theta = [2.0, 1.0, 0.1];
/// let pred = shannon.predict(&theta);  // softmax
///
/// assert!((pred.iter().sum::<f64>() - 1.0).abs() < 1e-6);
/// ```
#[derive(Debug, Clone, Copy, Default)]
pub struct Shannon;

impl Regularizer for Shannon {
    fn omega(&self, p: &[f64]) -> f64 {
        // Negative entropy: Σ pᵢ log pᵢ
        p.iter()
            .filter(|&&pi| pi > 0.0)
            .map(|&pi| pi * pi.ln())
            .sum()
    }

    fn predict(&self, theta: &[f64]) -> Vec<f64> {
        softmax(theta)
    }

    fn conjugate(&self, theta: &[f64]) -> f64 {
        // log-sum-exp
        log_sum_exp(theta)
    }
}

// ============================================================================
// Squared L2 → Sparsemax
// ============================================================================

/// Squared L2 regularizer (half squared norm).
///
/// ```text
/// Ω(p) = (1/2) ‖p‖²
/// ```
///
/// This gives:
/// - Prediction: sparsemax(θ) = Euclidean projection onto simplex
/// - Loss: sparsemax loss
/// - Conjugate: ⟨θ, sparsemax(θ)⟩ - (1/2) ‖sparsemax(θ)‖²
///
/// # Example
///
/// ```rust
/// use fynch::fenchel::{SquaredL2, Regularizer};
///
/// let l2 = SquaredL2;
/// let theta = [2.0, 1.0, 0.1];
/// let pred = l2.predict(&theta);  // sparsemax
///
/// // Sparsemax produces sparse outputs
/// assert!(pred.iter().any(|&p| p == 0.0));
/// ```
#[derive(Debug, Clone, Copy, Default)]
pub struct SquaredL2;

impl Regularizer for SquaredL2 {
    fn omega(&self, p: &[f64]) -> f64 {
        // (1/2) ‖p‖²
        0.5 * p.iter().map(|&x| x * x).sum::<f64>()
    }

    fn predict(&self, theta: &[f64]) -> Vec<f64> {
        sparsemax(theta)
    }

    fn conjugate(&self, theta: &[f64]) -> f64 {
        // Ω*(θ) = max_p { ⟨θ, p⟩ - Ω(p) } = ⟨θ, p*⟩ - (1/2)‖p*‖²
        // where p* = sparsemax(θ)
        let p = sparsemax(theta);
        let inner: f64 = theta.iter().zip(&p).map(|(&t, &pi)| t * pi).sum();
        let norm_sq: f64 = p.iter().map(|&x| x * x).sum();
        inner - 0.5 * norm_sq
    }

    fn loss(&self, theta: &[f64], y: &[f64]) -> f64 {
        // Sparsemax loss has a cleaner form
        sparsemax_loss(theta, y)
    }
}

// ============================================================================
// Tsallis α-Entropy → α-Entmax
// ============================================================================

/// Tsallis α-entropy regularizer.
///
/// ```text
/// Ω_α(p) = (1/(α(α-1))) (Σ pᵢ^α - 1)  for α ≠ 1
/// ```
///
/// This interpolates between:
/// - α → 1: Shannon (softmax)
/// - α = 2: Squared L2 (sparsemax)
/// - α = 1.5: A popular middle ground
///
/// # Example
///
/// ```rust
/// use fynch::fenchel::{Tsallis, Regularizer};
///
/// let tsallis = Tsallis::new(1.5);  // 1.5-entmax
/// let theta = [2.0, 1.0, 0.1];
/// let pred = tsallis.predict(&theta);
/// ```
#[derive(Debug, Clone, Copy)]
pub struct Tsallis {
    /// The α parameter. α=1 → Shannon, α=2 → sparsemax.
    pub alpha: f64,
}

impl Tsallis {
    /// Create a new Tsallis regularizer with parameter α.
    ///
    /// # Panics
    ///
    /// Panics if α ≤ 0.
    pub fn new(alpha: f64) -> Self {
        assert!(alpha > 0.0, "alpha must be positive");
        Self { alpha }
    }

    /// Create 1.5-entmax (a popular middle ground).
    pub fn entmax15() -> Self {
        Self::new(1.5)
    }
}

impl Default for Tsallis {
    fn default() -> Self {
        Self::new(1.5)
    }
}

impl Regularizer for Tsallis {
    fn omega(&self, p: &[f64]) -> f64 {
        if (self.alpha - 1.0).abs() < 1e-10 {
            // Shannon limit
            return Shannon.omega(p);
        }
        // (1/(α(α-1))) (Σ pᵢ^α - 1)
        let sum_powers: f64 = p.iter().map(|&pi| pi.powf(self.alpha)).sum();
        (sum_powers - 1.0) / (self.alpha * (self.alpha - 1.0))
    }

    fn predict(&self, theta: &[f64]) -> Vec<f64> {
        if (self.alpha - 1.0).abs() < 1e-10 {
            return softmax(theta);
        }
        if (self.alpha - 2.0).abs() < 1e-10 {
            return sparsemax(theta);
        }
        entmax(theta, self.alpha)
    }

    fn conjugate(&self, theta: &[f64]) -> f64 {
        if (self.alpha - 1.0).abs() < 1e-10 {
            return Shannon.conjugate(theta);
        }
        // For general α, conjugate is computed via the prediction
        let p = self.predict(theta);
        let inner: f64 = theta.iter().zip(&p).map(|(t, pi)| t * pi).sum();
        inner - self.omega(&p)
    }
}

// ============================================================================
// Helper Functions
// ============================================================================

/// Softmax: normalize via exp.
///
/// ```text
/// softmax(θ)ᵢ = exp(θᵢ) / Σⱼ exp(θⱼ)
/// ```
pub fn softmax(theta: &[f64]) -> Vec<f64> {
    if theta.is_empty() {
        return vec![];
    }
    let max = theta.iter().fold(f64::NEG_INFINITY, |a, &b| a.max(b));
    let exps: Vec<f64> = theta.iter().map(|&t| (t - max).exp()).collect();
    let sum: f64 = exps.iter().sum();
    exps.iter().map(|&e| e / sum).collect()
}

/// Log-sum-exp (numerically stable).
fn log_sum_exp(theta: &[f64]) -> f64 {
    if theta.is_empty() {
        return f64::NEG_INFINITY;
    }
    let max = theta.iter().fold(f64::NEG_INFINITY, |a, &b| a.max(b));
    if max == f64::NEG_INFINITY {
        return f64::NEG_INFINITY;
    }
    max + theta.iter().map(|&t| (t - max).exp()).sum::<f64>().ln()
}

/// Sparsemax: Euclidean projection onto the probability simplex.
///
/// ```text
/// sparsemax(θ) = argmin_p { ‖p - θ‖² : p ∈ Δ }
/// ```
///
/// where Δ is the probability simplex.
///
/// # Algorithm
///
/// Sort θ descending, find threshold τ, and return [θᵢ - τ]₊.
///
/// # References
///
/// Martins & Astudillo (2016). "From Softmax to Sparsemax"
pub fn sparsemax(theta: &[f64]) -> Vec<f64> {
    if theta.is_empty() {
        return vec![];
    }

    // Sort descending
    let mut sorted: Vec<f64> = theta.to_vec();
    sorted.sort_by(|a, b| b.partial_cmp(a).unwrap_or(std::cmp::Ordering::Equal));

    // Find k = max { j : 1 + j θ_(j) > Σᵢ≤j θ_(i) }
    let mut cumsum = 0.0;
    let mut k = 0;
    for (j, &s) in sorted.iter().enumerate() {
        cumsum += s;
        if 1.0 + (j + 1) as f64 * s > cumsum {
            k = j + 1;
        }
    }

    // Threshold τ = (Σᵢ≤k θ_(i) - 1) / k
    let tau = (sorted[..k].iter().sum::<f64>() - 1.0) / k as f64;

    // Return [θᵢ - τ]₊
    theta.iter().map(|&t| (t - tau).max(0.0)).collect()
}

/// Sparsemax loss (closed form).
///
/// ```text
/// L(θ; y) = (1/2) (‖y‖² - ‖ŷ‖²) + ⟨ŷ - y, θ⟩
/// ```
///
/// where ŷ = sparsemax(θ).
pub fn sparsemax_loss(theta: &[f64], y: &[f64]) -> f64 {
    if theta.len() != y.len() || theta.is_empty() {
        return f64::NAN;
    }

    let p = sparsemax(theta);

    // ‖y‖² and ‖ŷ‖²
    let y_sq: f64 = y.iter().map(|&yi| yi * yi).sum();
    let p_sq: f64 = p.iter().map(|&pi| pi * pi).sum();

    // ⟨ŷ - y, θ⟩
    let diff_inner: f64 = p
        .iter()
        .zip(y)
        .zip(theta)
        .map(|((&pi, &yi), &ti)| (pi - yi) * ti)
        .sum();

    0.5 * (y_sq - p_sq) + diff_inner
}

/// α-entmax: sparse transformation with tunable sparsity.
///
/// For α = 1: softmax (dense)
/// For α = 2: sparsemax (sparse)
/// For 1 < α < 2: interpolates
///
/// # Algorithm
///
/// Bisection to find threshold τ such that projection sums to 1.
///
/// # References
///
/// Peters, Niculae, Martins (2019). "Sparse Sequence-to-Sequence Models"
pub fn entmax(theta: &[f64], alpha: f64) -> Vec<f64> {
    if theta.is_empty() {
        return vec![];
    }
    if (alpha - 1.0).abs() < 1e-10 {
        return softmax(theta);
    }
    if (alpha - 2.0).abs() < 1e-10 {
        return sparsemax(theta);
    }

    // Find max for numerical stability
    let max_theta = theta.iter().fold(f64::NEG_INFINITY, |a, &b| a.max(b));

    // Bisection to find τ
    let mut tau_lo = max_theta - 10.0;
    let mut tau_hi = max_theta;

    // Precompute the exponent once; it is constant throughout bisection.
    let inv_alpha_m1 = 1.0 / (alpha - 1.0);

    for _ in 0..50 {
        let tau = (tau_lo + tau_hi) / 2.0;
        let sum: f64 = theta
            .iter()
            .map(|&t| ((t - tau).max(0.0)).powf(inv_alpha_m1))
            .sum();

        if sum < 1.0 {
            tau_hi = tau;
        } else {
            tau_lo = tau;
        }
    }

    let tau = (tau_lo + tau_hi) / 2.0;

    // Compute output
    let mut result: Vec<f64> = theta
        .iter()
        .map(|&t| ((t - tau).max(0.0)).powf(inv_alpha_m1))
        .collect();

    // Normalize to ensure sum = 1
    let sum: f64 = result.iter().sum();
    if sum > 0.0 {
        for r in &mut result {
            *r /= sum;
        }
    }

    result
}

/// Compute the Fenchel-Young loss for any regularizer.
///
/// Convenience function that dispatches to the regularizer's loss method.
pub fn fy_loss<R: Regularizer>(reg: &R, theta: &[f64], y: &[f64]) -> f64 {
    reg.loss(theta, y)
}

// ============================================================================
// Temperature Scaling
// ============================================================================

/// Apply temperature scaling to logits before softmax.
///
/// ```text
/// softmax(θ/τ)  where τ = temperature
/// ```
///
/// Temperature affects entropy calibration:
/// - τ > 1: Higher entropy (more diverse/random outputs)
/// - τ < 1: Lower entropy (more peaked/deterministic)
/// - τ = 1: Unmodified softmax
///
/// # Connection to Entropy Calibration
///
/// In LLM generation, temperature is the simplest calibration knob.
/// Cao, Valiant, Liang (2025) show that the "right" temperature is
/// similar across model sizes because miscalibration scales slowly
/// with model capacity for heavy-tailed (text-like) distributions.
///
/// For more principled calibration, see:
/// - `surp::entropy_calibration` for evaluation metrics
/// - `surp::zipf` for understanding why calibration is hard
///
/// # Example
///
/// ```rust
/// use fynch::fenchel::softmax_with_temperature;
///
/// let theta = [2.0, 1.0, 0.1];
///
/// let cold = softmax_with_temperature(&theta, 0.5);  // more peaked
/// let hot = softmax_with_temperature(&theta, 2.0);   // flatter
///
/// // Cold temperature increases max probability
/// assert!(cold.iter().cloned().fold(0.0_f64, f64::max) >
///         hot.iter().cloned().fold(0.0_f64, f64::max));
/// ```
pub fn softmax_with_temperature(theta: &[f64], temperature: f64) -> Vec<f64> {
    if temperature <= 0.0 || !temperature.is_finite() {
        // Invalid temperature: return uniform or empty
        if theta.is_empty() {
            return vec![];
        }
        let n = theta.len() as f64;
        return vec![1.0 / n; theta.len()];
    }

    // Fused: avoid the intermediate allocation that softmax() would require.
    let max = theta
        .iter()
        .fold(f64::NEG_INFINITY, |a, &b| a.max(b / temperature));
    let inv_temp = 1.0 / temperature;
    let exps: Vec<f64> = theta.iter().map(|&t| (t * inv_temp - max).exp()).collect();
    let sum: f64 = exps.iter().sum();
    exps.iter().map(|&e| e / sum).collect()
}

/// Compute entropy of a probability distribution (in nats).
///
/// H(p) = -Σ pᵢ ln(pᵢ)
///
/// Useful for checking calibration: compare entropy of model outputs
/// to log-loss on reference text.
pub fn entropy_nats(p: &[f64]) -> f64 {
    p.iter()
        .filter(|&&pi| pi > 0.0)
        .map(|&pi| -pi * pi.ln())
        .sum()
}

/// Compute entropy of a probability distribution (in bits).
pub fn entropy_bits(p: &[f64]) -> f64 {
    entropy_nats(p) / std::f64::consts::LN_2
}

// ============================================================================
// Presets
// ============================================================================

/// Softmax + cross-entropy (Shannon regularization).
pub fn softmax_loss(theta: &[f64], y: &[f64]) -> f64 {
    Shannon.loss(theta, y)
}

/// 1.5-entmax loss (Tsallis α=1.5).
pub fn entmax15_loss(theta: &[f64], y: &[f64]) -> f64 {
    Tsallis::entmax15().loss(theta, y)
}

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

    #[test]
    fn test_softmax_sums_to_one() {
        let theta = [2.0, 1.0, 0.1, -1.0];
        let p = softmax(&theta);
        let sum: f64 = p.iter().sum();
        assert!((sum - 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_sparsemax_sums_to_one() {
        let theta = [2.0, 1.0, 0.1, -1.0];
        let p = sparsemax(&theta);
        let sum: f64 = p.iter().sum();
        assert!((sum - 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_sparsemax_is_sparse() {
        let theta = [2.0, 1.0, 0.1, -1.0];
        let p = sparsemax(&theta);
        let zeros = p.iter().filter(|&&x| x == 0.0).count();
        assert!(zeros > 0, "sparsemax should produce zeros");
    }

    #[test]
    fn test_entmax_interpolates() {
        let theta = [2.0, 1.0, 0.1];

        // α = 1: softmax (dense)
        let p1 = entmax(&theta, 1.0);
        assert!(p1.iter().all(|&x| x > 0.0), "α=1 should be dense");

        // α = 2: sparsemax (sparse)
        let p2 = entmax(&theta, 2.0);
        let sparse2 = sparsemax(&theta);
        for (a, b) in p2.iter().zip(&sparse2) {
            assert!((a - b).abs() < 1e-6);
        }
    }

    #[test]
    fn test_shannon_loss_equals_cross_entropy() {
        let theta = [2.0, 1.0, 0.1];
        let y = [1.0, 0.0, 0.0]; // one-hot

        let loss = Shannon.loss(&theta, &y);

        // Cross-entropy: -Σ yᵢ log(softmax(θ)ᵢ)
        let p = softmax(&theta);
        let ce: f64 = -y.iter().zip(&p).map(|(&yi, &pi)| yi * pi.ln()).sum::<f64>();

        // They should be equal (up to constant)
        // Actually FY loss = CE when y is one-hot
        assert!((loss - ce).abs() < 1e-6, "loss={}, ce={}", loss, ce);
    }

    #[test]
    fn test_fy_loss_nonnegative() {
        let theta = [2.0, 1.0, 0.1];
        let y = [0.5, 0.3, 0.2];

        assert!(Shannon.loss(&theta, &y) >= -1e-10);
        assert!(SquaredL2.loss(&theta, &y) >= -1e-10);
        assert!(Tsallis::entmax15().loss(&theta, &y) >= -1e-10);
    }

    #[test]
    fn test_softmax_temperature_scaling() {
        let theta = [2.0, 1.0, 0.1];

        let cold = softmax_with_temperature(&theta, 0.5);
        let normal = softmax_with_temperature(&theta, 1.0);
        let hot = softmax_with_temperature(&theta, 2.0);

        // All should sum to 1
        assert!((cold.iter().sum::<f64>() - 1.0).abs() < 1e-10);
        assert!((normal.iter().sum::<f64>() - 1.0).abs() < 1e-10);
        assert!((hot.iter().sum::<f64>() - 1.0).abs() < 1e-10);

        // Normal should match regular softmax
        let regular = softmax(&theta);
        for (a, b) in normal.iter().zip(&regular) {
            assert!((a - b).abs() < 1e-10);
        }

        // Cold should be more peaked (lower entropy)
        let h_cold = entropy_bits(&cold);
        let h_normal = entropy_bits(&normal);
        let h_hot = entropy_bits(&hot);

        assert!(h_cold < h_normal, "cold={}, normal={}", h_cold, h_normal);
        assert!(h_normal < h_hot, "normal={}, hot={}", h_normal, h_hot);
    }

    #[test]
    fn test_entropy_uniform() {
        let p = [0.25, 0.25, 0.25, 0.25];
        let h = entropy_bits(&p);
        // log2(4) = 2 bits
        assert!((h - 2.0).abs() < 1e-10);
    }

    #[test]
    fn test_fy_loss_zero_at_prediction() {
        // L_Ω(θ; ŷ_Ω(θ)) = 0
        let theta = [2.0, 1.0, 0.1];

        let y_shannon = Shannon.predict(&theta);
        let loss_shannon = Shannon.loss(&theta, &y_shannon);
        assert!(
            loss_shannon.abs() < 1e-6,
            "Shannon loss at prediction: {}",
            loss_shannon
        );

        // For sparsemax, the loss at prediction should also be 0
        let y_sparse = SquaredL2.predict(&theta);
        let loss_sparse = SquaredL2.loss(&theta, &y_sparse);
        assert!(
            loss_sparse.abs() < 1e-6,
            "Sparsemax loss at prediction: {}",
            loss_sparse
        );
    }

    #[test]
    fn test_squared_l2_conjugate_definition() {
        // Ω*(θ) = <θ, p*> - Ω(p*) where p* = sparsemax(θ)
        let theta = [2.0, 1.0, 0.1, -1.0];
        let p_star = sparsemax(&theta);

        let inner: f64 = theta.iter().zip(&p_star).map(|(&t, &p)| t * p).sum();
        let omega_p = 0.5 * p_star.iter().map(|&x| x * x).sum::<f64>();
        let expected = inner - omega_p;

        let actual = SquaredL2.conjugate(&theta);
        assert!(
            (actual - expected).abs() < 1e-10,
            "conjugate={}, expected={}",
            actual,
            expected
        );
    }
}