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
6e6f3770
Commit
6e6f3770
authored
5 years ago
by
Gregory Ashton
Browse files
Options
Downloads
Patches
Plain Diff
Add option to only initialize some parameters
parent
72bf21a9
No related branches found
No related tags found
1 merge request
!750
Improve ptemcee
Pipeline
#113468
failed
5 years ago
Stage: test
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
bilby/core/sampler/ptemcee.py
+31
-12
31 additions, 12 deletions
bilby/core/sampler/ptemcee.py
with
31 additions
and
12 deletions
bilby/core/sampler/ptemcee.py
+
31
−
12
View file @
6e6f3770
...
...
@@ -98,23 +98,35 @@ class Ptemcee(MCMCSampler):
for
_
in
range
(
self
.
sampler_init_kwargs
[
"
nwalkers
"
])]
for
_
in
range
(
self
.
kwargs
[
'
ntemps
'
])]
def
get_pos0_from_minimize
(
self
):
def
get_pos0_from_minimize
(
self
,
minimize_list
=
None
):
logger
.
info
(
"
Attempting to set pos0 from minimize
"
)
from
scipy.optimize
import
minimize
if
minimize_list
is
None
:
minimize_list
=
self
.
search_parameter_keys
pos0
=
np
.
zeros
((
self
.
kwargs
[
"
ntemps
"
],
self
.
kwargs
[
"
nwalkers
"
],
self
.
ndim
))
else
:
pos0
=
np
.
array
(
self
.
get_pos0_from_prior
())
likelihood_copy
=
copy
.
copy
(
self
.
likelihood
)
def
neg_log_like
(
params
):
likelihood_copy
.
parameters
.
update
(
{
key
:
val
for
key
,
val
in
zip
(
minimize_list
,
params
)})
try
:
return
-
self
.
log_likelihood
(
params
)
return
-
likelihood_copy
.
log_likelihood
()
except
RuntimeError
:
return
+
np
.
inf
bounds
=
[(
self
.
priors
[
key
].
minimum
,
self
.
priors
[
key
].
maximum
)
for
key
in
self
.
search_parameter_keys
]
for
key
in
minimize_list
]
trials
=
0
success
=
[]
while
True
:
x0
=
self
.
get_random_draw_from_prior
()
draw
=
self
.
priors
.
sample
()
likelihood_copy
.
parameters
.
update
(
draw
)
x0
=
[
draw
[
key
]
for
key
in
minimize_list
]
res
=
minimize
(
neg_log_like
,
x0
,
bounds
=
bounds
,
method
=
'
L-BFGS-B
'
,
tol
=
1e-15
)
neg_log_like
,
x0
,
bounds
=
bounds
,
method
=
'
L-BFGS-B
'
)
if
res
.
success
:
success
.
append
(
res
.
x
)
if
trials
>
100
:
...
...
@@ -122,11 +134,16 @@ class Ptemcee(MCMCSampler):
if
len
(
success
)
>=
3
:
break
pos0_min
=
np
.
min
(
success
,
axis
=
0
)
pos0_max
=
np
.
max
(
success
,
axis
=
0
)
pos0
=
np
.
random
.
uniform
(
pos0_min
,
pos0_max
,
size
=
(
self
.
kwargs
[
"
ntemps
"
],
self
.
kwargs
[
"
nwalkers
"
],
self
.
ndim
))
success
=
np
.
array
(
success
)
for
i
,
key
in
enumerate
(
minimize_list
):
pos0_min
=
np
.
min
(
success
[:,
i
])
pos0_max
=
np
.
max
(
success
[:,
i
])
logger
.
info
(
"
Initialize {} walkers from {}->{}
"
.
format
(
key
,
pos0_min
,
pos0_max
))
j
=
self
.
search_parameter_keys
.
index
(
key
)
pos0
[:,
:,
j
]
=
np
.
random
.
uniform
(
pos0_min
,
pos0_max
,
size
=
(
self
.
kwargs
[
"
ntemps
"
],
self
.
kwargs
[
"
nwalkers
"
]))
return
pos0
def
setup_sampler
(
self
):
...
...
@@ -169,10 +186,12 @@ class Ptemcee(MCMCSampler):
return
self
.
sampler
,
pos0
def
get_pos0
(
self
):
if
self
.
pos0
.
lower
()
==
"
prior
"
:
if
isinstance
(
self
.
pos0
,
str
)
and
self
.
pos0
.
lower
()
==
"
prior
"
:
return
self
.
get_pos0_from_prior
()
elif
self
.
pos0
.
lower
()
==
"
minimize
"
:
elif
isinstance
(
self
.
pos0
,
str
)
and
self
.
pos0
.
lower
()
==
"
minimize
"
:
return
self
.
get_pos0_from_minimize
()
elif
isinstance
(
self
.
pos0
,
list
):
return
self
.
get_pos0_from_minimize
(
minimize_list
=
self
.
pos0
)
else
:
raise
SamplerError
(
"
pos0={} not implemented
"
.
format
(
self
.
pos0
))
...
...
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