Factor

Discrete

TabularCPD

Contains the different formats of CPDs used in PGM

class pgmpy.factors.discrete.CPD.TabularCPD(*args, **kwargs)[source]

Defines the conditional probability distribution table (cpd table)

For a distribution of P(grade|diff, intel)

diff easy hard
intel dumb avg smart dumb avg smart
gradeA 0.1 0.1 0.1 0.1 0.1 0.1
gradeB 0.1 0.1 0.1 0.1 0.1 0.1
gradeC 0.8 0.8 0.8 0.8 0.8 0.8

values should be [[0.1,0.1,0.1,0.1,0.1,0.1], [0.1,0.1,0.1,0.1,0.1,0.1], [0.8,0.8,0.8,0.8,0.8,0.8]]

>>> cpd = TabularCPD('grade',3,[[0.1,0.1,0.1,0.1,0.1,0.1],
                                [0.1,0.1,0.1,0.1,0.1,0.1],
                                [0.8,0.8,0.8,0.8,0.8,0.8]],
                                evidence=['diff', 'intel'], evidence_card=[2,3])
>>> print(cpd)
+---------+---------+---------+---------+---------+---------+---------+
| diff    | diff_0  | diff_0  | diff_0  | diff_1  | diff_1  | diff_1  |
+---------+---------+---------+---------+---------+---------+---------+
| intel   | intel_0 | intel_1 | intel_2 | intel_0 | intel_1 | intel_2 |
+---------+---------+---------+---------+---------+---------+---------+
| grade_0 | 0.1     | 0.1     | 0.1     | 0.1     | 0.1     | 0.1     |
+---------+---------+---------+---------+---------+---------+---------+
| grade_1 | 0.1     | 0.1     | 0.1     | 0.1     | 0.1     | 0.1     |
+---------+---------+---------+---------+---------+---------+---------+
| grade_2 | 0.8     | 0.8     | 0.8     | 0.8     | 0.8     | 0.8     |
+---------+---------+---------+---------+---------+---------+---------+
>>> cpd.values
array([[[ 0.1,  0.1,  0.1],
        [ 0.1,  0.1,  0.1]],
[[ 0.1, 0.1, 0.1],
[ 0.1, 0.1, 0.1]],
[[ 0.8, 0.8, 0.8],
[ 0.8, 0.8, 0.8]]])
>>> cpd.variables
['grade', 'diff', 'intel']
>>> cpd.cardinality
array([3, 2, 3])
>>> cpd.variable
'grade'
>>> cpd.variable_card
3
variable: int, string (any hashable python object)
The variable whose CPD is defined.
variable_card: integer
cardinality of variable
values: 2d array, 2d list or 2d tuple
values of the cpd table
evidence: array-like
evidences(if any) w.r.t. which cpd is defined
evidence_card: integer, array-like
cardinality of evidences (if any)

get_cpd() marginalize([variables_list]) normalize() reduce([values_list])

copy()[source]

Returns a copy of the TabularCPD object.

>>> from pgmpy.factors.discrete import TabularCPD
>>> cpd = TabularCPD('grade', 2,
...                  [[0.7, 0.6, 0.6, 0.2],[0.3, 0.4, 0.4, 0.8]],
...                  ['intel', 'diff'], [2, 2])
>>> copy = cpd.copy()
>>> copy.variable
'grade'
>>> copy.variable_card
2
>>> copy.evidence
['intel', 'diff']
>>> copy.values
array([[[ 0.7,  0.6],
        [ 0.6,  0.2]],
[[ 0.3, 0.4],
[ 0.4, 0.8]]])
get_cpd()[source]

Returns the cpd >>> from pgmpy.factors import TabularCPD >>> cpd = TabularCPD(‘grade’, 3, [[0.1, 0.1], ... [0.1, 0.1], ... [0.8, 0.8]], ... evidence=’evi1’, evidence_card=2) >>> cpd.get_cpd() array([[ 0.1, 0.1],

[ 0.1, 0.1], [ 0.8, 0.8]])
marginalize(variables, inplace=True)[source]

Modifies the cpd table with marginalized values.

variables: list, array-like
list of variable to be marginalized
inplace: boolean
If inplace=True it will modify the CPD itself, else would return a new CPD
>>> from pgmpy.factors.discrete import TabularCPD
>>> cpd_table = TabularCPD('grade', 2,
...                        [[0.7, 0.6, 0.6, 0.2],[0.3, 0.4, 0.4, 0.8]],
...                        ['intel', 'diff'], [2, 2])
>>> cpd_table.marginalize(['diff'])
>>> cpd_table.get_cpd()
array([[ 0.65,  0.4 ],
        [ 0.35,  0.6 ]])
normalize(inplace=True)[source]

Normalizes the cpd table.

inplace: boolean
If inplace=True it will modify the CPD itself, else would return a new CPD
>>> from pgmpy.factors.discrete import TabularCPD
>>> cpd_table = TabularCPD('grade', 2,
...                        [[0.7, 0.2, 0.6, 0.2],[0.4, 0.4, 0.4, 0.8]],
...                        ['intel', 'diff'], [2, 2])
>>> cpd_table.normalize()
>>> cpd_table.get_cpd()
array([[ 0.63636364,  0.33333333,  0.6       ,  0.2       ],
       [ 0.36363636,  0.66666667,  0.4       ,  0.8       ]])
reorder_parents(new_order, inplace=True)[source]

Returns a new cpd table according to provided order

new_order: list
list of new ordering of variables
inplace: boolean
If inplace == True it will modify the CPD itself otherwise new value will be returned without affecting old values

Consider a CPD P(grade| diff, intel) >>> cpd = TabularCPD(‘grade’,3,[[0.1,0.1,0.1,0.1,0.1,0.1],

[0.1,0.1,0.1,0.1,0.1,0.1], [0.8,0.8,0.8,0.8,0.8,0.8]],

evidence=[‘diff’, ‘intel’], evidence_card=[2,3])

