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
use crate::ThresholdError;
use validator::{Validate, ValidationError};

// While tag vectors are closely related to the Decision type, by not
// including them as fields and handling them separately, we allow
// the Decision type to be Copy-able and we avoid unnecessary cloning
// that would otherwise need to be done when operating on a Decision.
// It also potentially allows for Decision to be used in other contexts.

/// Represents a value from a continuous range taken from the [`pignistic`](Decision::pignistic)
/// transformation as a category that can be used to select a response to an operation.
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub enum Outcome {
    Trusted,
    Accepted,
    Suspected,
    Restricted,
}

/// A two-state [Dempster-Shafer](https://en.wikipedia.org/wiki/Dempster%E2%80%93Shafer_theory) mass
/// function that represents whether an operation should be accepted or restricted. The power set is represented
/// by the `unknown` value.
///
/// This representation allows for a fairly intuitive way of characterizing evidence in favor of or against
/// blocking an operation, while capturing uncertainty. Limiting to two states rather than a wider range of
/// classification possibilities allows for better performance optimizations, simplifies code readability, and
/// enables useful transformations like reweighting.
#[derive(Debug, Validate, Copy, Clone)]
#[validate(schema(function = "validate_sum", skip_on_field_errors = false))]
pub struct Decision {
    #[validate(range(min = 0.0, max = 1.0))]
    pub accept: f64,
    #[validate(range(min = 0.0, max = 1.0))]
    pub restrict: f64,
    #[validate(range(min = 0.0, max = 1.0))]
    pub unknown: f64,
}

/// Validates that a `Decision`'s components correctly sum to 1.0.
fn validate_sum(decision: &Decision) -> Result<(), ValidationError> {
    let sum = decision.accept + decision.restrict + decision.unknown;
    if sum < 0.0 - 2.0 * f64::EPSILON {
        return Err(ValidationError::new("sum cannot be negative"));
    } else if sum > 1.0 + 2.0 * f64::EPSILON {
        return Err(ValidationError::new("sum cannot be greater than one"));
    } else if !(sum > 1.0 - 2.0 * f64::EPSILON && sum < 1.0 + 2.0 * f64::EPSILON) {
        return Err(ValidationError::new("sum should be equal to one"));
    }

    Ok(())
}

impl Decision {
    /// Reassigns unknown mass evenly to accept and restrict.
    ///
    /// This function is used to convert to a form that is useful in producing a final outcome.
    pub fn pignistic(&self) -> Self {
        Self {
            accept: self.accept + self.unknown / 2.0,
            restrict: self.restrict + self.unknown / 2.0,
            unknown: 0.0,
        }
    }

    /// Checks the [`accept`](Decision::accept) value after [`pignistic`](Decision::pignistic)
    /// transformation against a threshold value. `true` if above the threshold.
    ///
    /// # Arguments
    ///
    /// * `threshold` - The minimum value required to accept a [`Decision`].
    pub fn accepted(&self, threshold: f64) -> bool {
        let p = self.pignistic();
        p.accept >= threshold
    }

    /// Checks the [`restrict`](Decision::restrict) value after [`pignistic`](Decision::pignistic)
    /// transformation against several threshold values.
    ///
    /// The [`Outcome`]s are arranged in ascending order: `Trusted` < `Accepted` < `Suspected` < `Restricted`
    ///
    /// Does not take an `accept` threshold to simplify validation. Returns [`ThresholdError`] if threshold values are
    /// either out-of-order or out-of-range. Thresholds must be between 0.0 and 1.0.
    ///
    /// # Arguments
    ///
    /// * `trust` - The `trust` threshold is an upper-bound threshold. If the `restrict` value is below it, the
    ///     operation is `Trusted`.
    /// * `suspicious` - The `suspicious` threshold is a lower-bound threshold that also defines the accepted range.
    ///     If the `restrict` value is above the `trust` threshold and below the `suspicious` threshold, the operation
    ///     is `Accepted`. If the `restrict` value is above the `suspicious` threshold but below the `restrict`
    ///     threshold, the operation is `Suspected`.
    /// * `restrict` -  The `restricted` threshold is a lower-bound threshold. If the `restrict` value is above it,
    ///     the operation is `Restricted`.
    pub fn outcome(
        &self,
        trust: f64,
        suspicious: f64,
        restrict: f64,
    ) -> Result<Outcome, ThresholdError> {
        let p = self.pignistic();
        if trust > suspicious || suspicious > restrict {
            return Err(ThresholdError::ThresholdOutOfOrder);
        }
        if !(0.0..=1.0).contains(&trust) {
            return Err(ThresholdError::ThresholdOutOfRange(trust));
        }
        if !(0.0..=1.0).contains(&suspicious) {
            return Err(ThresholdError::ThresholdOutOfRange(suspicious));
        }
        if !(0.0..=1.0).contains(&restrict) {
            return Err(ThresholdError::ThresholdOutOfRange(restrict));
        }
        match p.restrict {
            x if x <= trust => Ok(Outcome::Trusted),
            x if x < suspicious => Ok(Outcome::Accepted),
            x if x >= restrict => Ok(Outcome::Restricted),
            // x >= suspicious && x < restrict
            _ => Ok(Outcome::Suspected),
        }
    }

