-
Notifications
You must be signed in to change notification settings - Fork 7
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
Computing the evidence from NUTS chains #229
Comments
Hi @stefanocovino , great to see you're interested in this. I don't think we've applied to NUTS samples from numpyro but it's definitely on the list of things to do. If you're interested in this we'd be very happy to help to try to get things working. Do you have a minimal working problem so we can try to help? Basically you should just need to get posterior samples out and then harmonic can be applied to those. I would recommend starting with a low-dimensional problem first. Pinging @alicjapolanska, @CosmoMatt, @alessiospuriomancini, @dpiras, who make also be interested in this and able to help. |
Hi Jason,
Sure. I'd suggest to try the first example reported in this nice post by
Dan Foreman-Mackey: https://dfm.io/posts/intro-to-numpyro/
I found two problems. The first is simply that the samples you have (a
dictionary, from the get_samples method)) report also chains for the
"deterministic" parameters you might define, as in the example. It is
just a matter of removing them and reformatting the output. I tried
something like this and it seems to work with the add_chains_2d method:
inpsmpl = [samples[i].reshape(-1,1) for i in samples.keys() if i in ('theta'
,'b_perp')]
cdata = np.hstack(inpsmpl)
The second is trickier and depends on my lack of knowledge about the NUTS
sampler implementation, i.e. it is not fully clear where log_probabilities
are saved. I tried to save the "potential_energy" formatted as:
np.float64(mcmc.get_extra_fields()['potential_energy'].flatten())
And again add_chains_2d accepted it. Only, I'm not sure everything is
correct. Of course I am planning to test different evidence computation
tools (e.g. parallel tempering) on the same problem. However, I was
wondering whether anybody has already dealt with this issue.
Bye,
Stefano
————————————————————————————————
Only audience I care about is you.
Richard Castle to Kate Beckett
Stefano Covino
INAF / Osservatorio Astronomico di Brera
Via Emilio Bianchi 46, 23807
Merate (LC) - Italy
Tel.: +39 02 72320475 (office)
FAX: +39 02 72320401
Cell: +39 331 6748534
E-mail: ***@***.***
URL: http://www.merate.mi.astro.it/∼covino
-------------------------------------------------------------------------------------
|
Hi @stefanocovino! I have not used I'm interested to know if it works! Can we compare the evidence values against some ground truth in this simple example? |
Thanks @stefanocovino. Given @dpiras comment, it should indeed just be a matter of setting up the chains with the logprob values. If you have a script or a notebook with a minimal version we can help to get it running? Feel free to set up a WIP PR and we can work together to get things going. |
Hi Jason,
I tried this (in attachment).
Please, let me know if something is not clear. I hope it might help.
Stefano
Il giorno gio 8 giu 2023 alle ore 12:52 Jason McEwen <
***@***.***> ha scritto:
… Thanks @stefanocovino <https://github.com/stefanocovino>. Given @dpiras
<https://github.com/dpiras> comment, it should indeed just be a matter of
setting up the chains with the logprob values. If you have a script or a
notebook with a minimal version we can help to get it running? Feel free to
set up a WIP PR and we can work together to get things going.
—
Reply to this email directly, view it on GitHub
<#229 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAGVYHHIFQOPFV5LY7GSMT3XKGVFFANCNFSM6AAAAAAYZ346BE>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
--
————————————————————————————————
Only audience I care about is you.
Richard Castle to Kate Beckett
Stefano Covino
INAF / Osservatorio Astronomico di Brera
Via Emilio Bianchi 46, 23807
Merate (LC) - Italy
Tel.: +39 02 72320475 (office)
FAX: +39 02 72320401
Cell: +39 331 6748534
E-mail: ***@***.***
URL: http://www.merate.mi.astro.it/∼covino
-------------------------------------------------------------------------------------
|
Thanks @stefanocovino but I'm not sure the attachment made it's way to github? |
Hi Jason,
I don't kowm, actually. I attach the notebook again. Please, let me know if
you can get it.
Bye,
Stefano
Il giorno lun 12 giu 2023 alle ore 14:03 Jason McEwen <
***@***.***> ha scritto:
… Thanks @stefanocovino <https://github.com/stefanocovino> but I'm not sure
the attachment made it's way to github?
—
Reply to this email directly, view it on GitHub
<#229 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAGVYHG7MMBGTMJNLWO3C63XK4ASFANCNFSM6AAAAAAYZ346BE>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
--
————————————————————————————————
Only audience I care about is you.
Richard Castle to Kate Beckett
Stefano Covino
INAF / Osservatorio Astronomico di Brera
Via Emilio Bianchi 46, 23807
Merate (LC) - Italy
Tel.: +39 02 72320475 (office)
FAX: +39 02 72320401
Cell: +39 331 6748534
E-mail: ***@***.***
URL: http://www.merate.mi.astro.it/∼covino
-------------------------------------------------------------------------------------
|
@stefanocovino could you perhaps try to click on this link (#229), and post it as a comment here below? |
Actually, the system does not allow me to attach anything. I did not know that. So I just list the code below! Play with the notebook as you like. Stefano -- coding: utf-8 --"""Harmonic-Numpyro-Test.ipynb !pip install numpyro """# Simulated data""" Commented out IPython magic to ensure Python compatibility.%matplotlib inlineimport matplotlib.pyplot as plt data = np.array([[ 0.42, 0.72, 0. , 0.3 , 0.15, plt.errorbar(x, y, yerr=sigma_y, fmt='o') """# Probabilistic model""" import numpyro def y_model (x,m,q): def numpyro_model(x, ey, y=None): """## NUTS sampling""" nuts_kernel = NUTS(numpyro_model, dense_mass=True, target_accept_prob=0.9) mcmc.run(rng_key, x, sigma_y, y=y, extra_fields=('potential_energy',)) pred = infer.Predictive(numpyro_model, samples)(jax.random.PRNGKey(1), x, sigma_y) for n in np.random.default_rng(0).integers(len(pred_y), size=100): plt.errorbar(x, y, yerr=sigma_y, fmt=".k", capsize=0) """# Evidence computation by Harmonic""" import harmonic as hm """### Reformatting sample chains""" inpsmpl = [samples[i].reshape(-1,1) for i in samples.keys() if i in ('theta','q_perp')] """### Reformatting logprob (note the minus sign)""" nuprob = np.float64(-mcmc.get_extra_fields()['potential_energy'].flatten()) chains = hm.Chains(2) chains_train, chains_infer = hm.utils.split_data(chains, training_proportion=0.5) domains = [np.array([1E-1,1E1])] # hyper-sphere bounding domain Select modelmodel = hm.model.HyperSphere(2, domains) Train modelfit_success = model.fit(chains_train.samples, chains_train.ln_posterior) Instantiate harmonic's evidence classev = hm.Evidence(chains_infer.nchains, model) Pass the evidence class the inference chains and compute the log of the evidence!ev.add_chains(chains_infer) print(np.log(evidence), evidence_std/evidence) """## Nested sampling to check the evidence""" from numpyro.contrib.nested_sampling import NestedSampler ns = NestedSampler(numpyro_model) ns.print_summary() ns.diagnostics() |
Thank you @stefanocovino! I was able to run the Colab notebook. It seems that the log(evidence) values agree between
Perhaps there is also an explanation for the different values of the std deviations? |
It seems so. However, I made no attempt to optimize the nested sampling
chain. I guess it might be much better than it is now.
S.
Il giorno mar 13 giu 2023 alle 19:10 Davide Piras ***@***.***>
ha scritto:
Thank you @stefanocovino <https://github.com/stefanocovino>! I was able
to run the Colab notebook.
It seems that the log(evidence) values agree between harmonic applied to
the NUTS samples and jaxns (as implemented in NumPyro), right? I got:
log(Z) = 13.1 ± 0.1 (NUTS+harmonic)
log(Z) = 12.8 ± 0.4 (jaxns)
Perhaps there is also an explanation for the different values of the std
deviations?
—
Reply to this email directly, view it on GitHub
<#229 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAGVYHDGFXWHSTI7HAPFPWTXLCNHJANCNFSM6AAAAAAYZ346BE>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
--
Mobilis in mobile
|
Ok, fantastic. So it seems this is working. Is it ok to close this issue then? |
I'd say yes. I wonder if it could be worth adding an example about the
management of NUTS chains to the documentation of the package. Probably
just the part from the resulting dictionary with the samples converted to
be read by Harmonic. Unless it is trivial. It wasn't for me, but this does
not mean a lot... :)
Bye,
Stefano
Il giorno mer 14 giu 2023 alle ore 09:34 Jason McEwen <
***@***.***> ha scritto:
… Ok, fantastic. So it seems this is working. Is it ok to close this issue
then?
—
Reply to this email directly, view it on GitHub
<#229 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAGVYHFIZI6LQOYHXSZOVSTXLFSPFANCNFSM6AAAAAAYZ346BE>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
--
————————————————————————————————
Only audience I care about is you.
Richard Castle to Kate Beckett
Stefano Covino
INAF / Osservatorio Astronomico di Brera
Via Emilio Bianchi 46, 23807
Merate (LC) - Italy
Tel.: +39 02 72320475 (office)
FAX: +39 02 72320401
Cell: +39 331 6748534
E-mail: ***@***.***
URL: http://www.merate.mi.astro.it/∼covino
-------------------------------------------------------------------------------------
|
The negative potential energy returned by NUTS should actually be the |
Hi David,
No, I did not. Actually, I "assumed" that it already included all the
components. I could well be wrong.
Stefano
Il giorno mar 8 ago 2023 alle ore 18:28 Davide Piras <
***@***.***> ha scritto:
… @stefanocovino <https://github.com/stefanocovino> I just realised that in
the above I referred to the negative potential energy as log_probabilities,
but that is actually the log_likelihood. However, harmonic requires the
log_posterior, so one needs to add the log_prior too.
I don't think this is currently being done in the notebook you shared, but
let me know if I missed something. I will be shortly trying to run your
notebook with the log_prior too, and check if the results change
significantly.
—
Reply to this email directly, view it on GitHub
<#229 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAGVYHHTUKDS6BUMWZCIPDTXUJSKLANCNFSM6AAAAAAYZ346BE>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
--
————————————————————————————————
Il miglior modo per avere una buona idea è avere tante idee.
Linus Pauling
Stefano Covino
INAF / Osservatorio Astronomico di Brera
Via Emilio Bianchi 46, 23807
Merate (LC) - Italy
Tel.: +39 02 72320475 (office)
FAX: +39 02 72320401
Cell: +39 331 6748534
E-mail: ***@***.***
URL: http://www.merate.mi.astro.it/∼covino
-------------------------------------------------------------------------------------
|
Hi Stefano, please bear with us as we check the above. Sorry about it. |
Just the contrary. Great you are following this issue!
S.
Il giorno mar 8 ago 2023 alle ore 18:36 Davide Piras <
***@***.***> ha scritto:
… Hi Stefano, please bear with us as we check the above. Sorry about it.
—
Reply to this email directly, view it on GitHub
<#229 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAGVYHBYLWLHUBU3UZOPRTLXUJTIFANCNFSM6AAAAAAYZ346BE>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
--
————————————————————————————————
Il miglior modo per avere una buona idea è avere tante idee.
Linus Pauling
Stefano Covino
INAF / Osservatorio Astronomico di Brera
Via Emilio Bianchi 46, 23807
Merate (LC) - Italy
Tel.: +39 02 72320475 (office)
FAX: +39 02 72320401
Cell: +39 331 6748534
E-mail: ***@***.***
URL: http://www.merate.mi.astro.it/∼covino
-------------------------------------------------------------------------------------
|
After some more checking, it seems that the potential energy returned by NUTS should actually be the negative |
Dear friends,
I am trying to apply the harmonic algorithm using chains produced by the NUTS sampler under numpyro. However, so far, with little luck. Do you have any examples to post to show how you manipulate the NUTS chains to be compatible with harmonic?
Thanks,
Stefano
The text was updated successfully, but these errors were encountered: