Skip to main content

Differentiable (binned) likelihoods in JAX.

Project description

logo

evermore

Documentation Status Actions Status PyPI version PyPI platforms

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_logpdf_constraints(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


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.3.tar.gz (182.4 kB view details)

Uploaded Source

Built Distribution

evermore-0.2.3-py3-none-any.whl (15.9 kB view details)

Uploaded Python 3

File details

Details for the file evermore-0.2.3.tar.gz.

File metadata

  • Download URL: evermore-0.2.3.tar.gz
  • Upload date:
  • Size: 182.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for evermore-0.2.3.tar.gz
Algorithm Hash digest
SHA256 4995170790421e7afbb8f12c033f206a4d08c49931d538f1c11cd418d1c8bd56
MD5 bae02c577cc660a8bc654dab61a5e8b8
BLAKE2b-256 0ff6d08e92b3156311b40bb9e1c5c3188831a764d6b80a079edc00f09884cade

See more details on using hashes here.

File details

Details for the file evermore-0.2.3-py3-none-any.whl.

File metadata

  • Download URL: evermore-0.2.3-py3-none-any.whl
  • Upload date:
  • Size: 15.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for evermore-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 ae056abe50f845867186f3834034978b975c0eff3e5ddc0e201638890c78462a
MD5 ee183e6e663f9ea3c93717065b0a4d92
BLAKE2b-256 75da570c5638decf68308ec40551e16f7b2111a56be99f67ec4f1a943285899a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page