Automatic differentiation for high-energy physics correction factor calculations.
Project description
correctionlib-gradients
A JAX-friendly, auto-differentiable, Python-only implementation of correctionlib correction evaluations.
Table of Contents
Installation
pip install correctionlib-gradients
Usage
- construct a
CorrectionWithGradient
object from acorrectionlib.schemav2.Correction
- there is no point 2: you can use
CorrectionWithGradient.evaluate
as a normal JAX-friendly, auto-differentiable function
Example
import jax
import jax.numpy as jnp
from correctionlib import schemav2
from correctionlib_gradients import CorrectionWithGradient
# given a correctionlib schema:
formula_schema = schemav2.Correction(
name="x squared",
version=2,
inputs=[schemav2.Variable(name="x", type="real")],
output=schemav2.Variable(name="a scale", type="real"),
data=schemav2.Formula(
nodetype="formula",
expression="x * x",
parser="TFormula",
variables=["x"],
),
)
# construct a CorrectionWithGradient
c = CorrectionWithGradient(formula_schema)
# use c.evaluate as a JAX-friendly, auto-differentiable function
value, grad = jax.value_and_grad(c.evaluate)(3.0)
assert jnp.isclose(value, 9.0)
assert jnp.isclose(grad, 6.0)
# for Formula corrections, jax.jit and jax.vmap work too
xs = jnp.array([3.0, 4.0])
values, grads = jax.vmap(jax.jit(jax.value_and_grad(c.evaluate)))(xs)
assert jnp.allclose(values, jnp.array([9.0, 16.0]))
assert jnp.allclose(grads, jnp.array([6.0, 8.0]))
Supported types of corrections
Currently the following corrections from correctionlib.schemav2
are supported:
Formula
, including parametrical formulasBinning
with uniform or non-uniform bin edges andflow="clamp"
; bin contents can be either:- all scalar values
- all
Formula
orFormulaRef
- scalar constants
License
correctionlib-gradients
is distributed under the terms of the BSD 3-Clause 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
Built Distribution
Close
Hashes for correctionlib_gradients-0.2.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 19bdb55ed64d5355f824512ae1febe0f1a850a18a9543413f577599313103509 |
|
MD5 | 3f836286c5e0b90be5bf16f4f6fb4fac |
|
BLAKE2b-256 | e9ac9668af77943813bbe311fdfd9076828fc67eaa885dc26432df3915cf49c4 |
Close
Hashes for correctionlib_gradients-0.2.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 07f28ab566452fc69e9ec2b6115b548d6ea705a04574427f08945e9b9e4cbc1b |
|
MD5 | 2a0a1a9aa2e643cc6a5fe48ba9ff5098 |
|
BLAKE2b-256 | 15ad6454e1b68cf11bbec7a69f7877961c8433fdcffa4e243d8857f9efddd9b6 |