>>> print(cpd)
+---------+---------+---------+---------+---------+---------+---------+
| diff    | diff_0  | diff_0  | diff_0  | diff_1  | diff_1  | diff_1  |
+---------+---------+---------+---------+---------+---------+---------+
| intel   | intel_0 | intel_1 | intel_2 | intel_0 | intel_1 | intel_2 |
+---------+---------+---------+---------+---------+---------+---------+
| grade_0 | 0.1     | 0.1     | 0.1     | 0.1     | 0.1     | 0.1     |
+---------+---------+---------+---------+---------+---------+---------+
| grade_1 | 0.1     | 0.1     | 0.1     | 0.1     | 0.1     | 0.1     |
+---------+---------+---------+---------+---------+---------+---------+
| grade_2 | 0.8     | 0.8     | 0.8     | 0.8     | 0.8     | 0.8     |
+---------+---------+---------+---------+---------+---------+---------+
>>> cpd.values
array([[[ 0.1,  0.1,  0.1],
        [ 0.1,  0.1,  0.1]],
[[ 0.1, 0.1, 0.1],
[ 0.1, 0.1, 0.1]],
[[ 0.8, 0.8, 0.8],
[ 0.8, 0.8, 0.8]]])
>>> cpd.variables
['grade', 'diff', 'intel']
>>> cpd.cardinality
array([3, 2, 3])
>>> cpd.variable
'grade'
>>> cpd.variable_card
3
>>> cpd.reorder_parents(['intel', 'diff'])
array([[ 0.1,  0.1,  0.2,  0.2,  0.1,  0.1],
       [ 0.1,  0.1,  0.1,  0.1,  0.1,  0.1],
       [ 0.8,  0.8,  0.7,  0.7,  0.8,  0.8]])
>>> print(cpd)
+---------+---------+---------+---------+---------+---------+---------+
| intel   | intel_0 | intel_0 | intel_1 | intel_1 | intel_2 | intel_2 |
+---------+---------+---------+---------+---------+---------+---------+
| diff    | diff_0  | diff_1  | diff_0  | diff_1  | diff_0  | diff_1  |
+---------+---------+---------+---------+---------+---------+---------+
| grade_0 | 0.1     | 0.1     | 0.2     | 0.2     | 0.1     | 0.1     |
+---------+---------+---------+---------+---------+---------+---------+
| grade_1 | 0.1     | 0.1     | 0.1     | 0.1     | 0.1     | 0.1     |
+---------+---------+---------+---------+---------+---------+---------+
| grade_2 | 0.8     | 0.8     | 0.7     | 0.7     | 0.8     | 0.8     |
+---------+---------+---------+---------+---------+---------+---------+
>>> cpd.values
array([[[ 0.1,  0.1],
        [ 0.2,  0.2],
        [ 0.1,  0.1]],
[[ 0.1, 0.1],
[ 0.1, 0.1], [ 0.1, 0.1]],
[[ 0.8, 0.8],
[ 0.7, 0.7], [ 0.8, 0.8]]])
>>> cpd.variables
['grade', 'intel', 'diff']
>>> cpd.cardinality
array([3, 3, 2])
>>> cpd.variable
'grade'
>>> cpd.variable_card
3
to_factor()[source]

Returns an equivalent factor with the same variables, cardinality, values as that of the cpd

>>> from pgmpy.factors.discrete import TabularCPD
>>> cpd = TabularCPD('grade', 3, [[0.1, 0.1],
...                               [0.1, 0.1],
...                               [0.8, 0.8]],
...                  evidence='evi1', evidence_card=2)
>>> factor = cpd.to_factor()
>>> factor
<DiscreteFactor representing phi(grade:3, evi1:2) at 0x7f847a4f2d68>

Discrete Factor

class pgmpy.factors.discrete.DiscreteFactor.DiscreteFactor(*args, **kwargs)[source]

Base class for DiscreteFactor.

assignment(index) get_cardinality(variable) marginalize([variable_list]) normalize() product(*DiscreteFactor) reduce([variable_values_list])

copy()[source]

Returns a copy of the factor.

DiscreteFactor: copy of the factor

>>> import numpy as np
>>> from pgmpy.factors.discrete import DiscreteFactor
>>> phi = DiscreteFactor(['x1', 'x2', 'x3'], [2, 3, 3], np.arange(18))
>>> phi_copy = phi.copy()
>>> phi_copy.variables
['x1', 'x2', 'x3']
>>> phi_copy.cardinality
array([2, 3, 3])
>>> phi_copy.values
array([[[ 0,  1,  2],
        [ 3,  4,  5],
        [ 6,  7,  8]],
[[ 9, 10, 11],
[12, 13, 14], [15, 16, 17]]])
divide(phi1, inplace=True)[source]

DiscreteFactor division by phi1.

phi1
: DiscreteFactor instance
The denominator for division.
inplace: boolean
If inplace=True it will modify the factor itself, else would return a new factor.
DiscreteFactor or None: if inplace=True (default) returns None
if inplace=False returns a new DiscreteFactor instance.
>>> from pgmpy.factors.discrete import DiscreteFactor
>>> phi1 = DiscreteFactor(['x1', 'x2', 'x3'], [2, 3, 2], range(12))
>>> phi2 = DiscreteFactor(['x3', 'x1'], [2, 2], range(1, 5)])
>>> phi1.divide(phi2)
>>> phi1.variables
['x1', 'x2', 'x3']
>>> phi1.cardinality
array([2, 3, 2])
>>> phi1.values
array([[[ 0.        ,  0.33333333],
        [ 2.        ,  1.        ],
        [ 4.        ,  1.66666667]],
[[ 3. , 1.75 ],
[ 4. , 2.25 ], [ 5. , 2.75 ]]])
get_cardinality(variables)[source]

Returns cardinality of a given variable

variables: list, array-like
A list of variable names.

dict: Dictionary of the form {variable: variable_cardinality}

>>> from pgmpy.factors.discrete import DiscreteFactor
>>> phi = DiscreteFactor(['x1', 'x2', 'x3'], [2, 3, 2], range(12))
>>> phi.get_cardinality(['x1'])
{'x1': 2}
>>> phi.get_cardinality(['x1', 'x2'])
{'x1': 2, 'x2': 3}
identity_factor()[source]

Returns the identity factor.

Def: The identity factor of a factor has the same scope and cardinality as the original factor,
but the values for all the assignments is 1. When the identity factor is multiplied with the factor it returns the factor itself.

DiscreteFactor: The identity factor.

>>> from pgmpy.factors.discrete import DiscreteFactor
>>> phi = DiscreteFactor(['x1', 'x2', 'x3'], [2, 3, 2], range(12))
>>> phi_identity = phi.identity_factor()
>>> phi_identity.variables
['x1', 'x2', 'x3']
>>> phi_identity.values
array([[[ 1.,  1.],
        [ 1.,  1.],
        [ 1.,  1.]],
[[ 1., 1.],
[ 1., 1.], [ 1., 1.]]])
marginalize(variables, inplace=True)[source]

Modifies the factor with marginalized values.

variables: list, array-like
List of variables over which to marginalize.
inplace: boolean
If inplace=True it will modify the factor itself, else would return a new factor.
DiscreteFactor or None: if inplace=True (default) returns None
if inplace=False returns a new DiscreteFactor instance.
>>> from pgmpy.factors.discrete import DiscreteFactor
>>> phi = DiscreteFactor(['x1', 'x2', 'x3'], [2, 3, 2], range(12))
>>> phi.marginalize(['x1', 'x3'])
>>> phi.values
array([ 14.,  22.,  30.])
>>> phi.variables
['x2']
maximize(variables, inplace=True)[source]

Maximizes the factor with respect to variables.

variables: list, array-like
List of variables with respect to which factor is to be maximized
inplace: boolean
If inplace=True it will modify the factor itself, else would return a new factor.
DiscreteFactor or None: if inplace=True (default) returns None
if inplace=False returns a new DiscreteFactor instance.
>>> from pgmpy.factors.discrete import DiscreteFactor
>>> phi = DiscreteFactor(['x1', 'x2', 'x3'], [3, 2, 2], [0.25, 0.35, 0.08, 0.16, 0.05, 0.07,
...                                              0.00, 0.00, 0.15, 0.21, 0.09, 0.18])
>>> phi.variables
['x1','x2','x3']
>>> phi.maximize(['x2'])
>>> phi.variables
['x1', 'x3']
>>> phi.cardinality
array([3, 2])
>>> phi.values
array([[ 0.25,  0.35],
       [ 0.05,  0.07],
       [ 0.15,  0.21]])
normalize(inplace=True)[source]

Normalizes the values of factor so that they sum to 1.

inplace: boolean
If inplace=True it will modify the factor itself, else would return a new factor
DiscreteFactor or None: if inplace=True (default) returns None
if inplace=False returns a new DiscreteFactor instance.
>>> from pgmpy.factors.discrete import DiscreteFactor
>>> phi = DiscreteFactor(['x1', 'x2', 'x3'], [2, 3, 2], range(12))
>>> phi.values
array([[[ 0,  1],
        [ 2,  3],
        [ 4,  5]],
[[ 6, 7],
[ 8, 9], [10, 11]]])
>>> phi.normalize()
>>> phi.variables
['x1', 'x2', 'x3']
>>> phi.cardinality
array([2, 3, 2])
>>> phi.values
array([[[ 0.        ,  0.01515152],
        [ 0.03030303,  0.04545455],
        [ 0.06060606,  0.07575758]],
[[ 0.09090909, 0.10606061],
[ 0.12121212, 0.13636364], [ 0.15151515, 0.16666667]]])
product(phi1, inplace=True)[source]

DiscreteFactor product with phi1.

