Skip to content

Commit

Permalink
first working version of transformation wrapper for #1
Browse files Browse the repository at this point in the history
  • Loading branch information
vlas-sokolov committed Dec 5, 2017
1 parent fabd31b commit 2414727
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions pyspecnest/multiwrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from astropy import log
from itertools import compress
from collections import OrderedDict
import functools


class Parameter:
Expand Down Expand Up @@ -102,25 +103,27 @@ def xoff_transform(f_loglike):
parameters into it. The coordinate transform is pre-computed by the
the inverse matrix from the `get_xoff_transform` method.
"""
@functools.wraps(f_loglike) # preserves __name__, docstring
def log_like_transformed(*args, **kwargs):
# I know I know this can be write in a simpler way, but we want
# to really avoid making new arrays in memory and I am not sure
# what type the `cube` variable is inside PyMultinest
spec_model = args[0]
cube = args[1]
trans_xoff_vector = [cube[i] for i, is_xoff in
enumerate(kwargs["xoff_pars"]) if is_xoff]
enumerate(spec_model.xoff_pars) if is_xoff]

xoff_vector = np.dot(kwargs["inv_xoff_T"], trans_xoff_vector)
xoff_vector = np.dot(spec_model.inv_xoff_T, trans_xoff_vector)

# inject xoffs into cube
xoff_idx = 0
for i, is_xoff in enumerate(kwargs["xoff_pars"]):
for i, is_xoff in enumerate(spec_model.xoff_pars):
if is_xoff:
cube[i] = xoff_vector[xoff_idx]
xoff_idx += 1

# compute the log-likelihood in the (x1, x2, x3, ..., xn) system
f_loglike(*args)
return f_loglike(*args)

return log_like_transformed

Expand Down Expand Up @@ -238,6 +241,7 @@ def log_likelihood(self, cube, ndims, nparams):
par_list = [cube[i] for i in range(ndims)]
ymodel = self.model(pars=par_list)
log_L = (-0.5 * ((ymodel - self.ydata) / self.std_noise)**2).sum()

return log_L

@property
Expand All @@ -255,7 +259,7 @@ def xoff_pars(self, key=None):

@xoff_transform
def xoff_symmetric_log_likelihood(self, cube, ndims, nparams):
return self.log_likelihood(self, cube, ndims, nparams)
return self.log_likelihood(cube, ndims, nparams)

#@xoff_transform
#def xoff_symmetric_prior_uniform(self, cube, ndim, nparams):
Expand Down

0 comments on commit 2414727

Please sign in to comment.