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

results of Using gibss sampling method to approximately infer in bayesian network seems wrong #1715

Open
xy-whu opened this issue Dec 25, 2023 · 1 comment

Comments

@xy-whu
Copy link

xy-whu commented Dec 25, 2023

Subject of the issue

Describe your issue here.
I want to use alarm example to approximately infer in bayesian network. Burglary, JohnCall,Marycall,Earthquake, Alarm which are denoted by B,J,M,E,A. My goal is to infer P(B=True | M =True,J=True)

I tried three methods, that is, variable eliminate, approximate inference with bayesian network, approximate inference with
gibbs sampling. The problem is the results of variable eliminate and approximate inference with bayesian network are similar,

but the results of gibbs sampling and variable eliminate are too far apart

Your environment

  • pgmpy 0.1.24
  • Python 3.11
  • win 11

Steps to reproduce

Tell us how to reproduce this issue. Please provide a minimal reproducible code of
the issue you are facing if possible.

from pgmpy.models import BayesianNetwork
from pgmpy.factors.discrete import TabularCPD
from pgmpy.inference import VariableElimination
if __name__=="__main__":
  model=BayesianNetwork([['B','A'],['E','A'],['A','J'],['A','M']])
  B_cpd = TabularCPD('B', 2, [[0.001],[0.999]],
                          state_names={'B': ['T', 'F']})
  E_cpd = TabularCPD('E', 2, [[0.002],[0.998]],
                          state_names={'E': ['T', 'F']})
  A_cpd = TabularCPD('A', 2, [[0.95, 0.94,0.29,0.001], [0.05,0.06,0.71,0.999]],
                     evidence=['B',"E"],
                     evidence_card=[2,2],
                     state_names={'A': ['T', 'F'],
                                  'B': ['T', 'F'],
                                  'E': ['T', 'F']}
                     )
  model.add_cpds(E_cpd)
  model.add_cpds(B_cpd)
  model.add_cpds(A_cpd)
  J_cpd = TabularCPD('J', 2, [[0.9, 0.05], [0.1,0.95]],
                     evidence=['A'],
                     evidence_card=[2],
                     state_names={'A': ['T', 'F'],
                                  'J': ['T', 'F']}
                     )
  M_cpd = TabularCPD('M', 2, [[0.7, 0.01], [0.3,0.99]],
                     evidence=['A'],
                     evidence_card=[2],
                     state_names={'A': ['T', 'F'],
                                  'M': ['T', 'F']}
                     )
  model.add_cpds(J_cpd)
  model.add_cpds(M_cpd)
  inference = VariableElimination(model)
  ev_query = inference.query(variables=["B"],evidence={"M":"T","J":"T"})
  print(ev_query)

  from pgmpy.inference import ApproxInference

  infer = ApproxInference(model)
  approx_query1 = infer.query(variables=["B"],evidence={"M":"T","J":"T"})
  print(approx_query1)
  from pgmpy.sampling import GibbsSampling
  gibbs = GibbsSampling(model)
  sampledata=gibbs.sample(size=int(1e4))
  approx_query2 = infer.query(variables=["B"],evidence={"M":"T","J":"T"},samples=sampledata)
  print(approx_query2)

Expected behaviour

Tell us what should happen

The results of three methods should be similar, but the value of gibbs sampling method is to far from values of the methods of variable eliminate and approximate inference in bayesian network

Actual behaviour

Tell us what happens instead
+------+----------+
| B | phi(B) |
+======+==========+
| B(T) | 0.2842 |
+------+----------+
| B(F) | 0.7158 |
+------+----------+

0%| | 0/10000 [00:00<?, ?it/s]
32%|███▏ | 3160/10000 [00:03<00:07, 908.78it/s]
52%|█████▏ | 5225/10000 [00:05<00:05, 897.69it/s]
68%|██████▊ | 6798/10000 [00:07<00:03, 926.25it/s]
77%|███████▋ | 7740/10000 [00:08<00:02, 925.41it/s]
85%|████████▍ | 8488/10000 [00:09<00:01, 934.74it/s]
89%|████████▉ | 8948/10000 [00:09<00:01, 933.59it/s]
93%|█████████▎| 9293/10000 [00:10<00:00, 940.95it/s]
95%|█████████▌| 9531/10000 [00:10<00:00, 917.98it/s]
97%|█████████▋| 9663/10000 [00:10<00:00, 882.24it/s]
98%|█████████▊| 9770/10000 [00:10<00:00, 888.52it/s]
99%|█████████▉| 9901/10000 [00:10<00:00, 874.01it/s]
100%|█████████▉| 9990/10000 [00:10<00:00, 823.80it/s]
100%|██████████| 10000/10000 [00:11<00:00, 899.67it/s]
+------+----------+
| B | phi(B) |
+======+==========+
| B(F) | 0.7174 |
+------+----------+
| B(T) | 0.2826 |
+------+----------+
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers

