Spaces:
Sleeping
Sleeping
Commit
·
6f1dba6
1
Parent(s):
f63cf4c
refactor: improve clarity of Bayesian update with explicit posterior and marginal variables
Browse files- Replace implicit normalization with explicit posterior_unnormalized calculation
- Add explicit marginal calculation showing P(evidence) = sum of (prior * likelihood)
- Improve pedagogical clarity of Bayes' rule implementation
- Maintain identical mathematical behavior with better variable naming
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
domains/belief/belief_domain.py
CHANGED
|
@@ -79,13 +79,15 @@ class BayesianBeliefState:
|
|
| 79 |
)
|
| 80 |
likelihoods[target_idx] = likelihood
|
| 81 |
|
| 82 |
-
#
|
| 83 |
-
|
| 84 |
|
| 85 |
-
#
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
| 89 |
else:
|
| 90 |
# If all likelihoods are 0 (shouldn't happen with valid evidence),
|
| 91 |
# reset to uniform distribution
|
|
|
|
| 79 |
)
|
| 80 |
likelihoods[target_idx] = likelihood
|
| 81 |
|
| 82 |
+
# Calculate unnormalized posterior: prior * likelihood
|
| 83 |
+
posterior_unnormalized = self.beliefs * likelihoods
|
| 84 |
|
| 85 |
+
# Calculate marginal: P(evidence) = sum of (prior * likelihood) for all targets
|
| 86 |
+
marginal = np.sum(posterior_unnormalized)
|
| 87 |
+
|
| 88 |
+
# Apply Bayes' rule: posterior = (prior * likelihood) / marginal
|
| 89 |
+
if marginal > 0:
|
| 90 |
+
self.beliefs = posterior_unnormalized / marginal
|
| 91 |
else:
|
| 92 |
# If all likelihoods are 0 (shouldn't happen with valid evidence),
|
| 93 |
# reset to uniform distribution
|