Skip to main content

Expected cost

Methods for computing the expected cost (EC) on an evaluation dataset, as defined in statistical learning text books (e.g., Bishop's "Pattern recognition and machine learning", and Hastie's et al "The elements of statistical learning"). Given a matrix of user-defined costs with elements $c_{y\ d}$, where $y$ is the true class of a sample and $d$ is the decision made by the system for that sample, this metric is estimated as the average of the costs across all samples in the dataset. That is:

$\mathrm{EC} = \frac{1}{N} \sum_i c_{y_i,d_i}$

where the sum runs over the $N$ samples in the evaluation set and $c_{y_i,d_i}$ is the cost incurred at sample $i$.

The EC is a generalization of the total error (which, in turn, is 1 minus the accuracy) and the balanced total error (which is 1 minus the balanced accuracy). The generalization is in the following ways: (1) it allows for costs that are different for each type of error, and (2) it allows for decisions that do not correspond one to one to the classes (e.g., it allows for the introduction of an "abstain" decision). The EC comes with an elegant theory on how to make optimal decisions given a certain set of costs, and it enables analysis of calibration. For these reasons we believe it is superior to other commonly used classification metrics, like the F-beta score or the Mathews correlation coefficient. All these issues are discussed in detail in:

L. Ferrer, "Analysis and Comparison of Classification Metrics", arXiv:2209.05355

The results in the paper can be replicated with the code in the examples directory in this repository.

The code provides methods for computing the EC when decisions are given by:

  • hard decisions obtained with some external method,

  • Bayes decisions made by optimizing the cost given the scores from a system assuming they can be used to obtain well-calibrated posteriors, or

  • optimal decisions made by choosing the decision threshold that minimizes the cost. This last option is only applicable for the binary case and the standard square cost function.

The scripts in the examples directory can be used with any dataset of scores and targets. See the examples/data.py file for examples on how to load your own data in the format required by the examples.

How to install

You can install this package as:

pip install expected_cost

which will also install all the dependencies. Some of the notebooks in this repository also require the psrcal package, which you can install as:

pip install psrcal

This is not included in the requirements of expected_cost because its installation requires pytorch, which takes a while. If you only need to compute expected cost or make Bayes decisions, and do not want to do or evaluate calibration, you do not need psrcal (or pytorch).

If you want the latest stuff, along with all the notebooks, you can do the following:

  1. Clone this repository:

    git clone https://github.com/luferrer/expected_cost.git

  2. Install the requirements:

    pip install -r requirements.txt

    (You can delete the psrcal line if you do not need calibration capabilities).

  3. Add the resulting top directory in your PYTHONPATH. In bash this would be:

    export PYTHONPATH=ROOT_DIR/expected_cost:$PYTHONPATH

where ROOT_DIR is the absolute path (or the relative path from the directory where you have the scripts or notebooks you want to run) to the top directory from where you did the clone above.

  1. You can now run any notebook in the notebooks directory.

Metadata

Release files for expected-cost 1.0

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

Source distribution (sdist)

Source distribution for expected-cost 1.0
File Size Uploaded Provenance Actions
expected_cost-1.0.tar.gz 26.6 kB Without provenance Details for expected_cost-1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for expected-cost 1.0
File Interpreter ABI Platform Actions
expected_cost-1.0-py3-none-any.whl Python 3 none any Details for expected_cost-1.0-py3-none-any.whl

Total release size: 55.3 kB

Release files / expected_cost-1.0.tar.gz

Download URL expected_cost-1.0.tar.gz
Size 26.6 kB
Tags Source
SHA-256 checksum
How to use checksums
4be2c301d41f7cf21049451fdd91bb837c083caff8fb69ac5e437f34d28cf0c5
BLAKE2b-256 checksum
How to use checksums
5d962f533258f3693daacb4546dc218519f21706209d9a955e03320c4dc30713
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.13

Release files / expected_cost-1.0-py3-none-any.whl

Download URL expected_cost-1.0-py3-none-any.whl
Size 28.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8753a6df696cbe323bd8d18ae310a3f63f85797b6a41aee58ed28f1b4464ea35
BLAKE2b-256 checksum
How to use checksums
68c2bcbc654b584f2bc1e6c80fa41caa1319262c22f61718a487b68f691f5b4a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.13

Security for expected-cost 1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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