FactorSet#

class pgmpy.factors.FactorSet(*factors_list)[source]#

Bases: object

Base class of DiscreteFactor Sets.

A factor set provides a compact representation of higher dimensional factor \(\phi_1\cdot\phi_2\cdots\phi_n\)

For example the factor set corresponding to factor \(\phi_1\cdot\phi_2\) would be the union of the factors \(\phi_1\) and \(\phi_2\) i.e. factor set \(\vec\phi = \phi_1 \cup \phi_2\).

add_factors(*factors)[source]#

Adds factors to the factor set.

Parameters:
factors: Factor1, Factor2, …., Factorn

factors to be added into the factor set

Examples

>>> from pgmpy.factors import FactorSet
>>> from pgmpy.factors.discrete import DiscreteFactor
>>> phi1 = DiscreteFactor(
...     variables=["x1", "x2", "x3"], cardinality=[2, 3, 2], values=range(12)
... )
>>> phi2 = DiscreteFactor(
...     variables=["x3", "x4", "x1"], cardinality=[2, 2, 2], values=range(8)
... )
>>> factor_set1 = FactorSet(phi1, phi2)
>>> phi3 = DiscreteFactor(
...     variables=["x5", "x6", "x7"], cardinality=[2, 2, 2], values=range(8)
... )
>>> phi4 = DiscreteFactor(
...     variables=["x5", "x7", "x8"], cardinality=[2, 2, 2], values=range(8)
... )
>>> factor_set1.add_factors(phi3, phi4)
>>> print(factor_set1)
set([<DiscreteFactor representing phi(x1:2, x2:3, x3:2) at 0x7f8e32b4ca10>,
     <DiscreteFactor representing phi(x5:2, x7:2, x8:2) at 0x7f8e4c393690>,
     <DiscreteFactor representing phi(x5:2, x6:2, x7:2) at 0x7f8e32b4c750>,
     <DiscreteFactor representing phi(x3:2, x4:2, x1:2) at 0x7f8e32b4cb50>])
copy()[source]#

Create a copy of factor set.

Examples

>>> from pgmpy.factors import FactorSet
>>> from pgmpy.factors.discrete import DiscreteFactor
>>> phi1 = DiscreteFactor(
...     variables=["x1", "x2", "x3"], cardinality=[2, 3, 2], values=range(12)
... )
>>> phi2 = DiscreteFactor(
...     variables=["x3", "x4", "x1"], cardinality=[2, 2, 2], values=range(8)
... )
>>> factor_set = FactorSet(phi1, phi2)
>>> factor_set
<pgmpy.factors.FactorSet.FactorSet object at 0x...>
>>> factor_set_copy = factor_set.copy()
>>> factor_set_copy
<pgmpy.factors.FactorSet.FactorSet object at 0x...>
divide(factorset, inplace=True)[source]#

Returns a new factor set instance after division by the factor set

Division of two factor sets \(\frac{\vec\phi_1}{\vec\phi_2}\) basically translates to union of all the factors present in \(\vec\phi_2\) and \(\frac{1}{\phi_i}\) of all the factors present in \(\vec\phi_2\).

Parameters:
factorset: FactorSet

The divisor

inplace: A boolean (Default value True)

If inplace = True ,then it will modify the FactorSet object, if False then will return a new FactorSet object.

Returns:
If inplace = False, will return a new FactorSet Object which is division of
given factors.

Examples

>>> from pgmpy.factors import FactorSet
>>> from pgmpy.factors.discrete import DiscreteFactor
>>> phi1 = DiscreteFactor(
...     variables=["x1", "x2", "x3"], cardinality=[2, 3, 2], values=range(12)
... )
>>> phi2 = DiscreteFactor(
...     variables=["x3", "x4", "x1"], cardinality=[2, 2, 2], values=range(8)
... )
>>> factor_set1 = FactorSet(phi1, phi2)
>>> phi3 = DiscreteFactor(
...     variables=["x5", "x6", "x7"], cardinality=[2, 2, 2], values=range(8)
... )
>>> phi4 = DiscreteFactor(
...     variables=["x5", "x7", "x8"], cardinality=[2, 2, 2], values=range(8)
... )
>>> factor_set2 = FactorSet(phi3, phi4)
>>> factor_set3 = factor_set2.divide(factorset=factor_set1, inplace=False)
>>> print(factor_set3)
set([<DiscreteFactor representing phi(x3:2, x4:2, x1:2) at 0x7f8e32b5ba10>,
     <DiscreteFactor representing phi(x5:2, x6:2, x7:2) at 0x7f8e32b5b650>,
     <DiscreteFactor representing phi(x1:2, x2:3, x3:2) at 0x7f8e32b5b050>,
     <DiscreteFactor representing phi(x5:2, x7:2, x8:2) at 0x7f8e32b5b8d0>])
get_factors()[source]#

Returns all the factors present in factor set.

Examples

>>> from pgmpy.factors import FactorSet
>>> from pgmpy.factors.discrete import DiscreteFactor
>>> phi1 = DiscreteFactor(
...     variables=["x1", "x2", "x3"], cardinality=[2, 3, 2], values=range(12)
... )
>>> phi2 = DiscreteFactor(
...     variables=["x3", "x4", "x1"], cardinality=[2, 2, 2], values=range(8)
... )
>>> factor_set1 = FactorSet(phi1, phi2)
>>> phi3 = DiscreteFactor(
...     variables=["x5", "x6", "x7"], cardinality=[2, 2, 2], values=range(8)
... )
>>> factor_set1.add_factors(phi3)
>>> factor_set1.get_factors()
{<DiscreteFactor representing phi(x1:2, x2:3, x3:2) at 0x7f827c0a23c8>,
 <DiscreteFactor representing phi(x3:2, x4:2, x1:2) at 0x7f827c0a2358>,
 <DiscreteFactor representing phi(x5:2, x6:2, x7:2) at 0x7f825243f9e8>}