phi1: DiscreteFactor instance
DiscreteFactor to be multiplied.
inplace: boolean
If inplace=True it will modify the factor itself, else would return a new factor.
DiscreteFactor or None: if inplace=True (default) returns None
if inplace=False returns a new DiscreteFactor instance.
>>> from pgmpy.factors.discrete import DiscreteFactor
>>> phi1 = DiscreteFactor(['x1', 'x2', 'x3'], [2, 3, 2], range(12))
>>> phi2 = DiscreteFactor(['x3', 'x4', 'x1'], [2, 2, 2], range(8))
>>> phi1.product(phi2, inplace=True)
>>> phi1.variables
['x1', 'x2', 'x3', 'x4']
>>> phi1.cardinality
array([2, 3, 2, 2])
>>> phi1.values
array([[[[ 0,  0],
         [ 4,  6]],
[[ 0, 4],
[12, 18]],
[[ 0, 8],
[20, 30]]],
[[[ 6, 18],
[35, 49]],
[[ 8, 24],
[45, 63]],
[[10, 30],
[55, 77]]]]
scope()[source]

Returns the scope of the factor.

list: List of variable names in the scope of the factor.

>>> from pgmpy.factors.discrete import DiscreteFactor
>>> phi = DiscreteFactor(['x1', 'x2', 'x3'], [2, 3, 2], np.ones(12))
>>> phi.scope()
['x1', 'x2', 'x3']
sum(phi1, inplace=True)[source]

DiscreteFactor sum with phi1.

phi1: DiscreteFactor instance.
DiscreteFactor to be added.
inplace: boolean
If inplace=True it will modify the factor itself, else would return a new factor.
DiscreteFactor or None: if inplace=True (default) returns None
if inplace=False returns a new DiscreteFactor instance.
>>> from pgmpy.factors.discrete import DiscreteFactor
>>> phi1 = DiscreteFactor(['x1', 'x2', 'x3'], [2, 3, 2], range(12))
>>> phi2 = DiscreteFactor(['x3', 'x4', 'x1'], [2, 2, 2], range(8))
>>> phi1.sum(phi2, inplace=True)
>>> phi1.variables
['x1', 'x2', 'x3', 'x4']
>>> phi1.cardinality
array([2, 3, 2, 2])
>>> phi1.values
array([[[[ 0,  0],
         [ 4,  6]],
[[ 0, 4],
[12, 18]],
[[ 0, 8],
[20, 30]]],
[[[ 6, 18],
[35, 49]],
[[ 8, 24],
[45, 63]],
[[10, 30],
[55, 77]]]])
class pgmpy.factors.discrete.DiscreteFactor.State(var, state)
state

Alias for field number 1

var

Alias for field number 0

pgmpy.factors.discrete.DiscreteFactor.factor_divide(phi1, phi2)[source]

Returns DiscreteFactor representing phi1 / phi2.

phi1: Factor
The Dividend.
phi2: Factor
The Divisor.

DiscreteFactor: DiscreteFactor representing factor division phi1 / phi2.

>>> from pgmpy.factors.discrete import DiscreteFactor, factor_divide
>>> phi1 = DiscreteFactor(['x1', 'x2', 'x3'], [2, 3, 2], range(12))
>>> phi2 = DiscreteFactor(['x3', 'x1'], [2, 2], range(1, 5))
>>> phi = factor_divide(phi1, phi2)
>>> phi.variables
['x1', 'x2', 'x3']
>>> phi.cardinality
array([2, 3, 2])
>>> phi.values
array([[[ 0.        ,  0.33333333],
        [ 2.        ,  1.        ],
        [ 4.        ,  1.66666667]],
[[ 3. , 1.75 ],
[ 4. , 2.25 ], [ 5. , 2.75 ]]])
pgmpy.factors.discrete.DiscreteFactor.factor_product(*args)[source]

Returns factor product over args.

args: DiscreteFactor instances.
factors to be multiplied

DiscreteFactor: DiscreteFactor representing factor product over all the DiscreteFactor instances in args.

>>> from pgmpy.factors.discrete import DiscreteFactor, factor_product
>>> phi1 = DiscreteFactor(['x1', 'x2', 'x3'], [2, 3, 2], range(12))
>>> phi2 = DiscreteFactor(['x3', 'x4', 'x1'], [2, 2, 2], range(8))
>>> phi = factor_product(phi1, phi2)
>>> phi.variables
['x1', 'x2', 'x3', 'x4']
>>> phi.cardinality
array([2, 3, 2, 2])
>>> phi.values
array([[[[ 0,  0],
         [ 4,  6]],
[[ 0, 4],
[12, 18]],
[[ 0, 8],
[20, 30]]],
[[[ 6, 18],
[35, 49]],
[[ 8, 24],
[45, 63]],
[[10, 30],
[55, 77]]]])

Joint Probability Distribution

class pgmpy.factors.discrete.JointProbabilityDistribution.JointProbabilityDistribution(variables, cardinality, values)[source]

Base class for Joint Probability Distribution

conditional_distribution(values) create_bayesian_model() get_independencies() pmap() marginal_distribution(variables) minimal_imap() is_imap(model)

check_independence(event1, event2, event3=None, condition_random_variable=False)[source]

Check if the Joint Probability Distribution satisfies the given independence condition.

event1: list
random variable whose independence is to be checked.
event2: list
random variable from which event1 is independent.
values: 2D array or list like or 1D array or list like
A 2D list of tuples of the form (variable_name, variable_state). A 1D list or array-like to condition over randome variables (condition_random_variable must be True) The values on which to condition the Joint Probability Distribution.
condition_random_variable: Boolean (Default false)
If true and event3 is not None than will check independence condition over random variable.

For random variables say X, Y, Z to check if X is independent of Y given Z. event1 should be either X or Y. event2 should be either Y or X. event3 should Z.

>>> from pgmpy.factors.discrete import JointProbabilityDistribution as JPD
>>> prob = JPD(['I','D','G'],[2,2,3],
               [0.126,0.168,0.126,0.009,0.045,0.126,0.252,0.0224,0.0056,0.06,0.036,0.024])
>>> prob.check_independence(['I'], ['D'])
True
>>> prob.check_independence(['I'], ['D'], [('G', 1)])  # Conditioning over G_1
False
>>> # Conditioning over random variable G
>>> prob.check_independence(['I'], ['D'], ('G',), condition_random_variable=True)
False
conditional_distribution(values, inplace=True)[source]

Returns Conditional Probability Distribution after setting values to 1.

values: list or array_like
A list of tuples of the form (variable_name, variable_state). The values on which to condition the Joint Probability Distribution.
inplace: Boolean (default True)
If False returns a new instance of JointProbabilityDistribution
>>> import numpy as np
>>> from pgmpy.factors.discrete import JointProbabilityDistribution
>>> prob = JointProbabilityDistribution(['x1', 'x2', 'x3'], [2, 2, 2], np.ones(8)/8)
>>> prob.conditional_distribution([('x1', 1)])
>>> print(prob)
x2    x3      P(x2,x3)
----  ----  ----------
x2_0  x3_0      0.2500
x2_0  x3_1      0.2500
x2_1  x3_0      0.2500
x2_1  x3_1      0.2500
copy()[source]

Returns A copy of JointProbabilityDistribution object

>>> import numpy as np
>>> from pgmpy.factors.discrete import JointProbabilityDistribution
>>> prob = JointProbabilityDistribution(['x1', 'x2', 'x3'], [2, 3, 2], np.ones(12)/12)
>>> prob_copy = prob.copy()
>>> prob_copy.values == prob.values
True
>>> prob_copy.variables == prob.variables
True
>>> prob_copy.variables[1] = 'y'
>>> prob_copy.variables == prob.variables
False
get_independencies(condition=None)[source]

Returns the independent variables in the joint probability distribution. Returns marginally independent variables if condition=None. Returns conditionally independent variables if condition!=None

condition: array_like
Random Variable on which to condition the Joint Probability Distribution.
>>> import numpy as np
>>> from pgmpy.factors.discrete import JointProbabilityDistribution
>>> prob = JointProbabilityDistribution(['x1', 'x2', 'x3'], [2, 3, 2], np.ones(12)/12)
>>> prob.get_independencies()
(x1 _|_ x2)
(x1 _|_ x3)
(x2 _|_ x3)
is_imap(model)[source]

Checks whether the given BayesianModel is Imap of JointProbabilityDistribution

model
: An instance of BayesianModel Class, for which you want to
check the Imap
boolean
: True if given bayesian model is Imap for Joint Probability Distribution
False otherwise
>>> from pgmpy.models import BayesianModel
>>> from pgmpy.factors.discrete import TabularCPD
>>> from pgmpy.factors.discrete import JointProbabilityDistribution
>>> bm = BayesianModel([('diff', 'grade'), ('intel', 'grade')])
>>> diff_cpd = TabularCPD('diff', 2, [[0.2], [0.8]])
>>> intel_cpd = TabularCPD('intel', 3, [[0.5], [0.3], [0.2]])
>>> grade_cpd = TabularCPD('grade', 3,
...                        [[0.1,0.1,0.1,0.1,0.1,0.1],
...                         [0.1,0.1,0.1,0.1,0.1,0.1],
...                         [0.8,0.8,0.8,0.8,0.8,0.8]],
...                        evidence=['diff', 'intel'],
...                        evidence_card=[2, 3])
>>> bm.add_cpds(diff_cpd, intel_cpd, grade_cpd)
>>> val = [0.01, 0.01, 0.08, 0.006, 0.006, 0.048, 0.004, 0.004, 0.032,
           0.04, 0.04, 0.32, 0.024, 0.024, 0.192, 0.016, 0.016, 0.128]
>>> JPD = JointProbabilityDistribution(['diff', 'intel', 'grade'], [2, 3, 3], val)
>>> JPD.is_imap(bm)
True
marginal_distribution(variables, inplace=True)[source]

Returns the marginal distribution over variables.

variables: string, list, tuple, set, dict
Variable or list of variables over which marginal distribution needs to be calculated
inplace: Boolean (default True)
If False return a new instance of JointProbabilityDistribution
>>> import numpy as np
>>> from pgmpy.factors.discrete import JointProbabilityDistribution
>>> values = np.random.rand(12)
>>> prob = JointProbabilityDistribution(['x1', 'x2', 'x3'], [2, 3, 2], values/np.sum(values))
>>> prob.marginal_distribution(['x1', 'x2'])
>>> print(prob)
x1    x2      P(x1,x2)
----  ----  ----------
x1_0  x2_0      0.1502
x1_0  x2_1      0.1626
x1_0  x2_2      0.1197
x1_1  x2_0      0.2339
x1_1  x2_1      0.1996
x1_1  x2_2      0.1340
minimal_imap(order)[source]

Returns a Bayesian Model which is minimal IMap of the Joint Probability Distribution considering the order of the variables.

order: array-like
The order of the random variables.
>>> import numpy as np
>>> from pgmpy.factors.discrete import JointProbabilityDistribution
>>> prob = JointProbabilityDistribution(['x1', 'x2', 'x3'], [2, 3, 2], np.ones(12)/12)
>>> bayesian_model = prob.minimal_imap(order=['x2', 'x1', 'x3'])
>>> bayesian_model
<pgmpy.models.models.models at 0x7fd7440a9320>
>>> bayesian_model.edges()
[('x1', 'x3'), ('x2', 'x3')]
to_factor()[source]

Returns JointProbabilityDistribution as a DiscreteFactor object

>>> import numpy as np
>>> from pgmpy.factors.discrete import JointProbabilityDistribution
>>> prob = JointProbabilityDistribution(['x1', 'x2', 'x3'], [2, 3, 2], np.ones(12)/12)
>>> phi = prob.to_factor()
>>> type(phi)
pgmpy.factors.DiscreteFactor.DiscreteFactor

Continuous

Canonical Factor

class pgmpy.factors.continuous.CanonicalFactor.CanonicalFactor(variables, K, h, g)[source]

The intermediate factors in a Gaussian network can be described compactly using a simple parametric representation called the canonical form. This representation is closed under the basic operations used in inference: factor product, factor division, factor reduction, and marginalization. Thus, we define this CanonicalFactor class that allows the inference process to be performed on joint Gaussian networks.

A canonical form C (X; K,h,g) is defined as

C (X; K,h,g) = exp( ((-1/2) * X.T * K * X) + (h.T * X) + g)

Probabilistic Graphical Models, Principles and Techniques, Daphne Koller and Nir Friedman, Section 14.2, Chapter 14.

copy()[source]

Makes a copy of the factor.

CanonicalFactor object: Copy of the factor

>>> from pgmpy.factors.continuous import CanonicalFactor
>>> phi = CanonicalFactor(['X', 'Y'], np.array([[1, -1], [-1, 1]]),
                          np.array([[1], [-1]]), -3)
>>> phi.variables
['X', 'Y']
>>> phi.K
array([[1, -1],
       [-1, 1]])
>>> phi.h
array([[1],
       [-1]])
>>> phi.g
-3
>>> phi2 = phi.copy()
>>> phi2.variables
['X', 'Y']
>>> phi2.K
array([[1, -1],
       [-1, 1]])
>>> phi2.h
array([[1],
       [-1]])
>>> phi2.g
-3
marginalize(variables, inplace=True)[source]

Modifies the factor with marginalized values.

Let C(X,Y ; K, h, g) be some canonical form over X,Y where,

k = [[K_XX, K_XY], ; h = [[h_X],
[K_YX, K_YY]] [h_Y]]

In this case, the result of the integration operation is a canonical from C (K’, h’, g’) given by,

K' = K_{XX} - K_{XY} * {K^{-1}}_{YY} * K_YX

h' = h_X - K_{XY} * {K^{-1}}_{YY} * h_Y

g' = g + 0.5 * (|Y| * log(2*pi) - log(|K_{YY}|) + {h^T}_Y * K_{YY} * h_Y)

variables: list or array-like
List of variables over which to marginalize.
inplace: boolean
If inplace=True it will modify the distribution itself, else would return a new distribution.
CanonicalFactor or None :
if inplace=True (default) returns None if inplace=False return a new CanonicalFactor instance
>>> import numpy as np
>>> from pgmpy.factors.continuous import CanonicalFactor
>>> phi = CanonicalFactor(['X1', 'X2', 'X3'],
...                       np.array([[1, -1, 0], [-1, 4, -2], [0, -2, 4]]),
...                       np.array([[1], [4], [-1]]), -2)
>>> phi.K
array([[ 1, -1,  0],
        [-1,  4, -2],
        [ 0, -2,  4]])
>>> phi.h
array([[ 1],
        [ 4],
        [-1]])
>>> phi.g
-2
>>> phi.marginalize(['X3'])
>>> phi.K
array([[ 1., -1.],
        [-1.,  3.]])
>>> phi.h
array([[ 1. ],
        [ 3.5]])
>>> phi.g
0.22579135
reduce(values, inplace=True)[source]

Reduces the distribution to the context of the given variable values.

Let C(X,Y ; K, h, g) be some canonical form over X,Y where,

k = [[K_XX, K_XY], ; h = [[h_X],
[K_YX, K_YY]] [h_Y]]

The formula for the obtained conditional distribution for setting Y = y is given by,

K' = K_{XX}

h' = h_X - K_{XY} * y

g' = g + {h^T}_Y * y - 0.5 * y^T * K_{YY} * y

values: list, array-like
A list of tuples of the form (variable name, variable value).
inplace: boolean
If inplace=True it will modify the factor itself, else would return a new CaninicalFactor object.
CanonicalFactor or None:
if inplace=True (default) returns None if inplace=False returns a new CanonicalFactor instance.
>>> import numpy as np
>>> from pgmpy.factors.continuous import CanonicalFactor
>>> phi = CanonicalFactor(['X1', 'X2', 'X3'],
...                       np.array([[1, -1, 0], [-1, 4, -2], [0, -2, 4]]),
...                       np.array([[1], [4], [-1]]), -2)
>>> phi.variables
['X1', 'X2', 'X3']
>>> phi.K
array([[ 1., -1.],
       [-1.,  3.]])
>>> phi.h
array([[ 1. ],
       [ 3.5]])
>>> phi.g
-2
>>> phi.reduce([('X3', 0.25)])
>>> phi.variables
['X1', 'X2']
>>> phi.K
array([[ 1, -1],
       [-1,  4]])
>>> phi.h
array([[ 1. ],
       [ 4.5]])
>>> phi.g
-2.375
to_joint_gaussian()[source]

Return an equivalent Joint Gaussian Distribution.

>>> import numpy as np
>>> from pgmpy.factors.continuous import CanonicalFactor
>>> phi = CanonicalFactor(['x1', 'x2'], np.array([[3, -2], [-2, 4]]),
                          np.array([[5], [-1]]), 1)
>>> jgd = phi.to_joint_gaussian()
>>> jgd.variables
['x1', 'x2']
>>> jgd.covariance
array([[ 0.5  ,  0.25 ],
       [ 0.25 ,  0.375]])
>>> jgd.mean
array([[ 2.25 ],
       [ 0.875]])

Continuous Factor

class pgmpy.factors.continuous.ContinuousFactor.ContinuousFactor(variables, pdf)[source]

Base class for factors representing various multivariate representations.

assignment(*args)[source]

Returns a list of pdf assignments for the corresponding values.

*args: values
Values whose assignment is to be computed.
>>> from pgmpy.factors.continuous import ContinuousFactor
>>> from scipy.stats import multivariate_normal
>>> normal_pdf = lambda x1, x2: multivariate_normal.pdf((x1, x2), [0, 0], [[1, 0], [0, 1]])
>>> phi = ContinuousFactor(['x1', 'x2'], normal_pdf)
>>> phi.assignment(1, 2)
0.013064233284684921
copy()[source]

Return a copy of the distribution.

ContinuousFactor object: copy of the distribution

>>> import numpy as np
>>> from scipy.special import beta
>>> from pgmpy.factors.continuous import ContinuousFactor
# Two variable drichlet ditribution with alpha = (1,2)
>>> def dirichlet_pdf(x, y):
...     return (np.power(x, 1) * np.power(y, 2)) / beta(x, y)
>>> dirichlet_factor = ContinuousFactor(['x', 'y'], dirichlet_pdf)
>>> dirichlet_factor.variables
['x', 'y']
>>> copy_factor = dirichlet_factor.copy()
>>> copy_factor.variables
['x', 'y']
discretize(method, *args, **kwargs)[source]

Discretizes the continuous distribution into discrete probability masses using various methods.

method : A Discretizer Class from pgmpy.discretize

*args, **kwargs:
The parameters to be given to the Discretizer Class.

An n-D array or a DiscreteFactor object according to the discretiztion method used.

>>> import numpy as np
>>> from scipy.special import beta
>>> from pgmpy.factors.continuous import ContinuousFactor
>>> from pgmpy.factors.continuous import RoundingDiscretizer
>>> def dirichlet_pdf(x, y):
...     return (np.power(x, 1) * np.power(y, 2)) / beta(x, y)
>>> dirichlet_factor = ContinuousFactor(['x', 'y'], dirichlet_pdf)
>>> dirichlet_factor.discretize(RoundingDiscretizer, low=1, high=2, cardinality=5)
# TODO: finish this
divide(other, inplace=True)[source]

Gives the ContinuousFactor divide with the other factor.

other: ContinuousFactor
The ContinuousFactor to be multiplied.
ContinuousFactor or None:
if inplace=True (default) returns None if inplace=False returns a new DiscreteFactor instance.
>>> from pgmpy.factors.continuous import ContinuousFactor
>>> from scipy.stats import multivariate_normal
>>> sn_pdf1 = lambda x: multivariate_normal.pdf([x], [0], [[1]])
>>> sn_pdf2 = lambda x1,x2: multivariate_normal.pdf([x1, x2], [0, 0], [[1, 0], [0, 1]])
>>> sn1 = ContinuousFactor(['x2'], sn_pdf1)
>>> sn2 = ContinuousFactor(['x1', 'x2'], sn_pdf2)
>>> sn4 = sn2.divide(sn1, inplace=False)
>>> sn4.assignment(0, 0)
0.3989422804014327
>>> sn4 = sn2 / sn1
>>> sn4.assignment(0, 0)
0.3989422804014327
marginalize(variables, inplace=True)[source]

Maximizes the factor with respect to the given variables.

variables: list, array-like
List of variables with respect to which factor is to be maximized.
inplace: boolean
If inplace=True it will modify the factor itself, else would return a new ContinuousFactor instance.
DiscreteFactor or None: if inplace=True (default) returns None
if inplace=False returns a new ContinuousFactor instance.
>>> from pgmpy.factors.continuous import ContinuousFactor
>>> from scipy.stats import multivariate_normal
>>> std_normal_pdf = lambda *x: multivariate_normal.pdf(x, [0, 0], [[1, 0], [0, 1]])
>>> std_normal = ContinuousFactor(['x1', 'x2'], std_normal_pdf)
>>> std_normal.scope()
['x1', 'x2']
>>> std_normal.assignment([1, 1])
0.058549831524319168
>>> std_normal.marginalize(['x2'])
>>> std_normal.scope()
['x1']
>>> std_normal.assignment(1)
normalize(inplace=True)[source]

Normalizes the pdf of the continuous factor so that it integrates to 1 over all the variables.

inplace: boolean
If inplace=True it will modify the factor itself, else would return a new factor.
ContinuousFactor or None:
if inplace=True (default) returns None if inplace=False returns a new ContinuousFactor instance.
>>> from pgmpy.factors.continuous import ContinuousFactor
>>> from scipy.stats import multivariate_normal
>>> std_normal_pdf = lambda x: 2 * multivariate_normal.pdf(x, [0, 0], [[1, 0], [0, 1]])
>>> std_normal = ContinuousFactor(['x1', 'x2'], std_normal_pdf)
>>> std_normal.assignment(1, 1)
0.117099663049
>>> std_normal.normalize()
>>> std_normal.assignment(1, 1)
0.0585498315243
pdf

Returns the pdf of the ContinuousFactor.

product(other, inplace=True)[source]

Gives the ContinuousFactor product with the other factor.

other: ContinuousFactor
The ContinuousFactor to be multiplied.
ContinuousFactor or None:
if inplace=True (default) returns None if inplace=False returns a new DiscreteFactor instance.
>>> from pgmpy.factors.continuous import ContinuousFactor
>>> from scipy.stats import multivariate_normal
>>> sn_pdf1 = lambda x: multivariate_normal.pdf([x], [0], [[1]])
>>> sn_pdf2 = lambda x1,x2: multivariate_normal.pdf([x1, x2], [0, 0], [[1, 0], [0, 1]])
>>> sn1 = ContinuousFactor(['x2'], sn_pdf1)
>>> sn2 = ContinuousFactor(['x1', 'x2'], sn_pdf2)
>>> sn3 = sn1.product(sn2, inplace=False)
>>> sn3.assignment(0, 0)
0.063493635934240983
>>> sn3 = sn1 * sn2
>>> sn3.assignment(0, 0)
0.063493635934240983
reduce(values, inplace=True)[source]

Reduces the factor to the context of the given variable values.

values: list, array-like
A list of tuples of the form (variable_name, variable_value).
inplace: boolean
If inplace=True it will modify the factor itself, else would return a new ContinuosFactor object.
ContinuousFactor or None: if inplace=True (default) returns None
if inplace=False returns a new ContinuousFactor instance.
>>> import numpy as np
>>> from scipy.special import beta
>>> from pgmpy.factors.continuous import ContinuousFactor
>>> def custom_pdf(x, y, z):
...     return z*(np.power(x, 1) * np.power(y, 2)) / beta(x, y)
>>> custom_factor = ContinuousFactor(['x', 'y', 'z'], custom_pdf)
>>> custom_factor.variables
['x', 'y', 'z']
>>> custom_factor.assignment(1, 2, 3)
24.0
>>> custom_factor.reduce([('y', 2)])
>>> custom_factor.variables
['x', 'z']
>>> custom_factor.assignment(1, 3)
24.0
scope()[source]

Returns the scope of the factor.

list: List of variable names in the scope of the factor.

>>> from pgmpy.factors.continuous import ContinuousFactor
>>> from scipy.stats import multivariate_normal
>>> normal_pdf = lambda x: multivariate_normal(x, [0, 0], [[1, 0], [0, 1]])
>>> phi = ContinuousFactor(['x1', 'x2'], normal_pdf)
>>> phi.scope()
['x1', 'x2']

Joint Gaussian Distribution

class pgmpy.factors.continuous.JointGaussianDistribution.JointGaussianDistribution(variables, mean, covariance)[source]

In its most common representation, a multivariate Gaussian distribution over X1...........Xn is characterized by an n-dimensional mean vector μ, and a symmetric n x n covariance matrix Σ. This is the base class for its representation.

copy()[source]

Return a copy of the distribution.

JointGaussianDistribution: copy of the distribution

>>> import numpy as np
>>> from pgmpy.factors.continuous import JointGaussianDistribution as JGD
>>> gauss_dis = JGD(['x1', 'x2', 'x3'], np.array([[1], [-3], [4]]),
...                 np.array([[4, 2, -2], [2, 5, -5], [-2, -5, 8]]))
>>> copy_dis = gauss_dis.copy()
>>> copy_dis.variables
['x1', 'x2', 'x3']
>>> copy_dis.mean
array([[ 1],
        [-3],
        [ 4]])
>>> copy_dis.covariance
array([[ 4,  2, -2],
        [ 2,  5, -5],
        [-2, -5,  8]])
>>> copy_dis.precision_matrix
array([[ 0.3125    , -0.125     ,  0.        ],
        [-0.125     ,  0.58333333,  0.33333333],
        [ 0.        ,  0.33333333,  0.33333333]])
marginalize(variables, inplace=True)[source]

Modifies the distribution with marginalized values.

variables: iterator
List of variables over which marginalization is to be done.
inplace: boolean
If inplace=True it will modify the distribution itself, else would return a new distribution.
JointGaussianDistribution or None :
if inplace=True (default) returns None if inplace=False return a new JointGaussianDistribution instance
>>> import numpy as np
>>> from pgmpy.factors.continuous import JointGaussianDistribution as JGD
>>> dis = JGD(['x1', 'x2', 'x3'], np.array([[1], [-3], [4]]),
...             np.array([[4, 2, -2], [2, 5, -5], [-2, -5, 8]]))
>>> dis.variables
['x1', 'x2', 'x3']
>>> dis.mean
array([[ 1],
        [-3],
        [ 4]])
>>> dis.covariance
array([[ 4,  2, -2],
       [ 2,  5, -5],
       [-2, -5,  8]])
>>> dis.marginalize(['x3'])
dis.variables
['x1', 'x2']
>>> dis.mean
array([[ 1],
        [-3]]))
