CosmoCoffee Forum Index CosmoCoffee

 
 FAQFAQ   SearchSearch  MemberlistSmartFeed   MemberlistMemberlist    RegisterRegister 
   ProfileProfile   Log inLog in 
Arxiv New Filter | Bookmarks & clubs | Arxiv ref/author:

Cosmomc: importance sampling
 
Post new topic   Reply to topic    CosmoCoffee Forum Index -> Computers and software
View previous topic :: View next topic  
Author Message
Shu-Rong Chen



Joined: 12 Aug 2015
Posts: 7
Affiliation: National Taiwan University

PostPosted: October 25 2016  Reply with quote

Hello,
I'm using CosmoMC as a generic sampler.
I want to apply the constraint of one parameter derived from another MCMC analysis to further importance sampling the new data.

I adopt the following steps to use functions in GetDist to do importance sampling:
1.) use get1DDensity() and Prob() to get the probability distribution in another MCMC analysis
2.) use reweightAddingLogLikes(logLikes) with logLike=-log(probability)
to do importance sampling

Could you let me know whether it's the correct procedure to use GetDist for importance sampling?
If not, could you tell me which part of the CosmoMC code I should modify to load a .data file and recalculate the weight and likelihood to do importance sampling (action=1)?

I really appreciate your time and help.
Back to top
View user's profile  
Antony Lewis



Joined: 23 Sep 2004
Posts: 1248
Affiliation: University of Sussex

PostPosted: October 25 2016  Reply with quote

You can do something like this (where "samples" is an MCSamples instance)
Code:
 
#Example of how to do importance sampling, modifying the samples by re-weighting by a new likelihood
#e.g. to modify samples to be from the original distribution times a Gaussian in x1 (centered on 1, with sigma=1.2)

import copy
new_samples = copy.deepcopy(samples) #make a copy so don't change the original
p=samples.getParams()
new_loglike = (p.x1-1)**2/1.2**2/2
new_samples.loglikes = np.zeros(samples.numrows) #code currently assumes existing loglikes are set, set to zero here
new_samples.reweightAddingLogLikes(new_loglike) #re-weight cut_samples to account for the new likelihood


You don't normally need to modify CosmoMC to use it with action=1.
Back to top
View user's profile [ Hidden ] Visit poster's website
Shu-Rong Chen



Joined: 12 Aug 2015
Posts: 7
Affiliation: National Taiwan University

PostPosted: October 26 2016  Reply with quote

Thank you for the prompt and helpful reply!

Since I want to use the constraint from a previous MCMC run,
I was wondering if the following procedure is correct.

("samples_prior" is a MCSamples instance in a previous MCMC run)
Code:
new_loglike = np.zeros( samples.numrows )
prior1 = samples_prior.get1DDensity( 'x1' )
for i in range( 0 , samples1.numrows , 1 ):
   new_loglike[i] = -math.log( prior1.Prob(p.mb[i]) )


In addition, CosmoMC ReadMe suggests that, for importance sampling,
it's better to thin the chains further in order to keep nearly
uncorrelated samples.Should I thin the chain further when loading it
as "samples" (please refer to the code above)?

Thanks again for your great help!
Back to top
View user's profile  
Antony Lewis



Joined: 23 Sep 2004
Posts: 1248
Affiliation: University of Sussex

PostPosted: October 26 2016  Reply with quote

You don't need to thin.

However the importance sampling should be done in the full parameter space, not using one-dimensional marginalized distributions - i.e. your suggestion would be correct only if other likelihood only depends on x1 (and other parameters independent of the current likelihood). Using action=1 cosmomc run to re-weight all points by a another likelihood if needed.
Back to top
View user's profile [ Hidden ] Visit poster's website
Shu-Rong Chen



Joined: 12 Aug 2015
Posts: 7
Affiliation: National Taiwan University

PostPosted: October 27 2016  Reply with quote

Could you tell me which part of the CosmoMC code I should modify in order to use "action=1" to load another likelihood for re-weighting all points?

Thank you so much for the great help!
Back to top
View user's profile  
Antony Lewis



Joined: 23 Sep 2004
Posts: 1248
Affiliation: University of Sussex

PostPosted: October 27 2016  Reply with quote

Why do you need to modify it?
Back to top
View user's profile [ Hidden ] Visit poster's website
Shu-Rong Chen



Joined: 12 Aug 2015
Posts: 7
Affiliation: National Taiwan University

PostPosted: October 28 2016  Reply with quote

Sorry for the confusion. I'm a bit puzzled because I'm not using any
built-in likelihood installed in the batch2/ directory.

I understand that, for example, if I want to use the built-in BAO
constraint, I can add “DEFAULT(batch2/BAO.ini)” to the parameter file
"params_generic.ini". Then the corresponding datasets (MCMC chains)
will be loaded to re-weight all data points.

However, since I want to load a likelihood generated by my own
previous CosmoMC run, I was wondering whether I need to add something
to "params_generic.ini" and/or modify any CosmoMC code in order to
re-weight data accordingly.

Again, I really appreciate your time and kind help!
Back to top
View user's profile  
Antony Lewis



Joined: 23 Sep 2004
Posts: 1248
Affiliation: University of Sussex

PostPosted: October 28 2016  Reply with quote

Generally speaking, you cannot get a reliable likelihood from a previous MC run. But if you really want to do that (e.g. using a low-dimensional KDE fit), you'd have to make your own new likelihood module.
Back to top
View user's profile [ Hidden ] Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    CosmoCoffee Forum Index -> Computers and software All times are GMT + 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group. Sponsored by WordWeb online dictionary and dictionary software.