Blobs

Sometimes it is useful to store additional metadata or derived parameters during the run so that the user does not re-compute them afterwards. This can be achieved easily in pocoMC using the blobs framework, inspired by zeus and emcee samplers.

Any additional derived parameters can be returned by the log-likelihood function. The dtypes of these derived parameters should be defined using the blobs_dtype argument of the Sampler class.

For instance, for a Gaussian likelihood (with zero mean and unit variance in 5D) where we want to store the median value of the parameters and the number of positive parameters, we would do something like this:

import numpy as np
from scipy.stats import norm
import pocomc as pc

prior = pc.Prior(5*[norm(0,5)])

def log_likelihood(x):
    return -0.5 * np.dot(x,x), np.median(x), np.sum(x>0, dtype=int)

sampler = pc.Sampler(prior,
                    log_likelihood,
                    blobs_dtype=[('median', float), ('n_positive', int)],
                    )

sampler.run()

samples, weights, logl, logp, blobs = sampler.posterior(return_blobs=True)

print("Median: ", blobs["median"][:3])
print("Number of positive parameters: ", blobs["n_positive"][:3])
Iter: 25it [01:11,  2.84s/it, calls=11264, beta=1, logZ=-8.23, ESS=3.84e+3, acc=0.857, steps=2, logP=-15.1, eff=0.93]
Median:  [ 0.28968189 -2.48432868  1.40069292]
Number of positive parameters:  [3 1 4]