Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transient exception when using exact inference #1691

Open
fcmwork opened this issue Oct 26, 2023 · 3 comments
Open

Transient exception when using exact inference #1691

fcmwork opened this issue Oct 26, 2023 · 3 comments
Assignees
Labels

Comments

@fcmwork
Copy link

fcmwork commented Oct 26, 2023

Subject of the issue

Transient exception when using exact inference
I am trying to perform exact inference in a Bayesian network using the Junction Tree algporithm.
However I encounter a transient error when performing the infer.query() call, apparently showing that the subtrees do not have a correct mapping between state name -> state index. Sometime it works, sometimes not, with a rate of about 50% for me. Both pgmpy=0.1.24 and pgmpy==0.1.23

Your environment

  • pgmpy version: both pgmpy=0.1.24 and pgmpy==0.1.23
  • Python version: 3.8, 3.9, 3.10
  • Operating System: Ubuntu 22.04

Steps to reproduce

The code is taken from the example notebook and is provided below for reproduction.

from pgmpy.inference import BeliefPropagation
from pgmpy.utils import get_example_model

if __name__ == '__main__':
    asia_model = get_example_model('asia')
    algo =BeliefPropagation
    asia_infer = algo(asia_model)
    # Computing the probability of bronc given smoke=no.
    q = asia_infer.query(variables=["bronc", "asia"], evidence={"smoke": "yes"})
    print(q)
    pass

Expected behaviour

proper return giving the result of the query
+------------+---------+-------------------+
| bronc | asia | phi(bronc,asia) |
+============+=========+===================+
| bronc(yes) | asia(0) | 0.0060 |
+------------+---------+-------------------+
| bronc(yes) | asia(1) | 0.5940 |
+------------+---------+-------------------+
| bronc(no) | asia(0) | 0.0040 |
+------------+---------+-------------------+
| bronc(no) | asia(1) | 0.3960 |
+------------+---------+-------------------+

Actual behaviour

Not systematically but frequently (about 25% of the attempt) I get the following exception. Note that this happens with various forms of query, but seems to get more frequent with more query variables and can reach up to 75%. Note that the example exception is obtained with pgmpy==0.1.24 but is the same with 0.1.23 and also with other python versions

/home/franckm/WORKSPACE/WK_PYPGM_TEST/pypgm_test2/myenv3_8_pgmpy24/lib/python3.8/site-packages/pgmpy/factors/discrete/DiscreteFactor.py:799: RuntimeWarning: invalid value encountered in divide
  phi.values = phi.values / phi1.values
Traceback (most recent call last):
  File "./inference_basic.py", line 14, in <module>
    q = asia_infer.query(variables=["bronc", "asia"], evidence={"smoke": "yes"})
  File "/home/franckm/WORKSPACE/WK_PYPGM_TEST/pypgm_test2/myenv3_8_pgmpy24/lib/python3.8/site-packages/pgmpy/inference/ExactInference.py", line 1121, in query
    result = self._query(
  File "/home/franckm/WORKSPACE/WK_PYPGM_TEST/pypgm_test2/myenv3_8_pgmpy24/lib/python3.8/site-packages/pgmpy/inference/ExactInference.py", line 1022, in _query
    return variable_elimination.query(
  File "/home/franckm/WORKSPACE/WK_PYPGM_TEST/pypgm_test2/myenv3_8_pgmpy24/lib/python3.8/site-packages/pgmpy/inference/ExactInference.py", line 338, in query
    indexer[index] = phi.get_state_no(
  File "/home/franckm/WORKSPACE/WK_PYPGM_TEST/pypgm_test2/myenv3_8_pgmpy24/lib/python3.8/site-packages/pgmpy/utils/state_name.py", line 72, in get_state_no
    return self.name_to_no[var][state_name]
KeyError: 'yes'
@ankurankan ankurankan added the Bug label Oct 30, 2023
@ankurankan
Copy link
Member

@fcmwork Thanks for reporting this. This does seem like a bug; I will have a look at it. In the meantime, you could also use Variable Elimination for inference.

@ankurankan ankurankan self-assigned this Oct 30, 2023
@fcmwork
Copy link
Author

fcmwork commented Oct 30, 2023

@ankurankan thank you for your prompt answer. Have you been able to reproduce it? I am wondering if you are willing to share a full pipdeptree of your testing environment I could try to figure out if there are any differences.

@ankurankan
Copy link
Member

@fcmwork I am using the latest versions of all the dependencies. I have been able to reproduce it, the issue seems to be in the initialization of factors of the JunctionTree. The DiscreteFactors in the JunctionTree seem to not have all the state names.

In [4]: asia_infer.junction_tree
Out[4]: <pgmpy.models.JunctionTree.JunctionTree at 0x7f3111e1eb00>

In [5]: asia_infer.junction_tree.nodes()
Out[5]: NodeView((('bronc', 'dysp', 'either'), ('bronc', 'xray', 'lung', 'either', 'tub'), ('bronc', 'xray', 'lung', 'asia', 'tub'), ('bronc', 'xray', 'lung', 'asia', 'smoke')))

In [7]: asia_infer.junction_tree.factors
Out[7]: 
[<DiscreteFactor representing phi(bronc:2, dysp:2, either:2) at 0x7f3111e5d660>,
 <DiscreteFactor representing phi(lung:2, bronc:2, xray:2, either:2, tub:2) at 0x7f3111e5c040>,
 <DiscreteFactor representing phi(lung:2, bronc:2, asia:2, xray:2, tub:2) at 0x7f3111e5c160>,
 <DiscreteFactor representing phi(lung:2, bronc:2, asia:2, xray:2, smoke:2) at 0x7f3111e5cee0>]

In [8]: asia_infer.junction_tree.factors[0]
Out[8]: <DiscreteFactor representing phi(bronc:2, dysp:2, either:2) at 0x7f3111e5d660>

In [9]: asia_infer.junction_tree.factors[0].state_names
Out[9]: {'bronc': ['yes', 'no'], 'dysp': ['yes', 'no'], 'either': ['yes', 'no']}

In [10]: asia_infer.junction_tree.factors[1].state_names
Out[10]: 
{'bronc': [0, 1],
 'xray': ['yes', 'no'],
 'lung': ['yes', 'no'],
 'either': ['yes', 'no'],
 'tub': ['yes', 'no']}

In [11]: asia_infer.junction_tree.factors[2].state_names
Out[11]: 
{'bronc': [0, 1],
 'xray': [0, 1],
 'lung': [0, 1],
 'asia': ['yes', 'no'],
 'tub': ['yes', 'no']}

In [12]: asia_infer.junction_tree.factors[3].state_names
Out[12]: 
{'bronc': ['yes', 'no'],
 'xray': [0, 1],
 'lung': ['yes', 'no'],
 'asia': [0, 1],
 'smoke': ['yes', 'no']}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants