causal_hub/models/bayesian_network/
mod.rs

1mod categorical;
2pub use categorical::*;
3
4mod gaussian;
5pub use gaussian::*;
6
7use crate::{models::graphs::DiGraph, types::Map};
8
9/// A trait for Bayesian networks.
10pub trait BN {
11    /// The type of the CPD.
12    type CPD;
13    /// The type of the evidence.
14    type Evidence;
15    /// The type of the sample.
16    type Sample;
17    /// The type of the samples.
18    type Samples;
19
20    /// Constructs a new Bayesian network.
21    ///
22    /// # Arguments
23    ///
24    /// * `graph` - The underlying graph.
25    /// * `cpds` - The conditional probability distributions.
26    ///
27    /// # Returns
28    ///
29    /// A new Bayesian network instance.
30    ///
31    fn new<I>(graph: DiGraph, cpds: I) -> Self
32    where
33        I: IntoIterator<Item = Self::CPD>;
34
35    /// Returns the name of the model, if any.
36    ///
37    /// # Returns
38    ///
39    /// The name of the model, if it exists.
40    ///
41    fn name(&self) -> Option<&str>;
42
43    /// Returns the description of the model, if any.
44    ///
45    /// # Returns
46    ///
47    /// The description of the model, if it exists.
48    ///
49    fn description(&self) -> Option<&str>;
50
51    /// Returns the underlying graph.
52    ///
53    /// # Returns
54    ///
55    /// A reference to the graph.
56    ///
57    fn graph(&self) -> &DiGraph;
58
59    /// Returns the a map labels-distributions.
60    ///
61    /// # Returns
62    ///
63    /// A reference to the cpds.
64    ///
65    fn cpds(&self) -> &Map<String, Self::CPD>;
66
67    /// Returns the parameters size.
68    ///
69    /// # Returns
70    ///
71    /// The parameters size.
72    ///
73    fn parameters_size(&self) -> usize;
74
75    /// Returns the topological order of the graph.
76    ///
77    /// # Returns
78    ///
79    /// A reference to the topological order.
80    ///
81    fn topological_order(&self) -> &[usize];
82
83    /// Creates a new Bayesian network with optional fields.
84    ///
85    /// # Arguments
86    ///
87    /// * `name` - The name of the model.
88    /// * `description` - The description of the model.
89    /// * `graph` - The underlying graph.
90    /// * `cpds` - The conditional probability distributions.
91    ///
92    /// # Panics
93    ///
94    /// * Panics if `name` is an empty string.
95    /// * Panics if `description` is an empty string.
96    ///
97    /// # Returns
98    ///
99    /// A new Bayesian network instance.
100    ///
101    fn with_optionals<I>(
102        name: Option<String>,
103        description: Option<String>,
104        graph: DiGraph,
105        cpds: I,
106    ) -> Self
107    where
108        I: IntoIterator<Item = Self::CPD>;
109}