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.Parameter
    syst: evm.Parameter

    def __call__(self, hists: dict[str, Array]) -> Array:
        mu_modifier = self.mu.unconstrained()
        syst_modifier = self.syst.lnN(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_param_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.Parameter(1.0), syst=evm.Parameter(0.0))

# 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.2.tar.gz (174.2 kB view details)

Uploaded Source

Built Distribution

evermore-0.2.2-py3-none-any.whl (15.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: evermore-0.2.2.tar.gz
  • Upload date:
  • Size: 174.2 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.2.tar.gz
Algorithm Hash digest
SHA256 a003fb60b9ef51632acc5147de2ec7a63841e23f5354e6f02476129bca47d050
MD5 ebab833e38e146c34843fce9ee736c91
BLAKE2b-256 3431d09e0331ec212e844fcf0e6440e3374c3f36ec4c64d0c00cd07410cc31b4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: evermore-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 15.5 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 9b490f92bb5064ebb744d1cf0add6e1c29b3d30289b4181917b8431512bfc48e
MD5 a32daab4f71a51365f5b7e74a2d3c649
BLAKE2b-256 27da3a6b7ae2c19f00a9dc6e60ef79b6d429b30164f14e74c5edbc8cf75f7fa0

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