CobayaSampler / cobaya

Code for Bayesian Analysis
http://cobaya.readthedocs.io/en/latest/
Other
128 stars 127 forks source link

camb spectra on fixed params change after Starting minimization #142

Closed kimmywu closed 3 years ago

kimmywu commented 3 years ago

Hello,

I am testing the minimizer by sampling 1 (non-CAMB) parameter r_free with the rest of the CAMB input parameter fixed. Since I am not sampling the CAMB parameters, the output spectra (and in this case Cl['bb']) should remain the same. However the output from theory.get_Cl() are not the same before and after Starting Minimization.

I plotted the two Clbb outputs before and after minimization started:

Clbb comparison

The following shows part of the debug output. The likelihood on the same set of parameters was calculated before and after minimization was launched, with 'r_free': 0.2136268061607224 (and all other parameters fixed). Because of the difference in the output clbb from CAMB, the log-likelihood changed value.

I have included a minimum working example that reproduces this phenomenon: likelihood definition and yaml file.

Please advise. Thanks!

 2021-02-17 20:53:37,935 [minimize] Initializing
 2021-02-17 20:53:37,937 [prior] Evaluating prior at array([0.21362681])
 2021-02-17 20:53:37,938 [prior] Got logpriors = [0.0]
 2021-02-17 20:53:37,938 [model] Got input parameters: {'Alens': 1.0, 'r_free': 0.2136268061607224, 'ns': 0.9660499, 'tau': 0.05430842, 'ombh2': 0.0223828, 'omch2': 0.1201075, 'H0': 67.32117, 'As': 2.0957177373e-09}
 2021-02-17 20:53:37,938 [camb.transfers] Got parameters {'Alens': 1.0, 'tau': 0.05430842, 'ombh2': 0.0223828, 'omch2': 0.1201075, 'H0': 67.32117}
 2021-02-17 20:53:37,938 [camb.transfers] Computing new state
 2021-02-17 20:53:37,938 [camb] Setting parameters: {'Alens': 1.0, 'tau': 0.05430842, 'ombh2': 0.0223828, 'omch2': 0.1201075, 'H0': 67.32117} and {'lens_potential_accuracy': 2, 'num_massive_neutrinos': 1, 'nnu': 2.046, 'theta_H0_range': [20, 100], 'omk': 0, 'omnuh2': 0.0006451439, 'YHe': 0.2454006, 'nonlinear': 'NonLinear_lens', 'max_eta_k': 50000, 'max_eta_k_tensor': 3000, 'AccuracyBoost': 3, 'lAccuracyBoost': 1, 'lSampleBoost': 1, 'kmax': 1.2, 'lmax': 2400}
 2021-02-17 20:53:37,939 [root] *WARNING* Using kmax=1.2 with Halofit non-linear models may give inaccurate results
 2021-02-17 20:53:37,939 [camb] Setting attributes of CAMBparams: {'Want_CMB': True, 'Want_cl_2D_array': False, 'WantCls': True, 'NonLinear': 'NonLinear_lens'}
 2021-02-17 20:53:46,154 [camb] Got parameters {'ns': 0.9660499, 'As': 2.0957177373e-09}
 2021-02-17 20:53:46,154 [camb] Computing new state
 2021-02-17 20:53:51,818 [bblike.simplelike] Got parameters {'r_free': 0.2136268061607224, 'ns': 0.9660499, 'tau': 0.05430842, 'ombh2': 0.0223828, 'omch2': 0.1201075, 'H0': 67.32117, 'As': 2.0957177373e-09}
 2021-02-17 20:53:51,819 [bblike.simplelike] Computing new state
 2021-02-17 20:53:51,820 [bblike.simplelike] Computed log-likelihood = -1167.58
 2021-02-17 20:53:51,821 [minimize] Starting from random initial point:
 2021-02-17 20:53:51,821 [minimize] {'r_free': 0.2136268061607224}
 2021-02-17 20:53:51,822 [minimize] Arguments for pybobyqa.solve:
{'x0': array([0.]), 'bounds': array([[-7.13626806],
       [ 2.86373194]]), 'seek_global_minimum': True, 'maxfun': 1000000, 'rhoend': 0.05}
 2021-02-17 20:53:51,832 [minimize] Starting minimization.
 2021-02-17 20:53:51,832 [model] Got input parameters: {'Alens': 1.0, 'r_free': 0.2136268061607224, 'ns': 0.9660499, 'tau': 0.05430842, 'ombh2': 0.0223828, 'omch2': 0.1201075, 'H0': 67.32117, 'As': 2.0957177373e-09}
 2021-02-17 20:53:51,832 [camb.transfers] Got parameters {'Alens': 1.0, 'tau': 0.05430842, 'ombh2': 0.0223828, 'omch2': 0.1201075, 'H0': 67.32117}
 2021-02-17 20:53:51,833 [camb.transfers] Computing new state
 2021-02-17 20:53:51,833 [camb] Setting parameters: {'Alens': 1.0, 'tau': 0.05430842, 'ombh2': 0.0223828, 'omch2': 0.1201075, 'H0': 67.32117} and {'lens_potential_accuracy': 2, 'num_massive_neutrinos': 1, 'nnu': 2.046, 'theta_H0_range': [20, 100], 'omk': 0, 'omnuh2': 0.0006451439, 'YHe': 0.2454006, 'nonlinear': 'NonLinear_lens', 'max_eta_k': 50000, 'max_eta_k_tensor': 3000, 'AccuracyBoost': 3, 'lAccuracyBoost': 1, 'lSampleBoost': 1, 'kmax': 1.2, 'lmax': 2400}
 2021-02-17 20:54:00,298 [camb] Got parameters {'ns': 0.9660499, 'As': 2.0957177373e-09}
 2021-02-17 20:54:00,298 [camb] Computing new state
 2021-02-17 20:54:06,017 [bblike.simplelike] Got parameters {'r_free': 0.2136268061607224, 'ns': 0.9660499, 'tau': 0.05430842, 'ombh2': 0.0223828, 'omch2': 0.1201075, 'H0': 67.32117, 'As': 2.0957177373e-09}
 2021-02-17 20:54:06,017 [bblike.simplelike] Computing new state
 2021-02-17 20:54:06,018 [bblike.simplelike] Computed log-likelihood = -1349.86
