pmelchior / scarlet2

Scarlet, all new and shiny
MIT License
14 stars 3 forks source link

added options to NUTS; closes #57 #61

Closed pmelchior closed 3 months ago

pmelchior commented 3 months ago

This PR fixes #57 and brings more flexibility to run the MCMC sampler. We can now specify the full set of arguments for the NUTS sampler from numpyro.

For reference, these are a few example uses:


import numpyro.distributions as dist
parameters = scene.make_parameters()
prior = dist.Normal(scene.sources[0].morphology.center, scale=0.1)
parameters += Parameter(scene.sources[0].morphology.center, name="center:0", prior=prior)

from numpyro.infer.initialization import init_to_sample
num_samples = 100
mcmc = scene.sample(obs, parameters, num_samples=num_samples, dense_mass=True, init_strategy=init_to_sample)
mcmc.print_summary()

import corner
corner.corner(mcmc).show()

scenes = []
samples = mcmc.get_samples()
for i in range(num_samples):
    sample_ = tuple(samples[p.name][i] for p in parameters)
    scenes.append(scene.replace(parameters, sample_))

ani = plot.scene(scenes, obs, norm=norm, show_model=True, show_rendered=True, show_observed=True, show_residual=True)
ani.save("test.mp4")

which produces summaries and corner plots and a video of the sampler results (you have to look hard for the residuals of the point source fit, the center is constrained to within 0.002 of a pixel):

https://github.com/pmelchior/scarlet2/assets/1463403/4bd1c657-3c9d-4203-bd6b-7cd17d41f034