mbi.Factor
- class mbi.Factor(domain: Domain, values: Array)[source]
Bases:
objectRepresents a factor defined over a discrete domain.
A factor can be thought of as a potential function or an unnormalized probability distribution over a set of discrete variables defined by a Domain object. It maps each configuration of the domain to a value.
- values
A JAX array containing the factor’s values. The shape of this array matches the shape specified by the domain.
- Type:
jax.Array
- Supported Operations:
Creation: zeros, ones, random for creating factors.
Reshaping: transpose, expand for modifying the domain/shape.
Aggregation: sum, logsumexp, project for marginalizing attributes.
Element-wise: exp, log, normalize for value transformations.
Binary Ops: +, -, *, /, dot for combining factors.
- Example Usage:
>>> from mbi import Domain # Needed for doctest context >>> domain = Domain.fromdict({'X': 2, 'Y': 3}) >>> factor = Factor.ones(domain) >>> print(factor.domain) Domain(X: 2, Y: 3)
Method generated by attrs for class Factor.
Methods
__init__Method generated by attrs for class Factor.
Apply sharding constraint to the factor values.
Returns a copy of the factor (potentially shallow due to JAX).
Returns the factor's values as a flattened vector or original array.
Applies element-wise exponentiation (jnp.exp) to the factor's values.
Expands the factor's domain to include new attributes.
Applies element-wise logarithm (jnp.log) to the factor's values.
Computes the log-sum-exp along specified attribute axes.
Computes the maximum value along specified attribute axes.
Normalizes the factor so its values sum to total (or log-normalize).
Creates a Factor object with all values initialized to one.
Computes the marginal distribution by summing/logsumexp'ing out other attributes.
Creates a Factor object with random values (uniform 0-1).
Slices the factor by fixing specific attribute values.
Computes the sum along specified attribute axes.
Rearranges the factor's axes according to the new attribute order.
Creates a Factor object with all values initialized to zero.
Attributes
- values: Array
- classmethod zeros(domain: Domain) Factor[source]
Creates a Factor object with all values initialized to zero.
- classmethod ones(domain: Domain) Factor[source]
Creates a Factor object with all values initialized to one.
- classmethod random(domain: Domain) Factor[source]
Creates a Factor object with random values (uniform 0-1).
- transpose(attrs: Sequence[str]) Factor[source]
Rearranges the factor’s axes according to the new attribute order.
- max(attrs: Sequence[str] | None = None) Factor[source]
Computes the maximum value along specified attribute axes.
- sum(attrs: Sequence[str] | None = None) Factor[source]
Computes the sum along specified attribute axes.
- logsumexp(attrs: Sequence[str] | None = None) Factor[source]
Computes the log-sum-exp along specified attribute axes.
- project(attrs: str | Sequence[str], log: bool = False) Factor[source]
Computes the marginal distribution by summing/logsumexp’ing out other attributes.
- slice(evidence: dict[str, int | ndarray | Array]) Factor[source]
Slices the factor by fixing specific attribute values.
If at least one attribute has numpy-valued evidence, the returned factor will have a new leading dimension called ‘_mbi_evidence’ corresponding to the number of evidence points.
- Parameters:
evidence – A dictionary mapping attribute names to the values they should be fixed to.
- Returns:
A new Factor with the specified attributes fixed and removed from the domain.
- normalize(total: float = 1.0, log: bool = False) Factor[source]
Normalizes the factor so its values sum to total (or log-normalize).
- datavector(flatten: bool = True) Array[source]
Returns the factor’s values as a flattened vector or original array.
- apply_sharding(mesh: Mesh | None) Factor[source]
Apply sharding constraint to the factor values.
The sharding strategy is automatically determined based on the provided mesh, and the factor domain.
- Parameters:
mesh – The mesh over which the factor should be sharded.
- Returns:
A new factor identical to self with sharding constraints applied to the values.