Differentiable (binned) likelihoods in JAX.
Project description
evermore
Differentiable (binned) likelihoods in JAX.
Installation
python -m pip install evermore
From source:
git clone https://github.com/pfackeldey/evermore
cd evermore
python -m pip install .
Example - Model and Loss Definition
See more in examples/
evermore in a nutshell:
import equinox as eqx
import jax
import jax.numpy as jnp
from jaxtyping import Array
import evermore as evm
jax.config.update("jax_enable_x64", True)
# define a simple model with two processes and two parameters
class Model(eqx.Module):
mu: evm.FreeFloating
syst: evm.NormalConstrained
def __call__(self, hists: dict[str, Array]) -> Array:
mu_modifier = self.mu.unconstrained()
syst_modifier = self.syst.log_normal(up=jnp.array([1.1]), down=jnp.array([0.9]))
return mu_modifier(hists["signal"]) + syst_modifier(hists["bkg"])
nll = evm.loss.PoissonNLL()
def loss(model: Model, hists: dict[str, Array], observation: Array) -> Array:
expectation = model(hists)
# Poisson NLL of the expectation and observation
log_likelihood = nll(expectation, observation)
# Add parameter constraints from logpdfs
constraints = evm.loss.get_log_probs(model)
log_likelihood += evm.util.sum_leaves(constraints)
return -jnp.sum(log_likelihood)
# setup model and data
hists = {"signal": jnp.array([3]), "bkg": jnp.array([10])}
observation = jnp.array([15])
model = Model(mu=evm.FreeFloating(), syst=evm.NormalConstrained())
# negative log-likelihood
loss_val = loss(model, hists, observation)
# gradients of negative log-likelihood w.r.t. model parameters
grads = eqx.filter_grad(loss)(model, hists, observation)
print(f"{grads.mu.value=}, {grads.syst.value=}")
# -> grads.mu.value=Array([-0.46153846]), grads.syst.value=Array([-0.15436207])
Contributing
See CONTRIBUTING.md for instructions on how to contribute.
License
Distributed under the terms of the BSD license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
evermore-0.2.5.tar.gz
(130.7 kB
view details)
Built Distribution
evermore-0.2.5-py3-none-any.whl
(16.1 kB
view details)
File details
Details for the file evermore-0.2.5.tar.gz
.
File metadata
- Download URL: evermore-0.2.5.tar.gz
- Upload date:
- Size: 130.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9b4225f3adaff0987c02d0bff876e7e7c7003d28f4d3c140dbb2a384ee13ec51 |
|
MD5 | c0a4cc640515ef294784c0e4c42f58c9 |
|
BLAKE2b-256 | 7211ee46bdcc2389718656fae79567890cf8ef236d503c67eac9920ff8192918 |
File details
Details for the file evermore-0.2.5-py3-none-any.whl
.
File metadata
- Download URL: evermore-0.2.5-py3-none-any.whl
- Upload date:
- Size: 16.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9fbb484bd70fc623647abf12f4949741e59998a1059da503ae91e6831614e067 |
|
MD5 | 5d990cacd0512d9e080d2fee7deab808 |
|
BLAKE2b-256 | d4ec5d287ba9757cf9fa290ba190a661997e58e2e7b7f5180e980a22f3e0e4ea |