COBAYA: Issue with passing parameters values from config file

Use of Cobaya. camb, CLASS, cosmomc, compilers, etc.
Post Reply
Gabriele Autieri
Posts: 4
Joined: August 06 2024
Affiliation: SISSA

COBAYA: Issue with passing parameters values from config file

Post by Gabriele Autieri »

Hi everyone,

I'm trying to make my own likelihood class in Cobaya. However, I'm struggling with nuisance parameters. To make everything more clear, I'll make a simple example. Let's say this is the code for my likelihood class:

Code: Select all

from cobaya.likelihood import Likelihood
import numpy as np

class MyLikelihood(Likelihood):

    def initialize(self):
        # No external data files needed for this example
        pass

    def get_requirements(self):
        # No specific requirements for this simple example
        return {}

    def logp(self, **params_values):
        # Access the nuisance parameters
        print(params_values)
        my_foreground_amp = params_values['my_foreground_amp']
        another_nuisance_param = params_values['another_nuisance_param']
        H0 = params_values['H0']
        
        model_value = 70 + my_foreground_amp * 0.01 + another_nuisance_param * 0.1
        observed_value = 70  

        chi2 = (observed_value - model_value) ** 2 / 2.0 

        return -chi2 / 2
And this is the .yaml file in which the nuisance parameters are defined:

Code: Select all

likelihood:
  mylikelihood.MyLikelihood:
    

params:
  my_foreground_amp:
    prior:
      dist: uniform
      min: 0
      max: 100
    proposal: 27
    latex: A^{f}_{\rm{mine}}

  another_nuisance_param:
    prior:
      dist: norm
      loc: 1
      scale: 0.1
    proposal: 0.01
    latex: \theta_{\rm{nuisance}}

  H0:
    prior:
      dist: norm
      loc: 70
      scale: 2
    proposal: 0.5
    latex: H_0

sampler:
  mcmc:
    max_tries: 1000
However, when I try to run cobaya with this setup, I always get an error of this form:
Could not find anything to use input parameter(s) {'another_nuisance_param', 'my_foreground_amp', 'H0'}
stating that the nuisance parameters are not used in any likelihood or theory code. How do I fix this?

Thanks in advance for the help,

Gabriele Autieri
Antony Lewis
Posts: 1980
Joined: September 23 2004
Affiliation: University of Sussex
Contact:

Re: COBAYA: Issue with passing parameters values from config file

Post by Antony Lewis »

Your likelihood needs to either have an attribute

Code: Select all

params = {'another_nuisance_param': None, .. etc}
or declare params in a likelihood yaml. Or return the parameter names from .get_can_support_params() if you want to optionally support them. For H0, you can return from get_requirements() if you might be using from CAMB etc and not sampling it directly, or you can treat is as a parameter like the nuisances.
Gabriele Autieri
Posts: 4
Joined: August 06 2024
Affiliation: SISSA

Re: COBAYA: Issue with passing parameters values from config file

Post by Gabriele Autieri »

Hello and thank you for your very informative and quick answer!

I tried by specifying the parameters with an attribute and it worked!
On the other hand, I'm not sure I understand fully how to declare the parameters with a likelihood yaml file, nor how to use .get_can_support_params(). If you could point me to a reference/where to understand more about this, I would really appreciate it!
Gabriele Autieri
Posts: 4
Joined: August 06 2024
Affiliation: SISSA

Re: COBAYA: Issue with passing parameters values from config file

Post by Gabriele Autieri »

Thank you very much for answering very quickly and clearly!
Post Reply