Sampling from the posterior distribution with Cobaya
¶
In [ ]:
Copied!
## Uncomment the following lines to install greapy, cobaya and the required data for the sampling.
# !uv pip install greapy[extra]
# !cobaya-install bao.desi_2024_bao_all -p .
# !cobaya-install bao.desi_dr2 -p .
## Uncomment the following lines to install greapy, cobaya and the required data for the sampling.
# !uv pip install greapy[extra]
# !cobaya-install bao.desi_2024_bao_all -p .
# !cobaya-install bao.desi_dr2 -p .
In [37]:
Copied!
#Setup for plotting
%matplotlib inline
import matplotlib as mpl
mpl.rcParams['mathtext.fontset'] = 'stix'
mpl.rcParams['font.family'] = 'STIXGeneral'
mpl.rcParams['axes.unicode_minus'] = False
mpl.rcParams['text.usetex'] = False # Set to True if you have LaTeX installed
#Setup for plotting
%matplotlib inline
import matplotlib as mpl
mpl.rcParams['mathtext.fontset'] = 'stix'
mpl.rcParams['font.family'] = 'STIXGeneral'
mpl.rcParams['axes.unicode_minus'] = False
mpl.rcParams['text.usetex'] = False # Set to True if you have LaTeX installed
We also provide a wrapper for Cobaya. You can use it by mentioning it in the theory block.
In [38]:
Copied!
info={}
info['theory']={'greapy.cobaya.GREA': None}
info['sampler'] = {'mcmc': {
'Rminus1_stop':0.1,
'Rminus1_cl_stop':0.5,
'max_tries':5_000,
}
}
# Change to some output path if you want to save the results
info['output'] = None
info={}
info['theory']={'greapy.cobaya.GREA': None}
info['sampler'] = {'mcmc': {
'Rminus1_stop':0.1,
'Rminus1_cl_stop':0.5,
'max_tries':5_000,
}
}
# Change to some output path if you want to save the results
info['output'] = None
Likelihood¶
Next, we specify the likelihood. In this example, we use DESI DR1, but any likelihood installed in Cobaya will do.
In [39]:
Copied!
info['likelihood']={
'bao.desi_2024_bao_all': None,
}
info['likelihood']={
'bao.desi_2024_bao_all': None,
}
Priors¶
In [40]:
Copied!
info['params']={
# Sampled parameters
'h': {'prior': [0.6, 0.8], 'latex': r'$h$'},
'omega_cdm': {'prior': [0.1, 0.5], 'latex': r'$\omega_{\rm cdm}$'},
'omega_b': {'prior': [0.01, 0.1], 'latex': r'$\omega_{\rm b}$'},
'kappa': {'prior': [2.0, 5.0], 'latex': r'$\sqrt{-k}\eta_0$'},
# Derived parameters
'alpha': {'latex': r'$\alpha$'},
'H0': {'latex': r'$H_0$'},
'Omega_m': {'latex': r'$\Omega_m$'},
'rdrag': {'latex': r'$r_{\rm d}$'},
'rd': {'derived': 'lambda rdrag: rdrag','latex': r'$r_{\rm d}$'},
'H0rd': {'derived': 'lambda H0,rdrag: H0*rdrag','latex': r'$H_0r_{\rm d}$'},
# Fixed parameters
'Neff': 3.044
}
info['params']={
# Sampled parameters
'h': {'prior': [0.6, 0.8], 'latex': r'$h$'},
'omega_cdm': {'prior': [0.1, 0.5], 'latex': r'$\omega_{\rm cdm}$'},
'omega_b': {'prior': [0.01, 0.1], 'latex': r'$\omega_{\rm b}$'},
'kappa': {'prior': [2.0, 5.0], 'latex': r'$\sqrt{-k}\eta_0$'},
# Derived parameters
'alpha': {'latex': r'$\alpha$'},
'H0': {'latex': r'$H_0$'},
'Omega_m': {'latex': r'$\Omega_m$'},
'rdrag': {'latex': r'$r_{\rm d}$'},
'rd': {'derived': 'lambda rdrag: rdrag','latex': r'$r_{\rm d}$'},
'H0rd': {'derived': 'lambda H0,rdrag: H0*rdrag','latex': r'$H_0r_{\rm d}$'},
# Fixed parameters
'Neff': 3.044
}
Sampling¶
In [41]:
Copied!
from cobaya.run import run
updated_info, sampler = run(info)
from cobaya.run import run
updated_info, sampler = run(info)
[parameterization] *WARNING* There are repeated parameter labels: {'r_{\\rm d}': ['rdrag', 'rd']}
[bao.desi_2024_bao_all] Initialized.
[mcmc] Getting initial point... (this may take a few seconds)
[prior] Reference values or pdfs for some parameters were not provided. Sampling from the prior instead for those parameters.
[mcmc] Initial point: h:0.7464687, omega_cdm:0.1239151, omega_b:0.03306344, kappa:2.525634
[model] Measuring speeds... (this may take a few seconds)
[model] Setting measured speeds (per sec): {bao.desi_2024_bao_all: 2410.0, greapy.cobaya.GREA: 185.0}
[prior] *WARNING* Reference pdf not defined or improper for some parameters. Using prior's sigma instead for them.
[mcmc] Covariance matrix not present. We will start learning the covariance of the proposal earlier: R-1 = 30 (would be 2 if all params loaded).
[mcmc] Sampling!
[mcmc] Progress @ 2026-06-19 05:30:25 : 1 steps taken, and 0 accepted.
[mcmc] Learn + convergence test @ 160 samples accepted.
[mcmc] - Acceptance rate: 0.013
[mcmc] - Convergence of means: R-1 = 4.868164 after 128 accepted steps
[mcmc] - Updated covariance matrix of proposal pdf.
[mcmc] Learn + convergence test @ 320 samples accepted.
[mcmc] - Acceptance rate: 0.031
[mcmc] - Convergence of means: R-1 = 1.930686 after 256 accepted steps
[mcmc] - Updated covariance matrix of proposal pdf.
[mcmc] Learn + convergence test @ 480 samples accepted.
[mcmc] - Acceptance rate: 0.056
[mcmc] - Convergence of means: R-1 = 0.340914 after 384 accepted steps
[mcmc] - Updated covariance matrix of proposal pdf.
[mcmc] Learn + convergence test @ 640 samples accepted.
[mcmc] - Acceptance rate: 0.092
[mcmc] - Convergence of means: R-1 = 0.528744 after 512 accepted steps
[mcmc] - Updated covariance matrix of proposal pdf.
[mcmc] Learn + convergence test @ 800 samples accepted.
[mcmc] - Acceptance rate: 0.231
[mcmc] - Convergence of means: R-1 = 0.081924 after 640 accepted steps
[mcmc] - Updated covariance matrix of proposal pdf.
[mcmc] Learn + convergence test @ 960 samples accepted.
[mcmc] - Acceptance rate: 0.221
[mcmc] - Convergence of means: R-1 = 0.066347 after 768 accepted steps
[mcmc] - Convergence of bounds: R-1 = 0.284064 after 960 accepted steps
[mcmc] The run has converged!
[mcmc] Sampling complete after 960 accepted steps.
In [42]:
Copied!
# Export the results to GetDist
dr1_samples= sampler.products(to_getdist=True,skip_samples=0.3)["sample"]
# Export the results to GetDist
dr1_samples= sampler.products(to_getdist=True,skip_samples=0.3)["sample"]
[root] *WARNING* outlier fraction 0.15327380952380953
In [43]:
Copied!
import getdist.plots as gdplt
%matplotlib inline
gdplot = gdplt.get_subplot_plotter()
gdplot.triangle_plot(dr1_samples, ["H0","Omega_m", "kappa","alpha"], filled=True,title_limit=True)
mpl.pyplot.show()
import getdist.plots as gdplt
%matplotlib inline
gdplot = gdplt.get_subplot_plotter()
gdplot.triangle_plot(dr1_samples, ["H0","Omega_m", "kappa","alpha"], filled=True,title_limit=True)
mpl.pyplot.show()
Updated constraints with DESI DR2¶
Repeating the analysis with DESI DR2 instead is as simple as changing the likelihood
In [44]:
Copied!
dr2_info=info.copy() # Copy run settings from DR1
dr2_info['likelihood']={'bao.desi_dr2': None} # Update likelihood to DESI DR2
updated_dr2_info, dr2_sampler = run(dr2_info) # Start the sampling
dr2_info=info.copy() # Copy run settings from DR1
dr2_info['likelihood']={'bao.desi_dr2': None} # Update likelihood to DESI DR2
updated_dr2_info, dr2_sampler = run(dr2_info) # Start the sampling
[parameterization] *WARNING* There are repeated parameter labels: {'r_{\\rm d}': ['rdrag', 'rd']}
[bao.desi_dr2] Initialized.
[mcmc] Getting initial point... (this may take a few seconds)
[prior] Reference values or pdfs for some parameters were not provided. Sampling from the prior instead for those parameters.
[mcmc] Initial point: h:0.7973101, omega_cdm:0.1355908, omega_b:0.03608247, kappa:4.461816
[model] Measuring speeds... (this may take a few seconds)
[model] Setting measured speeds (per sec): {bao.desi_dr2: 2360.0, greapy.cobaya.GREA: 135.0}
[prior] *WARNING* Reference pdf not defined or improper for some parameters. Using prior's sigma instead for them.
[mcmc] Covariance matrix not present. We will start learning the covariance of the proposal earlier: R-1 = 30 (would be 2 if all params loaded).
[mcmc] Sampling!
[mcmc] Progress @ 2026-06-19 05:30:51 : 1 steps taken, and 0 accepted.
[mcmc] Learn + convergence test @ 160 samples accepted.
[mcmc] - Acceptance rate: 0.007
[mcmc] - Convergence of means: R-1 = 18.036765 after 128 accepted steps
[mcmc] - Updated covariance matrix of proposal pdf.
[mcmc] Learn + convergence test @ 320 samples accepted.
[mcmc] - Acceptance rate: 0.016
[mcmc] - Convergence of means: R-1 = 4.577595 after 256 accepted steps
[mcmc] - Updated covariance matrix of proposal pdf.
[mcmc] Learn + convergence test @ 480 samples accepted.
[mcmc] - Acceptance rate: 0.034
[mcmc] - Convergence of means: R-1 = 0.615836 after 384 accepted steps
[mcmc] - Updated covariance matrix of proposal pdf.
[mcmc] Learn + convergence test @ 640 samples accepted.
[mcmc] - Acceptance rate: 0.083
[mcmc] - Convergence of means: R-1 = 0.666447 after 512 accepted steps
[mcmc] - Updated covariance matrix of proposal pdf.
[mcmc] Learn + convergence test @ 800 samples accepted.
[mcmc] - Acceptance rate: 0.229
[mcmc] - Convergence of means: R-1 = 0.264684 after 640 accepted steps
[mcmc] - Updated covariance matrix of proposal pdf.
[mcmc] Learn + convergence test @ 960 samples accepted.
[mcmc] - Acceptance rate: 0.225
[mcmc] - Convergence of means: R-1 = 0.213501 after 768 accepted steps
[mcmc] - Updated covariance matrix of proposal pdf.
[mcmc] Learn + convergence test @ 1120 samples accepted.
[mcmc] - Acceptance rate: 0.212
[mcmc] - Convergence of means: R-1 = 0.283618 after 896 accepted steps
[mcmc] - Updated covariance matrix of proposal pdf.
[mcmc] Learn + convergence test @ 1280 samples accepted.
[mcmc] - Acceptance rate: 0.202
[mcmc] - Convergence of means: R-1 = 0.132171 after 1024 accepted steps
[mcmc] - Updated covariance matrix of proposal pdf.
[mcmc] Learn + convergence test @ 1440 samples accepted.
[mcmc] - Acceptance rate: 0.196
[mcmc] - Convergence of means: R-1 = 0.144726 after 1152 accepted steps
[mcmc] - Updated covariance matrix of proposal pdf.
[mcmc] Learn + convergence test @ 1600 samples accepted.
[mcmc] - Acceptance rate: 0.190
[mcmc] - Convergence of means: R-1 = 0.140266 after 1280 accepted steps
[mcmc] - Updated covariance matrix of proposal pdf.
[mcmc] Learn + convergence test @ 1760 samples accepted.
[mcmc] - Acceptance rate: 0.186
[mcmc] - Convergence of means: R-1 = 0.178641 after 1408 accepted steps
[mcmc] - Updated covariance matrix of proposal pdf.
[mcmc] Learn + convergence test @ 1920 samples accepted.
[mcmc] - Acceptance rate: 0.179
[mcmc] - Convergence of means: R-1 = 0.167148 after 1536 accepted steps
[mcmc] - Updated covariance matrix of proposal pdf.
[mcmc] Learn + convergence test @ 2080 samples accepted.
[mcmc] - Acceptance rate: 0.175
[mcmc] - Convergence of means: R-1 = 0.183369 after 1664 accepted steps
[mcmc] - Updated covariance matrix of proposal pdf.
[mcmc] Learn + convergence test @ 2240 samples accepted.
[mcmc] - Acceptance rate: 0.170
[mcmc] - Convergence of means: R-1 = 0.190908 after 1792 accepted steps
[mcmc] - Updated covariance matrix of proposal pdf.
[mcmc] Learn + convergence test @ 2400 samples accepted.
[mcmc] - Acceptance rate: 0.169
[mcmc] - Convergence of means: R-1 = 0.101986 after 1920 accepted steps
[mcmc] - Updated covariance matrix of proposal pdf.
[mcmc] Progress @ 2026-06-19 05:31:51 : 34734 steps taken, and 2420 accepted.
[mcmc] Learn + convergence test @ 2560 samples accepted.
[mcmc] - Acceptance rate: 0.167
[mcmc] - Convergence of means: R-1 = 0.089206 after 2048 accepted steps
[mcmc] - Updated covariance matrix of proposal pdf.
[mcmc] Learn + convergence test @ 2720 samples accepted.
[mcmc] - Acceptance rate: 0.165
[mcmc] - Convergence of means: R-1 = 0.132527 after 2176 accepted steps
[mcmc] - Updated covariance matrix of proposal pdf.
[mcmc] Learn + convergence test @ 2880 samples accepted.
[mcmc] - Acceptance rate: 0.162
[mcmc] - Convergence of means: R-1 = 0.108840 after 2304 accepted steps
[mcmc] - Updated covariance matrix of proposal pdf.
[mcmc] Learn + convergence test @ 3040 samples accepted.
[mcmc] - Acceptance rate: 0.162
[mcmc] - Convergence of means: R-1 = 0.094494 after 2432 accepted steps
[mcmc] - Updated covariance matrix of proposal pdf.
[mcmc] Learn + convergence test @ 3200 samples accepted.
[mcmc] - Acceptance rate: 0.160
[mcmc] - Convergence of means: R-1 = 0.100024 after 2560 accepted steps
[mcmc] - Updated covariance matrix of proposal pdf.
[mcmc] Learn + convergence test @ 3360 samples accepted.
[mcmc] - Acceptance rate: 0.159
[mcmc] - Convergence of means: R-1 = 0.091242 after 2688 accepted steps
[mcmc] - Updated covariance matrix of proposal pdf.
[mcmc] Learn + convergence test @ 3520 samples accepted.
[mcmc] - Acceptance rate: 0.160
[mcmc] - Convergence of means: R-1 = 0.106704 after 2816 accepted steps
[mcmc] - Updated covariance matrix of proposal pdf.
[mcmc] Learn + convergence test @ 3680 samples accepted.
[mcmc] - Acceptance rate: 0.159
[mcmc] - Convergence of means: R-1 = 0.059404 after 2944 accepted steps
[mcmc] - Updated covariance matrix of proposal pdf.
[mcmc] Learn + convergence test @ 3840 samples accepted.
[mcmc] - Acceptance rate: 0.159
[mcmc] - Convergence of means: R-1 = 0.065350 after 3072 accepted steps
[mcmc] - Convergence of bounds: R-1 = 0.176563 after 3840 accepted steps
[mcmc] The run has converged!
[mcmc] Sampling complete after 3840 accepted steps.
In [45]:
Copied!
%matplotlib inline
dr2_samples= dr2_sampler.products(to_getdist=True,skip_samples=0.3)["sample"]
gdplot = gdplt.get_subplot_plotter()
gdplot.triangle_plot(dr2_samples, ["H0","Omega_m", "kappa","alpha"], filled=True,title_limit=True)
%matplotlib inline
dr2_samples= dr2_sampler.products(to_getdist=True,skip_samples=0.3)["sample"]
gdplot = gdplt.get_subplot_plotter()
gdplot.triangle_plot(dr2_samples, ["H0","Omega_m", "kappa","alpha"], filled=True,title_limit=True)
[root] *WARNING* outlier fraction 0.005952380952380952
DR1 vs DR2¶
In [46]:
Copied!
gdplot = gdplt.get_subplot_plotter()
gdplot.triangle_plot([dr1_samples,dr2_samples], ["H0","Omega_m", "kappa","alpha"], filled=True, legend_labels=["DESI DR1","DESI DR2"], legend_loc='upper right' )
gdplot = gdplt.get_subplot_plotter()
gdplot.triangle_plot([dr1_samples,dr2_samples], ["H0","Omega_m", "kappa","alpha"], filled=True, legend_labels=["DESI DR1","DESI DR2"], legend_loc='upper right' )