subsume 0.5.0

Geometric region embeddings (boxes, cones, octagons, Gaussians, hyperbolic intervals, sheaf networks) for subsumption, entailment, and logical query answering
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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
//! TaxoBell combined training loss for taxonomy expansion.
//!
//! Implements the four-component loss from **TaxoBell** (WWW 2026,
//! [arXiv:2601.09633](https://arxiv.org/abs/2601.09633)):
//!
//! ```text
//! L = alpha * L_sym + beta * L_asym + gamma * L_reg + delta * L_clip
//! ```
//!
//! | Component | Purpose | Scoring function |
//! |-----------|---------|-----------------|
//! | `L_sym`   | Sibling triplet loss (symmetric) | Bhattacharyya coefficient |
//! | `L_asym`  | Parent-child containment (asymmetric) | KL divergence |
//! | `L_reg`   | Volume regularization (Eq. 13) | Per-dim squared hinge floor on variance |
//! | `L_clip`  | Variance ceiling (Eq. 14) | Per-dim linear hinge ceiling on variance |
//!
//! # Usage
//!
//! ```rust
//! use subsume::gaussian::GaussianBox;
//! use subsume::taxobell::{TaxoBellConfig, TaxoBellLoss};
//!
//! let config = TaxoBellConfig::default();
//! let loss_fn = TaxoBellLoss::new(config);
//!
//! let anchor = GaussianBox::new(vec![0.0; 8], vec![1.0; 8]).unwrap();
//! let positive = GaussianBox::new(vec![0.1; 8], vec![0.9; 8]).unwrap();
//! let negative = GaussianBox::new(vec![5.0; 8], vec![1.0; 8]).unwrap();
//!
//! let sym = loss_fn.symmetric_loss(&anchor, &positive, &negative).unwrap();
//! ```

use crate::gaussian::{
    bhattacharyya_coefficient, kl_divergence, sigma_ceiling_loss, volume_regularization,
    GaussianBox,
};
use crate::BoxError;
use serde::{Deserialize, Serialize};

/// Default weight for symmetric (Bhattacharyya triplet) loss.
const DEFAULT_ALPHA: f32 = 0.45;
/// Default weight for asymmetric (KL containment) loss.
const DEFAULT_BETA: f32 = 0.45;
/// Default weight for volume regularization loss.
const DEFAULT_GAMMA: f32 = 0.10;
/// Default weight for sigma clipping loss.
const DEFAULT_DELTA: f32 = 0.10;
/// Default margin for symmetric triplet loss.
const DEFAULT_SYMMETRIC_MARGIN: f32 = 0.1;
/// Default margin for asymmetric KL loss.
const DEFAULT_ASYMMETRIC_MARGIN: f32 = 0.5;
/// Default divergence ceiling for asymmetric loss.
const DEFAULT_ASYMMETRIC_DIVERGE_C: f32 = 1.5;
/// Default lambda for divergence contribution.
const DEFAULT_DIVERGE_LAMBDA: f32 = 0.3;
/// Default minimum variance (sigma floor).
const DEFAULT_MIN_VAR: f32 = 0.25;
/// Default maximum variance (sigma ceiling).
const DEFAULT_MAX_VAR: f32 = 10.0;

/// Configuration for TaxoBell combined loss.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TaxoBellConfig {
    /// Weight for symmetric (triplet) loss component.
    pub alpha: f32,
    /// Weight for asymmetric (KL containment) loss component.
    pub beta: f32,
    /// Weight for volume regularization loss.
    pub gamma: f32,
    /// Weight for sigma clipping loss.
    pub delta: f32,

    /// Margin for the symmetric triplet loss (used in triplet variant).
    pub symmetric_margin: f32,
    /// Margin for the asymmetric alignment triplet loss (delta in paper Eq. 10).
    pub asymmetric_margin: f32,
    /// Scale factor `C` for the diverge component (paper Eq. 11).
    /// `L_diverge = max(0, C * D_rep - KL(parent || child))` where
    /// D_rep = logVol(parent) - logVol(child) is computed dynamically.
    /// Default 1.5 per paper. Set to 0.0 to disable L_diverge.
    pub asymmetric_diverge_c: f32,
    /// Lambda weight for L_diverge in the asymmetric loss (paper Eq. 12).
    /// `L_asym = L_align + lambda * L_diverge`. Default 0.3 per paper.
    pub diverge_lambda: f32,

    /// Minimum variance threshold for L_reg (paper Eq. 13, delta_var).
    /// Variances below this are penalized with a squared hinge.
    /// Reference code uses 0.25 (= 0.5^2, hinge on std).
    pub min_var: f32,
    /// Maximum variance threshold for L_clip (paper Eq. 14, M_var).
    /// Variances above this are penalized with a linear hinge.
    /// Reference code uses 10.0.
    pub max_var: f32,
}

impl Default for TaxoBellConfig {
    fn default() -> Self {
        Self {
            alpha: DEFAULT_ALPHA,
            beta: DEFAULT_BETA,
            gamma: DEFAULT_GAMMA,
            delta: DEFAULT_DELTA,
            symmetric_margin: DEFAULT_SYMMETRIC_MARGIN,
            asymmetric_margin: DEFAULT_ASYMMETRIC_MARGIN,
            asymmetric_diverge_c: DEFAULT_ASYMMETRIC_DIVERGE_C,
            diverge_lambda: DEFAULT_DIVERGE_LAMBDA,
            min_var: DEFAULT_MIN_VAR,
            max_var: DEFAULT_MAX_VAR,
        }
    }
}

/// TaxoBell combined loss for taxonomy expansion training.
///
/// Wraps the four loss components (symmetric, asymmetric, regularization,
/// clipping) with configurable weights and margins.
#[derive(Debug, Clone)]
pub struct TaxoBellLoss {
    /// Loss configuration (weights, margins, thresholds).
    pub config: TaxoBellConfig,
}

impl TaxoBellLoss {
    /// Create a new TaxoBell loss with the given configuration.
    #[must_use]
    pub fn new(config: TaxoBellConfig) -> Self {
        Self { config }
    }

    /// Symmetric loss using Bhattacharyya coefficient (paper Eq. 9, BCE form).
    ///
    /// ```text
    /// L_sym = -log(BC(anchor, positive)) - log(1 - BC(anchor, negative))
    /// ```
    ///
    /// This is the binary cross-entropy formulation from the TaxoBell paper:
    /// the first term pulls siblings together (maximize BC with positive),
    /// and the second term pushes non-siblings apart (minimize BC with negative).
    ///
    /// BC values are clamped to `[eps, 1-eps]` for numerical stability.
    ///
    /// # Errors
    ///
    /// Returns [`BoxError::DimensionMismatch`] if any pair differs in dimensionality.
    pub fn symmetric_loss(
        &self,
        anchor: &GaussianBox,
        positive: &GaussianBox,
        negative: &GaussianBox,
    ) -> Result<f32, BoxError> {
        let bc_pos = bhattacharyya_coefficient(anchor, positive)?;
        let bc_neg = bhattacharyya_coefficient(anchor, negative)?;
        let eps = 1e-7_f32;
        let loss = -bc_pos.clamp(eps, 1.0 - eps).ln() - (1.0 - bc_neg.clamp(eps, 1.0 - eps)).ln();
        Ok(loss.max(0.0))
    }

    /// Alternative symmetric loss: triplet margin form.
    ///
    /// ```text
    /// L_sym_triplet = max(0, margin + BC(anchor, negative) - BC(anchor, positive))
    /// ```
    ///
    /// This is a simpler triplet-margin variant (not the paper's Eq. 9).
    /// Use [`symmetric_loss`](Self::symmetric_loss) for the paper-faithful BCE version.
    ///
    /// # Errors
    ///
    /// Returns [`BoxError::DimensionMismatch`] if any pair differs in dimensionality.
    pub fn symmetric_loss_triplet(
        &self,
        anchor: &GaussianBox,
        positive: &GaussianBox,
        negative: &GaussianBox,
    ) -> Result<f32, BoxError> {
        let bc_pos = bhattacharyya_coefficient(anchor, positive)?;
        let bc_neg = bhattacharyya_coefficient(anchor, negative)?;
        Ok((self.config.symmetric_margin + bc_neg - bc_pos).max(0.0))
    }

    /// Asymmetric containment loss using KL divergence (paper Eq. 10-12).
    ///
    /// ```text
    /// L_align   = max(0, KL(child || parent) - delta)
    /// D_rep     = logVol(parent) - logVol(child)          [dynamic]
    /// L_diverge = max(0, C * D_rep - KL(parent || child))
    /// L_asym    = L_align + lambda * L_diverge
    /// ```
    ///
    /// - **L_align**: hinge ensuring child is "contained" (small forward KL).
    /// - **L_diverge** (Eq. 11): ensures the parent is sufficiently "larger"
    ///   than the child. D_rep is the log-volume gap, computed dynamically.
    ///
    /// When called without a negative (this 2-argument form), L_align uses a
    /// simple hinge. For the triplet form, use [`asymmetric_loss_triplet`](Self::asymmetric_loss_triplet).
    ///
    /// # Errors
    ///
    /// Returns [`BoxError::DimensionMismatch`] if boxes differ in dimensionality.
    pub fn asymmetric_loss(
        &self,
        child: &GaussianBox,
        parent: &GaussianBox,
    ) -> Result<f32, BoxError> {
        let kl = kl_divergence(child, parent)?;
        let l_align = (kl - self.config.asymmetric_margin).max(0.0);

        let l_diverge = if self.config.asymmetric_diverge_c > 0.0 {
            let kl_reverse = kl_divergence(parent, child)?;
            let d_rep = parent.log_volume() - child.log_volume();
            (self.config.asymmetric_diverge_c * d_rep - kl_reverse).max(0.0)
        } else {
            0.0
        };

        Ok(l_align + self.config.diverge_lambda * l_diverge)
    }

    /// Asymmetric containment loss with negative sample (paper Eq. 10-12).
    ///
    /// ```text
    /// L_align   = max(0, KL(child || parent) - KL(child || negative) + delta)
    /// D_rep     = logVol(parent) - logVol(child)          [dynamic]
    /// L_diverge = max(0, C * D_rep - KL(parent || child))
    /// ```
    ///
    /// This is the full triplet form from the paper. Use this when negative
    /// samples are available for contrastive training.
    ///
    /// # Errors
    ///
    /// Returns [`BoxError::DimensionMismatch`] if boxes differ in dimensionality.
    pub fn asymmetric_loss_triplet(
        &self,
        child: &GaussianBox,
        parent: &GaussianBox,
        negative: &GaussianBox,
    ) -> Result<f32, BoxError> {
        let kl_pos = kl_divergence(child, parent)?;
        let kl_neg = kl_divergence(child, negative)?;
        let l_align = (kl_pos - kl_neg + self.config.asymmetric_margin).max(0.0);

        let l_diverge = if self.config.asymmetric_diverge_c > 0.0 {
            let kl_reverse = kl_divergence(parent, child)?;
            let d_rep = parent.log_volume() - child.log_volume();
            (self.config.asymmetric_diverge_c * d_rep - kl_reverse).max(0.0)
        } else {
            0.0
        };

        Ok(l_align + self.config.diverge_lambda * l_diverge)
    }