    /// Clamps all values to the 0.0 to 1.0 range.
    ///
    /// Does not guarantee that values will sum to 1.0.
    pub fn clamp(&self) -> Self {
        self.clamp_min_unknown(0.0)
    }

    /// Clamps all values to the 0.0 to 1.0 range, guaranteeing that the unknown value will be at least `min`.
    ///
    /// Does not guarantee that values will sum to 1.0.
    ///
    /// # Arguments
    ///
    /// * `min` - The minimum [`unknown`](Decision::unknown) value.
    pub fn clamp_min_unknown(&self, min: f64) -> Self {
        let mut accept: f64 = self.accept;
        let mut restrict: f64 = self.restrict;
        let mut unknown: f64 = self.unknown;

        if accept < 0.0 {
            accept = 0.0
        } else if accept > 1.0 {
            accept = 1.0
        }
        if restrict < 0.0 {
            restrict = 0.0
        } else if restrict > 1.0 {
            restrict = 1.0
        }
        if unknown < 0.0 {
            unknown = 0.0
        } else if unknown > 1.0 {
            unknown = 1.0
        }
        if unknown < min {
            unknown = min
        }

        Self {
            accept,
            restrict,
            unknown,
        }
    }

    /// If the component values sum to less than 1.0, assigns the remainder to the
    /// [`unknown`](Decision::unknown) value.
    pub fn fill_unknown(&self) -> Self {
        // Check for negative unknown values, much of this function's behavior becomes unintuitive otherwise.
        let unknown = if self.unknown + f64::EPSILON >= 0.0 {
            self.unknown
        } else {
            0.0
        };
        let sum = self.accept + self.restrict + unknown;
        Self {
            accept: self.accept,
            restrict: self.restrict,
            unknown: if sum - f64::EPSILON <= 1.0 {
                1.0 - self.accept - self.restrict
            } else {
                unknown
            },
        }
    }

    /// Rescales a [`Decision`] to ensure all component values are in the 0.0-1.0 range and sum to 1.0.
    ///
    /// It will preserve the relative relationship between [`accept`](Decision::accept) and
    /// [`restrict`](Decision::restrict).
    pub fn scale(&self) -> Self {
        self.scale_min_unknown(0.0)
    }

    /// Rescales a [`Decision`] to ensure all component values are in the 0.0-1.0 range and sum to 1.0 while
    /// ensuring that the [`unknown`](Decision::unknown) value is at least `min`.
    ///
    /// It will preserve the relative relationship between [`accept`](Decision::accept) and
    /// [`restrict`](Decision::restrict).
    ///
    /// # Arguments
    ///
    /// * `min` - The minimum [`unknown`](Decision::unknown) value.
    pub fn scale_min_unknown(&self, min: f64) -> Self {
        let d = self.fill_unknown().clamp();
        let mut sum = d.accept + d.restrict + d.unknown;
        let mut accept = d.accept;
        let mut restrict = d.restrict;
        let mut unknown = d.unknown;

        if sum > 0.0 {
            accept /= sum;
            restrict /= sum;
            unknown /= sum
        }
        if unknown < min {
            unknown = min
        }
        sum = 1.0 - unknown;
        if sum > 0.0 {
            let denominator = accept + restrict;
            accept = sum * (accept / denominator);
            restrict = sum * (restrict / denominator)
        }
        Self {
            accept,
            restrict,
            unknown,
        }
    }

