pub struct Factor {
pub variables: Vec<String>,
pub values: ArrayD<f64>,
pub name: String,
}Expand description
A factor in a probabilistic graphical model.
Represents a function over a subset of variables: φ(X₁, X₂, …, Xₖ) → ℝ⁺
Fields§
§variables: Vec<String>Variables this factor depends on
values: ArrayD<f64>Probability/potential values
name: StringFactor name for debugging
Implementations§
Source§impl Factor
impl Factor
Sourcepub fn new(
name: String,
variables: Vec<String>,
values: ArrayD<f64>,
) -> Result<Self>
pub fn new( name: String, variables: Vec<String>, values: ArrayD<f64>, ) -> Result<Self>
Create a new factor.
Sourcepub fn uniform(name: String, variables: Vec<String>, card: usize) -> Self
pub fn uniform(name: String, variables: Vec<String>, card: usize) -> Self
Create a uniform factor.
Sourcepub fn get_cardinality(&self, var: &str) -> Option<usize>
pub fn get_cardinality(&self, var: &str) -> Option<usize>
Get cardinality of a variable.
Source§impl Factor
impl Factor
Sourcepub fn product(&self, other: &Factor) -> Result<Factor>
pub fn product(&self, other: &Factor) -> Result<Factor>
Compute the product of two factors.
φ₁(X₁) * φ₂(X₂) = φ(X₁ ∪ X₂)
Sourcepub fn marginalize_out(&self, var: &str) -> Result<Factor>
pub fn marginalize_out(&self, var: &str) -> Result<Factor>
Marginalize out a variable by summing over it.
∑ₓ φ(X, Y) = φ(Y)
Sourcepub fn marginalize_out_vars(&self, vars: &[String]) -> Result<Factor>
pub fn marginalize_out_vars(&self, vars: &[String]) -> Result<Factor>
Marginalize out multiple variables.
Sourcepub fn marginalize_out_all_except(&self, keep_vars: &[String]) -> Result<Factor>
pub fn marginalize_out_all_except(&self, keep_vars: &[String]) -> Result<Factor>
Marginalize out all variables except the specified ones.
This is useful for extracting marginals: to get P(X), marginalize out all variables except X.
Sourcepub fn maximize_out(&self, var: &str) -> Result<Factor>
pub fn maximize_out(&self, var: &str) -> Result<Factor>
Maximize out a variable (for max-product algorithm).
max_x φ(X, Y) = φ(Y) where φ(Y) = max over X
Sourcepub fn maximize_out_vars(&self, vars: &[String]) -> Result<Factor>
pub fn maximize_out_vars(&self, vars: &[String]) -> Result<Factor>
Maximize out multiple variables.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Factor
impl<'de> Deserialize<'de> for Factor
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Source§impl QuantRSDistribution for Factor
impl QuantRSDistribution for Factor
Source§fn to_quantrs_distribution(&self) -> Result<DistributionExport>
fn to_quantrs_distribution(&self) -> Result<DistributionExport>
Convert a factor to a QuantRS-compatible distribution. Read more
Source§fn from_quantrs_distribution(dist: &DistributionExport) -> Result<Self>
fn from_quantrs_distribution(dist: &DistributionExport) -> Result<Self>
Create a factor from a QuantRS distribution. Read more
Source§fn is_normalized(&self) -> bool
fn is_normalized(&self) -> bool
Check if the distribution is normalized.
impl StructuralPartialEq for Factor
Auto Trait Implementations§
impl Freeze for Factor
impl RefUnwindSafe for Factor
impl Send for Factor
impl Sync for Factor
impl Unpin for Factor
impl UnwindSafe for Factor
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> Serialize for T
impl<T> Serialize for T
fn erased_serialize(&self, serializer: &mut dyn Serializer) -> Result<(), Error>
fn do_erased_serialize( &self, serializer: &mut dyn Serializer, ) -> Result<(), ErrorImpl>
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.