    /// Combined loss over a batch of taxonomy relationships.
    ///
    /// Sums the four components:
    /// - `L_sym`: symmetric triplet loss over `(anchor, positive, negative)` triples
    /// - `L_asym`: asymmetric KL loss over `(child, parent)` pairs
    /// - `L_reg`: volume regularization over all unique boxes
    /// - `L_clip`: sigma clipping over all unique boxes
    ///
    /// # Arguments
    ///
    /// * `positives` - `(child, parent)` pairs from the taxonomy
    /// * `negatives` - `(anchor, positive, negative)` triples for sibling discrimination
    /// * `all_boxes` - all unique boxes in the batch (for regularization/clipping)
    ///
    /// # Errors
    ///
    /// Returns [`BoxError::DimensionMismatch`] if any pair of boxes differs in dimensionality.
    pub fn combined_loss(
        &self,
        positives: &[(&GaussianBox, &GaussianBox)],
        negatives: &[(&GaussianBox, &GaussianBox, &GaussianBox)],
        all_boxes: &[&GaussianBox],
    ) -> Result<CombinedLossResult, BoxError> {
        self.combined_loss_with_negative(positives, negatives, all_boxes, None)
    }

    /// Combined loss with an optional negative for the asymmetric component.
    ///
    /// When `asym_negative` is `Some`, each `(child, parent)` pair in `positives`
    /// uses [`asymmetric_loss_triplet`](Self::asymmetric_loss_triplet) with the
    /// provided negative. When `None`, falls back to the 2-argument
    /// [`asymmetric_loss`](Self::asymmetric_loss).
    ///
    /// # Errors
    ///
    /// Returns [`BoxError::DimensionMismatch`] if any pair of boxes differs in dimensionality.
    pub fn combined_loss_with_negative(
        &self,
        positives: &[(&GaussianBox, &GaussianBox)],
        negatives: &[(&GaussianBox, &GaussianBox, &GaussianBox)],
        all_boxes: &[&GaussianBox],
        asym_negative: Option<&GaussianBox>,
    ) -> Result<CombinedLossResult, BoxError> {
        // L_sym: symmetric triplet losses
        let mut l_sym = 0.0f32;
        for &(anchor, positive, negative) in negatives {
            l_sym += self.symmetric_loss(anchor, positive, negative)?;
        }
        if !negatives.is_empty() {
            l_sym /= negatives.len() as f32;
        }

        // L_asym: asymmetric KL containment losses
        let mut l_asym = 0.0f32;
        for &(child, parent) in positives {
            l_asym += match asym_negative {
                Some(neg) => self.asymmetric_loss_triplet(child, parent, neg)?,
                None => self.asymmetric_loss(child, parent)?,
            };
        }
        if !positives.is_empty() {
            l_asym /= positives.len() as f32;
        }

        // L_reg (Eq. 13) + L_clip (Eq. 14): regularization over all boxes
        let mut l_reg = 0.0f32;
        let mut l_clip = 0.0f32;
        for &g in all_boxes {
            l_reg += volume_regularization(g, self.config.min_var);
            l_clip += sigma_ceiling_loss(g, self.config.max_var);
        }
        if !all_boxes.is_empty() {
            let n = all_boxes.len() as f32;
            l_reg /= n;
            l_clip /= n;
        }

        let total = self.config.alpha * l_sym
            + self.config.beta * l_asym
            + self.config.gamma * l_reg
            + self.config.delta * l_clip;

        Ok(CombinedLossResult {
            total,
            l_sym,
            l_asym,
            l_reg,
            l_clip,
        })
    }
}

