Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logging, gradients, and bibtex #33

Merged
merged 34 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
045297d
optimise gradients
jackaraz Jan 26, 2024
cce1ad9
add ability to get bibtex entry for backend
jackaraz Jan 27, 2024
07ca2dd
update api
jackaraz Jan 27, 2024
6a7d38c
add logger
jackaraz Jan 27, 2024
fb64b4e
update bibliography capabilities
jackaraz Jan 27, 2024
3287155
optimise
jackaraz Jan 27, 2024
d9b6f74
add debug
jackaraz Jan 27, 2024
22aaf8e
add requests
jackaraz Jan 27, 2024
e4ca89e
update docstring
jackaraz Jan 27, 2024
0a47777
add logging
jackaraz Jan 28, 2024
489c696
update
jackaraz Jan 28, 2024
014e41e
update changelog
jackaraz Jan 28, 2024
68a20b9
bump version
jackaraz Jan 28, 2024
deadb21
add more tests
jackaraz Jan 28, 2024
d81d799
Merge pull request #32 from SpeysideHEP/refs
jackaraz Jan 28, 2024
a46b31e
add callers for grad and hessian
jackaraz Jan 28, 2024
a955112
Merge branch 'dev' into value_and_grad
jackaraz Jan 28, 2024
fce776c
update changelog
jackaraz Jan 28, 2024
b7de44a
Merge pull request #31 from SpeysideHEP/value_and_grad
jackaraz Jan 28, 2024
a9f4c5f
bugfix
jackaraz Jan 28, 2024
8d43287
tester for math
jackaraz Jan 28, 2024
9bd30e7
add python 3.8
jackaraz Jan 28, 2024
d3b9a11
improve logging
jackaraz Jan 28, 2024
fc04229
remove unnecessary functions
jackaraz Jan 28, 2024
4fff2c5
improve logging
jackaraz Jan 28, 2024
a004a10
remove unused imports
jackaraz Jan 28, 2024
6264870
update docstring
jackaraz Jan 28, 2024
9bba9d8
extend tutorials
jackaraz Jan 28, 2024
9922f6f
remove wf
jackaraz Jan 28, 2024
6790c00
update changelog
jackaraz Jan 28, 2024
d030903
update changelog
jackaraz Jan 28, 2024
0b03ba6
update changelog
jackaraz Jan 29, 2024
0d09279
update gradients.md
jackaraz Jan 29, 2024
b5c786a
typo fix
jackaraz Jan 29, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 0 additions & 43 deletions .github/workflows/black.yml

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.9", "3.10", "3.11"]
python-version: ["3.8", "3.9", "3.10", "3.11"]
os: [ubuntu-latest, macos-latest]