0%| | 0/9999 [00:00<?, ?it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

3%|▎ | 297/9999 [00:00<00:03, 2750.53it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

6%|▌ | 573/9999 [00:00<00:06, 1500.41it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

8%|▊ | 801/9999 [00:00<00:05, 1671.97it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

11%|█ | 1083/9999 [00:00<00:05, 1633.98it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

13%|█▎ | 1340/9999 [00:00<00:04, 1869.27it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

15%|█▌ | 1546/9999 [00:00<00:05, 1603.54it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

18%|█▊ | 1807/9999 [00:01<00:04, 1844.53it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

20%|██ | 2011/9999 [00:01<00:04, 1729.60it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

22%|██▏ | 2197/9999 [00:01<00:06, 1260.39it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

23%|██▎ | 2348/9999 [00:01<00:05, 1279.79it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

25%|██▍ | 2494/9999 [00:01<00:06, 1179.53it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

26%|██▌ | 2624/9999 [00:01<00:06, 1122.62it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

27%|██▋ | 2746/9999 [00:01<00:06, 1098.25it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

29%|██▊ | 2861/9999 [00:02<00:06, 1066.99it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

30%|██▉ | 2971/9999 [00:02<00:06, 1034.36it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

31%|███ | 3077/9999 [00:02<00:06, 1003.18it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

32%|███▏ | 3203/9999 [00:02<00:06, 992.43it/s] WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

34%|███▍ | 3448/9999 [00:02<00:04, 1341.73it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

36%|███▌ | 3607/9999 [00:02<00:05, 1253.55it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

37%|███▋ | 3737/9999 [00:02<00:05, 1051.09it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

40%|███▉ | 3964/9999 [00:02<00:04, 1329.32it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

41%|████ | 4112/9999 [00:03<00:04, 1267.16it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

42%|████▏ | 4249/9999 [00:03<00:05, 1076.02it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

44%|████▎ | 4367/9999 [00:03<00:05, 966.09it/s] WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

46%|████▌ | 4588/9999 [00:03<00:04, 1240.42it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

47%|████▋ | 4728/9999 [00:03<00:04, 1188.22it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

49%|████▊ | 4858/9999 [00:03<00:05, 952.99it/s] WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

50%|████▉ | 4967/9999 [00:03<00:05, 947.66it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

52%|█████▏ | 5219/9999 [00:04<00:03, 1304.49it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

55%|█████▍ | 5487/9999 [00:04<00:02, 1635.94it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

57%|█████▋ | 5671/9999 [00:04<00:02, 1510.84it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

58%|█████▊ | 5838/9999 [00:04<00:03, 1276.73it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

60%|█████▉ | 5982/9999 [00:04<00:03, 1046.70it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

62%|██████▏ | 6234/9999 [00:04<00:02, 1346.20it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

64%|██████▍ | 6394/9999 [00:04<00:02, 1270.93it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

66%|██████▌ | 6574/9999 [00:05<00:02, 1388.02it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

67%|██████▋ | 6729/9999 [00:05<00:02, 1275.30it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

69%|██████▉ | 6885/9999 [00:05<00:02, 1255.64it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

71%|███████ | 7099/9999 [00:05<00:02, 1325.70it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

72%|███████▏ | 7238/9999 [00:05<00:02, 1135.72it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

74%|███████▍ | 7390/9999 [00:05<00:02, 1142.69it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

75%|███████▌ | 7510/9999 [00:05<00:02, 1079.68it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

77%|███████▋ | 7724/9999 [00:06<00:01, 1197.79it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

79%|███████▊ | 7871/9999 [00:06<00:01, 1179.47it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

80%|████████ | 8042/9999 [00:06<00:01, 1206.46it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

82%|████████▏ | 8217/9999 [00:06<00:01, 1233.98it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

84%|████████▍ | 8416/9999 [00:06<00:01, 1291.81it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

86%|████████▌ | 8596/9999 [00:06<00:01, 1289.70it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

87%|████████▋ | 8739/9999 [00:06<00:01, 1232.01it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

89%|████████▊ | 8863/9999 [00:07<00:00, 1138.66it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

90%|█████████ | 9016/9999 [00:07<00:00, 1147.33it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

91%|█████████▏| 9131/9999 [00:07<00:01, 834.77it/s] WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

93%|█████████▎| 9310/9999 [00:07<00:00, 951.12it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

94%|█████████▍| 9415/9999 [00:07<00:00, 952.06it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

96%|█████████▌| 9609/9999 [00:07<00:00, 1091.38it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

98%|█████████▊| 9776/9999 [00:07<00:00, 1153.32it/s]WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.
WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: 1.1102230246251565e-16. Adjusting values.

100%|█████████▉| 9973/9999 [00:08<00:00, 1238.18it/s]
100%|██████████| 9999/9999 [00:08<00:00, 1237.33it/s]
+------+----------+
| B | phi(B) |
+======+==========+
| B(1) | 0.9974 |
+------+----------+
| B(0) | 0.0026 |
+------+----------+

@xy-whu
Copy link
Author

xy-whu commented Dec 26, 2023

Another minimal working example:

from pgmpy.models import BayesianNetwork
from pgmpy.factors.discrete import TabularCPD
from pgmpy.inference import VariableElimination
if __name__=="__main__":
  model=BayesianNetwork([['B','A'],['E','A'],['A','J'],['A','M']])
  B_cpd = TabularCPD('B', 2, [[0.001],[0.999]],
                          state_names={'B': ['T', 'F']})
  E_cpd = TabularCPD('E', 2, [[0.002],[0.998]],
                          state_names={'E': ['T', 'F']})
  A_cpd = TabularCPD('A', 2, [[0.95, 0.94,0.29,0.001], [0.05,0.06,0.71,0.999]],
                     evidence=['B',"E"],
                     evidence_card=[2,2],
                     state_names={'A': ['T', 'F'],
                                  'B': ['T', 'F'],
                                  'E': ['T', 'F']}
                     )
  model.add_cpds(E_cpd)
  model.add_cpds(B_cpd)
  model.add_cpds(A_cpd)
  J_cpd = TabularCPD('J', 2, [[0.9, 0.05], [0.1,0.95]],
                     evidence=['A'],
                     evidence_card=[2],
                     state_names={'A': ['T', 'F'],
                                  'J': ['T', 'F']}
                     )
  M_cpd = TabularCPD('M', 2, [[0.7, 0.01], [0.3,0.99]],
                     evidence=['A'],
                     evidence_card=[2],
                     state_names={'A': ['T', 'F'],
                                  'M': ['T', 'F']}
                     )
  model.add_cpds(J_cpd)
  model.add_cpds(M_cpd)
  inference = VariableElimination(model)
  ev_query = inference.query(variables=["B"],evidence={"M":"T","J":"T"})
  print(ev_query)
  from pgmpy.inference import ApproxInference
  infer = ApproxInference(model)
  from pgmpy.sampling import GibbsSampling
  gibbs = GibbsSampling(model)
  sampledata=gibbs.sample(size=int(7),include_latents=True,seed=123)
  print(sampledata)
  approx_query2 = infer.query(variables=["B"],evidence={"M":"T","J":"T"},samples=sampledata,seed=1123)
  print(approx_query2)
  approx_query2 = infer.query(variables=["B"],samples=sampledata,seed=1123)
  print(approx_query2)
  

+------+----------+
| B | phi(B) |
+======+==========+
| B(T) | 0.2842 |
+------+----------+
| B(F) | 0.7158 |
+------+----------+
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
WARNING:pgmpy:Found unknown state name. Trying to switch to using all state names as state numbers
100%|██████████████████████████████████████████████████████████████████████████████████| 6/6 [00:00<00:00, 6017.65it/s]
The following is a dataframe returned by gibbs sample

B A E J M
0 0 1 0 1 0
1 1 0 0 0 1
2 1 1 1 1 1
3 1 1 1 1 1
4 1 1 1 1 1
5 1 1 1 1 1
6 1 1 1 1 1

The following is the P(B | M=1,J=1)
+------+----------+
| B | phi(B) |
+======+==========+
| B(0) | 0.1429 |
+------+----------+
| B(1) | 0.8571 |
+------+----------+

The following is the P(B )
+------+----------+
| B | phi(B) |
+======+==========+
| B(0) | 0.1429 |
+------+----------+
| B(1) | 0.8571 |
+------+----------+
All in all, whether given evidence or not does not affect the value of query P(B)?

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

No branches or pull requests

1 participant