Skip to content

Commit

Permalink
fix corner case hdi censored (#590)
Browse files Browse the repository at this point in the history
  • Loading branch information
aloctavodia authored Nov 19, 2024
1 parent 3343509 commit dad44af
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
13 changes: 8 additions & 5 deletions preliz/internal/optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,19 +229,22 @@ def optimize_beta_mode(lower, upper, tau_not, mode, dist, mass, prob):
def optimize_hdi(dist, mass):
def interval_loss(params):
cdf = dist.cdf(params)
loss = (cdf[1] - cdf[0]) - mass
loss = (cdf[1] - cdf[0] + mass_at_boundaries) - mass
return loss

def interval_short(params):
lower, upper = params
return upper - lower
if params[1] < params[0]:
return np.inf
return params[1] - params[0]

cons = {
"type": "eq",
"fun": interval_loss,
}
init_vals = dist.eti(mass=mass)
bounds = np.array([dist.support])
lower, upper = dist.support
mass_at_boundaries = dist.pdf(lower) + dist.pdf(upper)
init_vals = dist.eti(mass=mass, fmt="none")
bounds = [(lower, upper)]
opt = minimize(interval_short, x0=init_vals, bounds=bounds, constraints=cons)

lower, upper = opt.x
Expand Down
2 changes: 2 additions & 0 deletions preliz/internal/special.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,8 @@ def xlogx(x):
def xprody(x, y):
if np.isinf(x):
return 0
if np.isinf(y):
return 0
else:
return x * y

Expand Down

0 comments on commit dad44af

Please sign in to comment.