Dynamic Bayesian Network Inference

class pgmpy.inference.dbn_inference.DBNInference(model)[source]
backward_inference(variables, evidence=None)[source]

Backward inference method using belief propagation.

Parameters
  • variables (list) – list of variables for which you want to compute the probability

  • evidence (dict) – a dict key, value pair as {var: state_of_var_observed} None if no evidence

Examples

>>> from pgmpy.factors.discrete import TabularCPD
>>> from pgmpy.models import DynamicBayesianNetwork as DBN
>>> from pgmpy.inference import DBNInference
>>> dbnet = DBN()
>>> dbnet.add_edges_from([(('Z', 0), ('X', 0)), (('X', 0), ('Y', 0)),
...                       (('Z', 0), ('Z', 1))])
>>> z_start_cpd = TabularCPD(('Z', 0), 2, [[0.5], [0.5]])
>>> x_i_cpd = TabularCPD(('X', 0), 2, [[0.6, 0.9],
...                                    [0.4, 0.1]],
...                      evidence=[('Z', 0)],
...                      evidence_card=[2])
>>> y_i_cpd = TabularCPD(('Y', 0), 2, [[0.2, 0.3],
...                                    [0.8, 0.7]],
...                      evidence=[('X', 0)],
...                      evidence_card=[2])
>>> z_trans_cpd = TabularCPD(('Z', 1), 2, [[0.4, 0.7],
...                                        [0.6, 0.3]],
...                      evidence=[('Z', 0)],
...                      evidence_card=[2])
>>> dbnet.add_cpds(z_start_cpd, z_trans_cpd, x_i_cpd, y_i_cpd)
>>> dbnet.initialize_initial_state()
>>> dbn_inf = DBNInference(dbnet)
>>> dbn_inf.backward_inference([('X', 0)], {('Y', 0):0, ('Y', 1):1, ('Y', 2):1})[('X', 0)].values
array([0.66594382, 0.33405618])
forward_inference(variables, evidence=None, args=None)[source]

Forward inference method using belief propagation.

Parameters
  • variables (list) – list of variables for which you want to compute the probability

  • evidence (dict) – a dict key, value pair as {var: state_of_var_observed} None if no evidence

  • Examples

  • --------

  • TabularCPD (>>> from pgmpy.factors.discrete import) –

  • DBN (>>> from pgmpy.models import DynamicBayesianNetwork as) –

  • DBNInference (>>> from pgmpy.inference import) –

  • DBN() (>>> dbnet =) –

  • dbnet.add_edges_from([(('Z' (>>>) –

  • 0)

  • ('X'

  • 0))

  • (('X'

  • 0)

  • ('Y'

  • 0))

:param : :param … ((‘Z’: :param 0): :param (‘Z’: :param 1))]): :param >>> z_start_cpd = TabularCPD((‘Z’: :param 0): :param 2: :param [[0.5]: :param [0.5]]): :param >>> x_i_cpd = TabularCPD((‘X’: :param 0): :param 2: :param [[0.6: :param 0.9]: :param : :param … [0.4: :param 0.1]]: :param : :param … evidence=[(‘Z’: :param 0)]: :param : :param … evidence_card=[2]): :param >>> y_i_cpd = TabularCPD((‘Y’: :param 0): :param 2: :param [[0.2: :param 0.3]: :param : :param … [0.8: :param 0.7]]: :param : :param … evidence=[(‘X’: :param 0)]: :param : :param … evidence_card=[2]): :param >>> z_trans_cpd = TabularCPD((‘Z’: :param 1): :param 2: :param [[0.4: :param 0.7]: :param : :param … [0.6: :param 0.3]]: :param : :param … evidence=[(‘Z’: :param 0)]: :param : :param … evidence_card=[2]): :param >>> dbnet.add_cpds(z_start_cpd: :param z_trans_cpd: :param x_i_cpd: :param y_i_cpd): :param >>> dbnet.initialize_initial_state(): :param >>> dbn_inf = DBNInference(dbnet): :param >>> dbn_inf.forward_inference([(‘X’: :type >>> dbn_inf.forward_inference([(‘X’: 1, (‘Y’, 1):0, (‘Y’, 2):1})[(‘X’, 2)].values :param 2)]: :type 2)]: 1, (‘Y’, 1):0, (‘Y’, 2):1})[(‘X’, 2)].values :param {(‘Y’: :type {(‘Y’: 1, (‘Y’, 1):0, (‘Y’, 2):1})[(‘X’, 2)].values :param 0): :type 0): 1, (‘Y’, 1):0, (‘Y’, 2):1})[(‘X’, 2)].values :param array([0.76738736: :param 0.23261264]):

query(variables, evidence=None, args='exact')[source]

Query method for Dynamic Bayesian Network using Interface Algorithm.

Parameters
  • variables (list) – list of variables for which you want to compute the probability

  • evidence (dict) – a dict key, value pair as {var: state_of_var_observed} None if no evidence

Examples

>>> from pgmpy.factors.discrete import TabularCPD
>>> from pgmpy.models import DynamicBayesianNetwork as DBN
>>> from pgmpy.inference import DBNInference
>>> dbnet = DBN()
>>> dbnet.add_edges_from([(('Z', 0), ('X', 0)), (('X', 0), ('Y', 0)),
...                       (('Z', 0), ('Z', 1))])
>>> z_start_cpd = TabularCPD(('Z', 0), 2, [[0.5], [0.5]])
>>> x_i_cpd = TabularCPD(('X', 0), 2, [[0.6, 0.9],
...                                    [0.4, 0.1]],
...                      evidence=[('Z', 0)],
...                      evidence_card=[2])
>>> y_i_cpd = TabularCPD(('Y', 0), 2, [[0.2, 0.3],
...                                    [0.8, 0.7]],
...                      evidence=[('X', 0)],
...                      evidence_card=[2])
>>> z_trans_cpd = TabularCPD(('Z', 1), 2, [[0.4, 0.7],
...                                        [0.6, 0.3]],
...                      evidence=[('Z', 0)],
...                      evidence_card=[2])
>>> dbnet.add_cpds(z_start_cpd, z_trans_cpd, x_i_cpd, y_i_cpd)
>>> dbnet.initialize_initial_state()
>>> dbn_inf = DBNInference(dbnet)
>>> dbn_inf.query([('X', 0)], {('Y', 0):0, ('Y', 1):1, ('Y', 2):1})[('X', 0)].values
array([0.66594382, 0.33405618])