cmbant commented 3 years ago

I'm looking at it - it's caused by setting nnu/YHe via extra_args, which we should be able to fix. (but I guess you don't really want only 2 neutrinos anyway!?)

Btw, likelihoods should only depend on C_l not cosmological parameters, so better to fix those params in the yaml.

cmbant commented 3 years ago

Try https://github.com/CobayaSampler/cobaya/commit/b17e233adc6d7dae7f571f80df8431034273f551

kimmywu commented 3 years ago

@cmbant thanks for the quick response. The number of neutrinos doesn't matter for what I'm doing, but I agree that 3.046 is more standard.

Btw, likelihoods should only depend on C_l not cosmological parameters, so better to fix those params in the yaml.

Yeah, I had other iterations that had a separate simplelike.yaml file to define the parameters. But in the readthedocs, this example teaches us to put parameters in the likelihood class definition:

class MyLikelihood(Likelihood):
    cl_file = "/path/do/data_file"
    # Aliases for automatic covariance matrix
    aliases = ["myOld"]
    # Speed in evaluations/second (after theory inputs calculated).
    speed = 500
    params = {"my_foreground_amp":
                  {"prior": {"dist": "uniform", "min": 0, "max": 0},
                   "ref" {"dist": "norm", "loc": 153, "scale": 27},
                   "proposal": 27,
                   "latex": r"A^{f}_{\rm{mine}"}}

Is this an exception or should this example be removed as this is not advisable? Or do you just mean the LCDM parameters shouldn't be in the likelihood definition?

kimmywu commented 3 years ago

Try b17e233

Thanks. On it.

cmbant commented 3 years ago

Likelihood-specific parameters should be in the likelihood, but not LCDM/cosmology/camb parameters, since likelihoods only depend on them indirectly via C_l.

kimmywu commented 3 years ago

Fixed. Thanks!