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

More options for get_post_parameters #193

Merged
merged 4 commits into from
Nov 26, 2024
Merged
Changes from 1 commit
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
32 changes: 24 additions & 8 deletions appletree/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,17 +272,33 @@ def continue_fitting(self, context=None, iteration=500, batch_size=1_000_000):
self._dump_meta(batch_size=batch_size)
return result

def get_post_parameters(self):
"""Get parameters correspondes to max posterior."""
logp = self.sampler.get_log_prob(flat=True)
chain = self.sampler.get_chain(flat=True)
mpe_parameters = chain[np.argmax(logp)]
mpe_parameters = emcee.ensemble.ndarray_to_list_of_dicts(
[mpe_parameters],
def get_post_parameters(self, which='mpe'):
"""Get parameters from the backend.

Args:
which: str, 'mpe', 'random' or 'median'.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add comments about what they are.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

"""
# Assign attributes for the first time
# This speeds up if the user wanna call this function many times
if not hasattr(self, '_logp'):
self._logp = self.sampler.get_log_prob(flat=True)
if not hasattr(self, '_chain'):
self._chain = self.sampler.get_chain(flat=True)
if which == 'mpe':
_parameters = self._chain[np.argmax(self._logp)]
elif which == 'random':
_parameters = self._chain[np.random.randint(len(self._logp))]
elif which == 'median':
_parameters = np.median(self._chain, axis=0)
else:
raise ValueError(f"which should be 'mpe', 'random' or 'median', got {which}!")

_parameters = emcee.ensemble.ndarray_to_list_of_dicts(
[_parameters],
self.sampler.parameter_names,
)[0]
parameters = copy.deepcopy(self.par_manager.get_all_parameter())
parameters.update(mpe_parameters)
parameters.update(_parameters)
return parameters

def get_all_post_parameters(self, **kwargs):
Expand Down
Loading