Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
EMRI_Pop_Inference_old
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
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
Christian Chapman-Bird
EMRI_Pop_Inference_old
Commits
4ff3dd42
Commit
4ff3dd42
authored
3 years ago
by
Christian Chapman-Bird
Browse files
Options
Downloads
Patches
Plain Diff
Added custom Bilby hyper-likelihood.
parent
c9f62659
No related branches found
Branches containing commit
No related tags found
Loading
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/inference/likelihood.py
+50
-0
50 additions, 0 deletions
src/inference/likelihood.py
with
50 additions
and
0 deletions
src/inference/likelihood.py
0 → 100644
+
50
−
0
View file @
4ff3dd42
import
bilby
import
numpy
as
np
import
os
import
pandas
as
pd
class
HyperLikelihood
(
bilby
.
Likelihood
):
"""
Hyperparameter Likelihood class. Calculates loglikelihood using samples from pandas dataframes in directory
eventdir (one per event).
If the population content is changed, this must be re-initialised.
pdet (optional): Detection probability function for the population parameters. If supplied, detection selection
effects will be included.
"""
def
__init__
(
self
,
population_container
,
eventdir
,
pdet
=
None
):
self
.
eventdir
=
eventdir
self
.
populations
=
population_container
self
.
n_obs
=
len
(
os
.
listdir
(
eventdir
))
self
.
events
=
[
pd
.
read_csv
(
ev
)
for
ev
in
os
.
listdir
(
eventdir
)
if
'
.csv
'
in
ev
]
self
.
pdetfun
=
pdet
super
().
__init__
()
def
_update_params
(
self
):
self
.
populations
.
set_hypers
(
self
.
parameters
)
def
_get_beta
(
self
):
if
self
.
pdetfun
is
not
None
:
# draw samples from population, plug in to p_det(theta).
samples
=
self
.
populations
.
sample
(
int
(
1e5
))
pdetvals
=
self
.
pdetfun
(
samples
)
return
np
.
mean
(
pdetvals
)
# take the mean over our population
else
:
return
1
# without selection effects, 100% of the population is assumed to be detectable
def
log_likelihood
(
self
):
self
.
_update_params
()
bet
=
np
.
log
(
self
.
_get_beta
())
log_lhood
=
bet
*
self
.
n_obs
# N_obs * log(beta(lambda))
for
event
in
self
.
events
:
sample_ppops
=
self
.
populations
.
get_pdf
(
dict
(
zip
(
event
.
index
,
event
.
values
)))
# dictionary of {key: samples} pairs
sum_over_samples
=
np
.
sum
(
sample_ppops
)
# take the sum of our samples
log_lhood
+=
np
.
log
(
sum_over_samples
)
# take the log of the sum and add it to the log likelihood
log_lhood
-=
np
.
log
(
len
(
event
))
# 1/S_i # normalise by subtracting the log of the number of samples
return
log_lhood
\ No newline at end of file
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