    /// Multiplies the [`accept`](Decision::accept) and [`restrict`](Decision::restrict) by the `factor`
    /// parameter, replacing the [`unknown`](Decision::unknown) value with the remainder.
    ///
    /// Weights below 1.0 will reduce the weight of a [`Decision`], while weights above 1.0 will increase it.
    /// A 1.0 weight has no effect on the result, aside from scaling it to a valid range if necessary.
    ///
    /// # Arguments
    ///
    /// * `factor` - A scale factor used to multiply the [`accept`](Decision::accept) and
    ///     [`restrict`](Decision::restrict) values.
    pub fn weight(&self, factor: f64) -> Self {
        Self {
            accept: self.accept * factor,
            restrict: self.restrict * factor,
            unknown: 0.0,
        }
        .scale()
    }

    /// Performs the conjunctive combination of two decisions.
    ///
    /// It is a helper function for [`combine`](Decision::combine).
    ///
    /// # Arguments
    ///
    /// * `left` - The first [`Decision`] of the pair.
    /// * `right` - The second [`Decision`] of the pair.
    fn pairwise_combine(left: &Self, right: &Self) -> Self {
        // The mass assigned to the null hypothesis due to non-intersection.
        let nullh = left.accept * right.restrict + left.restrict * right.accept;

        Self {
            // These are essentially an unrolled loop over the power set.
            // Each focal element from the left is multiplied by each on the right
            // and then appended to the intersection.
            // Finally, each focal element is normalized with respect to whatever
            // was assigned to the null hypothesis.
            accept: (left.accept * right.accept
                + left.accept * right.unknown
                + left.unknown * right.accept)
                / (1.0 - nullh),
            restrict: (left.restrict * right.restrict
                + left.restrict * right.unknown
                + left.unknown * right.restrict)
                / (1.0 - nullh),
            unknown: (left.unknown * right.unknown) / (1.0 - nullh),
        }
    }

    /// Calculates the conjunctive combination of a set of decisions, returning a new [`Decision`] as the result.
    ///
    /// Unlike [`combine_murphy`](Decision::combine_murphy), `combine_conjunctive` will produce a `NaN` result under
    /// high conflict.
    ///
    /// # Arguments
    ///
    /// * `decisions` - The `Decision`s to be combined.
    pub fn combine_conjunctive<'a, I>(decisions: I) -> Self
    where
        Self: 'a,
        I: IntoIterator<Item = &'a Self>,
    {
        let mut d = Self {
            accept: 0.0,
            restrict: 0.0,
            unknown: 1.0,
        };
        for m in decisions {
            d = Self::pairwise_combine(&d, m);
        }
        d
    }

