Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MLE: return index for sorting distributions #184

Merged
merged 1 commit into from
Jan 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion preliz/internal/optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,15 @@ def fit_to_sample(selected_distributions, sample, x_min, x_max):
if dist.__class__.__name__ in ["BetaScaled", "TruncatedNormal"]:
update_bounds_beta_scaled(dist, x_min, x_max)

loss = np.inf
if dist._check_endpoints(x_min, x_max, raise_error=False):
dist._fit_mle(sample) # pylint:disable=protected-access
if dist.kind == "continuous":
loss = -dist.rv_frozen.logpdf(sample).sum()
else:
loss = -dist.rv_frozen.logpmf(sample).sum()

fitted.update(loss, dist)
fitted.update(loss, dist)

return fitted

Expand Down
13 changes: 8 additions & 5 deletions preliz/unidimensional/mle.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ def mle(

Returns
-------
axes: matplotlib axes
idx : array with the indexes to sort ``distributions`` from best to worst match
axes : matplotlib axes
"""
sample = np.array(sample)
x_min = sample.min()
Expand All @@ -45,10 +46,12 @@ def mle(
if plot > len(distributions):
plot = len(distributions)

idx = np.argsort(fitted.losses)

if plot:
idx = np.argsort(fitted.losses)
idx = idx[:plot]
for dist in fitted.distributions[idx]:
plot_idx = idx[:plot]
for dist in fitted.distributions[plot_idx]:
if dist is not None:
ax = dist.plot_pdf(plot_kwargs)
return ax

return idx, ax