thompsonson Claude commited on
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>

Files changed (1) hide show
  1. domains/belief/belief_domain.py +8 -6
domains/belief/belief_domain.py CHANGED
@@ -79,13 +79,15 @@ class BayesianBeliefState:
79
  )
80
  likelihoods[target_idx] = likelihood
81
 
82
- # Apply Bayes' rule: posterior ∝ prior * likelihood
83
- self.beliefs = self.beliefs * likelihoods
84
 
85
- # Normalize to ensure probabilities sum to 1
86
- total_belief = np.sum(self.beliefs)
87
- if total_belief > 0:
88
- self.beliefs = self.beliefs / total_belief
 
 
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