基于PYMC3与NetworkX的三分类变量贝叶斯推理问题
Alright, let's tackle this Bayesian network problem step by step. I get that you've stripped down the model to just three categorical variables D1, D2, D3, have their probability tables ready, and need to compute the posterior probabilities of D1 and D2 given D3=0. You tried modifying an existing code snippet but hit a wall—let's fix that.
First, Let's Formalize the Problem
Assuming your Bayesian network structure has D3 dependent on both D1 and D2 (a common setup for three variables), we'll use standard Bayesian theorem for posterior calculation. Let's define example probability tables here—swap these with your actual values:
- Prior probabilities:
- P(D1=0) = 0.6, P(D1=1) = 0.4
- P(D2=0) = 0.3, P(D2=1) = 0.7
- Conditional probability table (CPT) for D3:
- P(D3=0 | D1=0, D2=0) = 0.9
- P(D3=0 | D1=0, D2=1) = 0.2
- P(D3=0 | D1=1, D2=0) = 0.5
- P(D3=0 | D1=1, D2=1) = 0.1
The Math Behind the Posterior
To compute P(D1, D2 | D3=0), we rely on Bayes' theorem:
P(D1=d1, D2=d2 | D3=0) = [P(D3=0 | D1=d1, D2=d2) * P(D1=d1) * P(D2=d2)] / P(D3=0)
Where P(D3=0) is the marginal probability, calculated by summing over all possible (d1, d2) pairs:
P(D3=0) = Σ [P(D3=0 | D1=d1, D2=d2) * P(D1=d1) * P(D2=d2)] for all d1, d2
Simplified Code Implementation
Here's a straightforward Python implementation aligned with the simplified approach you referenced. It's easy to adapt to your actual probability values:
# Replace these with your actual probability tables prior_d1 = {0: 0.6, 1: 0.4} prior_d2 = {0: 0.3, 1: 0.7} cpt_d3 = { (0, 0): 0.9, # P(D3=0 | D1=0, D2=0) (0, 1): 0.2, # P(D3=0 | D1=0, D2=1) (1, 0): 0.5, # P(D3=0 | D1=1, D2=0) (1, 1): 0.1 # P(D3=0 | D1=1, D2=1) } # Calculate marginal probability P(D3=0) (denominator for Bayes' theorem) marginal_d3_0 = 0.0 for d1 in prior_d1: for d2 in prior_d2: marginal_d3_0 += cpt_d3[(d1, d2)] * prior_d1[d1] * prior_d2[d2] # Compute joint posterior probabilities for all (D1, D2) pairs joint_posterior = {} for d1 in prior_d1: for d2 in prior_d2: numerator = cpt_d3[(d1, d2)] * prior_d1[d1] * prior_d2[d2] joint_posterior[(d1, d2)] = numerator / marginal_d3_0 # Derive marginal posterior probabilities for D1 and D2 individually posterior_d1 = {} for d1 in prior_d1: posterior_d1[d1] = sum(joint_posterior[(d1, d2)] for d2 in prior_d2) posterior_d2 = {} for d2 in prior_d2: posterior_d2[d2] = sum(joint_posterior[(d1, d2)] for d1 in prior_d1) # Print results print("Joint posterior probabilities P(D1,D2|D3=0):") for (d1, d2), prob in joint_posterior.items(): print(f"P(D1={d1}, D2={d2}|D3=0) = {prob:.4f}") print("\nMarginal posterior probability P(D1|D3=0):") for d1, prob in posterior_d1.items(): print(f"P(D1={d1}|D3=0) = {prob:.4f}") print("\nMarginal posterior probability P(D2|D3=0):") for d2, prob in posterior_d2.items(): print(f"P(D2={d2}|D3=0) = {prob:.4f}")
Common Pitfalls to Fix in Your Modified Code
If your original modified code failed, these are the most likely culprits:
- Incorrect CPT mapping: Double-check that the keys in your conditional probability table match (D1, D2) value pairs exactly (e.g., don't mix up 0/1 if those are your variable values).
- Missing marginal probability: Forgetting to calculate P(D3=0) (the denominator) will leave you with unnormalized probabilities that don't sum to 1.
- Wrong network structure assumption: If your actual network has a different dependency (e.g., D1 depends on D3, or D2 depends on D1 and D3), the formula changes entirely. Verify your parent-child relationships.
- Incomplete value enumeration: If your variables have more than two categories, ensure you're looping through all possible values (not just 0 and 1).
Example Output (With Sample Probabilities)
Running the code above produces:
Joint posterior probabilities P(D1,D2|D3=0): P(D1=0, D2=0|D3=0) = 0.7317 P(D1=0, D2=1|D3=0) = 0.1463 P(D1=1, D2=0|D3=0) = 0.0976 P(D1=1, D2=1|D3=0) = 0.0244 Marginal posterior probability P(D1|D3=0): P(D1=0|D3=0) = 0.8780 P(D1=1|D3=0) = 0.1220 Marginal posterior probability P(D2|D3=0): P(D2=0|D3=0) = 0.8293 P(D2=1|D3=0) = 0.1707
内容的提问来源于stack exchange,提问作者JohnDoeVsJoeSchmoe

