Skip to main content

Monte Carlo Metrics for evaluating machine learning models using confusion matrices.

Project description

Monte Carlo Metrics

Figure: Example of a screening curve.

Table of Contents

Overview

Monte Carlo Metrics (MCMetrics) is an easy to use Python library that leverages Bayesian inference to calculate conditional performance metric uncertainty with respect to sample size. MCMetrics differs from other conditional performance metric libraries by providing credible intervals (CIs) for each metric, which are implicitly calculated from the posterior distribution of the metric. This allows users to understand the uncertainty in their metric estimates, which is particularly useful in fields like medical diagnostics, machine learning, and any domain where performance metrics are critical. Additionally, MCMetrics is built on top of NumPy and Scikit-Learn, making it easy to integrate into existing workflows.

MCMetrics calculates CIs by sampling a posterior Dirichlet distribution with a default or specified prior distribution combined with the data. MCMetrics then calculates statistics like accuracy, recall (i.e., sensitivity), positive predictive value (i.e., precision), among others from the samples (see the List of Metrics section).

MCMetrics takes a relatively straight forward approach when calculating conditional performance metrics and CIs. First, it samples $n$ times from the posterior Dirichlet distribution comprised of the prior and confusion matrix. After samples are generated, the user can specify which conditional performance metrics to calculate. Then, each metric can be summarized by its posterior distribution in the form of summary statistics and CIs. Once a metric is calculated, it is permanently stored and only removed if another $n$ samples are called and the resample parameter set to True.

MCMetrics works by automating calling several independent functions in a workflow. As such, MCMetrics functions can be called independently and function similar to the Scikit-Learn metric functions by specifying either a confusion matrix or the raw parameter values (i.e., TPs, FPs, FNs, and FPs, or the predicted y and actual y values).

Notes and Future Additions

Note 1: Currently, MCMetrics only fully supports binary classification confusion matrices. It is possible to calculate metrics for multiclass confusion matrices, but the statistics and CIs are equivalent to micro-averaging since each sample from the posterior is weighted equally. Future versions will support macro-averaging and specifying weights.

Note 2: MCMetrics is currently in active development and all features are not fully implemented. Some of the API may change in future versions, but the core functionality and workflow specified in the Quick Start will remain the same.

Installation

Stable

The pip version is the last stable release. Version: 0.1.1

pip install mcmetrics

Quick Start

import numpy as np
import mcmetrics

# Create a 2x2 confusion matrix. By default 
# mcmetrics interpets the 'actual' condition 
# as rows and 'predicted' condition as columns.

cm1 = np.array([[100,10],
                [10,100]])

# initiate the Metrics class
mc = MCMetrics(model_name="model_1", cm=cm1) # prior is set to 1 by default

# sample posterior
mc.sample(n=1_000) # default n = 100_000

# calculate and return metric and credible intervals

# calculate sensitivity
mc.calculate_metric(metric='sensitivity', averaging=None)
# calculate specificity
mc.calculate_metric(metric='specificity', averaging=None)

# loop calculations
metrics = ['accuracy', 'positive predictive value', 'prevalence threshold']
mc.calculate_metric(metric=metrics, averaging=None)

# calculate summary statistics
mc.calc_mean(metric='sensitivity') # mean
mc.calc_std(metric='sensitivity') # standard deviation
mc.calc_median(metric='sensitivity') # median
mc.calc_mode(metric='sensitivity') # mode
mc.calc_var(metric='sensitivity', ddof=0) # population variance

# chain together calculations
mc.calculate_metric(metric='specificity').calc_mean()

# calculate credible intervals
mc.calculate_ci(metric='sensitivity', ci=0.95)

# the upper and lower bounds of the credible interval can be explicited specified
mc.calculate_ci(metric='sensitivity', cil=0.005, ciu=0.99)

# Remember, the credible intervals are a property of the posterior distribution 
# for a  metric, along with the mean, median, and mode. All of these statistics 
# describe the conditional performance metric uncertainty with respect to 
# sample size.

List of Metrics

A full list of metrics is included below. Each metric is called based by the exact command below in the metric parameter. For instance, to call precision, you would call metric='precision'. Or for a metric with spaces, you would call metric='positive predictive value'.

  • prevalence
  • accuracy
  • balanced accuracy
  • precision
  • positive predictive value Same as precision.
  • false discovery rate
  • f1 score
  • false omission rate
  • negative predictive value
  • fowlkes-mallows index
  • informedness
  • sensitivity
  • true positive rate Same as sensitivity.
  • recall Same as sensitivity.
  • false positive rate
  • positive likelihood ratio
  • markedness
  • true negative rate
  • matthews correlation coefficient
  • prevalence threshold
  • false negative rate
  • specificity Same as true negative rate.
  • negative likelihood ratio
  • diagnostic odds ratio
  • jaccard index
  • critical success index Same as Jaccard Index.
  • threat score Same as Jaccard Index.
  • screening coefficient

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mcmetrics-0.1.1.tar.gz (23.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mcmetrics-0.1.1-py3-none-any.whl (21.9 kB view details)

Uploaded Python 3

File details

Details for the file mcmetrics-0.1.1.tar.gz.

File metadata

  • Download URL: mcmetrics-0.1.1.tar.gz
  • Upload date:
  • Size: 23.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for mcmetrics-0.1.1.tar.gz
Algorithm Hash digest
SHA256 a077fef99c5336c70060b242588e979e5babc8e25f7f0c13539619d54ca5dd76
MD5 27c5baab49bbed7602df1e4c56e5ce3a
BLAKE2b-256 573087ce09f7fd28a96d52a35a9c5ad709a771b17b974e0d933786e223711d1e

See more details on using hashes here.

File details

Details for the file mcmetrics-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: mcmetrics-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 21.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for mcmetrics-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9de3895e3dee5e692dd8fa1368987112b866b1c879e72ace4d0f138b53e20c2f
MD5 85f25c9a9726e87fa4dbf63e24cbbd42
BLAKE2b-256 fe7a78f695f1e3663300618893e46a5898b7abcb16d25b80c311c996638d7576

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page