>>> dis.covariance
narray([[4, 2],
       [2, 5]])
precision_matrix

Returns the precision matrix of the distribution.

>>> import numpy as np
>>> from pgmpy.factors.continuous import JointGaussianDistribution as JGD
>>> dis = JGD(['x1', 'x2', 'x3'], np.array([[1], [-3], [4]]),
...             np.array([[4, 2, -2], [2, 5, -5], [-2, -5, 8]]))
>>> dis.precision_matrix
array([[ 0.3125    , -0.125     ,  0.        ],
        [-0.125     ,  0.58333333,  0.33333333],
        [ 0.        ,  0.33333333,  0.33333333]])
reduce(values, inplace=True)[source]

Reduces the distribution to the context of the given variable values.

The formula for the obtained conditional distribution is given by -

For, .. math:: N(X_j | X_i = x_i) ~ N(mu_{j.i} ; sig_{j.i})

where, .. math:: mu_{j.i} = mu_j + sig_{j, i} * {sig_{i, i}^{-1}} * (x_i - mu_i) .. math:: sig_{j.i} = sig_{j, j} - sig_{j, i} * {sig_{i, i}^{-1}} * sig_{i, j}

values: list, array-like
A list of tuples of the form (variable_name, variable_value).
inplace: boolean
If inplace=True it will modify the factor itself, else would return a new ContinuosFactor object.
JointGaussianDistribution or None:
if inplace=True (default) returns None if inplace=False returns a new JointGaussianDistribution instance.
>>> import numpy as np
>>> from pgmpy.factors.continuous import JointGaussianDistribution as JGD
>>> dis = JGD(['x1', 'x2', 'x3'], np.array([[1], [-3], [4]]),
...             np.array([[4, 2, -2], [2, 5, -5], [-2, -5, 8]]))
>>> dis.variables
['x1', 'x2', 'x3']
>>> dis.variables
['x1', 'x2', 'x3']
>>> dis.mean
array([[ 1.],
       [-3.],
       [ 4.]])
