Working with posteriors and popclass
popclass includes convenience functions for ingesting common inference data objects. While an array of samples and associated parameter labels can be passed directly to popclass.Posterior, we currently support conversion from the following output formats:
ArviZ
pymultinest
This notebook will provide the information to interface with the different posteriors that popclass supports, as well as general construction.
[28]:
# imports
import numpy as np
import arviz as az
from pymultinest import Analyzer
import sys
from popclass.posterior import Posterior
popclass.Posterior general handling
Constructing a popclass.Posterior requires two sets of information:
posterior_samples(array,(N_samples, N_dim)): posterior samplesparameter_labels(list,(N_dim)): ordedred list of the parameters corresponding to the samples.
Initializing a Posterior for popclass can be as simple as the following:
[21]:
test_samples = np.random.rand(1000, 3)
test_params = ["A", "B", "C"]
post = Posterior(
samples=test_samples,
parameter_labels=test_params
)
Interacting with a Posterior is also straightforward. To access the attributes of the object:
[ ]:
print(post.samples[0], post.parameter_labels)
Converting from an existing posterior
popclass includes utilities to convert from existing posterior formats.
We can demonstrate by mocking up a simple ArviZ posterior:
[23]:
post = {
test_params[0]: test_samples[:, 0],
test_params[1]: test_samples[:, 1],
test_params[2]: test_samples[:, 2],
}
az_inference_data = az.convert_to_inference_data(post)
popclass.Posterior includes a classmethod for interacting with an existing ArviZ posterior.
[ ]:
popclass_post = Posterior.from_arviz(az_inference_data)
print(popclass_post.samples[0], popclass_post.parameter_labels)
We can follow a similar workflow to interface with pymultinest:
First, mock up a posterior:
[ ]:
test_samples = np.loadtxt("../../../tests/test_post_equal_weights.dat")
pymultinest_posterior = Analyzer(3, "../../../tests/test_")
test_params = ["A", "B", "C"]
[ ]:
popclass_post = Posterior.from_pymultinest(pymultinest_posterior, test_params)
print(popclass_post.samples[0], popclass_post.parameter_labels)