robustmodels 0.1.0__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.
- robustmodels-0.1.0/.gitignore +15 -0
- robustmodels-0.1.0/CHANGELOG.md +18 -0
- robustmodels-0.1.0/LICENSE +28 -0
- robustmodels-0.1.0/PKG-INFO +151 -0
- robustmodels-0.1.0/README.md +120 -0
- robustmodels-0.1.0/docs/changelog.md +2 -0
- robustmodels-0.1.0/docs/conf.py +23 -0
- robustmodels-0.1.0/docs/covariance.md +135 -0
- robustmodels-0.1.0/docs/index.md +73 -0
- robustmodels-0.1.0/docs/location.md +67 -0
- robustmodels-0.1.0/docs/scale.md +95 -0
- robustmodels-0.1.0/pyproject.toml +58 -0
- robustmodels-0.1.0/src/robustmodels/__init__.py +12 -0
- robustmodels-0.1.0/src/robustmodels/_validation.py +40 -0
- robustmodels-0.1.0/src/robustmodels/covariance.py +1191 -0
- robustmodels-0.1.0/src/robustmodels/location.py +241 -0
- robustmodels-0.1.0/src/robustmodels/scale.py +758 -0
- robustmodels-0.1.0/tests/conftest.py +34 -0
- robustmodels-0.1.0/tests/data/r_reference.json +1 -0
- robustmodels-0.1.0/tests/reference.py +57 -0
- robustmodels-0.1.0/tests/test_covariance.py +291 -0
- robustmodels-0.1.0/tests/test_location.py +115 -0
- robustmodels-0.1.0/tests/test_r_parity.py +82 -0
- robustmodels-0.1.0/tests/test_scale.py +211 -0
- robustmodels-0.1.0/tests/test_selection.py +37 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.0 (2026-11-09)
|
|
4
|
+
|
|
5
|
+
First release.
|
|
6
|
+
|
|
7
|
+
- `scale`: `mad`, `qn`, `sn`, `pn`, `tau_scale`, `m_scale`, `medcouple`, `lmc`, `rmc`.
|
|
8
|
+
Qn, Sn and Pn apply finite-sample corrections by default (Pn: Tarr 2014,
|
|
9
|
+
Appendix B.3); pass `finite_correction=False` to disable them.
|
|
10
|
+
- `location`: `hodges_lehmann`, `m_location`, `tau_location`, `trimmed_mean`, `winsorized_mean`.
|
|
11
|
+
- `covariance`: `MCD` (FAST-MCD and DetMCD), `MRCD`, `MVE`, `OGK`, `SEstimator`,
|
|
12
|
+
`MMEstimator`, with the scikit-learn estimator interface.
|
|
13
|
+
- Input and tuning parameters are validated; invalid values raise `ValueError`
|
|
14
|
+
naming the parameter.
|
|
15
|
+
- Documentation at https://omarbecerrasierra.github.io/robustmodels/, with a user
|
|
16
|
+
guide and a reference for every function and class; its examples run as tests.
|
|
17
|
+
- CI tests the installed wheel on Python 3.10 to 3.14 and publishes
|
|
18
|
+
tagged releases to PyPI with trusted publishing.
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026, omarbecerrasierra
|
|
4
|
+
|
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
|
6
|
+
modification, are permitted provided that the following conditions are met:
|
|
7
|
+
|
|
8
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
9
|
+
list of conditions and the following disclaimer.
|
|
10
|
+
|
|
11
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
12
|
+
this list of conditions and the following disclaimer in the documentation
|
|
13
|
+
and/or other materials provided with the distribution.
|
|
14
|
+
|
|
15
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
16
|
+
contributors may be used to endorse or promote products derived from
|
|
17
|
+
this software without specific prior written permission.
|
|
18
|
+
|
|
19
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
20
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
21
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
22
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
23
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
24
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
25
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
26
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
27
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
28
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: robustmodels
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Robust statistics for Python: univariate location and scale, multivariate location and scatter.
|
|
5
|
+
Project-URL: Homepage, https://github.com/omarbecerrasierra/robustmodels
|
|
6
|
+
Project-URL: Documentation, https://omarbecerrasierra.github.io/robustmodels/
|
|
7
|
+
Project-URL: Repository, https://github.com/omarbecerrasierra/robustmodels
|
|
8
|
+
Project-URL: Issues, https://github.com/omarbecerrasierra/robustmodels/issues
|
|
9
|
+
Project-URL: Changelog, https://github.com/omarbecerrasierra/robustmodels/blob/main/CHANGELOG.md
|
|
10
|
+
Author: omarbecerrasierra
|
|
11
|
+
License-Expression: BSD-3-Clause
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
Keywords: MCD,Qn,Sn,covariance,medcouple,outliers,robust statistics
|
|
14
|
+
Classifier: Development Status :: 3 - Alpha
|
|
15
|
+
Classifier: Intended Audience :: Science/Research
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
24
|
+
Classifier: Topic :: Scientific/Engineering
|
|
25
|
+
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
|
26
|
+
Requires-Python: >=3.10
|
|
27
|
+
Requires-Dist: numpy>=1.24
|
|
28
|
+
Requires-Dist: scikit-learn>=1.6
|
|
29
|
+
Requires-Dist: scipy>=1.10
|
|
30
|
+
Description-Content-Type: text/markdown
|
|
31
|
+
|
|
32
|
+
# robustmodels
|
|
33
|
+
|
|
34
|
+
[](https://pypi.org/project/robustmodels/)
|
|
35
|
+
[](https://omarbecerrasierra.github.io/robustmodels/)
|
|
36
|
+
[](https://github.com/omarbecerrasierra/robustmodels/actions/workflows/ci.yml)
|
|
37
|
+
|
|
38
|
+
Robust location, scale and covariance estimators for Python. Early development:
|
|
39
|
+
the API may change before version 1.0.
|
|
40
|
+
|
|
41
|
+
## Install
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
pip install robustmodels
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Requires Python 3.10+, NumPy, SciPy and scikit-learn.
|
|
48
|
+
|
|
49
|
+
## Use
|
|
50
|
+
|
|
51
|
+
```python
|
|
52
|
+
from robustmodels import location, scale
|
|
53
|
+
|
|
54
|
+
x = [1, 2, 2, 3, 4, 100]
|
|
55
|
+
location.hodges_lehmann(x)
|
|
56
|
+
scale.mad(x)
|
|
57
|
+
scale.qn(x)
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Covariance estimators follow the scikit-learn interface:
|
|
61
|
+
|
|
62
|
+
```python
|
|
63
|
+
import numpy as np
|
|
64
|
+
from robustmodels.covariance import MCD
|
|
65
|
+
|
|
66
|
+
X = np.random.default_rng(0).normal(size=(100, 3))
|
|
67
|
+
X[:10] += 8
|
|
68
|
+
|
|
69
|
+
model = MCD(random_state=0).fit(X)
|
|
70
|
+
model.location_
|
|
71
|
+
model.covariance_
|
|
72
|
+
model.mahalanobis(X) # squared distances
|
|
73
|
+
model.support_ # observations kept after reweighting
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Estimators
|
|
77
|
+
|
|
78
|
+
| Module | Contents |
|
|
79
|
+
| --- | --- |
|
|
80
|
+
| `robustmodels.location` | `hodges_lehmann`, `m_location`, `tau_location`, `trimmed_mean`, `winsorized_mean` |
|
|
81
|
+
| `robustmodels.scale` | `mad`, `qn`, `sn`, `pn`, `tau_scale`, `m_scale`; skewness and tail weight: `medcouple`, `lmc`, `rmc` |
|
|
82
|
+
| `robustmodels.covariance` | `MCD`, `MRCD`, `MVE`, `OGK`, `SEstimator`, `MMEstimator`; correction factors: `h_alpha_n`, `mcd_consistency`, `mcd_small_sample` |
|
|
83
|
+
|
|
84
|
+
The [documentation](https://omarbecerrasierra.github.io/robustmodels/) explains how
|
|
85
|
+
to choose and use each estimator and documents every function and class. The same
|
|
86
|
+
information is in the docstrings: `help(robustmodels.scale.qn)`.
|
|
87
|
+
|
|
88
|
+
Univariate functions accept finite, one-dimensional samples. Empty samples return
|
|
89
|
+
`NaN`. A single observation has zero scale. Covariance estimators accept matrices
|
|
90
|
+
with observations in rows. MRCD supports more variables than observations and
|
|
91
|
+
uses an identity regularization target. MVE requires `alpha < 1`.
|
|
92
|
+
MRCD needs an explicit positive `rho` when a starting subset has zero scatter.
|
|
93
|
+
|
|
94
|
+
Qn, Sn and Pn include finite-sample corrections by default. Pass
|
|
95
|
+
`finite_correction=False` to disable them. Pn uses the factors from
|
|
96
|
+
[Tarr (2014), Appendix B.3](https://www.researchgate.net/publication/267927753_Quantile_Based_Estimation_of_Scale_and_Dependence).
|
|
97
|
+
|
|
98
|
+
## Development
|
|
99
|
+
|
|
100
|
+
```bash
|
|
101
|
+
uv sync --locked
|
|
102
|
+
uv run pytest
|
|
103
|
+
uv run ruff check .
|
|
104
|
+
uv run ruff format --check .
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Build and check the distributions:
|
|
108
|
+
|
|
109
|
+
```bash
|
|
110
|
+
uv build
|
|
111
|
+
uvx twine check --strict dist/*
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
Run the docstring examples and build the documentation:
|
|
115
|
+
|
|
116
|
+
```bash
|
|
117
|
+
uv run pytest --doctest-modules --pyargs robustmodels
|
|
118
|
+
uv run --group docs sphinx-build -W -b html docs docs/_build/html
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
CI runs the tests against the installed wheel on Python 3.10 to 3.14.
|
|
122
|
+
The Python 3.10 job uses the minimum supported NumPy, SciPy and scikit-learn versions.
|
|
123
|
+
|
|
124
|
+
The examples in `docs/` and in the docstrings run as tests. The tests cover estimator definitions, statistical properties, edge cases,
|
|
125
|
+
agreement with R's robustbase and rrcov, and
|
|
126
|
+
[scikit-learn compatibility](https://scikit-learn.org/stable/developers/develop.html).
|
|
127
|
+
|
|
128
|
+
Code lives in `src/robustmodels`:
|
|
129
|
+
|
|
130
|
+
| File | Contents | Internal dependencies |
|
|
131
|
+
| --- | --- | --- |
|
|
132
|
+
| `location.py` | Location estimators | `scale.py`, `_validation.py` |
|
|
133
|
+
| `scale.py` | Scale, skewness, correction factors and pairwise selection | `_validation.py` |
|
|
134
|
+
| `covariance.py` | Covariance estimators and their fitting steps | `scale.py`, `_validation.py` |
|
|
135
|
+
| `_validation.py` | Input and parameter checks | None |
|
|
136
|
+
|
|
137
|
+
Each method and its steps stay together. Auditing an estimator requires its file
|
|
138
|
+
and at most two supporting files. `__init__.py` exposes the three public modules.
|
|
139
|
+
|
|
140
|
+
### Release
|
|
141
|
+
|
|
142
|
+
1. Set `version` in `pyproject.toml`, run `uv lock`, and replace "unreleased"
|
|
143
|
+
in [CHANGELOG.md](https://github.com/omarbecerrasierra/robustmodels/blob/main/CHANGELOG.md)
|
|
144
|
+
with the date.
|
|
145
|
+
2. Merge into `main`, then tag and push: `git tag v0.1.0 && git push origin v0.1.0`.
|
|
146
|
+
3. CI tests, builds and, after approval of the `pypi` environment, publishes to
|
|
147
|
+
PyPI through trusted publishing. The tag must match the package version.
|
|
148
|
+
|
|
149
|
+
## License
|
|
150
|
+
|
|
151
|
+
BSD 3-Clause; see [LICENSE](https://github.com/omarbecerrasierra/robustmodels/blob/main/LICENSE).
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
# robustmodels
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/project/robustmodels/)
|
|
4
|
+
[](https://omarbecerrasierra.github.io/robustmodels/)
|
|
5
|
+
[](https://github.com/omarbecerrasierra/robustmodels/actions/workflows/ci.yml)
|
|
6
|
+
|
|
7
|
+
Robust location, scale and covariance estimators for Python. Early development:
|
|
8
|
+
the API may change before version 1.0.
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
pip install robustmodels
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Requires Python 3.10+, NumPy, SciPy and scikit-learn.
|
|
17
|
+
|
|
18
|
+
## Use
|
|
19
|
+
|
|
20
|
+
```python
|
|
21
|
+
from robustmodels import location, scale
|
|
22
|
+
|
|
23
|
+
x = [1, 2, 2, 3, 4, 100]
|
|
24
|
+
location.hodges_lehmann(x)
|
|
25
|
+
scale.mad(x)
|
|
26
|
+
scale.qn(x)
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Covariance estimators follow the scikit-learn interface:
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
import numpy as np
|
|
33
|
+
from robustmodels.covariance import MCD
|
|
34
|
+
|
|
35
|
+
X = np.random.default_rng(0).normal(size=(100, 3))
|
|
36
|
+
X[:10] += 8
|
|
37
|
+
|
|
38
|
+
model = MCD(random_state=0).fit(X)
|
|
39
|
+
model.location_
|
|
40
|
+
model.covariance_
|
|
41
|
+
model.mahalanobis(X) # squared distances
|
|
42
|
+
model.support_ # observations kept after reweighting
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Estimators
|
|
46
|
+
|
|
47
|
+
| Module | Contents |
|
|
48
|
+
| --- | --- |
|
|
49
|
+
| `robustmodels.location` | `hodges_lehmann`, `m_location`, `tau_location`, `trimmed_mean`, `winsorized_mean` |
|
|
50
|
+
| `robustmodels.scale` | `mad`, `qn`, `sn`, `pn`, `tau_scale`, `m_scale`; skewness and tail weight: `medcouple`, `lmc`, `rmc` |
|
|
51
|
+
| `robustmodels.covariance` | `MCD`, `MRCD`, `MVE`, `OGK`, `SEstimator`, `MMEstimator`; correction factors: `h_alpha_n`, `mcd_consistency`, `mcd_small_sample` |
|
|
52
|
+
|
|
53
|
+
The [documentation](https://omarbecerrasierra.github.io/robustmodels/) explains how
|
|
54
|
+
to choose and use each estimator and documents every function and class. The same
|
|
55
|
+
information is in the docstrings: `help(robustmodels.scale.qn)`.
|
|
56
|
+
|
|
57
|
+
Univariate functions accept finite, one-dimensional samples. Empty samples return
|
|
58
|
+
`NaN`. A single observation has zero scale. Covariance estimators accept matrices
|
|
59
|
+
with observations in rows. MRCD supports more variables than observations and
|
|
60
|
+
uses an identity regularization target. MVE requires `alpha < 1`.
|
|
61
|
+
MRCD needs an explicit positive `rho` when a starting subset has zero scatter.
|
|
62
|
+
|
|
63
|
+
Qn, Sn and Pn include finite-sample corrections by default. Pass
|
|
64
|
+
`finite_correction=False` to disable them. Pn uses the factors from
|
|
65
|
+
[Tarr (2014), Appendix B.3](https://www.researchgate.net/publication/267927753_Quantile_Based_Estimation_of_Scale_and_Dependence).
|
|
66
|
+
|
|
67
|
+
## Development
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
uv sync --locked
|
|
71
|
+
uv run pytest
|
|
72
|
+
uv run ruff check .
|
|
73
|
+
uv run ruff format --check .
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Build and check the distributions:
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
uv build
|
|
80
|
+
uvx twine check --strict dist/*
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Run the docstring examples and build the documentation:
|
|
84
|
+
|
|
85
|
+
```bash
|
|
86
|
+
uv run pytest --doctest-modules --pyargs robustmodels
|
|
87
|
+
uv run --group docs sphinx-build -W -b html docs docs/_build/html
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
CI runs the tests against the installed wheel on Python 3.10 to 3.14.
|
|
91
|
+
The Python 3.10 job uses the minimum supported NumPy, SciPy and scikit-learn versions.
|
|
92
|
+
|
|
93
|
+
The examples in `docs/` and in the docstrings run as tests. The tests cover estimator definitions, statistical properties, edge cases,
|
|
94
|
+
agreement with R's robustbase and rrcov, and
|
|
95
|
+
[scikit-learn compatibility](https://scikit-learn.org/stable/developers/develop.html).
|
|
96
|
+
|
|
97
|
+
Code lives in `src/robustmodels`:
|
|
98
|
+
|
|
99
|
+
| File | Contents | Internal dependencies |
|
|
100
|
+
| --- | --- | --- |
|
|
101
|
+
| `location.py` | Location estimators | `scale.py`, `_validation.py` |
|
|
102
|
+
| `scale.py` | Scale, skewness, correction factors and pairwise selection | `_validation.py` |
|
|
103
|
+
| `covariance.py` | Covariance estimators and their fitting steps | `scale.py`, `_validation.py` |
|
|
104
|
+
| `_validation.py` | Input and parameter checks | None |
|
|
105
|
+
|
|
106
|
+
Each method and its steps stay together. Auditing an estimator requires its file
|
|
107
|
+
and at most two supporting files. `__init__.py` exposes the three public modules.
|
|
108
|
+
|
|
109
|
+
### Release
|
|
110
|
+
|
|
111
|
+
1. Set `version` in `pyproject.toml`, run `uv lock`, and replace "unreleased"
|
|
112
|
+
in [CHANGELOG.md](https://github.com/omarbecerrasierra/robustmodels/blob/main/CHANGELOG.md)
|
|
113
|
+
with the date.
|
|
114
|
+
2. Merge into `main`, then tag and push: `git tag v0.1.0 && git push origin v0.1.0`.
|
|
115
|
+
3. CI tests, builds and, after approval of the `pypi` environment, publishes to
|
|
116
|
+
PyPI through trusted publishing. The tag must match the package version.
|
|
117
|
+
|
|
118
|
+
## License
|
|
119
|
+
|
|
120
|
+
BSD 3-Clause; see [LICENSE](https://github.com/omarbecerrasierra/robustmodels/blob/main/LICENSE).
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"""Sphinx configuration."""
|
|
2
|
+
|
|
3
|
+
from importlib.metadata import version as package_version
|
|
4
|
+
|
|
5
|
+
project = "robustmodels"
|
|
6
|
+
author = "omarbecerrasierra"
|
|
7
|
+
copyright = "2026, omarbecerrasierra"
|
|
8
|
+
release = package_version("robustmodels")
|
|
9
|
+
|
|
10
|
+
extensions = [
|
|
11
|
+
"myst_parser",
|
|
12
|
+
"sphinx.ext.autodoc",
|
|
13
|
+
"sphinx.ext.napoleon",
|
|
14
|
+
"sphinx.ext.viewcode",
|
|
15
|
+
]
|
|
16
|
+
exclude_patterns = ["_build"]
|
|
17
|
+
|
|
18
|
+
autodoc_member_order = "bysource"
|
|
19
|
+
add_module_names = False
|
|
20
|
+
napoleon_google_docstring = False
|
|
21
|
+
|
|
22
|
+
html_theme = "furo"
|
|
23
|
+
html_title = f"robustmodels {release}"
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
# Covariance
|
|
2
|
+
|
|
3
|
+
Robust estimators of multivariate location and scatter, in
|
|
4
|
+
`robustmodels.covariance`.
|
|
5
|
+
|
|
6
|
+
## Choosing an estimator
|
|
7
|
+
|
|
8
|
+
| Class | Needs | Random | Notes |
|
|
9
|
+
| --- | --- | --- | --- |
|
|
10
|
+
| `MCD` | `n > p` | Optional | The standard choice; `algorithm="deterministic"` avoids random starts |
|
|
11
|
+
| `MRCD` | Any `n`, `p` | No | Regularized MCD for many variables, including `p > n` |
|
|
12
|
+
| `MVE` | `n > p + 1` | Yes | Smallest covering ellipsoid; less efficient than MCD |
|
|
13
|
+
| `OGK` | `n >= 2` | No | Fast for many variables; not rotation equivariant |
|
|
14
|
+
| `SEstimator` | `n > p` | Yes | Smooth weights instead of a hard subset |
|
|
15
|
+
| `MMEstimator` | `n > p` | Yes | S-estimator refined to 95% shape efficiency |
|
|
16
|
+
|
|
17
|
+
Here `n` is the number of observations and `p` the number of variables. MCD,
|
|
18
|
+
MVE, S and MM are affine equivariant and reach a 50% breakdown point with their
|
|
19
|
+
default settings.
|
|
20
|
+
|
|
21
|
+
## Use
|
|
22
|
+
|
|
23
|
+
All classes are scikit-learn estimators: parameters go to the constructor,
|
|
24
|
+
`fit(X)` returns the estimator, and results are attributes ending in `_`.
|
|
25
|
+
|
|
26
|
+
```pycon
|
|
27
|
+
>>> import numpy as np
|
|
28
|
+
>>> from robustmodels.covariance import MCD
|
|
29
|
+
>>> X = np.random.default_rng(0).normal(size=(100, 2))
|
|
30
|
+
>>> X[:10] += 10 # ten outliers
|
|
31
|
+
>>> mcd = MCD(random_state=0).fit(X)
|
|
32
|
+
>>> mcd.location_.round(2).tolist()
|
|
33
|
+
[0.01, 0.1]
|
|
34
|
+
>>> mcd.covariance_.round(2).tolist()
|
|
35
|
+
[[0.93, 0.15], [0.15, 1.06]]
|
|
36
|
+
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Every fitted estimator has:
|
|
40
|
+
|
|
41
|
+
| Attribute or method | Meaning |
|
|
42
|
+
| --- | --- |
|
|
43
|
+
| `location_` | Robust location |
|
|
44
|
+
| `covariance_` | Robust covariance matrix |
|
|
45
|
+
| `precision_` | Its (pseudo-)inverse |
|
|
46
|
+
| `dist_` | Squared robust distances of the training data |
|
|
47
|
+
| `mahalanobis(X)` | Squared robust distances of any observations |
|
|
48
|
+
| `score(X)`, `error_norm(...)`, `get_precision()` | As in `sklearn.covariance.EmpiricalCovariance` |
|
|
49
|
+
|
|
50
|
+
Estimators that select observations (`MCD`, `MRCD`, `MVE`, `OGK`) also have
|
|
51
|
+
`support_`, a boolean mask of the observations used.
|
|
52
|
+
|
|
53
|
+
### Detecting outliers
|
|
54
|
+
|
|
55
|
+
Under the normal model, squared robust distances follow approximately a
|
|
56
|
+
chi-square distribution with `p` degrees of freedom. Flag the observations
|
|
57
|
+
above a high quantile:
|
|
58
|
+
|
|
59
|
+
```pycon
|
|
60
|
+
>>> from scipy.stats import chi2
|
|
61
|
+
>>> flagged = mcd.mahalanobis(X) > chi2.ppf(0.975, df=2)
|
|
62
|
+
>>> np.flatnonzero(flagged).tolist()
|
|
63
|
+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 69]
|
|
64
|
+
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
The ten planted outliers are found. Observation 69 is a clean point in the
|
|
68
|
+
tail: with a 0.975 quantile, about 2.5% of the clean data are expected there.
|
|
69
|
+
|
|
70
|
+
### Reproducibility
|
|
71
|
+
|
|
72
|
+
`MCD` (with `algorithm="fast"`), `MVE`, `SEstimator` and `MMEstimator` draw
|
|
73
|
+
random subsets. Pass `random_state` to get the same result on every run, or use
|
|
74
|
+
a deterministic estimator:
|
|
75
|
+
|
|
76
|
+
```pycon
|
|
77
|
+
>>> det = MCD(algorithm="deterministic").fit(X)
|
|
78
|
+
>>> int(det.support_[:10].sum()) # no outlier is used
|
|
79
|
+
0
|
|
80
|
+
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### More variables than observations
|
|
84
|
+
|
|
85
|
+
`MCD` needs more observations than variables. `MRCD` regularizes the covariance
|
|
86
|
+
and works in any dimension:
|
|
87
|
+
|
|
88
|
+
```pycon
|
|
89
|
+
>>> from robustmodels.covariance import MRCD
|
|
90
|
+
>>> wide = np.random.default_rng(0).normal(size=(20, 50))
|
|
91
|
+
>>> mrcd = MRCD().fit(wide)
|
|
92
|
+
>>> mrcd.covariance_.shape
|
|
93
|
+
(50, 50)
|
|
94
|
+
>>> 0 < mrcd.rho_ < 1 # regularization weight
|
|
95
|
+
True
|
|
96
|
+
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### With scikit-learn
|
|
100
|
+
|
|
101
|
+
The estimators work with `clone`, `get_params`, `set_params` and pipelines:
|
|
102
|
+
|
|
103
|
+
```pycon
|
|
104
|
+
>>> from sklearn.base import clone
|
|
105
|
+
>>> clone(mcd).set_params(alpha=0.75).fit(X).h_
|
|
106
|
+
75
|
|
107
|
+
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## Reference
|
|
111
|
+
|
|
112
|
+
```{eval-rst}
|
|
113
|
+
.. currentmodule:: robustmodels.covariance
|
|
114
|
+
|
|
115
|
+
.. autoclass:: MCD
|
|
116
|
+
:members: fit
|
|
117
|
+
.. autoclass:: MRCD
|
|
118
|
+
:members: fit
|
|
119
|
+
.. autoclass:: MVE
|
|
120
|
+
:members: fit
|
|
121
|
+
.. autoclass:: OGK
|
|
122
|
+
:members: fit
|
|
123
|
+
.. autoclass:: SEstimator
|
|
124
|
+
:members: fit
|
|
125
|
+
.. autoclass:: MMEstimator
|
|
126
|
+
:members: fit
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### Correction factors
|
|
130
|
+
|
|
131
|
+
```{eval-rst}
|
|
132
|
+
.. autofunction:: h_alpha_n
|
|
133
|
+
.. autofunction:: mcd_consistency
|
|
134
|
+
.. autofunction:: mcd_small_sample
|
|
135
|
+
```
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# robustmodels
|
|
2
|
+
|
|
3
|
+
Robust statistics for Python: estimators of location, scale and covariance that
|
|
4
|
+
stay reliable when part of the data are outliers.
|
|
5
|
+
|
|
6
|
+
```bash
|
|
7
|
+
pip install robustmodels
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
Requires Python 3.10+, NumPy, SciPy and scikit-learn.
|
|
11
|
+
|
|
12
|
+
## Quick start
|
|
13
|
+
|
|
14
|
+
One gross outlier ruins the mean and the standard deviation, but not their
|
|
15
|
+
robust counterparts:
|
|
16
|
+
|
|
17
|
+
```pycon
|
|
18
|
+
>>> import numpy as np
|
|
19
|
+
>>> from robustmodels import location, scale
|
|
20
|
+
>>> x = [1, 2, 2, 3, 4, 100]
|
|
21
|
+
>>> round(float(np.mean(x)), 2), round(float(np.std(x, ddof=1)), 2)
|
|
22
|
+
(18.67, 39.86)
|
|
23
|
+
>>> location.hodges_lehmann(x), round(scale.qn(x), 2)
|
|
24
|
+
(3.0, 1.36)
|
|
25
|
+
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
The covariance estimators are scikit-learn estimators:
|
|
29
|
+
|
|
30
|
+
```pycon
|
|
31
|
+
>>> from robustmodels.covariance import MCD
|
|
32
|
+
>>> X = np.random.default_rng(0).normal(size=(100, 2))
|
|
33
|
+
>>> X[:10] += 10 # ten outliers
|
|
34
|
+
>>> mcd = MCD(random_state=0).fit(X)
|
|
35
|
+
>>> mcd.location_.round(2).tolist()
|
|
36
|
+
[0.01, 0.1]
|
|
37
|
+
>>> mcd.covariance_.round(2).tolist()
|
|
38
|
+
[[0.93, 0.15], [0.15, 1.06]]
|
|
39
|
+
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Contents
|
|
43
|
+
|
|
44
|
+
| Page | What it covers |
|
|
45
|
+
| --- | --- |
|
|
46
|
+
| [Location](location.md) | Hodges-Lehmann, M-estimators, tau location, trimmed and winsorized means |
|
|
47
|
+
| [Scale and skewness](scale.md) | MAD, Qn, Sn, Pn, tau scale, M-scale, medcouple and tail weights |
|
|
48
|
+
| [Covariance](covariance.md) | MCD, MRCD, MVE, OGK, S- and MM-estimators; outlier detection |
|
|
49
|
+
|
|
50
|
+
Each page explains how to choose and use the estimators, then documents every
|
|
51
|
+
function and class: definition, parameters, return value, references and an
|
|
52
|
+
example.
|
|
53
|
+
|
|
54
|
+
## Conventions
|
|
55
|
+
|
|
56
|
+
- Univariate functions take a one-dimensional sample of finite values and return
|
|
57
|
+
a Python `float`. `NaN` or infinite values raise `ValueError`.
|
|
58
|
+
- An empty sample returns `nan`; a single observation has zero scale.
|
|
59
|
+
- Covariance estimators take a matrix with observations in rows and variables
|
|
60
|
+
in columns.
|
|
61
|
+
- Invalid parameters raise `ValueError` with the name of the parameter.
|
|
62
|
+
- The **breakdown point** is the largest fraction of arbitrary outliers an
|
|
63
|
+
estimator tolerates. The **Gaussian efficiency** compares its variance with
|
|
64
|
+
that of the classical estimator on clean normal data.
|
|
65
|
+
|
|
66
|
+
```{toctree}
|
|
67
|
+
:hidden:
|
|
68
|
+
|
|
69
|
+
location
|
|
70
|
+
scale
|
|
71
|
+
covariance
|
|
72
|
+
changelog
|
|
73
|
+
```
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# Location
|
|
2
|
+
|
|
3
|
+
Robust alternatives to the sample mean, in `robustmodels.location`.
|
|
4
|
+
|
|
5
|
+
## Choosing an estimator
|
|
6
|
+
|
|
7
|
+
| Function | Breakdown point | Gaussian efficiency | Notes |
|
|
8
|
+
| --- | --- | --- | --- |
|
|
9
|
+
| `hodges_lehmann` | 29.3% | 95.5% | No tuning; a good default |
|
|
10
|
+
| `m_location` (Huber) | 50% | 95% | Monotone psi, unique solution |
|
|
11
|
+
| `m_location` (bisquare) | 50% | 95% | Rejects gross outliers completely |
|
|
12
|
+
| `tau_location` | 50% | | One weighted mean; no iteration |
|
|
13
|
+
| `trimmed_mean` | `proportion` | | Removes the tails |
|
|
14
|
+
| `winsorized_mean` | `proportion` | | Clips the tails |
|
|
15
|
+
|
|
16
|
+
The median itself (`numpy.median`) has a 50% breakdown point and 64% efficiency.
|
|
17
|
+
|
|
18
|
+
## Use
|
|
19
|
+
|
|
20
|
+
```pycon
|
|
21
|
+
>>> from robustmodels import location
|
|
22
|
+
>>> x = [1, 2, 2, 3, 4, 100]
|
|
23
|
+
>>> location.hodges_lehmann(x)
|
|
24
|
+
3.0
|
|
25
|
+
>>> round(location.m_location(x), 4)
|
|
26
|
+
2.7988
|
|
27
|
+
>>> round(location.m_location(x, psi="bisquare"), 4)
|
|
28
|
+
2.3869
|
|
29
|
+
>>> round(location.tau_location(x), 4)
|
|
30
|
+
2.3918
|
|
31
|
+
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Trimming works with whole observations: it removes `floor(proportion * n)` values
|
|
35
|
+
from each end, so the default 10% removes nothing from a sample of six.
|
|
36
|
+
|
|
37
|
+
```pycon
|
|
38
|
+
>>> round(location.trimmed_mean(x), 4)
|
|
39
|
+
18.6667
|
|
40
|
+
>>> location.trimmed_mean(x, proportion=0.2)
|
|
41
|
+
2.75
|
|
42
|
+
>>> round(location.winsorized_mean(x, proportion=0.2), 4)
|
|
43
|
+
2.8333
|
|
44
|
+
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
`m_location` keeps the scale fixed. By default it is the MAD; pass another
|
|
48
|
+
estimate with `scale`:
|
|
49
|
+
|
|
50
|
+
```pycon
|
|
51
|
+
>>> from robustmodels import scale
|
|
52
|
+
>>> round(location.m_location(x, scale=scale.qn(x)), 4)
|
|
53
|
+
2.7655
|
|
54
|
+
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Reference
|
|
58
|
+
|
|
59
|
+
```{eval-rst}
|
|
60
|
+
.. currentmodule:: robustmodels.location
|
|
61
|
+
|
|
62
|
+
.. autofunction:: hodges_lehmann
|
|
63
|
+
.. autofunction:: m_location
|
|
64
|
+
.. autofunction:: tau_location
|
|
65
|
+
.. autofunction:: trimmed_mean
|
|
66
|
+
.. autofunction:: winsorized_mean
|
|
67
|
+
```
|