>>> dis.covariance
array([[ 4.,  2., -2.],
       [ 2.,  5., -5.],
       [-2., -5.,  8.]])
>>> dis.reduce([('x1', 7)])
>>> dis.variables
['x2', 'x3']
>>> dis.mean
array([[ 0.],
       [ 1.]])
>>> dis.covariance
array([[ 4., -4.],
       [-4.,  7.]])
to_canonical_factor()[source]

Returns an equivalent CanonicalFactor object.

The formulas for calculating the cannonical factor parameters for N(μ; Σ) = C(K; h; g) are as follows -

K = sigma^(-1) h = sigma^(-1) * mu g = -(0.5) * mu.T * sigma^(-1) * mu -

log((2*pi)^(n/2) * det(sigma)^(0.5))

where, K,h,g are the canonical factor parameters sigma is the covariance_matrix of the distribution, mu is the mean_vector of the distribution, mu.T is the transpose of the matrix mu, and det(sigma) is the determinant of the matrix sigma.

>>> import numpy as np
>>> from pgmpy.factors.continuous import JointGaussianDistribution as JGD
>>> dis = JGD(['x1', 'x2', 'x3'], np.array([[1], [-3], [4]]),
...             np.array([[4, 2, -2], [2, 5, -5], [-2, -5, 8]]))
>>> phi = dis.to_canonical_factor()
>>> phi.variables
['x1', 'x2', 'x3']
>>> phi.K
array([[0.3125, -0.125, 0.],
        [-0.125, 0.5833, 0.333],
        [     0., 0.333, 0.333]])
