causal-hub 0.0.5

A library for causal models, inference and discovery.
Documentation
mod categorical;
pub use categorical::*;

mod gaussian;
pub use gaussian::*;

use crate::{
    models::graphs::DiGraph,
    types::{Map, Result, Set},
};

/// A trait for Bayesian networks.
pub trait BN {
    /// The type of the CPD.
    type CPD;
    /// The type of the evidence.
    type Evidence;
    /// The type of the sample.
    type Sample;
    /// The type of the samples.
    type Samples;
    /// The type of the incomplete samples.
    type IncSamples;
    /// The type of the weighted samples.
    type WtdSamples;

    /// Constructs a new Bayesian network.
    ///
    /// # Arguments
    ///
    /// * `graph` - The underlying graph.
    /// * `cpds` - The conditional probability distributions.
    ///
    /// # Errors
    ///
    /// * If the number of CPDs does not match the number of vertices in the graph.
    /// * If the labels of the CPDs do not match the labels of the graph.
    /// * If the conditioning labels of the CPDs do not match the parents of the vertices in the graph.
    ///
    /// # Returns
    ///
    /// A new Bayesian network instance.
    ///
    fn new<I>(graph: DiGraph, cpds: I) -> Result<Self>
    where
        I: IntoIterator<Item = Self::CPD>,
        Self: Sized;

    /// Returns the name of the model, if any.
    ///
    /// # Returns
    ///
    /// The name of the model, if it exists.
    ///
    fn name(&self) -> Option<&str>;

    /// Returns the description of the model, if any.
    ///
    /// # Returns
    ///
    /// The description of the model, if it exists.
    ///
    fn description(&self) -> Option<&str>;

    /// Returns the underlying graph.
    ///
    /// # Returns
    ///
    /// A reference to the graph.
    ///
    fn graph(&self) -> &DiGraph;

    /// Returns a map labels-distributions.
    ///
    /// # Returns
    ///
    /// A reference to the cpds.
    ///
    fn cpds(&self) -> &Map<String, Self::CPD>;

    /// Returns the parameters size.
    ///
    /// # Returns
    ///
    /// The parameters size.
    ///
    fn parameters_size(&self) -> usize;

    /// Restrict the model to the specified variables.
    ///
    /// # Arguments
    ///
    /// * `x` - Set of variables to select.
    ///
    /// # Errors
    ///
    /// * If the set of variables is empty.
    /// * If any variable in the set is out of bounds.
    ///
    /// # Returns
    ///
    /// A model restricted to the specified variables.
    ///
    fn select(&self, x: &Set<usize>) -> Result<Self>
    where
        Self: Sized;

    /// Returns the topological order of the graph.
    ///
    /// # Returns
    ///
    /// A reference to the topological order.
    ///
    fn topological_order(&self) -> &[usize];

    /// Creates a new Bayesian network with optional fields.
    ///
    /// # Arguments
    ///
    /// * `name` - The name of the model.
    /// * `description` - The description of the model.
    /// * `graph` - The underlying graph.
    /// * `cpds` - The conditional probability distributions.
    ///
    /// # Errors
    ///
    /// * If the number of CPDs does not match the number of vertices in the graph.
    /// * If the labels of the CPDs do not match the labels of the graph.
    /// * If the conditioning labels of the CPDs do not match the parents of the vertices in the graph.
    ///
    /// # Returns
    ///
    /// A new Bayesian network instance.
    ///
    fn with_optionals<I>(
        name: Option<String>,
        description: Option<String>,
        graph: DiGraph,
        cpds: I,
    ) -> Result<Self>
    where
        I: IntoIterator<Item = Self::CPD>,
        Self: Sized;
}