causal_hub/datasets/table/gaussian/
evidence.rs

1use crate::{datasets::GaussType, models::Labelled, types::Labels};
2
3/// Gaussian evidence type.
4#[non_exhaustive]
5#[derive(Clone, Debug)]
6pub enum GaussEvT {
7    /// Certain positive evidence.
8    CertainPositive {
9        /// The observed event of the evidence.
10        event: usize,
11        /// The value of the evidence.
12        value: GaussType,
13    },
14}
15
16impl GaussEvT {
17    /// Return the observed event of the evidence.
18    ///
19    /// # Returns
20    ///
21    /// The observed event of the evidence.
22    ///
23    pub const fn event(&self) -> usize {
24        match self {
25            Self::CertainPositive { event, .. } => *event,
26        }
27    }
28}
29
30/// Gaussian evidence structure.
31#[derive(Clone, Debug)]
32pub struct GaussEv {
33    labels: Labels,
34    evidences: Vec<Option<GaussEvT>>,
35}
36
37impl Labelled for GaussEv {
38    #[inline]
39    fn labels(&self) -> &Labels {
40        &self.labels
41    }
42}
43
44impl GaussEv {
45    /// Create a new Gaussian evidence structure.
46    ///
47    /// # Arguments
48    ///
49    /// * `labels` - The labels of the evidence structure.
50    /// * `values` - An iterator over the evidence values.
51    ///
52    /// # Returns
53    ///
54    /// A new Gaussian evidence structure.
55    ///
56    pub fn new<I>(mut labels: Labels, values: I) -> Self
57    where
58        I: IntoIterator<Item = GaussEvT>,
59    {
60        // Get shortened variable type.
61        use GaussEvT as E;
62
63        // Allocate evidences.
64        let mut evidences = vec![None; labels.len()];
65
66        // Fill the evidences.
67        values.into_iter().for_each(|e| {
68            // Get the event of the evidence.
69            let event = e.event();
70            // Push the value into the variable events.
71            evidences[event] = Some(e);
72        });
73
74        // Sort labels, if necessary.
75        if !labels.is_sorted() {
76            // Clone the labels.
77            let mut new_labels = labels.clone();
78            // Sort the labels.
79            new_labels.sort();
80
81            // Create new evidences.
82            let mut new_evidences = vec![None; new_labels.len()];
83
84            // Sort the evidences.
85            evidences.into_iter().flatten().for_each(|e| {
86                // Get the event of the evidence.
87                let event = labels
88                    .get_index(e.event())
89                    .expect("Failed to get label of evidence.");
90                // Sort the event index.
91                let event = new_labels
92                    .get_index_of(event)
93                    .expect("Failed to get index of evidence.");
94
95                // Sort the variable events.
96                let e = match e {
97                    E::CertainPositive { value, .. } => E::CertainPositive { event, value },
98                };
99
100                // Push the value into the variable events.
101                new_evidences[event] = Some(e);
102            });
103
104            // Update the labels.
105            labels = new_labels;
106            // Update the evidences.
107            evidences = new_evidences;
108        }
109
110        Self { labels, evidences }
111    }
112
113    /// The evidences of the evidence.
114    ///
115    /// # Returns
116    ///
117    /// A reference to the evidences of the evidence.
118    ///
119    #[inline]
120    pub const fn evidences(&self) -> &Vec<Option<GaussEvT>> {
121        &self.evidences
122    }
123}