JohannesBuchner / UltraNest

Fit and compare complex models reliably and rapidly. Advanced nested sampling.
https://johannesbuchner.github.io/UltraNest/
Other
142 stars 30 forks source link

enhancement: mechanism for passing non-parameter arguments to the likelihood function #51

Closed MichaelDAlbrow closed 2 years ago

MichaelDAlbrow commented 2 years ago

It would be very nice to have a way of passing extra (non-model-parameter) arguments through to the likelihood function, rather than relying on global variables. The emcee sampler has "args" and dynesty has "logl_args" to accomplish this.

regards, Michael Albrow

JohannesBuchner commented 2 years ago

You can do this with a class, or with a function that builds and returns a function.

erinaldi commented 2 years ago

I have done this with a lambda function and it worked. @JohannesBuchner is this what you were suggesting with your second part of the sentence above?

JohannesBuchner commented 2 years ago

Yes, sorry for being terse. I'll close this now then.

JohannesBuchner commented 2 years ago

To expand, you can do something like this:

def make_likelihood(data):
     def mylikelihood(params):
            return -0.5 * ((model(params) - data)**2).sum()
     return mylikelihood

sampler = ReactiveNestedSampler(loglike=make_likelihood(mydata), ...)

or as a class:

class MyLikelihood():
     def __init__(self, data):
            self.data = data
     def __call__(self, params):
            return -0.5 * ((model(params) - self.data)**2).sum()

sampler = ReactiveNestedSampler(loglike=MyLikelihood(mydata), ...)