Update geobot/inference/bayesian_engine.py
Browse files
geobot/inference/bayesian_engine.py
CHANGED
|
@@ -198,13 +198,30 @@ class BayesianEngine:
|
|
| 198 |
"""
|
| 199 |
prior = self.priors[variable]
|
| 200 |
|
| 201 |
-
# Create grid
|
| 202 |
n_grid = 1000
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
else:
|
| 207 |
-
# Default
|
| 208 |
mean = prior.parameters.get('loc', 0)
|
| 209 |
std = prior.parameters.get('scale', 1)
|
| 210 |
grid = np.linspace(mean - 4*std, mean + 4*std, n_grid)
|
|
@@ -428,6 +445,7 @@ class BeliefUpdater:
|
|
| 428 |
"""
|
| 429 |
if belief_type == 'continuous':
|
| 430 |
distribution = stats.norm
|
|
|
|
| 431 |
elif belief_type == 'probability':
|
| 432 |
# Use beta distribution for probabilities
|
| 433 |
# Convert mean/std to alpha/beta parameters
|
|
@@ -436,15 +454,16 @@ class BeliefUpdater:
|
|
| 436 |
alpha = mean * (mean * (1 - mean) / var - 1)
|
| 437 |
beta = (1 - mean) * (mean * (1 - mean) / var - 1)
|
| 438 |
distribution = stats.beta
|
| 439 |
-
|
| 440 |
-
|
| 441 |
else:
|
| 442 |
distribution = stats.norm
|
|
|
|
| 443 |
|
| 444 |
prior = Prior(
|
| 445 |
name=name,
|
| 446 |
distribution=distribution,
|
| 447 |
-
parameters=
|
| 448 |
)
|
| 449 |
|
| 450 |
self.engine.set_prior(prior)
|
|
@@ -603,4 +622,4 @@ class BeliefUpdater:
|
|
| 603 |
'p_belief1_greater': np.mean(samples1 > samples2),
|
| 604 |
'mean_difference': np.mean(samples1 - samples2),
|
| 605 |
'correlation': np.corrcoef(samples1, samples2)[0, 1]
|
| 606 |
-
}
|
|
|
|
| 198 |
"""
|
| 199 |
prior = self.priors[variable]
|
| 200 |
|
| 201 |
+
# Create grid based on distribution type
|
| 202 |
n_grid = 1000
|
| 203 |
+
|
| 204 |
+
# Check distribution type by name
|
| 205 |
+
dist_name = prior.distribution.name if hasattr(prior.distribution, 'name') else 'unknown'
|
| 206 |
+
|
| 207 |
+
if dist_name == 'beta':
|
| 208 |
+
# Beta distribution always has support [0, 1]
|
| 209 |
+
grid = np.linspace(0, 1, n_grid)
|
| 210 |
+
elif dist_name == 'gamma':
|
| 211 |
+
# Gamma distribution has support [0, inf)
|
| 212 |
+
# Use reasonable upper bound based on parameters
|
| 213 |
+
shape = prior.parameters.get('a', 1)
|
| 214 |
+
scale = prior.parameters.get('scale', 1)
|
| 215 |
+
mean = shape * scale
|
| 216 |
+
std = np.sqrt(shape) * scale
|
| 217 |
+
grid = np.linspace(0, mean + 4*std, n_grid)
|
| 218 |
+
elif dist_name == 'uniform':
|
| 219 |
+
# Uniform distribution uses loc and scale
|
| 220 |
+
loc = prior.parameters.get('loc', 0)
|
| 221 |
+
scale = prior.parameters.get('scale', 1)
|
| 222 |
+
grid = np.linspace(loc, loc + scale, n_grid)
|
| 223 |
else:
|
| 224 |
+
# Default for normal and other distributions
|
| 225 |
mean = prior.parameters.get('loc', 0)
|
| 226 |
std = prior.parameters.get('scale', 1)
|
| 227 |
grid = np.linspace(mean - 4*std, mean + 4*std, n_grid)
|
|
|
|
| 445 |
"""
|
| 446 |
if belief_type == 'continuous':
|
| 447 |
distribution = stats.norm
|
| 448 |
+
parameters = {'loc': prior_mean, 'scale': prior_std}
|
| 449 |
elif belief_type == 'probability':
|
| 450 |
# Use beta distribution for probabilities
|
| 451 |
# Convert mean/std to alpha/beta parameters
|
|
|
|
| 454 |
alpha = mean * (mean * (1 - mean) / var - 1)
|
| 455 |
beta = (1 - mean) * (mean * (1 - mean) / var - 1)
|
| 456 |
distribution = stats.beta
|
| 457 |
+
# Beta uses 'a' and 'b' parameters, not 'loc' and 'scale'
|
| 458 |
+
parameters = {'a': alpha, 'b': beta}
|
| 459 |
else:
|
| 460 |
distribution = stats.norm
|
| 461 |
+
parameters = {'loc': prior_mean, 'scale': prior_std}
|
| 462 |
|
| 463 |
prior = Prior(
|
| 464 |
name=name,
|
| 465 |
distribution=distribution,
|
| 466 |
+
parameters=parameters
|
| 467 |
)
|
| 468 |
|
| 469 |
self.engine.set_prior(prior)
|
|
|
|
| 622 |
'p_belief1_greater': np.mean(samples1 > samples2),
|
| 623 |
'mean_difference': np.mean(samples1 - samples2),
|
| 624 |
'correlation': np.corrcoef(samples1, samples2)[0, 1]
|
| 625 |
+
}
|