steps:
Expand Down
6 changes: 3 additions & 3 deletions .zenodo.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"description": "smooth inference for reinterpretation studies",
"license": "MIT",
"title": "SpeysideHEP/spey: v0.1.5",
"version": "v0.1.5",
"title": "SpeysideHEP/spey: v0.1.6",
"version": "v0.1.6",
"upload_type": "software",
"creators": [
{
Expand All @@ -29,7 +29,7 @@
},
{
"scheme": "url",
"identifier": "https://github.com/SpeysideHEP/spey/tree/v0.1.5",
"identifier": "https://github.com/SpeysideHEP/spey/tree/v0.1.6",
"relation": "isSupplementTo"
},
{
Expand Down
15 changes: 15 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@ Top-Level

version
about
check_updates
ExpectationType
AvailableBackends
get_backend
get_backend_metadata
get_backend_bibtex
cite
reset_backend_entries
statistical_model_wrapper
helper_functions.correlation_to_covariance
helper_functions.covariance_to_correlation
optimizer.core.fit
set_log_level

Main Classes
------------
Expand Down Expand Up @@ -82,6 +86,17 @@ Hypothesis testing
asymptotic_calculator.compute_asymptotic_confidence_level
toy_calculator.compute_toy_confidence_level

Gradient Tools
--------------

.. currentmodule:: spey.math

.. autosummary::
:toctree: _generated/

value_and_grad
hessian

Default Backends
----------------

Expand Down
35 changes: 27 additions & 8 deletions docs/releases/changelog-v0.1.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
# Release notes v0.1

## New features since last release
Specific upgrades for the latest release can be found [here](https://github.com/SpeysideHEP/spey/releases/latest).

* Exclusion limit calculator has been extended to include p-value computation
from chi-square.
## New features since the last release

* The exclusion limit calculator has been extended to include p-value computation from chi-square.
([#17](https://github.com/SpeysideHEP/spey/pull/17))

* $\chi^2$ function has been extended to compute background + signal vs background only model.
([#17](https://github.com/SpeysideHEP/spey/pull/17))

* Poisson based likelihood constructor without uncertainties has been added
* A Poisson-based likelihood constructor without uncertainties has been added
(Request by Veronica Sanz for EFT studies).
([#22](https://github.com/SpeysideHEP/spey/pull/22))

Expand All @@ -25,19 +26,37 @@
before approximating through $q_{\mu,A}$.
[#25](https://github.com/SpeysideHEP/spey/pull/25)

* Update clarification on text based keyword arguments.
* Update clarification on text-based keyword arguments.
([#30](https://github.com/SpeysideHEP/spey/pull/30))

* Adding logging across the software and implementing tools to silence them.
([#32](https://github.com/SpeysideHEP/spey/pull/32))

* Spey will automatically look for updates during initiation.
([#32](https://github.com/SpeysideHEP/spey/pull/32))

* Utilities to retrieve BibTeX information for third-party plug-ins.
([#32](https://github.com/SpeysideHEP/spey/pull/32))

* Add math utilities for users to extract gradient and hessian of negative log-likelihood
([#31](https://github.com/SpeysideHEP/spey/pull/31))

* Improve gradient execution for `default_pdf`.
([#31](https://github.com/SpeysideHEP/spey/pull/31))

* Add more tests for code coverage.
([#33](https://github.com/SpeysideHEP/spey/pull/33))

## Bug Fixes

* In accordance to the latest updates ```UnCorrStatisticsCombiner``` has been updated with
chi-square computer. See issue [#19](https://github.com/SpeysideHEP/spey/issues/19).
* In accordance with the latest updates, `UnCorrStatisticsCombiner` has been updated with
a chi-square computer. See issue [#19](https://github.com/SpeysideHEP/spey/issues/19).
([#20](https://github.com/SpeysideHEP/spey/pull/20))

* Execution error fix during likelihood computation for models with single nuisance parameter.
([#22](https://github.com/SpeysideHEP/spey/pull/22))

* Numeric problem rising from `==` which has been updated to `np.isclose`
* The numeric problem rising from `==` which has been updated to `np.isclose`
see issue [#23](https://github.com/SpeysideHEP/spey/issues/23).
([#25](https://github.com/SpeysideHEP/spey/pull/25))

Expand Down
1 change: 1 addition & 0 deletions docs/tutorials.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ Kitchen Sink
tutorials/intro_spey
tutorials/functional_tuto
tutorials/histogram
tutorials/gradients
tuto_plugin
Introduction to Spey (PyHEP 2023) <https://github.com/SpeysideHEP/PyHEP-2023>
92 changes: 92 additions & 0 deletions docs/tutorials/gradients.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
---
myst:
html_meta:
"property=og:title": "Gradient of a Statistical Model"
"property=og:description": "Modules to compute gradient and Hessian of negative log-probabilities"
"property=og:image": "https://spey.readthedocs.io/en/main/_static/spey-logo.png"
jupytext:
formats: ipynb,md:myst
text_representation:
extension: .md
format_name: myst
format_version: 0.12
jupytext_version: 1.8.2
kernelspec:
display_name: Python 3
language: python
name: python3
---

# Gradient of a Statistical Model

````{margin}
```{note}
In previous versions gradient and Hessian was limited to internal computations only.
```
````

With version 0.1.6, Spey includes additional functionalities to extract gradient and Hessian information directly from the statistical model. The gradient and Hessian are defined as follows

$$
{\rm Gradient} = -\frac{d\log\mathcal{L}(\theta)}{d\theta}\quad , \quad {\rm Hessian} = -\frac{d^2\log\mathcal{L}(\theta)}{d\theta_i d\theta_j}\quad , \quad \mu ,\theta_i \in \theta \ .
$$

In order to access this information we will use `spey.math` module.

```{code-cell} ipython3
:tags: [hide-cell]
import spey
from spey.math import value_and_grad, hessian
import numpy as np
np.random.seed(14)
```

{py:func}`spey.math.value_and_grad` returns a function that computes negative log-likelihood and its gradient for a given statistical model and {py:func}`spey.math.hessian` returns a function that computes Hessian of negative log-likelihood.

Let us examine this on ``"default_pdf.uncorrelated_background"``:

```{code-cell} ipython3
pdf_wrapper = spey.get_backend("default_pdf.uncorrelated_background")

data = [36, 33]
signal_yields = [12.0, 15.0]
background_yields = [50.0, 48.0]
background_unc = [12.0, 16.0]

stat_model = pdf_wrapper(
signal_yields=signal_yields,
background_yields=background_yields,
data=data,
absolute_uncertainties=background_unc,
)
```

Here we constructed a two-bin statistical model with observations $36,\ 33$, signal yields $12,\ 15$ and background yields $50\pm12,\ 48\pm16$. We can construct the function that will return negative log probability and its gradient as follows

```{code-cell} ipython3
neg_logprob = value_and_grad(stat_model)
```

Notice that this function constructs a negative log-probability for the observed statistical model using the default data that we provided earlier. This can be changed using ``expected`` and ``data`` keywords. Now we can choose nuisance parameters and execute the function:

```{code-cell} ipython3
nui = np.random.uniform(0,1,(3,))
neg_logprob(nui)
```

```python
(27.81902589793928, array([13.29067478, 6.17223275, 9.28814191]))
```

For this particular model, we have only two nuisance parameters, $\theta_i$, and signal strength, $\mu$, due to the structure of the statistical model. For Hessian, we can use the same formulation:

```{code-cell} ipython3
hess = hessian(stat_model)
hess(nui)
```

```python
array([[ 2.74153126, 1.21034187, 1.63326868],
[ 1.21034187, 2.21034187, -0. ],
[ 1.63326868, -0. , 2.74215326]])
```
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"autograd==1.5",
"semantic_version~=2.10",
"tqdm>=4.64.0",
"requests>=2.31.0",
]

backend_plugins = [
Expand Down
Loading