Skip to main content

ahoi (A Horrible Optimisation Instrument)

This module contains a few python functions to run Brute-force scans for rectangular cut optimization.

Installation

To install ahoi run

python3 -m pip install [--user] ahoi

Use --user if not in a virtual environment or conda environment.

It's recommended to use python3, but currently python2 is also supported.

Example

The basic functionality uses a masks_list which is a list of lists or a list of 2D numpy arrays that represent pass flags for selection criteria.

For example, the following represents pass flags for the criteria >0, >0.1, >0.2, ..., >0.9 for 5 random uniform variables in 10000 events:

import numpy as np
np.random.seed(42)
x = np.random.rand(10000, 5)
masks_list = [[x[:,i] > v for v in np.linspace(0, 0.9, 10)] for i in range(x.shape[1])]

To count all matching combinations for all criteria on each variable run

import ahoi
counts = ahoi.scan(masks_list)

The entry [0, 1, 2, 3, 4] of counts will contain the number of matching events where the first column of x is >0, the second one >0.1, the third one >0.2 etc.

>>> counts[0, 1, 2, 3, 4]
3032
>>> np.count_nonzero((x[:,0] > 0) & (x[:,1] > 0.1) & (x[:,2] > 0.2) & (x[:,3] > 0.3) & (x[:,4] > 0.4))
3032

You can also pass weights

weights = np.random.normal(loc=1, size=len(x))
counts, sumw, sumw2 = ahoi.scan(masks_list, weights=weights)

The arrays sumw and sumw2 will contain the sum of weights and sum of squares of weights for matching combinations. The sum of squares of weights can be used to estimate the statistical uncertainty on the sum of weights ($\sigma = \sqrt{\sum w_i^2}$).

>>> sumw[0, 1, 2, 3, 4]
3094.2191136427627
>>> np.dot(
...     (x[:,0] > 0) & (x[:,1] > 0.1) & (x[:,2] > 0.2) & (x[:,3] > 0.3) & (x[:,4] > 0.4),
...     weights
... )
3094.219113642755
>>> np.sqrt(sumw2[0, 1, 2, 3, 4])
78.5528532026876
>>> np.sqrt(
...     np.dot(
...         (x[:,0] > 0) & (x[:,1] > 0.1) & (x[:,2] > 0.2) & (x[:,3] > 0.3) & (x[:,4] > 0.4),
...         weights ** 2
...     )
... )
78.55285320268761

Tutorial/Notebook

Have a look at the examples for a tutorial that explains how to use this for solving a classification problem.

Tests/Coverage

Run the tests and coverage report inside the project directory with

python3 -m pytest --cov=ahoi --doctest-modules
coverage html

Release files for ahoi 0.6.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ahoi 0.6.1
File Size Uploaded
ahoi-0.6.1.tar.gz 23.0 kB Details

Release files / ahoi-0.6.1.tar.gz

Download URL ahoi-0.6.1.tar.gz
Size 23.0 kB
Tags Source
SHA-256 checksum
How to use checksums
447672ae9610df9dfb5440c20f460fbc8b6ec6b4b55f4ce60b1cd89523411ee1
BLAKE2b-256 checksum
How to use checksums
c3f722ac8b16c838679119c57fe043f2071c8c617c062c25ba29403456caef37
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6

Release history Release notifications | RSS feed

This release

0.6.1 This release

1 release file

0.6

1 release file

0.5

1 release file

0.4.1

1 release file

0.4

1 release file

0.3

1 release file

0.2

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page