brinckmann / montepython_public

Public repository for the Monte Python Code
MIT License
90 stars 77 forks source link

how to use BOBYQA with Montepython #236

Open yashi0195 opened 2 years ago

yashi0195 commented 2 years ago

I have the following queries:

  1. Is there any procedure to obtain the best fit values for parameters with chi-square minimization using BOBYQA (Powell 2009) in montepython (as it is present in cosmomc).
  2. If not, is there any other similar minimization routine in montepython, to deeper span the parameter space after the analysis of chains to converge upon a minimum chi-square.

Thanks in advance!

brinckmann commented 2 years ago

MontePython has a wrapper for scipy minimizers in the function get_minimum starting on line 356 of montepython/sampler.py , which is called via the flags --minimize and --minimize-tol <float> .

BOBYQA is not available in the scipy package, but it hopefully shouldn't be too difficult to adapt the function to instead wrap the BOBYQA minimizer. Since I did not have a lot of luck applying the scipy minimizers to the multi-dimensional parameter space we usually consider in cosmology (I found they were either too slow or not precise enough), adding the BOBYQA minimizer instead may be worthwhile doing. If you (or anyone else) would like to try to do this I'd be happy to provide assistance.

Best, Thejs

alexreevesy commented 2 years ago

Dear both,

I have now adapted the MontePython function get_minimum to wrap the BOBYQA minimizer and after doing some initial tests it seems that all is working pretty well and the minimum chi-2 fond using this method is consistently lower than for the default scipy minimizer.

Thejs- would you like me to send the adapted code?

Best wishes,

Alexander Reeves

brinckmann commented 2 years ago

Hi Alex,

Certainly! You can either send me an email with your modifications or do a pull request. Make sure to include a line giving yourself credit!

Thanks a lot!

Best, Thejs

alexreevesy commented 2 years ago

I'll write to you in an email so I can add a few sentences about exactly what I've done:).

Thanks,

Alex

HoisW commented 1 year ago

Hi, alexreevesy!

I try your mcmc.py, parser_mp.py and sampler.py, and use the flag --bobyqa to use BOBYQA MINIMISER. But an error is reported: ... File "montepython/sampler.py", line 478, in get_minimum lower = np.zeros([len(parameter_names), ], 'float64') UnboundLocalError: local variable 'parameter_names' referenced before assignment ...

Do you know how to fix it?

brinckmann commented 1 year ago

This reminds me I still need to add this to the official version (sorry Alex!)

In the montepython version on Alex's github you find on line 374 of montepython/sampler.py parameter_names = data.get_mcmc_parameters(['varying']) This needs to be move to outside the if statement if not command_line.bobyqa: or a added after the else statement before line 478.

Best, Thejs

HoisW commented 1 year ago

Thanks for your guide! In the montepython version on Alex's github, I move line 368~385 to the line before 363, and it works. Best, Hois

alexreevesy commented 1 year ago

Hi both,

Apologies for my delay in getting back to this- I had a look and indeed I made an error in transporting my code- apologies! I think after this change everything should be working as required but feel free to email me (areeves@phys.ethz.ch) if you have any questions about this. I also experimented with a few other minimizers in a Cosmological context which could be better suited depending on your use case.

Cheers,

Alex

alexreevesy commented 1 year ago

@brinckmann if there's anything I can do on my end to help put this in the official version please let me know:).

brinckmann commented 1 year ago

@alexreevesy It's mostly just on us to find the time to get the next version of MontePython out, which has been pending for very long. Currently there are some other priorities, but I'm really hoping we can get to this before Christmas. But if you want to make it marginally easier and improve your visibility then you can set up a pull request (after fixing the bug! ;) ).

Best, Thejs