>>> phi.h
array([[  0.6875],
        [-0.54166],
        [ 0.33333]]))
>>> phi.g
-6.51533

Linear Gaussian CPD

class pgmpy.factors.continuous.LinearGaussianCPD.LinearGaussianCPD(variable, beta_0, variance, evidence=[], beta_vector=[])[source]

For, X -> Y the Linear Gaussian model assumes that the mean of Y is a linear function of mean of X and the variance of Y does not depend on X.

For example, p(Y|X) = N(-2x + 0.9 ; 1) Here, x is the mean of the variable X.

Let Y be a continuous variable with continuous parents X1 ............ Xk . We say that Y has a linear Gaussian CPD if there are parameters β0,.........βk and σ2 such that,

p(Y |x1.......xk) = N(β0 + x1*β1 + ......... + xk*βk ; σ2)

In vector notation,

p(Y |x) = N(β0 + β.T * x ; σ2)

copy()[source]

Return a copy of the distribution.

LinearGaussianCPD: copy of the distribution

>>> from pgmpy.factors.continuous import LinearGaussianCPD
>>> cpd = LinearGaussianCPD('Y', 0.2, 9.6, ['X1', 'X2', 'X3'], [-2, 3, 7])
>>> copy_cpd = cpd.copy()
>>> copy_cpd.variable
'Y'
>>> copy_cpd.evidence
['X1', 'X2', 'X3']