marginalize(variables, inplace=True)[source]#

Marginalizes the factors present in the factor sets with respect to the given variables.

Parameters:
variables: list, array-like

List of the variables to be marginalized.

inplace: boolean (Default value True)

If inplace=True it will modify the factor set itself, would create a new factor set

Returns:
If inplace = False, will return a new marginalized FactorSet object.

Examples

>>> from pgmpy.factors import FactorSet
>>> from pgmpy.factors.discrete import DiscreteFactor
>>> phi1 = DiscreteFactor(
...     variables=["x1", "x2", "x3"], cardinality=[2, 3, 2], values=range(12)
... )
>>> phi2 = DiscreteFactor(
...     variables=["x3", "x4", "x1"], cardinality=[2, 2, 2], values=range(8)
... )
>>> factor_set1 = FactorSet(phi1, phi2)
>>> factor_set1.marginalize(variables=["x1"])
>>> print(factor_set1)
set([<DiscreteFactor representing phi(x2:3, x3:2) at 0x7f8e32b4cc10>,
     <DiscreteFactor representing phi(x3:2, x4:2) at 0x7f8e32b4cf90>])
product(factorset, inplace=True)[source]#

Return the factor sets product with the given factor sets

Suppose \(\vec\phi_1\) and \(\vec\phi_2\) are two factor sets then their product is a another factors set \(\vec\phi_3 = \vec\phi_1 \cup \vec\phi_2\).

Parameters:
factorsets: FactorSet1, FactorSet2, …, FactorSetn

FactorSets to be multiplied

inplace: A boolean (Default value True)

If inplace = True , then it will modify the FactorSet object, if False, it will return a new FactorSet object.

Returns:
If inpalce = False, will return a new FactorSet object, which is product of two factors

Examples

>>> from pgmpy.factors import FactorSet
>>> from pgmpy.factors.discrete import DiscreteFactor
>>> phi1 = DiscreteFactor(
...     variables=["x1", "x2", "x3"], cardinality=[2, 3, 2], values=range(12)
... )
>>> phi2 = DiscreteFactor(
...     variables=["x3", "x4", "x1"], cardinality=[2, 2, 2], values=range(8)
... )
>>> factor_set1 = FactorSet(phi1, phi2)
>>> phi3 = DiscreteFactor(
...     variables=["x5", "x6", "x7"], cardinality=[2, 2, 2], values=range(8)
... )
>>> phi4 = DiscreteFactor(
...     variables=["x5", "x7", "x8"], cardinality=[2, 2, 2], values=range(8)
... )
>>> factor_set2 = FactorSet(phi3, phi4)
>>> print(factor_set2)
{<DiscreteFactor representing phi(x5:2, x6:2, x7:2) at 0x...>,
... <DiscreteFactor representing phi(x5:2, x7:2, x8:2) at 0x...>}
>>> factor_set2.product(factorset=factor_set1)
>>> print(factor_set2)
set([<DiscreteFactor representing phi(x1:2, x2:3, x3:2) at 0x7f8e32b4c910>,
     <DiscreteFactor representing phi(x3:2, x4:2, x1:2) at 0x7f8e32b4cc50>,
     <DiscreteFactor representing phi(x5:2, x6:2, x7:2) at 0x7f8e32b5b050>,
     <DiscreteFactor representing phi(x5:2, x7:2, x8:2) at 0x7f8e32b5b690>])
>>> factor_set2 = FactorSet(phi3, phi4)
>>> factor_set3 = factor_set2.product(factorset=factor_set1, inplace=False)
>>> print(factor_set2)
{<DiscreteFactor representing phi(x5:2, x6:2, x7:2) at 0x20d4b0f49d0>,
... <DiscreteFactor representing phi(x5:2, x7:2, x8:2) at 0x20d4b0f51d0>}
remove_factors(*factors)[source]#

Removes factors from the factor set.

Parameters:
factors: Factor1, Factor2, …., Factorn

factors to be removed from the factor set

Examples

>>> from pgmpy.factors import FactorSet
>>> from pgmpy.factors.discrete import DiscreteFactor
>>> phi1 = DiscreteFactor(
...     variables=["x1", "x2", "x3"], cardinality=[2, 3, 2], values=range(12)
... )
>>> phi2 = DiscreteFactor(
...     variables=["x3", "x4", "x1"], cardinality=[2, 2, 2], values=range(8)
... )
>>> factor_set1 = FactorSet(phi1, phi2)
>>> phi3 = DiscreteFactor(
...     variables=["x5", "x6", "x7"], cardinality=[2, 2, 2], values=range(8)
... )
>>> factor_set1.add_factors(phi3)
>>> print(factor_set1)
set([<DiscreteFactor representing phi(x1:2, x2:3, x3:2) at 0x7f8e32b5b050>,
     <DiscreteFactor representing phi(x5:2, x6:2, x7:2) at 0x7f8e32b5b250>,
     <DiscreteFactor representing phi(x3:2, x4:2, x1:2) at 0x7f8e32b5b150>])
>>> factor_set1.remove_factors(phi1, phi2)
>>> print(factor_set1)
{<DiscreteFactor representing phi(x5:2, x6:2, x7:2) at 0x...>}