Skip to content

Commit

Permalink
marginals returned now with integral 1, so that plotting works
Browse files Browse the repository at this point in the history
  • Loading branch information
guillermoaguilar committed Sep 20, 2024
1 parent a7c50ed commit 6adfc2d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
10 changes: 7 additions & 3 deletions psignifit/_posterior.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,13 @@ def objective(x):
def marginalize_posterior(parameter_grid: ParameterGrid, posterior_mass: np.ndarray) -> Dict[str, np.ndarray]:
marginals = dict()
for i, (param, grid) in enumerate(sorted(parameter_grid.items())):
if grid is None:
if grid is None or len(grid)==1:
marginals[param] = None
axis = tuple(range(0, i)) + tuple(range(i + 1, len(parameter_grid)))
marginals[param] = np.squeeze(posterior_mass.sum(axis))
else:
axis = tuple(range(0, i)) + tuple(range(i + 1, len(parameter_grid)))
# we get first the unnormalized marginal, and then we scale it
nmarginal = np.squeeze(posterior_mass.sum(axis))
integral = np.trapz(nmarginal, x=grid)
marginals[param] = nmarginal / integral

return marginals
6 changes: 3 additions & 3 deletions psignifit/psignifit.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ def psignifit(data: np.ndarray, conf: Optional[Configuration] = None,
return Result(parameter_estimate=fit_dict,
configuration=conf,
confidence_intervals=intervals_dict,
parameter_values={k: v.tolist() for k, v in grid.items()},
prior_values={param: priors[param](values).tolist() for param, values in grid.items()},
marginal_posterior_values={k: v.tolist() for k, v in marginals.items()},
parameter_values=grid,
prior_values={param: priors[param](values) for param, values in grid.items()},
marginal_posterior_values=marginals,
posterior_mass=posteriors,
data=data.tolist())

Expand Down

0 comments on commit 6adfc2d

Please sign in to comment.