/// Breakdown of the combined TaxoBell loss into its four components.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct CombinedLossResult {
    /// Weighted total: `alpha * l_sym + beta * l_asym + gamma * l_reg + delta * l_clip`.
    pub total: f32,
    /// Mean symmetric (triplet) loss before weighting.
    pub l_sym: f32,
    /// Mean asymmetric (KL containment) loss before weighting.
    pub l_asym: f32,
    /// Mean volume regularization loss before weighting.
    pub l_reg: f32,
    /// Mean sigma clipping loss before weighting.
    pub l_clip: f32,
}

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

    fn make_box(mu: &[f32], sigma: &[f32]) -> GaussianBox {
        GaussianBox::new(mu.to_vec(), sigma.to_vec()).unwrap()
    }

    #[test]
    fn test_symmetric_loss_small_when_positive_closer() {
        let loss_fn = TaxoBellLoss::new(TaxoBellConfig::default());
        // anchor and positive are nearby; negative is far away
        let anchor = make_box(&[0.0, 0.0], &[1.0, 1.0]);
        let positive = make_box(&[0.1, 0.1], &[1.0, 1.0]);
        let negative = make_box(&[10.0, 10.0], &[1.0, 1.0]);

        let l = loss_fn
            .symmetric_loss(&anchor, &positive, &negative)
            .unwrap();
        // BCE: -log(BC_pos ~1) - log(1 - BC_neg ~0) should be small
        assert!(
            l < 1.0,
            "loss should be small when positive is much closer than negative, got {l}"
        );
    }

    #[test]
    fn test_symmetric_loss_large_when_negative_closer() {
        let loss_fn = TaxoBellLoss::new(TaxoBellConfig::default());
        // anchor and negative are nearby; positive is far away
        let anchor = make_box(&[0.0, 0.0], &[1.0, 1.0]);
        let positive = make_box(&[10.0, 10.0], &[1.0, 1.0]);
        let negative = make_box(&[0.1, 0.1], &[1.0, 1.0]);

        let l = loss_fn
            .symmetric_loss(&anchor, &positive, &negative)
            .unwrap();
        assert!(
            l > 1.0,
            "loss should be large when negative is closer than positive, got {l}"
        );
    }

    #[test]
    fn test_symmetric_loss_triplet_variant() {
        let loss_fn = TaxoBellLoss::new(TaxoBellConfig::default());
        let anchor = make_box(&[0.0, 0.0], &[1.0, 1.0]);
        let positive = make_box(&[0.1, 0.1], &[1.0, 1.0]);
        let negative = make_box(&[10.0, 10.0], &[1.0, 1.0]);

        let l = loss_fn
            .symmetric_loss_triplet(&anchor, &positive, &negative)
            .unwrap();
        assert!(
            l < 1e-6,
            "triplet loss should be ~0 when positive is much closer, got {l}"
        );
    }

    #[test]
    fn test_asymmetric_loss_small_for_contained_child() {
        let config = TaxoBellConfig {
            asymmetric_margin: 0.0,
            ..TaxoBellConfig::default()
        };
        let loss_fn = TaxoBellLoss::new(config);
        // narrow child inside wide parent
        let child = make_box(&[0.0, 0.0], &[0.5, 0.5]);
        let parent = make_box(&[0.0, 0.0], &[2.0, 2.0]);

        let l_good = loss_fn.asymmetric_loss(&child, &parent).unwrap();

        // Reverse direction: wide "child" in narrow "parent" should have much larger KL
        let l_bad = loss_fn.asymmetric_loss(&parent, &child).unwrap();
        assert!(
            l_good < l_bad,
            "KL(narrow||wide) should be less than KL(wide||narrow): {l_good} vs {l_bad}"
        );
    }

    #[test]
    fn test_asymmetric_loss_large_for_non_contained() {
        let config = TaxoBellConfig {
            asymmetric_margin: 0.0,
            ..TaxoBellConfig::default()
        };
        let loss_fn = TaxoBellLoss::new(config);
        // wide "child" vs narrow "parent" -- bad containment
        let child = make_box(&[0.0, 0.0], &[5.0, 5.0]);
        let parent = make_box(&[0.0, 0.0], &[0.1, 0.1]);

        let l = loss_fn.asymmetric_loss(&child, &parent).unwrap();
        assert!(
            l > 5.0,
            "containment loss should be large when child doesn't fit, got {l}"
        );
    }

    #[test]
    fn test_asymmetric_loss_with_diverge() {
        let config = TaxoBellConfig {
            asymmetric_margin: 0.0,
            asymmetric_diverge_c: 1.0,
            ..TaxoBellConfig::default()
        };
        let loss_fn = TaxoBellLoss::new(config);
        // narrow child inside wide parent: KL(parent||child) should be large
        let child = make_box(&[0.0, 0.0], &[0.5, 0.5]);
        let parent = make_box(&[0.0, 0.0], &[5.0, 5.0]);

        let l = loss_fn.asymmetric_loss(&child, &parent).unwrap();
        // KL(parent||child) is large (wide->narrow), so L_diverge should be small/zero
        assert!(l.is_finite(), "loss should be finite, got {l}");
    }

    #[test]
    fn test_asymmetric_loss_triplet() {
        let config = TaxoBellConfig {
            asymmetric_margin: 1.0,
            ..TaxoBellConfig::default()
        };
        let loss_fn = TaxoBellLoss::new(config);
        let child = make_box(&[0.0, 0.0], &[0.5, 0.5]);
        let parent = make_box(&[0.0, 0.0], &[2.0, 2.0]);
        let negative = make_box(&[10.0, 10.0], &[0.5, 0.5]);

        let l = loss_fn
            .asymmetric_loss_triplet(&child, &parent, &negative)
            .unwrap();
        // KL(child||parent) should be much less than KL(child||negative)
        // so triplet loss should be ~0
        assert!(
            l < 1e-2,
            "triplet loss should be small when parent is closer than negative, got {l}"
        );
    }

    #[test]
    fn test_combined_loss_all_components() {
        let config = TaxoBellConfig {
            alpha: 1.0,
            beta: 1.0,
            gamma: 0.1,
            delta: 0.1,
            symmetric_margin: 0.1,
            asymmetric_margin: 0.0,
            asymmetric_diverge_c: 0.0,
            diverge_lambda: 0.3,
            min_var: 0.01,
            max_var: 100.0,
        };
        let loss_fn = TaxoBellLoss::new(config);

        let a = make_box(&[0.0, 0.0], &[1.0, 1.0]);
        let b = make_box(&[0.1, 0.1], &[0.8, 0.8]);
        let c = make_box(&[5.0, 5.0], &[1.0, 1.0]);

        let positives = vec![(&b, &a)]; // b is child of a
        let negatives = vec![(&a, &b, &c)]; // a-b similar, c dissimilar
        let all_boxes = vec![&a, &b, &c];

        let result = loss_fn
            .combined_loss(&positives, &negatives, &all_boxes)
            .unwrap();

        assert!(result.total >= 0.0, "total loss must be non-negative");
        assert!(result.l_reg >= 0.0, "l_reg must be non-negative");
        assert!(result.l_clip >= 0.0, "l_clip must be non-negative");
        // total = alpha*l_sym + beta*l_asym + gamma*l_reg + delta*l_clip
        let expected =
            1.0 * result.l_sym + 1.0 * result.l_asym + 0.1 * result.l_reg + 0.1 * result.l_clip;
        assert!(
            (result.total - expected).abs() < 1e-6,
            "total={} != expected={expected}",
            result.total
        );
    }

    #[test]
    fn test_combined_loss_empty_batch() {
        let loss_fn = TaxoBellLoss::new(TaxoBellConfig::default());
        let result = loss_fn.combined_loss(&[], &[], &[]).unwrap();
        assert!(
            (result.total).abs() < 1e-6,
            "empty batch should yield zero loss"
        );
    }

    #[test]
    fn test_dimension_mismatch_propagates() {
        let loss_fn = TaxoBellLoss::new(TaxoBellConfig::default());
        let a = make_box(&[0.0, 0.0], &[1.0, 1.0]);
        let b = make_box(&[0.0], &[1.0]);

        assert!(loss_fn.symmetric_loss(&a, &a, &b).is_err());
        assert!(loss_fn.asymmetric_loss(&a, &b).is_err());
    }

    #[test]
    fn test_default_config() {
        let cfg = TaxoBellConfig::default();
        assert!((cfg.alpha - 0.45).abs() < f32::EPSILON);
        assert!((cfg.beta - 0.45).abs() < f32::EPSILON);
        assert!((cfg.gamma - 0.10).abs() < f32::EPSILON);
        assert!((cfg.delta - 0.10).abs() < f32::EPSILON);
        assert!(cfg.symmetric_margin > 0.0);
        assert!(cfg.min_var > 0.0);
        assert!(cfg.max_var > cfg.min_var);
    }

    // -- Audit-driven regression tests --

    /// BCE loss is 0 when BC_pos=1 and BC_neg=0.
    /// symmetric_loss = -log(BC_pos) - log(1 - BC_neg)
    /// When BC_pos=1 and BC_neg=0: -log(1) - log(1) = 0.
    #[test]
    fn test_symmetric_loss_bce_properties() {
        let loss_fn = TaxoBellLoss::new(TaxoBellConfig::default());

        // BC_pos ~ 1: anchor == positive (identical distributions)
        // BC_neg ~ 0: negative very far away
        let anchor = make_box(&[0.0, 0.0, 0.0, 0.0], &[1.0, 1.0, 1.0, 1.0]);
        let negative = make_box(&[1000.0; 4], &[0.01; 4]);

        let l = loss_fn.symmetric_loss(&anchor, &anchor, &negative).unwrap();
        // -log(1-eps) - log(1 - ~0) ~ 0
        assert!(
            l < 1e-4,
            "BCE loss should be ~0 when BC_pos=1 and BC_neg=0, got {l}"
        );
    }

    /// Compare BCE and triplet symmetric loss on the same inputs.
    /// They should agree on direction (when one is high, the other should be too).
    #[test]
    fn test_symmetric_loss_bce_vs_triplet() {
        let loss_fn = TaxoBellLoss::new(TaxoBellConfig {
            symmetric_margin: 0.1,
            ..TaxoBellConfig::default()
        });

        let anchor = make_box(&[0.0; 4], &[1.0; 4]);
        let positive = make_box(&[0.1; 4], &[1.0; 4]);
        let negative = make_box(&[10.0; 4], &[1.0; 4]);

        let bce = loss_fn
            .symmetric_loss(&anchor, &positive, &negative)
            .unwrap();
        let triplet = loss_fn
            .symmetric_loss_triplet(&anchor, &positive, &negative)
            .unwrap();

        // Both should be small when positive is much closer than negative
        assert!(bce < 1.0, "BCE loss should be small, got {bce}");
        assert!(triplet < 0.2, "Triplet loss should be small, got {triplet}");

        // Now swap: negative closer than positive
        let bce_bad = loss_fn
            .symmetric_loss(&anchor, &negative, &positive)
            .unwrap();
        let triplet_bad = loss_fn
            .symmetric_loss_triplet(&anchor, &negative, &positive)
            .unwrap();

        // Both should be larger in the bad case
        assert!(
            bce_bad > bce,
            "BCE loss should increase when positive/negative swapped"
        );
        assert!(
            triplet_bad > triplet,
            "Triplet loss should increase when positive/negative swapped"
        );
    }

    /// Test L_diverge component: when c > 0, L_diverge = max(0, c*D_rep - KL(parent||child))
    /// where D_rep = logVol(parent) - logVol(child) (dynamic).
    /// When parent is moderately wider than child, D_rep > 0 but KL(parent||child) is small,
    /// so L_diverge should be positive.
    #[test]
    fn test_asymmetric_loss_with_diverge_active() {
        let config = TaxoBellConfig {
            asymmetric_margin: 100.0, // large margin so l_align = 0
            asymmetric_diverge_c: 2.0,
            ..TaxoBellConfig::default()
        };
        let loss_fn = TaxoBellLoss::new(config);

        // Parent moderately wider than child => D_rep > 0, KL(parent||child) moderate
        // With sigma_c=1.0, sigma_p=2.0: D_rep=4*ln(2)=2.77, KL(p||c)=3.23, c*D_rep=5.55>3.23
        let child = make_box(&[0.0; 4], &[1.0; 4]);
        let parent = make_box(&[0.0; 4], &[2.0; 4]);

        let l = loss_fn.asymmetric_loss(&child, &parent).unwrap();
        // l_align should be 0 (large margin).
        // D_rep = logVol(parent) - logVol(child) > 0, c*D_rep should exceed KL
        assert!(
            l > 0.0,
            "L_diverge should be active when parent is moderately larger, got {l}"
        );

        // Now with parent much wider: KL(parent||child) grows faster than D_rep
        let parent_wide = make_box(&[0.0; 4], &[100.0; 4]);
        let l2 = loss_fn.asymmetric_loss(&child, &parent_wide).unwrap();
        assert!(
            l2 < l,
            "L_diverge should decrease when parent is much wider: {l2} vs {l}"
        );
    }

    /// Compare asymmetric_loss (2-arg) vs asymmetric_loss_triplet (3-arg).
    #[test]
    fn test_asymmetric_loss_triplet_vs_simple() {
        let config = TaxoBellConfig {
            asymmetric_margin: 1.0,
            asymmetric_diverge_c: 0.0,
            ..TaxoBellConfig::default()
        };
        let loss_fn = TaxoBellLoss::new(config);

        let child = make_box(&[0.0; 4], &[0.5; 4]);
        let parent = make_box(&[0.0; 4], &[2.0; 4]);
        let negative = make_box(&[50.0; 4], &[0.5; 4]);

        let l_simple = loss_fn.asymmetric_loss(&child, &parent).unwrap();
        let l_triplet = loss_fn
            .asymmetric_loss_triplet(&child, &parent, &negative)
            .unwrap();

        // Simple: max(0, KL(child||parent) - margin)
        // Triplet: max(0, KL(child||parent) - KL(child||negative) + margin)
        // KL(child||negative) should be large, so triplet should be smaller
        assert!(
            l_triplet <= l_simple + 1e-6,
            "triplet with distant negative should be <= simple: triplet={l_triplet}, simple={l_simple}"
        );
    }

    /// Test with the paper's recommended weight split: 0.45/0.45/0.10.
    #[test]
    fn test_combined_loss_paper_weights() {
        let config = TaxoBellConfig {
            alpha: 0.45,
            beta: 0.45,
            gamma: 0.05,
            delta: 0.05,
            symmetric_margin: 0.1,
            asymmetric_margin: 1.0,
            asymmetric_diverge_c: 0.0,
            diverge_lambda: 0.3,
            min_var: 0.01,
            max_var: 100.0,
        };
        let loss_fn = TaxoBellLoss::new(config);

        let a = make_box(&[0.0; 8], &[2.0; 8]);
        let b = make_box(&[0.1; 8], &[1.0; 8]);
        let c = make_box(&[5.0; 8], &[1.0; 8]);

        let positives = vec![(&b, &a)];
        let negatives = vec![(&a, &b, &c)];
        let all = vec![&a, &b, &c];

        let r = loss_fn.combined_loss(&positives, &negatives, &all).unwrap();

        // Verify weighted sum
        let expected = 0.45 * r.l_sym + 0.45 * r.l_asym + 0.05 * r.l_reg + 0.05 * r.l_clip;
        assert!(
            (r.total - expected).abs() < 1e-5,
            "total={} != weighted sum={expected}",
            r.total
        );
        assert!(r.total.is_finite());
    }

    // -- Integration test with taxonomy module --

    #[test]
    fn test_loss_decreases_when_child_moved_inside_parent() {
        let loss_fn = TaxoBellLoss::new(TaxoBellConfig::default());

        // Parent: wide box centered at origin.
        let parent = make_box(&[0.0, 0.0, 0.0, 0.0], &[3.0, 3.0, 3.0, 3.0]);

        // Child far outside parent.
        let child_outside = make_box(&[10.0, 10.0, 10.0, 10.0], &[0.5, 0.5, 0.5, 0.5]);
        // Child inside parent (same center, narrower).
        let child_inside = make_box(&[0.0, 0.0, 0.0, 0.0], &[0.5, 0.5, 0.5, 0.5]);

        let l_outside = loss_fn.asymmetric_loss(&child_outside, &parent).unwrap();
        let l_inside = loss_fn.asymmetric_loss(&child_inside, &parent).unwrap();

        assert!(
            l_inside < l_outside,
            "moving child inside parent should decrease loss: {l_inside} vs {l_outside}"
        );
    }

    #[test]
    fn test_loss_increases_when_child_moved_outside_parent() {
        let loss_fn = TaxoBellLoss::new(TaxoBellConfig::default());

        let parent = make_box(&[0.0, 0.0, 0.0], &[2.0, 2.0, 2.0]);
        let child_near = make_box(&[0.1, 0.1, 0.1], &[0.5, 0.5, 0.5]);
        let child_far = make_box(&[20.0, 20.0, 20.0], &[0.5, 0.5, 0.5]);

        let l_near = loss_fn.asymmetric_loss(&child_near, &parent).unwrap();
        let l_far = loss_fn.asymmetric_loss(&child_far, &parent).unwrap();

        assert!(
            l_far > l_near,
            "moving child outside parent should increase loss: {l_far} vs {l_near}"
        );
    }

    #[test]
    fn test_combined_loss_mini_taxonomy() {
        // Inline 5-node taxonomy: root -> {A, B}, A -> {C, D}
        let loss_fn = TaxoBellLoss::new(TaxoBellConfig {
            alpha: 1.0,
            beta: 1.0,
            gamma: 0.01,
            delta: 0.01,
            symmetric_margin: 0.1,
            asymmetric_margin: 0.0,
            asymmetric_diverge_c: 0.0,
            diverge_lambda: 0.3,
            min_var: 0.01,
            max_var: 100.0,
        });

        let root = make_box(&[0.0; 8], &[4.0; 8]);
        let a = make_box(&[-1.0; 8], &[2.0; 8]);
        let b = make_box(&[1.0; 8], &[2.0; 8]);
        let c = make_box(&[-1.5; 8], &[0.5; 8]);
        let d = make_box(&[-0.5; 8], &[0.5; 8]);

        // Parent-child pairs: (child, parent)
        let positives: Vec<(&GaussianBox, &GaussianBox)> =
            vec![(&a, &root), (&b, &root), (&c, &a), (&d, &a)];

        // Sibling triples: siblings share parent, non-sibling is from different subtree
        // (anchor=A, positive=B (siblings under root), negative=C (child of A, not sibling))
        let negatives: Vec<(&GaussianBox, &GaussianBox, &GaussianBox)> =
            vec![(&a, &b, &c), (&c, &d, &b)];

        let all: Vec<&GaussianBox> = vec![&root, &a, &b, &c, &d];
        let result = loss_fn.combined_loss(&positives, &negatives, &all).unwrap();

        assert!(result.total.is_finite(), "total loss must be finite");
        assert!(result.l_sym >= 0.0, "l_sym must be non-negative");
        assert!(result.l_asym >= 0.0, "l_asym must be non-negative");
        assert!(result.l_reg >= 0.0, "l_reg must be non-negative");
        assert!(result.l_clip >= 0.0, "l_clip must be non-negative");

        // Verify weighted sum.
        let expected =
            1.0 * result.l_sym + 1.0 * result.l_asym + 0.01 * result.l_reg + 0.01 * result.l_clip;
        assert!(
            (result.total - expected).abs() < 1e-5,
            "total={} != expected={expected}",
            result.total
        );
    }

    // -- Property tests --

    mod proptests {
        use super::*;
        use proptest::prelude::*;

        /// Strategy for a valid GaussianBox of a given dimension.
        fn arb_gaussian_box(dim: usize) -> impl Strategy<Value = GaussianBox> {
            let mu_strat = prop::collection::vec(-10.0f32..10.0f32, dim);
            let sigma_strat = prop::collection::vec(0.01f32..10.0f32, dim);
            (mu_strat, sigma_strat).prop_map(|(mu, sigma)| GaussianBox::new(mu, sigma).unwrap())
        }

        proptest! {
            #[test]
            fn symmetric_loss_is_non_negative(
                anchor in arb_gaussian_box(4),
                positive in arb_gaussian_box(4),
                negative in arb_gaussian_box(4),
            ) {
                let loss_fn = TaxoBellLoss::new(TaxoBellConfig::default());
                let l = loss_fn.symmetric_loss(&anchor, &positive, &negative).unwrap();
                prop_assert!(l >= 0.0, "symmetric_loss must be >= 0, got {l}");
                prop_assert!(l.is_finite(), "symmetric_loss must be finite, got {l}");
            }

            #[test]
            fn symmetric_loss_zero_when_well_separated(
                anchor in arb_gaussian_box(4),
            ) {
                // Construct a negative that is very far from anchor.
                let far_mu: Vec<f32> = anchor.mu().iter().map(|&m| m + 100.0).collect();
                let negative = GaussianBox::new(far_mu, vec![0.1; 4]).unwrap();

                let config = TaxoBellConfig {
                    symmetric_margin: 0.1,
                    ..TaxoBellConfig::default()
                };
                let loss_fn = TaxoBellLoss::new(config);

                // BC(anchor, negative) should be ~0.
                // When positive=anchor, BC_pos=1 >> margin, so loss=0.
                let l = loss_fn.symmetric_loss(&anchor, &anchor, &negative).unwrap();
                prop_assert!(
                    l < 1e-4,
                    "loss should be ~0 when positive=anchor and negative is far: {l}"
                );
            }

            #[test]
            fn asymmetric_loss_is_non_negative(
                child in arb_gaussian_box(4),
                parent in arb_gaussian_box(4),
            ) {
                let loss_fn = TaxoBellLoss::new(TaxoBellConfig::default());
                let l = loss_fn.asymmetric_loss(&child, &parent).unwrap();
                prop_assert!(l >= 0.0, "asymmetric_loss must be >= 0, got {l}");
                prop_assert!(l.is_finite(), "asymmetric_loss must be finite, got {l}");
            }

            #[test]
            fn combined_loss_components_sum_correctly(
                a in arb_gaussian_box(4),
                b in arb_gaussian_box(4),
                c in arb_gaussian_box(4),
            ) {
                let config = TaxoBellConfig {
                    alpha: 2.0,
                    beta: 3.0,
                    gamma: 0.5,
                    delta: 0.25,
                    ..TaxoBellConfig::default()
                };
                let loss_fn = TaxoBellLoss::new(config);

                let positives = vec![(&b, &a)];
                let negatives = vec![(&a, &b, &c)];
                let all = vec![&a, &b, &c];

                let r = loss_fn.combined_loss(&positives, &negatives, &all).unwrap();
                let expected = 2.0 * r.l_sym + 3.0 * r.l_asym + 0.5 * r.l_reg + 0.25 * r.l_clip;
                prop_assert!(
                    (r.total - expected).abs() < 1e-4,
                    "total={} != weighted sum={expected}",
                    r.total
                );
            }

            #[test]
            fn all_loss_components_finite(
                a in arb_gaussian_box(8),
                b in arb_gaussian_box(8),
                c in arb_gaussian_box(8),
            ) {
                let loss_fn = TaxoBellLoss::new(TaxoBellConfig::default());
                let positives = vec![(&b, &a)];
                let negatives = vec![(&a, &b, &c)];
                let all = vec![&a, &b, &c];

                let r = loss_fn.combined_loss(&positives, &negatives, &all).unwrap();
                prop_assert!(r.total.is_finite(), "total not finite: {}", r.total);
                prop_assert!(r.l_sym.is_finite(), "l_sym not finite: {}", r.l_sym);
                prop_assert!(r.l_asym.is_finite(), "l_asym not finite: {}", r.l_asym);
                prop_assert!(r.l_reg.is_finite(), "l_reg not finite: {}", r.l_reg);
                prop_assert!(r.l_clip.is_finite(), "l_clip not finite: {}", r.l_clip);
            }

            /// diverge_lambda scales L_diverge linearly in the asymmetric loss.
            #[test]
            fn diverge_lambda_scales_loss(
                child in arb_gaussian_box(4),
                parent in arb_gaussian_box(4),
            ) {
                let config_zero = TaxoBellConfig {
                    asymmetric_margin: 10.0,
                    asymmetric_diverge_c: 1.0,
                    diverge_lambda: 0.0,
                    ..TaxoBellConfig::default()
                };
                let config_one = TaxoBellConfig {
                    diverge_lambda: 1.0,
                    ..config_zero.clone()
                };
                let config_half = TaxoBellConfig {
                    diverge_lambda: 0.5,
                    ..config_zero.clone()
                };

                let loss_zero = TaxoBellLoss::new(config_zero).asymmetric_loss(&child, &parent).unwrap();
                let loss_one = TaxoBellLoss::new(config_one).asymmetric_loss(&child, &parent).unwrap();
                let loss_half = TaxoBellLoss::new(config_half).asymmetric_loss(&child, &parent).unwrap();

                // L_align is the same regardless of lambda. The difference
                // between lambda=1 and lambda=0 is exactly the diverge term.
                let diverge_contribution = loss_one - loss_zero;
                // lambda=0.5 should contribute half the diverge term.
                let expected_half = loss_zero + 0.5 * diverge_contribution;
                // Use relative tolerance since loss magnitudes vary widely.
                let tol = 1e-4 * (1.0 + expected_half.abs());
                prop_assert!(
                    (loss_half - expected_half).abs() < tol,
                    "lambda=0.5 should give half diverge: got {loss_half}, expected {expected_half} \
                     (zero={loss_zero}, one={loss_one}, tol={tol})"
                );
                // diverge contribution should be non-negative.
                prop_assert!(
                    diverge_contribution >= -1e-5,
                    "diverge contribution should be non-negative: {diverge_contribution}"
                );
            }

            /// When a negative is provided, combined_loss_with_negative uses triplet scoring
            /// for the asymmetric component.
            #[test]
            fn combined_loss_with_negative_uses_triplet(
                a in arb_gaussian_box(4),
                b in arb_gaussian_box(4),
                c in arb_gaussian_box(4),
            ) {
                let config = TaxoBellConfig {
                    asymmetric_diverge_c: 0.0,
                    diverge_lambda: 0.3,
                    ..TaxoBellConfig::default()
                };
                let loss_fn = TaxoBellLoss::new(config);

                let positives = vec![(&b, &a)];
                let negatives_sym = vec![(&a, &b, &c)];
                let all = vec![&a, &b, &c];

                let r_no_neg = loss_fn.combined_loss(&positives, &negatives_sym, &all).unwrap();
                let r_with_neg = loss_fn.combined_loss_with_negative(
                    &positives, &negatives_sym, &all, Some(&c)
                ).unwrap();

                // Symmetric, reg, clip components should be identical.
                prop_assert!(
                    (r_no_neg.l_sym - r_with_neg.l_sym).abs() < 1e-5,
                    "l_sym should be the same: {} vs {}", r_no_neg.l_sym, r_with_neg.l_sym
                );
                prop_assert!(
                    (r_no_neg.l_reg - r_with_neg.l_reg).abs() < 1e-5,
                    "l_reg should be the same: {} vs {}", r_no_neg.l_reg, r_with_neg.l_reg
                );
                prop_assert!(
                    (r_no_neg.l_clip - r_with_neg.l_clip).abs() < 1e-5,
                    "l_clip should be the same: {} vs {}", r_no_neg.l_reg, r_with_neg.l_clip
                );

                // The asymmetric components may differ (2-arg vs triplet).
                // Both must be finite and non-negative.
                prop_assert!(r_with_neg.l_asym >= 0.0, "l_asym with neg must be >= 0");
                prop_assert!(r_with_neg.l_asym.is_finite(), "l_asym with neg must be finite");
                prop_assert!(r_with_neg.total.is_finite(), "total with neg must be finite");
            }
        }
    }
}