Skip to content

Commit

Permalink
Merge pull request #105 from wichmann-lab/fixscaling
Browse files Browse the repository at this point in the history
Fixes scaling for real
  • Loading branch information
otizonaizit authored Sep 20, 2024
2 parents e5dea94 + 1a9bd11 commit 7e1f748
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 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
5 changes: 2 additions & 3 deletions psignifit/_priors.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,8 @@ def setup_priors(custom_priors, bounds, stimulus_range, width_min, width_alpha,
priors.update(custom_priors)
check_priors(priors, stimulus_range, width_min)

npriors = {}
for parameter, prior in priors.items():
npriors[parameter] = normalize_prior(prior, bounds[parameter])
return npriors, priors
priors[parameter] = normalize_prior(prior, bounds[parameter])
return priors


14 changes: 7 additions & 7 deletions psignifit/psignifit.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ def psignifit(data: np.ndarray, conf: Optional[Configuration] = None,
for param, value in conf.fixed_parameters.items():
bounds[param] = (value, value)

npriors, priors = setup_priors(custom_priors=conf.priors, bounds=bounds,
stimulus_range=stimulus_range, width_min=width_min, width_alpha=conf.width_alpha,
beta_prior=conf.beta_prior, threshold_perc_correct=conf.thresh_PC)
fit_dict, posteriors, grid = _fit_parameters(data, bounds, npriors, sigmoid, conf.steps_moving_bounds,
priors = setup_priors(custom_priors=conf.priors, bounds=bounds,
stimulus_range=stimulus_range, width_min=width_min, width_alpha=conf.width_alpha,
beta_prior=conf.beta_prior, threshold_perc_correct=conf.thresh_PC)
fit_dict, posteriors, grid = _fit_parameters(data, bounds, priors, sigmoid, conf.steps_moving_bounds,
conf.max_bound_value, conf.grid_steps)

grid_values = [grid_value for _, grid_value in sorted(grid.items())]
Expand All @@ -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 7e1f748

Please sign in to comment.