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:
from typing import NamedTuple
import equinox as eqx
import jax
import jax.numpy as jnp
from jaxtyping import Array, PyTree
import evermore as evm
jax.config.update("jax_enable_x64", True)
# define a simple model with two processes and two parameters
def model(params: PyTree, hists: dict[str, Array]) -> Array:
mu_modifier = params.mu.scale()
syst_modifier = params.syst.scale_log(up=1.1, down=0.9)
return mu_modifier(hists["signal"]) + syst_modifier(hists["bkg"])
def loss(
diffable: PyTree,
static: PyTree,
hists: dict[str, Array],
observation: Array,
) -> Array:
params = eqx.combine(diffable, static)
expectation = model(params, hists)
# Poisson NLL of the expectation and observation
log_likelihood = evm.loss.PoissonLogLikelihood()(expectation, observation)
# Add parameter constraints from logpdfs
constraints = evm.loss.get_log_probs(params)
log_likelihood += evm.util.sum_over_leaves(constraints)
return -jnp.sum(log_likelihood)
# setup data
hists = {"signal": jnp.array([3]), "bkg": jnp.array([10])}
observation = jnp.array([15])
# define parameters, can be any PyTree of evm.Parameters
class Params(NamedTuple):
mu: evm.Parameter
syst: evm.NormalParameter
params = Params(mu=evm.Parameter(1.0), syst=evm.NormalParameter(0.0))
diffable, static = evm.parameter.partition(params)
# Calculate negative log-likelihood/loss
loss_val = loss(diffable, static, hists, observation)
# gradients of negative log-likelihood w.r.t. diffable parameters
grads = eqx.filter_grad(loss)(diffable, static, 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.6.tar.gz
(140.3 kB
view details)
Built Distribution
evermore-0.2.6-py3-none-any.whl
(18.2 kB
view details)
File details
Details for the file evermore-0.2.6.tar.gz
.
File metadata
- Download URL: evermore-0.2.6.tar.gz
- Upload date:
- Size: 140.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | defaba981c467879b57563bcb1d66079e6b1303ed232682f0fd6aa2b7992c0cc |
|
MD5 | e41a48069b9435fb57823100b8fa8b0c |
|
BLAKE2b-256 | 5f47b80d2a2d61274811cde493e1cb0f4184e06e109429a9374d5a11289b6446 |
File details
Details for the file evermore-0.2.6-py3-none-any.whl
.
File metadata
- Download URL: evermore-0.2.6-py3-none-any.whl
- Upload date:
- Size: 18.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 873492382a2413c228bbe3f4b5172d66b534c1973cdbc08bad0093afae021350 |
|
MD5 | 1d5f7e68cb4b9b2763fb91bb128dc29a |
|
BLAKE2b-256 | 795d6ebcd39ad357109b91ca46c927b7f2f13b272807f694f17c6c337249855e |