Discretizing Methods

class pgmpy.factors.continuous.discretize.BaseDiscretizer(factor, low, high, cardinality)[source]

Base class for the discretizer classes in pgmpy. The discretizer classes are used to discretize a continuous random variable distribution into discrete probability masses.

factor: A ContinuousNode or a ContinuousFactor object
the continuous node or factor representing the distribution to be discretized.
low, high: float
the range over which the function will be discretized.
cardinality: int
the number of states required in the discretized output.
>>> from scipy.stats import norm
>>> from pgmpy.factors.continuous import ContinuousNode
>>> normal = ContinuousNode(norm(0, 1).pdf)
>>> from pgmpy.discretize import BaseDiscretizer
>>> class ChildDiscretizer(BaseDiscretizer):
...     def get_discrete_values(self):
...         pass
>>> discretizer = ChildDiscretizer(normal, -3, 3, 10)
>>> discretizer.factor
<pgmpy.factors.continuous.ContinuousNode.ContinuousNode object at 0x04C98190>
>>> discretizer.cardinality
10
>>> discretizer.get_labels()
['x=-3.0', 'x=-2.4', 'x=-1.8', 'x=-1.2', 'x=-0.6', 'x=0.0', 'x=0.6', 'x=1.2', 'x=1.8', 'x=2.4']
get_discrete_values()[source]

This method implements the algorithm to discretize the given continuous distribution.

It must be implemented by all the subclasses of BaseDiscretizer.

A list of discrete values or a DiscreteFactor object.

get_labels()[source]

Returns a list of strings representing the values about which the discretization method calculates the probabilty masses.

Default value is the points - [low, low+step, low+2*step, ......... , high-step] unless the method is overridden by a subclass.

>>> from pgmpy.factors import ContinuousNode
>>> from pgmpy.discretize import BaseDiscretizer
>>> class ChildDiscretizer(BaseDiscretizer):
...     def get_discrete_values(self):
...         pass
>>> from scipy.stats import norm
>>> node = ContinuousNode(norm(0).pdf)
>>> child = ChildDiscretizer(node, -5, 5, 20)
>>> chld.get_labels()
['x=-5.0', 'x=-4.5', 'x=-4.0', 'x=-3.5', 'x=-3.0', 'x=-2.5',
 'x=-2.0', 'x=-1.5', 'x=-1.0', 'x=-0.5', 'x=0.0', 'x=0.5', 'x=1.0',
 'x=1.5', 'x=2.0', 'x=2.5', 'x=3.0', 'x=3.5', 'x=4.0', 'x=4.5']
class pgmpy.factors.continuous.discretize.RoundingDiscretizer(factor, low, high, cardinality)[source]

This class uses the rounding method for discretizing the given continuous distribution.

For the rounding method,

The probability mass is, cdf(x+step/2)-cdf(x), for x = low

cdf(x+step/2)-cdf(x-step/2), for low < x <= high

where, cdf is the cumulative density function of the distribution and step = (high-low)/cardinality.

>>> import numpy as np
>>> from pgmpy.factors.continuous import ContinuousNode
>>> from pgmpy.factors.continuous import RoundingDiscretizer
>>> std_normal_pdf = lambda x : np.exp(-x*x/2) / (np.sqrt(2*np.pi))
>>> std_normal = ContinuousNode(std_normal_pdf)
>>> std_normal.discretize(RoundingDiscretizer, low=-3, high=3,
...                       cardinality=12)
[0.001629865203424451, 0.009244709419989363, 0.027834684208773178,
 0.065590616803038182, 0.120977578710013, 0.17466632194020804,
 0.19741265136584729, 0.17466632194020937, 0.12097757871001302,
 0.065590616803036905, 0.027834684208772664, 0.0092447094199902269]
class pgmpy.factors.continuous.discretize.UnbiasedDiscretizer(factor, low, high, cardinality)[source]

This class uses the unbiased method for discretizing the given continuous distribution.

The unbiased method for discretization is the matching of the first moment method. It involves calculating the first order limited moment of the distribution which is done by the _lim_moment method.

For this method,

The probability mass is, (E(x) - E(x + step))/step + 1 - cdf(x), for x = low

(2 * E(x) - E(x - step) - E(x + step))/step, for low < x < high

(E(x) - E(x - step))/step - 1 + cdf(x), for x = high

where, E(x) is the first limiting moment of the distribution about the point x, cdf is the cumulative density function and step = (high-low)/cardinality.

Klugman, S. A., Panjer, H. H. and Willmot, G. E., Loss Models, From Data to Decisions, Fourth Edition, Wiley, section 9.6.5.2 (Method of local monment matching) and exercise 9.41.

>>> import numpy as np
>>> from pgmpy.factors import ContinuousNode
>>> from pgmpy.factors.continuous import UnbiasedDiscretizer
# exponential distribution with rate = 2
>>> exp_pdf = lambda x: 2*np.exp(-2*x) if x>=0 else 0
>>> exp_node = ContinuousNode(exp_pdf)
>>> exp_node.discretize(UnbiasedDiscretizer, low=0, high=5, cardinality=10)
[0.39627368905806137, 0.4049838434034298, 0.13331784003148325,
 0.043887287876647259, 0.014447413395300212, 0.0047559685431339703,
 0.0015656350182896128, 0.00051540201980112557, 0.00016965346326140994,
 3.7867260839208328e-05]