Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
bilby
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
lscsoft
bilby
Commits
5b2182f1
Commit
5b2182f1
authored
4 years ago
by
Gregory Ashton
Browse files
Options
Downloads
Patches
Plain Diff
Adds a generic reweighting method for arbitrary likelihood/priors
parent
dded5f6c
No related branches found
Branches containing commit
No related tags found
Tags containing commit
2 merge requests
!778
Add dynesty sample dump
,
!776
Adds a generic reweighting method for arbitrary likelihood/priors
Pipeline
#123160
passed
4 years ago
Stage: test
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
bilby/core/result.py
+108
-0
108 additions, 0 deletions
bilby/core/result.py
with
108 additions
and
0 deletions
bilby/core/result.py
+
108
−
0
View file @
5b2182f1
...
...
@@ -94,6 +94,114 @@ def read_in_result(filename=None, outdir=None, label=None, extension='json', gzi
return
result
def
reweight
(
result
,
label
=
None
,
new_likelihood
=
None
,
new_prior
=
None
,
old_likelihood
=
None
,
old_prior
=
None
,
resample
=
True
,
fraction
=
0.1
,
replace
=
False
):
"""
Reweight a result to a new likelihood/prior
Parameters
----------
label: str, optional
An updated label to apply to the result object
new_likelihood: bilby.core.likelood.Likelihood, (optional)
If given, the new likelihood to reweight too. If not given, likelihood
reweighting is not applied
new_prior: bilby.core.prior.PriorDict, (optional)
If given, the new prior to reweight too. If not given, prior
reweighting is not applied
old_likelihood: bilby.core.likelihood.Likelihood, (optional)
If given, calculate the old likelihoods from this object. If not given,
the values stored in the posterior are used.
old_prior: bilby.core.prior.PriorDict, (optional)
If given, calculate the old prior from this object. If not given,
the values stored in the posterior are used.
resample: bool (default: True)
If true, resample to the effective number of samples after reweighting
fraction: float [0, 1]
A multiplictive factor to apply to the effective number of samples
replace: bool
If true, sample with replacement
Note:
The default settings have been tested in low dimensional cases.
Appropriate steps should be taken in high dimensional cases and when
changing the fraction, and replace settings.
Returns
-------
result: bilby.core.result.Result
A copy of the result object with a reweighted posterior
"""
result
=
copy
(
result
)
nposterior
=
len
(
result
.
posterior
)
logger
.
info
(
"
Reweighting posterior with {} samples
"
.
format
(
nposterior
))
old_log_likelihood_array
=
np
.
zeros
(
nposterior
)
old_log_prior_array
=
np
.
zeros
(
nposterior
)
new_log_likelihood_array
=
np
.
zeros
(
nposterior
)
new_log_prior_array
=
np
.
zeros
(
nposterior
)
for
ii
,
sample
in
result
.
posterior
.
iterrows
():
# Convert sample to dictionary
par_sample
=
{
key
:
sample
[
key
]
for
key
in
result
.
search_parameter_keys
}
if
old_likelihood
is
not
None
:
old_likelihood
.
parameters
.
update
(
par_sample
)
old_log_likelihood_array
[
ii
]
=
old_likelihood
.
log_likelihood
()
else
:
old_log_likelihood_array
[
ii
]
=
sample
[
"
log_likelihood
"
]
if
new_likelihood
is
not
None
:
new_likelihood
.
parameters
.
update
(
par_sample
)
new_log_likelihood_array
[
ii
]
=
new_likelihood
.
log_likelihood
()
else
:
# Don't perform likelihood reweighting (i.e. likelihood isn't updated)
new_log_likelihood_array
[
ii
]
=
old_log_likelihood_array
[
ii
]
if
old_prior
is
not
None
:
old_log_prior_array
[
ii
]
=
old_prior
.
ln_prob
(
par_sample
)
else
:
old_log_prior_array
[
ii
]
=
sample
[
"
log_prior
"
]
if
new_prior
is
not
None
:
new_log_prior_array
[
ii
]
=
new_prior
.
ln_prob
(
par_sample
)
else
:
# Don't perform prior reweighting (i.e. prior isn't updated)
new_log_prior_array
[
ii
]
=
old_log_prior_array
[
ii
]
ln_weights
=
(
new_log_likelihood_array
+
new_log_prior_array
-
old_log_likelihood_array
-
old_log_prior_array
)
log_n_eff
=
2
*
logsumexp
(
ln_weights
)
-
logsumexp
(
2
*
ln_weights
)
n_eff
=
int
(
fraction
*
np
.
exp
(
log_n_eff
))
logger
.
info
(
"
Reweighted posterior has {} effective samples
"
.
format
(
n_eff
))
weights
=
np
.
exp
(
ln_weights
)
if
resample
:
nsamples
=
n_eff
else
:
nsamples
=
nposterior
# Overwrite the likelihood and prior evaluations
result
.
posterior
[
"
log_likelihood
"
]
=
new_log_likelihood_array
result
.
posterior
[
"
log_prior
"
]
=
new_log_prior_array
# Resample
result
.
posterior
=
result
.
posterior
.
sample
(
nsamples
,
weights
=
weights
,
replace
=
replace
)
result
.
priors
=
new_prior
result
.
log_evidence
+=
np
.
mean
(
weights
)
if
label
:
result
.
label
=
label
else
:
result
.
label
+=
"
_reweighted
"
return
result
class
Result
(
object
):
def
__init__
(
self
,
label
=
'
no_label
'
,
outdir
=
'
.
'
,
sampler
=
None
,
search_parameter_keys
=
None
,
fixed_parameter_keys
=
None
,
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment