Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bayesian interface -- timing model + white noise #1407

Merged
merged 88 commits into from
Oct 19, 2022

Conversation

abhisrkckl
Copy link
Contributor

@abhisrkckl abhisrkckl commented Sep 27, 2022

A bayesian interface that allows the user to use PINT in combination with any sampler of their choice (both MCMC and nested samplers).

This PR implements the BayesianTiming class that provides the following methods:

  1. Log likelihood
  2. Log prior
  3. Prior transform
  4. Log posterior

At present, this only works with white noise models and does not support correlated noise models.

See the attachment for a sample posterior plot obtained using this interface and the NGC6440E sample data.
NGC6440E_efac_post
I have checked that the result is consistent with the ENTERPRISE result.

Work in progress

  1. Support for correlated noise
  2. The derivatives of log-likelihood, log prior, and log posterior (necessary for using derivative-aware samplers (issue PINT should have a derivative-aware MCMC method #1303)).

@dlakaplan
Copy link
Contributor

I'll try to review this more closely, but with a quick look this looks really great & useful. My main comment would be to fix some of the formatting to make:
https://nanograv-pint--1407.org.readthedocs.build/en/1407/_autosummary/pint.bayesian.BayesianTiming.html#pint.bayesian.BayesianTiming
a little easier to read. Some of the code-block indenting and other things that are supposed to be bullet points aren't quite right. This can be pretty annoying to debug but you can try make docs locally (although I don't always see the same results locally and on RTD).

Also, your example notebook looks really good, but I assume it won't be included in the built tutorials, right? Can you maybe then include some of the basic usage commands in the docstring since otherwise the notebook can be hard to find? Those don't get run, so there is no penalty for a complex calculation. Or maybe there's a better place to put that.

param = getattr(model, par)
param_min = float(param.value - 10 * param.uncertainty_value)
param_span = float(20 * param.uncertainty_value)
param.prior = Prior(uniform(param_min, param_span))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you are setting an attribute that persist in the model object? Can you also include in the example how you set the priors with the separate dictionary?

Copy link
Contributor Author

@abhisrkckl abhisrkckl Oct 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you are setting an attribute that persist in the model object?

The model attribute in BayesianTiming is supposed to be a clone of the input model. So line 39 should have been param = getattr(self.model, par). I have fixed this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have included the prior_info usage in bayesian-example-NGC6440E.py and a new test for this in test_bayesian.py.

f"Unbounded uniform priors are not supported. (param : {param.name})"
)

def _decide_likelihood_method(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work for both narrowband and Wideband data?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works only works with narrow band now. I haven't tested with wideband data.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, probably worth putting in the docs (& an issue?) and maybe checking when the toas are fed in?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checking when the toas are fed in?

Yes... I think this is needed. I'll implement this. Maybe throw a NotImplementedError upon encountering wideband TOAs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented.

@dlakaplan
Copy link
Contributor

Also (very minor) please include a CHANGELOG entry.

3. Currently, only uniform and normal distributions are supported in prior_info. More
general priors should be set directly in the TimingModel object before creating the
BayesianTiming object. Here is an example prior_info object:
>>> prior_info = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code block is still not formatting correctly for me at https://nanograv-pint--1407.org.readthedocs.build/en/1407/_autosummary/pint.bayesian.BayesianTiming.html. Maybe add in a blank line and indent further?

@dlakaplan
Copy link
Contributor

I think the python code blocks don't work (or at least I've had issues with those in the past, especially when also trying other languages). I would suggest just trying a blank line and extra indent. But we can also probably defer some of the final formatting until later.

@abhisrkckl
Copy link
Contributor Author

I have just made that example into a single line and pointed the user to the bayesian example script in the docstring. If this doesn't work either let us just remove that one line and proceed.

@abhisrkckl
Copy link
Contributor Author

BTW is there a way to build the readthedocs locally?

@dlakaplan
Copy link
Contributor

Yes, look at https://nanograv-pint--1407.org.readthedocs.build/en/1407/contributing.html#write-documentation. make docs should work. It will still take ~10 min.

@dlakaplan dlakaplan merged commit 18c643a into nanograv:master Oct 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting review This PR needs someone to review it so it can be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants