-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
114 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,14 @@ | ||
from relaxed._version import version as __version__ | ||
|
||
__all__ = ("__version__", "hist", "cramer_rao_uncert", "fisher_info", "mle", "infer") | ||
__all__ = ( | ||
"__version__", | ||
"hist", | ||
"cramer_rao_uncert", | ||
"fisher_info", | ||
"mle", | ||
"infer", | ||
"gaussianity", | ||
) | ||
|
||
from relaxed import infer, mle | ||
from relaxed.ops import cramer_rao_uncert, fisher_info, hist | ||
from relaxed.ops import cramer_rao_uncert, fisher_info, gaussianity, hist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from __future__ import annotations | ||
|
||
__all__ = ("gaussianity",) | ||
|
||
from functools import partial | ||
from typing import TYPE_CHECKING | ||
|
||
import jax.numpy as jnp | ||
import jax.scipy as jsp | ||
from chex import Array | ||
from jax import jit, vmap | ||
from jax.random import PRNGKey, multivariate_normal | ||
|
||
if TYPE_CHECKING: | ||
import pyhf | ||
|
||
|
||
def gaussian_logpdf( | ||
bestfit_pars: Array, | ||
data: Array, | ||
cov: Array, | ||
) -> Array: | ||
return jsp.stats.multivariate_normal.logpdf(data, bestfit_pars, cov).reshape( | ||
1, | ||
) | ||
|
||
|
||
@partial(jit, static_argnames=["model", "rng_key", "n_samples"]) | ||
def gaussianity( | ||
model: pyhf.Model, | ||
bestfit_pars: Array, | ||
cov_approx: Array, | ||
observed_data: Array, | ||
rng_key: PRNGKey, | ||
n_samples: int = 1000, | ||
) -> Array: | ||
# - compare the likelihood of the fitted model with a gaussian approximation | ||
# that has the same MLE (fitted_pars) | ||
# - do this across a number of points in parspace (sampled from the gaussian approx) | ||
# and take the mean squared diff | ||
# - centre the values wrt the best-fit vals to scale the differences | ||
gaussian_parspace_samples = multivariate_normal( | ||
key=rng_key, | ||
mean=bestfit_pars, | ||
cov=cov_approx, | ||
shape=(n_samples,), | ||
) | ||
|
||
relative_nlls_model = vmap( | ||
lambda pars, data: -( | ||
model.logpdf(pars, data)[0] - model.logpdf(bestfit_pars, data)[0] | ||
), # scale origin to bestfit pars | ||
in_axes=(0, None), | ||
)(gaussian_parspace_samples, observed_data) | ||
|
||
relative_nlls_gaussian = vmap( | ||
lambda pars, data: -( | ||
gaussian_logpdf(pars, data, cov_approx)[0] | ||
- gaussian_logpdf(bestfit_pars, data, cov_approx)[0] | ||
), # data fixes the lhood shape | ||
in_axes=(0, None), | ||
)(gaussian_parspace_samples, bestfit_pars) | ||
|
||
diffs = relative_nlls_model - relative_nlls_gaussian | ||
return jnp.nanmean(diffs ** 2, axis=0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters