ankurankan on dev
factor_product returns a copy o… (compare)
get_model
method to get the actual BayesianNetwork object. Here's an example: https://pgmpy.org/readwrite/bif.html#pgmpy.readwrite.BIF.BIFReader.get_model . Alternatively, you could just use the load method to get the model: https://pgmpy.org/models/bayesiannetwork.html#pgmpy.models.BayesianNetwork.BayesianNetwork.load
DBNInference.query
method if you are looking to get prediction probabilities instead of forward_inference
. The forward_inference method is the first part of running belief propagation, the query method handles both forward and backward inference automatically. No, only discrete variables are supported for DBNs right now.
Hi, is there a way to initialize a bayesian network with independent nodes and state information? psuedo code e.g.
model.add_node("weather", {"good","bad"})
model.add_node("wind strength", {"strong", "weak})
after initializing the nodes should all be independent and the states probability all equal weighted.
How can I do this in pgmpy? Thanks!
In [1]: from pgmpy.models import BayesianNetwork
In [2]: model = BayesianNetwork()
In [3]: model.add_nodes_from(['weather', 'wind'])
In [4]: cpd_weather = TabularCPD('weather', 2, [[0.5], [0.5]])
In [5]: cpd_wind = TabularCPD('wind', 2, [[0.5], [0.5]])
In [6]: model.add_cpds(cpd_weather, cpd_wind)
In [7]: model.nodes()
Out[7]: NodeView(('weather', 'wind'))
In [8]: model.cpds
Out[8]:
[<TabularCPD representing P(weather:2) at 0x7fa4e22976d0>,
<TabularCPD representing P(wind:2) at 0x7fa4e2297940>]
Hi, I am trying to learn BN recently with pgmpy and bnlearn. I am just wondering is there a way to do these things:
Thanks!
Hi @ankurankan , is there a way to speed up Gibbs sampling with a MarkovNetwork
? e.g. I want to sample from an Ising model using the following code but it hangs at the gibbs_chain=GibbsSampling(G)
line
n=20
thetas=np.random.rand(n)
G = MarkovNetwork()
G.add_nodes_from([str(i) for i in range(n)])
# add theta factors
theta_factors=[DiscreteFactor( [str(i)], [2], [np.exp(-1*thetas[i]), np.exp(thetas[i])] ) for i in range(n)]
G.add_factors(*theta_factors)
gibbs_chain = GibbsSampling(G)
samples=gibbs_chain.sample(size=1000).to_numpy()
Thank you so much
@anna_jingwen:matrix.org There's no direct function to compute that but you can essentially compute it using: P(A, B|C) = P(A, B, C) / P(C). In code it would look something like this:
In [30]: from pgmpy.utils import get_example_model
In [31]: from pgmpy.inference import VariableElimination
In [32]: model = get_example_model('alarm')
In [33]: infer = VariableElimination(model)
In [34]: joint_p = infer.query(variables=['VENTLUNG', 'VENTALV', 'ARTCO2'])
Finding Elimination Order: : 100%|███████████████████████████████████████████████████████████████████████████████████████| 6/6 [00:00<00:00, 6636.56it/s]
Eliminating: INTUBATION: 100%|███████████████████████████████████████████████████████████████████████████████████████████| 6/6 [00:00<00:00, 1751.76it/s]
In [35]: p_artco2 = joint_p.marginalize(['VENTLUNG', 'VENTALV'], inplace=False)
In [36]: conditional_p = joint_p / p_artco2
In [37]: conditional_p.get_value(VENTLUNG='LOW', VENTALV='NORMAL', ARTCO2='LOW')
Out[37]: 0.0004884522839265131
# Verify that the value matches when doing a simple inference with evidence
In [39]: infer.query(variables=['VENTLUNG', 'VENTALV'], evidence={'ARTCO2': 'LOW'}).get_value(VENTLUNG='LOW', VENTALV='NORMAL')
Finding Elimination Order: : 100%|██████████████████████████████████████████████████████████████████████████████████████| 6/6 [00:00<00:00, 10819.36it/s]
Eliminating: INTUBATION: 100%|███████████████████████████████████████████████████████████████████████████████████████████| 6/6 [00:00<00:00, 2728.89it/s]
Out[39]: 0.0004884522839265131
Here conditional_p
would be a DiscreteFactor
object instead of TabularCPD
as TabularCPD can't represent joint distributions.
DBNInference
(https://pgmpy.org/exact_infer/dbn_infer.html?highlight=dbninference#pgmpy.inference.dbn_inference.DBNInference) to make queries on dynamic BNs.
@824761521 Yes, it should be something like this:
# Define a random network
In [30]: from pgmpy.models import DynamicBayesianNetwork as DBN
...: from pgmpy.factors.discrete import TabularCPD
...: dbn = DBN([(("D", 0), ("G", 0)), (("I", 0), ("G", 0)),
...: (("D", 0), ("D", 1)), (("I", 0), ("I", 1)),])
...: diff_cpd = TabularCPD(("D", 0), 2, [[0.6], [0.4]])
...: grade_cpd = TabularCPD(variable=("G", 0), variable_card=3,
...: values=[[0.3, 0.05, 0.9, 0.5],
...: [0.4, 0.25, 0.08, 0.3],
...: [0.3, 0.7, 0.02, 0.2]],
...: evidence=[("I", 0), ("D", 0)],
...: evidence_card=[2, 2])
...: d_i_cpd = TabularCPD(variable=("D", 1), variable_card=2,
...: values=[[0.6, 0.3], [0.4, 0.7]],
...: evidence=[("D", 0)],
...: evidence_card=[2])
...: intel_cpd = TabularCPD(("I", 0), 2, [[0.7], [0.3]])
...: i_i_cpd = TabularCPD(variable=("I", 1), variable_card=2,
...: values=[[0.5, 0.4], [0.5, 0.6]],
...: evidence=[("I", 0)],
...: evidence_card=[2])
...: g_i_cpd = TabularCPD(variable=("G", 1), variable_card=3,
...: values=[[0.3, 0.05, 0.9, 0.5],
...: [0.4, 0.25, 0.08, 0.3],
...: [0.3, 0.7, 0.02, 0.2]],
...: evidence=[("I", 1), ("D", 1)],
...: evidence_card=[2, 2])
...: dbn.add_cpds(diff_cpd, grade_cpd, d_i_cpd, intel_cpd, i_i_cpd, g_i_cpd)
# Sample from the network
In [31]: samples = dbn.simulate(n_time_slices=10, n_samples=int(1e4))
0%| | 0/30 [00:00<?, ?it/s]
# Select samples relevant to our query. Let's say we want to query: P((I, 2) | (I, 0) = 1, (D, 1) = 0)
In [33]: rel_samples = samples.loc[(samples.loc[:, [('I', 0)]] == 1).values & (samples.loc[:, [('D', 1)]] == 0).values, [('I', 2)]]
# Then probability values can be calculated as:
In [40]: rel_samples.value_counts() / rel_samples.shape[0]
Out[40]:
(I, 2)
1 0.568954
0 0.431046
dtype: float64
Depending on how many queries you want to do you can also specify the evidence directly in the simulate method, and it will directly generate rel_samples
with the downside that you will have to regenerate the samples if you want to specify a different evidence. Also, since this method will give approximate results, you can increase the accuracy by increasing the number of samples generated.
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-1b4b423c17da> in <module>
2 from pgmpy.models.BayesianModel import BayesianModel
3 #from pgmpy.inference.causal_inference import CausalInference
----> 4 import CausalInference
ModuleNotFoundError: No module named 'CausalInference'
from pgmpy.inference import CausalInference