Skip to content

Commit

Permalink
Simple implementation for vector parameters #191
Browse files Browse the repository at this point in the history
  • Loading branch information
cmbant committed Apr 25, 2024
1 parent 50309b2 commit 3465837
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 16 deletions.
16 changes: 6 additions & 10 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
## 3.x
## 3.5.1

### General

- Use of vector parameters now documented (PR #191; inspired by @lukashergt, thanks!)

### Cosmology

- Added DESI 1yr BAO data and SN from Pantheon Plus, DESY5 and Union3 (thanks DESI team, @adematti, @rubind, @WillMatt4 and @rodri981)

## 3.5 – 2024-02-16
Expand Down Expand Up @@ -145,15 +150,6 @@

- Documented uses of `Model` class in general contexts (previously only cosmo)
- `Model` methods to compute log-probabilities and derived parameters now have an `as_dict` keyword (default `False`), for more informative return value.
- Added `--minimize` flag to `cobaya-run` for quick minimization (replaces sampler, uses previous output).
- Add `COBAYA_USE_FILE_LOCKING` environment variable to allow disabling of file locks. Warning not to use `--test` with MPI.
- Installation of external packages is now version-aware for some packages; added `--upgrade` option to `cobaya-install`, off by default to preserve possible user changes.
- Introduced `cobaya.component.ComponentNotFoundError` to handle cases in which internal or external components cannot be found.
- In Linux terminals, added `COBAYA_COLOR` environment variable to get colourful output, useful e.g. for debugging, but *not* recommended for output to a file (e.g. running in a cluster).

### PolyChord

- Updated to v1.20.1: adds `nfail` to control failed initialisation, `synchronous` to choose sync/async parallelisation, variable number of live points, and the possibility to use an internal maximiser. Merges #232 (thanks @williamjameshandley).

### Cosmological likelihoods and theory codes

Expand Down
2 changes: 1 addition & 1 deletion cobaya/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


__author__ = "Jesus Torrado and Antony Lewis"
__version__ = "3.5"
__version__ = "3.5.1"
__obsolete__ = False
__year__ = "2024"
__url__ = "https://cobaya.readthedocs.io"
14 changes: 12 additions & 2 deletions cobaya/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,18 @@ def _cache_add_row(self, pos: int, values: Union[Sequence[float], np.ndarray],
self._cache[pos, self._icol[name]] = -2 * value
self._cache[pos, self._icol[OutPar.chi2]] = -2 * logposterior.loglike
if len(logposterior.derived):
for name, value in zip(self.derived_params, logposterior.derived):
self._cache[pos, self._icol[name]] = value
try:
for name, value in zip(self.derived_params, logposterior.derived):
self._cache[pos, self._icol[name]] = value
except ValueError:
raise LoggedError(
self.log, "Was expecting float for derived parameter %r, but "
"got %r (type %r) instead. If you have defined this "
"parameter manually (e.g. with a 'lambda') either make "
"sure that it returns a number (or nan), or set "
"'derived: False' for this parameter, so that its value"
" is not stored in the sample.",
name, value, type(value).__class__)

def _cache_dump(self):
"""
Expand Down
37 changes: 37 additions & 0 deletions cobaya/prior.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,43 @@
value: "lambda logx: np.exp(logx)"
Vector parameters
-----------------
If your theory/likelihood takes an array of parameters as an argument, you can define the
individual components' priors separately, and then combine them into an array of any shape
using a dynamically defined parameter.
Notice that the individual components must have
``drop: True`` in order not to be passed down the pipeline, and the dynamically-defined
vector must carry ``derived: False``, since only numbers, not arrays, can be saved as
derived parameters.
You can see an example below, defining a Gaussian likelihood on the sum of two parameters,
which are taken as components of a single array-like argument:
.. code:: Python
from scipy import stats
def gauss_band(x_vector):
return stats.norm.logpdf(
x_vector[0] + x_vector[1], loc=1, scale=0.02)
# NB: don't forget the 'drop: True' in the individual parameters
# and 'derived: False' in the vector parameters
info = {
"likelihood": {"band": gauss_band},
"params": {
"x": {"prior": {"min": 0, "max": 2}, "proposal": 0.1, "drop": True},
"y": {"prior": {"min": 0, "max": 2}, "proposal": 0.1, "drop": True},
"x_vector": {"value": lambda x, y: [x, y], "derived": False}},
"sampler": {"mcmc": None}}
.. _prior_inheritance:
Changing and redefining parameters; inheritance
Expand Down
4 changes: 2 additions & 2 deletions cobaya/samplers/minimize/minimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def minuslogp_transf(x):
self.log.debug("Starting point: %r", initial_point)
self._affine_transform_baseline = initial_point
initial_point = self.affine_transform(initial_point)
np.testing.assert_allclose(initial_point, np.zeros(initial_point.shape))
assert np.allclose(initial_point, 0.)
bounds = np.array(
[self.affine_transform(self._bounds[:, i]) for i in range(2)]).T
try:
Expand Down Expand Up @@ -444,7 +444,7 @@ def products(self):
"X0": self._affine_transform_baseline}

def getdist_point_text(self, params, weight=None, minuslogpost=None):
"""Creates the multi-line string containing the minumum in GetDist format."""
"""Creates the multi-line string containing the minimum in GetDist format."""
lines = []
if weight is not None:
lines.append(' weight = %s' % weight)
Expand Down
11 changes: 10 additions & 1 deletion cobaya/theories/classy/classy.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,23 @@
sense to add it.
String-vector parameters
^^^^^^^^^^^^^^^^^^^^^^^^
At the time of writing, the CLASS Python interface takes some vector-like parameters
as string in which different components are separater by a space. To be able to set priors
or fixed values on each components, see `this trick
<https://github.com/CobayaSampler/cobaya/issues/110#issuecomment-652333489>`_, and don't
forget the ``derived: False`` in the vector parameter (thanks to Lukas Hergt).
.. _classy_modify:
Modifying CLASS
^^^^^^^^^^^^^^^
If you modify CLASS and add new variables, make sure that the variables you create are
exposed in the Python interface
(`instructions here <https://github.com/lesgourg/class_public/wiki/Python-wrapper>`__).
(`instructions here <https://github.com/lesgourg/class_public/wiki/Python-wrapper>`_).
If you follow those instructions you do not need to make any additional modification in
Cobaya.
Expand Down

0 comments on commit 3465837

Please sign in to comment.