-
Notifications
You must be signed in to change notification settings - Fork 11
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
Update mcmc to be compatible with v1.2 #32
Conversation
This versio comapres two MCMC fitting algorithms and shows the trace and corner plots
Check out this pull request on See visual diffs & provide feedback on Jupyter Notebooks. Powered by ReviewNB |
@@ -45,7 +45,7 @@ | |||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line #2. "/Users/facero/Documents/Work/Program/gammapy-0.19/gammapy-datasets/cta-1dc/caldb/data/cta/1dc/bcf/South_z20_50h/irf_file.fits"
The correct file is $GAMMAPY_DATA/cta-1dc/caldb/data/cta/1dc/bcf/South_z20_50h/irf_file.fits
Reply via ReviewNB
@@ -45,7 +45,7 @@ | |||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An easy way to do this is with the following commands:
from utils import notebook_black, notebook_strip
notebook_black(path_to_notebook)
notebook_strip(path_to_notebook)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A trick to pu maybe in the dev documentation;)
Thanks Kirsty
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Astro-Kirsty : I've tried the notebook_black thing and could not find it.
It's not in gammapy.utils
Which utils are you referring to ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apologies, it is in this repo under scripts
@@ -45,7 +45,7 @@ | |||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line #3. # Update model parameters factors inplace
Can you add a description of the parameters?
Reply via ReviewNB
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed! And if it really is factor, then I would suggest to rename the pars
to factors
for consistency.
Adding a docstring to this would be really helpful.
Then you could mention all the comments inside the docstring instead
@@ -45,7 +45,7 @@ | |||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line #4. for factor, par in zip(pars, dataset.models.parameters.free_parameters):`
pars
is a factor
in fact?
Reply via ReviewNB
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
par was badly chosen, in fact par is a gammapy parameter object and pars is a list of factors.
But it more conventional to have lnprob() function taking list of pars as input
and the factor thing is an internal gammapy thing.
Not sure what to do
for now I'm changing to . What do you think ?
def lnprob(pars, dataset):
"""
Estimate the likelihood of a model including prior on parameters.
Input :
pars : a list of parameters
dataset: a gammapy dataset
"""
# Update model parameters factors inplace
# The MCMC sampler will evaluate the likelihood of the model given
# a set of parameters. We need to update the model parameters value before
# evaluating the new likelihood value.
for factor, parameter in zip(pars, dataset.models.parameters.free_parameters):
parameter.factor = factor # What the sampler will need is the factor (e.g. 1) instead of the value (1e-12)
# dataset.likelihood returns Cash statistics values
# emcee will maximisise the LogLikelihood so we need -dataset.likelihood
total_lnprob = -dataset.stat_sum() #stat_sum now includes stat + stat_priors
return total_lnprob
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks
@@ -45,7 +45,7 @@ | |||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line #20. nwalkers, ndim, lnprob, args=[dataset], #pool=pool
lnprob
trakes two arguments. What are they?
Reply via ReviewNB
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the list of parameters (the free params in the model) and the gammapy dataset.
I've added a description in the lnprob() function definitoin (see previsou comment).
I've updated the env yaml file as the previous failure seemed to be related to a python 3.12 not compatible .
We'll need to undertand how to fix this Mambaforge issue
|
@@ -45,7 +45,7 @@ | |||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would rather make this a markdown cell, to make it more clear.
Then for the code you could utilise the python
code functionality of markdown?
Reply via ReviewNB
@@ -45,7 +45,7 @@ | |||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your effort here @facero! edit: can you correct the mcmc plot for the amplitude. The |
This is related to the automatic corner plots generation not in my "simple" control. About the PeVatrons, No good reasons for removing it, I can add it back indeed. |
I'll clean and format the notebook once we all agree
One question that we need to think about is whether we want the sampling to be done in terms of factor or values. By default I was working on factor but I realized that the model I define by default has scale = 1 for the amplitude : So the factor and the value were the same. In order to scale the amplitude I had to use : But this brings some complications for example in the trace or corner plots where the samples are shown without rescaling (amplitude close to 1 and lambda_ close to 1 even if true lamba is 0.01). The corner plot would show lambda = 5 whereas it really means 0.05. So it can be a bit confusing. Do we leave everything in terms of factor : But just not rescale or do I change everything from |
After an in person discussion we decided to keep the free_par.values approach over the factor approach. |
I clean the notebook and applied formatting on it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you a lot @facero, We have now a very nice updated recipes;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @facero . Looks good to me too!
This PR updates the recipe to match with v1.2 and in particular to make use of the new
Priors
.With the priors, there is no longer need for the
sampling.py
external file.A comparison of two MCMC sampling algorithms is shown.