mcmetrics 0.1.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Charles Sither
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,176 @@
1
+ Metadata-Version: 2.4
2
+ Name: mcmetrics
3
+ Version: 0.1.1
4
+ Summary: Monte Carlo Metrics for evaluating machine learning models using confusion matrices.
5
+ Home-page: https://github.com/cbsither/monte-carlo-metrics
6
+ Download-URL: https://github.com/cbsither/monte-carlo-metrics.git
7
+ Author: Charlie Sither
8
+ Author-email: toxorhynchitini@gmail.com
9
+ Maintainer: Charlie Sither
10
+ Maintainer-email: toxorhynchitini@gmail.com
11
+ Keywords: monte carlo,prevalence threshold,model evaluation,machine learning
12
+ Classifier: Programming Language :: Python :: 3.11
13
+ Classifier: Programming Language :: Python :: 3.12
14
+ Classifier: Programming Language :: Python :: 3.13
15
+ Classifier: Operating System :: OS Independent
16
+ Classifier: License :: OSI Approved :: MIT License
17
+ Classifier: Natural Language :: English
18
+ Classifier: Intended Audience :: Developers
19
+ Classifier: Intended Audience :: Science/Research
20
+ Classifier: Topic :: Scientific/Engineering
21
+ Classifier: Topic :: Scientific/Engineering :: Information Analysis
22
+ Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
23
+ Description-Content-Type: text/markdown
24
+ License-File: LICENSE
25
+ Requires-Dist: numpy
26
+ Requires-Dist: scikit-learn
27
+ Dynamic: author
28
+ Dynamic: author-email
29
+ Dynamic: classifier
30
+ Dynamic: description
31
+ Dynamic: description-content-type
32
+ Dynamic: download-url
33
+ Dynamic: home-page
34
+ Dynamic: keywords
35
+ Dynamic: license-file
36
+ Dynamic: maintainer
37
+ Dynamic: maintainer-email
38
+ Dynamic: requires-dist
39
+ Dynamic: summary
40
+
41
+ # **Monte Carlo Metrics**
42
+
43
+ <p align="center">
44
+ <a href="https://github.com/cbsither/mcmetrics">
45
+ <img src="data/figures/screening_curve.png">
46
+ </a>
47
+ </p>
48
+
49
+ Figure: Example of a screening curve.
50
+
51
+ ## **Table of Contents**
52
+
53
+ <!--ts-->
54
+ * [Overview](#overview)
55
+ * [Notes and Future Additions](#notes-and-future-additions)
56
+ * [Installation](#installation)
57
+ * [Quick Start](#quick-start)
58
+ * [List of Metrics](#list-of-metrics)
59
+ <!--te-->
60
+
61
+ ## **Overview**
62
+
63
+ 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.
64
+
65
+ 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](#list-of-metrics) section).
66
+
67
+ 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```.
68
+
69
+ 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).
70
+
71
+ ## **Notes and Future Additions**
72
+
73
+ 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.
74
+
75
+ 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](#quick-start) will remain the same.
76
+
77
+ ## **Installation**
78
+
79
+ Stable
80
+ ------
81
+ The ```pip``` version is the last stable release. Version: *0.1.1*
82
+ ```sh
83
+ pip install mcmetrics
84
+ ```
85
+
86
+ ## **Quick Start**
87
+
88
+ ```python
89
+ import numpy as np
90
+ import mcmetrics
91
+
92
+ # Create a 2x2 confusion matrix. By default
93
+ # mcmetrics interpets the 'actual' condition
94
+ # as rows and 'predicted' condition as columns.
95
+
96
+ cm1 = np.array([[100,10],
97
+ [10,100]])
98
+
99
+ # initiate the Metrics class
100
+ mc = MCMetrics(model_name="model_1", cm=cm1) # prior is set to 1 by default
101
+
102
+ # sample posterior
103
+ mc.sample(n=1_000) # default n = 100_000
104
+
105
+ # calculate and return metric and credible intervals
106
+
107
+ # calculate sensitivity
108
+ mc.calculate_metric(metric='sensitivity', averaging=None)
109
+ # calculate specificity
110
+ mc.calculate_metric(metric='specificity', averaging=None)
111
+
112
+ # loop calculations
113
+ metrics = ['accuracy', 'positive predictive value', 'prevalence threshold']
114
+ mc.calculate_metric(metric=metrics, averaging=None)
115
+
116
+ # calculate summary statistics
117
+ mc.calc_mean(metric='sensitivity') # mean
118
+ mc.calc_std(metric='sensitivity') # standard deviation
119
+ mc.calc_median(metric='sensitivity') # median
120
+ mc.calc_mode(metric='sensitivity') # mode
121
+ mc.calc_var(metric='sensitivity', ddof=0) # population variance
122
+
123
+ # chain together calculations
124
+ mc.calculate_metric(metric='specificity').calc_mean()
125
+
126
+ # calculate credible intervals
127
+ mc.calculate_ci(metric='sensitivity', ci=0.95)
128
+
129
+ # the upper and lower bounds of the credible interval can be explicited specified
130
+ mc.calculate_ci(metric='sensitivity', cil=0.005, ciu=0.99)
131
+
132
+ # Remember, the credible intervals are a property of the posterior distribution
133
+ # for a metric, along with the mean, median, and mode. All of these statistics
134
+ # describe the conditional performance metric uncertainty with respect to
135
+ # sample size.
136
+
137
+ ```
138
+
139
+ ## **List of Metrics**
140
+
141
+ 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'`.
142
+
143
+ * `prevalence`
144
+ * `accuracy`
145
+ * `balanced accuracy`
146
+ * `precision`
147
+ * `positive predictive value`
148
+ Same as `precision`.
149
+ * `false discovery rate`
150
+ * `f1 score`
151
+ * `false omission rate`
152
+ * `negative predictive value`
153
+ * `fowlkes-mallows index`
154
+ * `informedness`
155
+ * `sensitivity`
156
+ * `true positive rate`
157
+ Same as `sensitivity`.
158
+ * `recall`
159
+ Same as `sensitivity`.
160
+ * `false positive rate`
161
+ * `positive likelihood ratio`
162
+ * `markedness`
163
+ * `true negative rate`
164
+ * `matthews correlation coefficient`
165
+ * `prevalence threshold`
166
+ * `false negative rate`
167
+ * `specificity`
168
+ Same as `true negative rate`.
169
+ * `negative likelihood ratio`
170
+ * `diagnostic odds ratio`
171
+ * `jaccard index`
172
+ * `critical success index`
173
+ Same as `Jaccard Index`.
174
+ * `threat score`
175
+ Same as `Jaccard Index`.
176
+ * `screening coefficient`
@@ -0,0 +1,136 @@
1
+ # **Monte Carlo Metrics**
2
+
3
+ <p align="center">
4
+ <a href="https://github.com/cbsither/mcmetrics">
5
+ <img src="data/figures/screening_curve.png">
6
+ </a>
7
+ </p>
8
+
9
+ Figure: Example of a screening curve.
10
+
11
+ ## **Table of Contents**
12
+
13
+ <!--ts-->
14
+ * [Overview](#overview)
15
+ * [Notes and Future Additions](#notes-and-future-additions)
16
+ * [Installation](#installation)
17
+ * [Quick Start](#quick-start)
18
+ * [List of Metrics](#list-of-metrics)
19
+ <!--te-->
20
+
21
+ ## **Overview**
22
+
23
+ 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.
24
+
25
+ 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](#list-of-metrics) section).
26
+
27
+ 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```.
28
+
29
+ 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).
30
+
31
+ ## **Notes and Future Additions**
32
+
33
+ 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.
34
+
35
+ 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](#quick-start) will remain the same.
36
+
37
+ ## **Installation**
38
+
39
+ Stable
40
+ ------
41
+ The ```pip``` version is the last stable release. Version: *0.1.1*
42
+ ```sh
43
+ pip install mcmetrics
44
+ ```
45
+
46
+ ## **Quick Start**
47
+
48
+ ```python
49
+ import numpy as np
50
+ import mcmetrics
51
+
52
+ # Create a 2x2 confusion matrix. By default
53
+ # mcmetrics interpets the 'actual' condition
54
+ # as rows and 'predicted' condition as columns.
55
+
56
+ cm1 = np.array([[100,10],
57
+ [10,100]])
58
+
59
+ # initiate the Metrics class
60
+ mc = MCMetrics(model_name="model_1", cm=cm1) # prior is set to 1 by default
61
+
62
+ # sample posterior
63
+ mc.sample(n=1_000) # default n = 100_000
64
+
65
+ # calculate and return metric and credible intervals
66
+
67
+ # calculate sensitivity
68
+ mc.calculate_metric(metric='sensitivity', averaging=None)
69
+ # calculate specificity
70
+ mc.calculate_metric(metric='specificity', averaging=None)
71
+
72
+ # loop calculations
73
+ metrics = ['accuracy', 'positive predictive value', 'prevalence threshold']
74
+ mc.calculate_metric(metric=metrics, averaging=None)
75
+
76
+ # calculate summary statistics
77
+ mc.calc_mean(metric='sensitivity') # mean
78
+ mc.calc_std(metric='sensitivity') # standard deviation
79
+ mc.calc_median(metric='sensitivity') # median
80
+ mc.calc_mode(metric='sensitivity') # mode
81
+ mc.calc_var(metric='sensitivity', ddof=0) # population variance
82
+
83
+ # chain together calculations
84
+ mc.calculate_metric(metric='specificity').calc_mean()
85
+
86
+ # calculate credible intervals
87
+ mc.calculate_ci(metric='sensitivity', ci=0.95)
88
+
89
+ # the upper and lower bounds of the credible interval can be explicited specified
90
+ mc.calculate_ci(metric='sensitivity', cil=0.005, ciu=0.99)
91
+
92
+ # Remember, the credible intervals are a property of the posterior distribution
93
+ # for a metric, along with the mean, median, and mode. All of these statistics
94
+ # describe the conditional performance metric uncertainty with respect to
95
+ # sample size.
96
+
97
+ ```
98
+
99
+ ## **List of Metrics**
100
+
101
+ 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'`.
102
+
103
+ * `prevalence`
104
+ * `accuracy`
105
+ * `balanced accuracy`
106
+ * `precision`
107
+ * `positive predictive value`
108
+ Same as `precision`.
109
+ * `false discovery rate`
110
+ * `f1 score`
111
+ * `false omission rate`
112
+ * `negative predictive value`
113
+ * `fowlkes-mallows index`
114
+ * `informedness`
115
+ * `sensitivity`
116
+ * `true positive rate`
117
+ Same as `sensitivity`.
118
+ * `recall`
119
+ Same as `sensitivity`.
120
+ * `false positive rate`
121
+ * `positive likelihood ratio`
122
+ * `markedness`
123
+ * `true negative rate`
124
+ * `matthews correlation coefficient`
125
+ * `prevalence threshold`
126
+ * `false negative rate`
127
+ * `specificity`
128
+ Same as `true negative rate`.
129
+ * `negative likelihood ratio`
130
+ * `diagnostic odds ratio`
131
+ * `jaccard index`
132
+ * `critical success index`
133
+ Same as `Jaccard Index`.
134
+ * `threat score`
135
+ Same as `Jaccard Index`.
136
+ * `screening coefficient`
@@ -0,0 +1 @@
1
+ from .main import MCMetrics