Skip to content

Customized inference

Matteo Breschi edited this page May 26, 2022 · 2 revisions

The user might be interested in running a parameter estimation job providing a customized likelihood and prior. In bajes this is possible employing the main routine.

Specify the prior

The user should write a prior.ini file containing the information associated to every parameter, e.g.

[x1]
min=-10
max=10

[x2]
min=-10
max=10

[x3]
min=-10
max=10
prior=custom

The names of the parameters are taken from the string contained in the brackets. The min and max arguments are mandatory and they define the prior bounds for each parameter. Additional information can be passed to the prior object: the periodic argument activates periodic boundary conditions and the prior option modifies the prior distribution. For the latter, the following keywords are allowed:

  • uniform (default)
  • log-uniform
  • linear
  • quadratic
  • cosinusoidal
  • sinusoidal
  • power-law (requires deg)
  • triangular (requires mode)
  • exponential (requires tau)
  • normal (requires mu and sigma)
  • custom

In our example, the prior.ini file will generate a three-dimensional parameter space, where x1 and x2 are uniformly distributed, while x3 has a customized prior distribution. With the option prior=custom, the routine will inspect the like.py file for a log_prior_{parameter-name} method.

Define the likelihood

Then, the user can write a customized likelihood in a like.py file defining a standard Python method. The function should be named log_like and it should take a single argument corrisponding to a parameter dictionary. Following the previous example, we can define a Gaussian likelihood as

import numpy as np

def log_prior_x3(x):
    return 0.5*np.log(1+x**2)

def log_like(pars):
    x = np.array([pars['x1'], pars['x2'], pars['x3']])
    return -0.5*np.sum(x**2)

Note that the like.py contains also the method for the prior of x3, named log_prior_x3 and requested by the argument prior=custom. In this case the prior method is expected to receive a float number.

Parameter estimation

Given the prior.ini and like.py files, the parameter estimation run can be submitted with the command

python -m bajes -p prior.ini -l like.py -o ./outdir/

where the command -o points to the output directory. The full list of arguments can be found typing python -m bajes --help. Visit inf_tutorial for more details.

Clone this wiki locally