    /// Calculates the Murphy average of a set of decisions, returning a new [`Decision`] as the result.
    ///
    /// The Murphy average rule[^1] takes the mean value of each focal element across
    /// all mass functions to create a new mass function. This new mass function
    /// is then combined conjunctively with itself N times where N is the total
    /// number of functions that were averaged together.
    ///
    /// # Arguments
    ///
    /// * `decisions` - The `Decision`s to be combined.
    ///
    /// [^1]: Catherine K. Murphy. 2000. Combining belief functions when evidence conflicts.
    ///     Decision Support Systems 29, 1 (2000), 1-9. DOI:<https://doi.org/10.1016/s0167-9236(99)00084-6>
    pub fn combine_murphy<'a, I>(decisions: I) -> Self
    where
        Self: 'a,
        I: IntoIterator<Item = &'a Self>,
    {
        let mut sum_a = 0.0;
        let mut sum_d = 0.0;
        let mut sum_u = 0.0;
        let mut length: usize = 0;
        for m in decisions {
            sum_a += m.accept;
            sum_d += m.restrict;
            sum_u += m.unknown;
            length += 1;
        }
        let avg_d = Self {
            accept: sum_a / length as f64,
            restrict: sum_d / length as f64,
            unknown: sum_u / length as f64,
        };
        let mut d = Self {
            accept: 0.0,
            restrict: 0.0,
            unknown: 1.0,
        };
        for _ in 0..length {
            d = Self::pairwise_combine(&d, &avg_d);
        }
        d
    }
}

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

    macro_rules! test_decision {
        ($name:ident, $dec:expr, $v:expr $(, $attr:ident = $val:expr)*) => {
            #[test]
            fn $name() {
                $(assert_relative_eq!($dec.$attr, $val, epsilon = 2.0 * f64::EPSILON);)*
                // test suite repeated, this time with validation
                if $v {
                    match $dec.validate() {
                        Ok(_) => assert!(true),
                        Err(e) => assert!(false, "decision should have validated: {}", e),
                    }
                } else {
                    match $dec.validate() {
                        Ok(_) => assert!(false, "decision should not validate"),
                        Err(_) => assert!(true),
                    }
                }
            }
        }
    }

    test_decision!(
        validate_zero,
        Decision {
            accept: 0.0,
            restrict: 0.0,
            unknown: 0.0,
        },
        false,
        accept = 0.0,
        restrict = 0.0,
        unknown = 0.0
    );

    test_decision!(
        validate_simple,
        Decision {
            accept: 0.25,
            restrict: 0.25,
            unknown: 0.50,
        },
        true,
        accept = 0.25,
        restrict = 0.25,
        unknown = 0.50
    );

    test_decision!(
        validate_negative,
        Decision {
            accept: -0.25,
            restrict: 0.75,
            unknown: 0.50,
        },
        false,
        accept = -0.25,
        restrict = 0.75,
        unknown = 0.50
    );

    test_decision!(
        pignistic_simple,
        Decision {
            accept: 0.25,
            restrict: 0.25,
            unknown: 0.50,
        }
        .pignistic(),
        true,
        accept = 0.5,
        restrict = 0.5,
        unknown = 0.0
    );

    test_decision!(
        clamp_zero,
        Decision {
            accept: 0.0,
            restrict: 0.0,
            unknown: 0.0,
        }
        .clamp(),
        false,
        accept = 0.0,
        restrict = 0.0,
        unknown = 0.0
    );

    test_decision!(
        clamp_three_halves,
        Decision {
            accept: 0.50,
            restrict: 0.50,
            unknown: 0.50,
        }
        .clamp(),
        false,
        accept = 0.50,
        restrict = 0.50,
        unknown = 0.50
    );

    test_decision!(
        clamp_three_whole,
        Decision {
            accept: 1.0,
            restrict: 1.0,
            unknown: 1.0,
        }
        .clamp(),
        false,
        accept = 1.0,
        restrict = 1.0,
        unknown = 1.0
    );

    test_decision!(
        clamp_negative,
        Decision {
            accept: -1.0,
            restrict: -1.0,
            unknown: -1.0,
        }
        .clamp(),
        false,
        accept = 0.0,
        restrict = 0.0,
        unknown = 0.0
    );

    test_decision!(
        clamp_triple_double,
        Decision {
            accept: 2.0,
            restrict: 2.0,
            unknown: 2.0,
        }
        .clamp(),
        false,
        accept = 1.0,
        restrict = 1.0,
        unknown = 1.0
    );

    test_decision!(
        fill_unknown_zero,
        Decision {
            accept: 0.0,
            restrict: 0.0,
            unknown: 0.0,
        }
        .fill_unknown(),
        true,
        accept = 0.0,
        restrict = 0.0,
        unknown = 1.0
    );

    test_decision!(
        fill_unknown_normal,
        Decision {
            accept: 0.25,
            restrict: 0.25,
            unknown: 0.1,
        }
        .fill_unknown(),
        true,
        accept = 0.25,
        restrict = 0.25,
        unknown = 0.5
    );

    test_decision!(
        fill_unknown_over_one,
        Decision {
            accept: 0.5,
            restrict: 0.5,
            unknown: 0.5,
        }
        .fill_unknown(),
        false,
        accept = 0.5,
        restrict = 0.5,
        unknown = 0.5
    );

    test_decision!(
        fill_unknown_mixed,
        Decision {
            accept: 2.0,
            restrict: 2.0,
            unknown: -3.5,
        }
        .fill_unknown(),
        false,
        accept = 2.0,
        restrict = 2.0,
        unknown = 0.0
    );

    test_decision!(
        scale_zero,
        Decision {
            accept: 0.0,
            restrict: 0.0,
            unknown: 0.0,
        }
        .scale(),
        true,
        accept = 0.0,
        restrict = 0.0,
        unknown = 1.0
    );

    test_decision!(
        scale_unknown,
        Decision {
            accept: 0.0,
            restrict: 0.0,
            unknown: 1.0,
        }
        .scale(),
        true,
        accept = 0.0,
        restrict = 0.0,
        unknown = 1.0
    );

    test_decision!(
        scale_negative,
        Decision {
            accept: -1.0,
            restrict: -1.0,
            unknown: -1.0,
        }
        .scale(),
        true,
        accept = 0.0,
        restrict = 0.0,
        unknown = 1.0
    );

    test_decision!(
        scale_double,
        Decision {
            accept: 2.0,
            restrict: 2.0,
            unknown: 2.0,
        }
        .scale(),
        true,
        accept = 0.3333333333333333,
        restrict = 0.3333333333333333,
        unknown = 0.3333333333333333
    );

    test_decision!(
        scale_mixed,
        Decision {
            accept: 2.0,
            restrict: 2.0,
            unknown: -3.5,
        }
        .scale(),
        true,
        accept = 0.5,
        restrict = 0.5,
        unknown = 0.0
    );

    test_decision!(
        weight_zero_by_zero,
        Decision {
            accept: 0.0,
            restrict: 0.0,
            unknown: 0.0,
        }
        .weight(0.0),
        true,
        accept = 0.0,
        restrict = 0.0,
        unknown = 1.0
    );

    test_decision!(
        weight_zero_by_one,
        Decision {
            accept: 0.0,
            restrict: 0.0,
            unknown: 0.0,
        }
        .weight(1.0),
        true,
        accept = 0.0,
        restrict = 0.0,
        unknown = 1.0
    );

    test_decision!(
        weight_one_by_zero,
        Decision {
            accept: 0.0,
            restrict: 0.0,
            unknown: 1.0,
        }
        .weight(0.0),
        true,
        accept = 0.0,
        restrict = 0.0,
        unknown = 1.0
    );

    test_decision!(
        weight_one_by_one,
        Decision {
            accept: 0.0,
            restrict: 0.0,
            unknown: 1.0,
        }
        .weight(1.0),
        true,
        accept = 0.0,
        restrict = 0.0,
        unknown = 1.0
    );

    test_decision!(
        weight_negative,
        Decision {
            accept: -1.0,
            restrict: -1.0,
            unknown: -1.0,
        }
        .weight(1.0),
        true,
        accept = 0.0,
        restrict = 0.0,
        unknown = 1.0
    );

    test_decision!(
        weight_two_by_one,
        Decision {
            accept: 2.0,
            restrict: 2.0,
            unknown: 2.0,
        }
        .weight(1.0),
        true,
        accept = 0.50,
        restrict = 0.50,
        unknown = 0.0
    );

    test_decision!(
        weight_two_by_two,
        Decision {
            accept: 2.0,
            restrict: 2.0,
            unknown: 2.0,
        }
        .weight(2.0),
        true,
        accept = 0.50,
        restrict = 0.50,
        unknown = 0.0
    );

    test_decision!(
        weight_two_by_one_eighth,
        Decision {
            accept: 2.0,
            restrict: 2.0,
            unknown: 2.0,
        }
        .weight(0.125),
        true,
        accept = 0.25,
        restrict = 0.25,
        unknown = 0.5
    );

    test_decision!(
        weight_one_eighth_by_two,
        Decision {
            accept: 0.125,
            restrict: 0.125,
            unknown: 0.75,
        }
        .weight(2.0),
        true,
        accept = 0.25,
        restrict = 0.25,
        unknown = 0.50
    );

    test_decision!(
        pairwise_combine_simple,
        Decision::pairwise_combine(
            &Decision {
                accept: 0.25,
                restrict: 0.5,
                unknown: 0.25,
            },
            &Decision {
                accept: 0.25,
                restrict: 0.1,
                unknown: 0.65,
            }
        ),
        true,
        accept = 0.338235294117647,
        restrict = 0.4705882352941177,
        unknown = 0.1911764705882353
    );

    test_decision!(
        pairwise_combine_factored_out,
        Decision::pairwise_combine(
            &Decision {
                accept: 0.25,
                restrict: 0.5,
                unknown: 0.25,
            },
            &Decision {
                accept: 0.0,
                restrict: 0.0,
                unknown: 1.0,
            }
        ),
        true,
        accept = 0.25,
        restrict = 0.5,
        unknown = 0.25
    );

    test_decision!(
        pairwise_combine_certainty,
        Decision::pairwise_combine(
            &Decision {
                accept: 0.25,
                restrict: 0.5,
                unknown: 0.25,
            },
            &Decision {
                accept: 1.0,
                restrict: 0.0,
                unknown: 0.0,
            }
        ),
        true,
        accept = 1.0,
        restrict = 0.0,
        unknown = 0.0
    );

    test_decision!(
        combine_conjunctive_simple_with_unknown,
        Decision::combine_conjunctive(&[
            Decision {
                accept: 0.35,
                restrict: 0.20,
                unknown: 0.45,
            },
            Decision {
                accept: 0.0,
                restrict: 0.0,
                unknown: 1.0,
            }
        ]),
        true,
        accept = 0.35,
        restrict = 0.2,
        unknown = 0.45
    );

    test_decision!(
        combine_murphy_simple_with_unknown,
        Decision::combine_murphy(&[
            Decision {
                accept: 0.35,
                restrict: 0.20,
                unknown: 0.45,
            },
            Decision {
                accept: 0.0,
                restrict: 0.0,
                unknown: 1.0,
            }
        ]),
        true,
        accept = 0.2946891191709844,
        restrict = 0.16062176165803108,
        unknown = 0.5446891191709845
    );

    #[test]
    fn test_combine_conjunctive_high_conflict() {
        let d = Decision::combine_conjunctive(&[
            Decision {
                accept: 1.0,
                restrict: 0.0,
                unknown: 0.0,
            },
            Decision {
                accept: 0.0,
                restrict: 1.0,
                unknown: 0.0,
            },
        ]);
        assert!(d.accept.is_nan());
        assert!(d.restrict.is_nan());
        assert!(d.unknown.is_nan());
    }

    test_decision!(
        combine_murphy_high_conflict,
        Decision::combine_murphy(&[
            Decision {
                accept: 1.0,
                restrict: 0.0,
                unknown: 0.0,
            },
            Decision {
                accept: 0.0,
                restrict: 1.0,
                unknown: 0.0,
            }
        ]),
        true,
        accept = 0.5,
        restrict = 0.5,
        unknown = 0.0
    );

    #[test]
    fn decision_accepted() {
        let d = Decision {
            accept: 0.25,
            restrict: 0.2,
            unknown: 0.55,
        };
        assert!(d.accepted(0.5));

        let d = Decision {
            accept: 0.25,
            restrict: 0.25,
            unknown: 0.50,
        };
        assert!(d.accepted(0.5));

        let d = Decision {
            accept: 0.2,
            restrict: 0.25,
            unknown: 0.55,
        };
        assert!(!d.accepted(0.5));
    }

    #[test]
    fn decision_outcome() -> Result<(), Box<dyn std::error::Error>> {
        let d = Decision {
            accept: 0.65,
            restrict: 0.0,
            unknown: 0.35,
        };
        let outcome = d.outcome(0.2, 0.4, 0.8)?;
        assert_eq!(outcome, Outcome::Trusted);

        let d = Decision {
            accept: 0.45,
            restrict: 0.05,
            unknown: 0.5,
        };
        let outcome = d.outcome(0.2, 0.4, 0.8)?;
        assert_eq!(outcome, Outcome::Accepted);

        let d = Decision {
            accept: 0.25,
            restrict: 0.2,
            unknown: 0.55,
        };
        let outcome = d.outcome(0.2, 0.4, 0.8)?;
        assert_eq!(outcome, Outcome::Suspected);

        let d = Decision {
            accept: 0.05,
            restrict: 0.65,
            unknown: 0.3,
        };
        let outcome = d.outcome(0.2, 0.4, 0.8)?;
        assert_eq!(outcome, Outcome::Restricted);

        Ok(())
    }
}