robpy 0.0.2__tar.gz → 0.0.4__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.
- {robpy-0.0.2 → robpy-0.0.4}/PKG-INFO +5 -3
- {robpy-0.0.2 → robpy-0.0.4}/README.rst +3 -1
- {robpy-0.0.2 → robpy-0.0.4}/pyproject.toml +1 -1
- {robpy-0.0.2 → robpy-0.0.4}/robpy/covariance/mcd.py +7 -2
- {robpy-0.0.2 → robpy-0.0.4}/robpy/outliers/ddc.py +3 -3
- {robpy-0.0.2 → robpy-0.0.4}/robpy/pca/robpca.py +5 -1
- {robpy-0.0.2 → robpy-0.0.4}/robpy/utils/outlyingness.py +19 -4
- {robpy-0.0.2 → robpy-0.0.4}/LICENSE +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/__init__.py +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/covariance/__init__.py +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/covariance/base.py +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/covariance/cellmcd.py +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/covariance/initial_ddcw.py +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/covariance/kendall.py +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/covariance/ogk.py +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/covariance/utils/alter_covariance.py +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/covariance/utils/cellmcd_utils.py +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/covariance/utils/cellmcd_visualization_utils.py +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/covariance/wrapping.py +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/datasets/__init__.py +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/datasets/base.py +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/datasets/data/animals.csv +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/datasets/data/glass.csv +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/datasets/data/stars.csv +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/datasets/data/telephone.csv +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/datasets/data/topgear.csv +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/datasets/descr/animals.rst +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/datasets/descr/glass.rst +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/datasets/descr/stars.rst +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/datasets/descr/telephone.rst +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/datasets/descr/topgear.rst +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/outliers/__init__.py +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/pca/__init__.py +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/pca/base.py +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/pca/spca.py +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/preprocessing/__init__.py +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/preprocessing/data_cleaner.py +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/preprocessing/scaling.py +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/preprocessing/transfo.py +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/preprocessing/utils.py +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/regression/__init__.py +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/regression/base.py +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/regression/lts.py +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/regression/mm.py +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/regression/s.py +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/univariate/__init__.py +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/univariate/adjusted_boxplot.py +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/univariate/base.py +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/univariate/mcd.py +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/univariate/onestep_m.py +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/univariate/qn.py +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/univariate/tau.py +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/utils/distance.py +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/utils/general.py +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/utils/logging.py +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/utils/median.py +0 -0
- {robpy-0.0.2 → robpy-0.0.4}/robpy/utils/rho.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
2
|
Name: robpy
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.4
|
|
4
4
|
Summary:
|
|
5
5
|
Author: Thomas Servotte
|
|
6
6
|
Author-email: servottethomas@gmail.com
|
|
@@ -25,7 +25,9 @@ Robpy
|
|
|
25
25
|
SciPy.
|
|
26
26
|
|
|
27
27
|
The project was started in 2023 by Tim Verdonck, Jakob Raymaekers, Thomas Servotte, Sarah Leyder & Thomas Decorte at the University of Antwerp and imec IDLab.
|
|
28
|
-
The project is currently maintained by
|
|
28
|
+
The project is currently maintained by the same team and distributed under MIT license.
|
|
29
|
+
|
|
30
|
+
Documentation is available at https://robpy.readthedocs.io/en/latest/
|
|
29
31
|
|
|
30
32
|
|
|
31
33
|
|
|
@@ -5,7 +5,9 @@ Robpy
|
|
|
5
5
|
SciPy.
|
|
6
6
|
|
|
7
7
|
The project was started in 2023 by Tim Verdonck, Jakob Raymaekers, Thomas Servotte, Sarah Leyder & Thomas Decorte at the University of Antwerp and imec IDLab.
|
|
8
|
-
The project is currently maintained by
|
|
8
|
+
The project is currently maintained by the same team and distributed under MIT license.
|
|
9
|
+
|
|
10
|
+
Documentation is available at https://robpy.readthedocs.io/en/latest/
|
|
9
11
|
|
|
10
12
|
|
|
11
13
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[tool.poetry]
|
|
2
2
|
name = "robpy"
|
|
3
|
-
version = "0.0.
|
|
3
|
+
version = "0.0.4" # versioning is done by poetry-dynamic-versioning
|
|
4
4
|
description = ""
|
|
5
5
|
authors = ["Thomas Servotte <servottethomas@gmail.com>", "Sarah Leyder <sarah.leyder@uantwerpen.be>"]
|
|
6
6
|
readme = "README.rst"
|
|
@@ -38,6 +38,7 @@ class FastMCD(RobustCovariance):
|
|
|
38
38
|
verbosity: int = logging.WARNING,
|
|
39
39
|
store_precision=True,
|
|
40
40
|
assume_centered=False,
|
|
41
|
+
random_seed: int | None = None,
|
|
41
42
|
):
|
|
42
43
|
"""
|
|
43
44
|
Fast MCD estimator based on the algorithm proposed in Rousseeuw and Van Driessen (1999)
|
|
@@ -66,6 +67,8 @@ class FastMCD(RobustCovariance):
|
|
|
66
67
|
Whether to apply a consistency correction to the raw covariance estimate
|
|
67
68
|
reweighting (bool, optional):
|
|
68
69
|
Whether to apply reweighting to the raw covariance estimate
|
|
70
|
+
random_seed (int | None, optional):
|
|
71
|
+
Can be used to provide a random seed.
|
|
69
72
|
References:
|
|
70
73
|
Rousseeuw and Van Driessen, A Fast Algorithm for the Minimum Covariance Determinant
|
|
71
74
|
Estimator, 1999, American Statistical Association and
|
|
@@ -83,12 +86,14 @@ class FastMCD(RobustCovariance):
|
|
|
83
86
|
self.reweighting = reweighting
|
|
84
87
|
self.logger = get_logger("FastMCD", level=verbosity)
|
|
85
88
|
self.verbosity = verbosity
|
|
89
|
+
self.random_seed = random_seed
|
|
86
90
|
|
|
87
91
|
def calculate_covariance(self, X) -> np.ndarray:
|
|
88
92
|
if self.alpha == 1 or self.alpha == X.shape[0]:
|
|
89
93
|
self.logger.warning(f"Default covariance is returned as alpha is {self.alpha}.")
|
|
90
94
|
self.location_ = X.mean(0)
|
|
91
95
|
return np.cov(X, rowvar=False)
|
|
96
|
+
self.rng = np.random.default_rng(self.random_seed)
|
|
92
97
|
# partition data (n_partitions > 1 can speed up algorithm for large datasets)
|
|
93
98
|
partitions = self._partition_data(X)
|
|
94
99
|
self.logger.info(f"Partitioned data into {len(partitions)} partitions")
|
|
@@ -224,7 +229,7 @@ class FastMCD(RobustCovariance):
|
|
|
224
229
|
det = np.linalg.det(cov)
|
|
225
230
|
if ensure_non_singular:
|
|
226
231
|
while math.isclose(det, 0):
|
|
227
|
-
new_index =
|
|
232
|
+
new_index = self.rng.choice(np.delete(np.arange(X.shape[0]), indices))
|
|
228
233
|
indices = np.append(indices, new_index)
|
|
229
234
|
mu = X[indices].mean(axis=0)
|
|
230
235
|
cov = np.cov(X[indices], rowvar=False)
|
|
@@ -234,7 +239,7 @@ class FastMCD(RobustCovariance):
|
|
|
234
239
|
def _get_initial_subsets(self, X: np.ndarray, n_subsets: int) -> list[HSubset]:
|
|
235
240
|
return [
|
|
236
241
|
self._get_subset(
|
|
237
|
-
indices=
|
|
242
|
+
indices=self.rng.choice(X.shape[0], X.shape[1] + 1, replace=False),
|
|
238
243
|
X=X,
|
|
239
244
|
ensure_non_singular=True,
|
|
240
245
|
)
|
|
@@ -14,13 +14,13 @@ from robpy.utils.distance import mahalanobis_distance
|
|
|
14
14
|
from robpy.univariate.base import RobustScale
|
|
15
15
|
|
|
16
16
|
|
|
17
|
-
def get_custom_cmap(vmax_clip: float):
|
|
17
|
+
def get_custom_cmap(vmax_clip: float, neutral_color: str = "#f7f286") -> matplotlib.colors.Colormap:
|
|
18
18
|
norm = matplotlib.colors.Normalize(-vmax_clip, vmax_clip)
|
|
19
19
|
colors = [
|
|
20
20
|
[norm(-vmax_clip), "#4652a3"],
|
|
21
21
|
[norm(-(vmax_clip + 2.5) / 2), "#8d6d8c"],
|
|
22
|
-
[norm(-2.5),
|
|
23
|
-
[norm(2.5),
|
|
22
|
+
[norm(-2.5), neutral_color],
|
|
23
|
+
[norm(2.5), neutral_color],
|
|
24
24
|
[norm(vmax_clip), "#ec2123"],
|
|
25
25
|
]
|
|
26
26
|
return matplotlib.colors.LinearSegmentedColormap.from_list("", colors)
|
|
@@ -16,6 +16,7 @@ class ROBPCA(RobustPCA):
|
|
|
16
16
|
k_min_var_explained: float = 0.8,
|
|
17
17
|
alpha: float = 0.75,
|
|
18
18
|
final_MCD_step: bool = True,
|
|
19
|
+
random_seed: int | None = None,
|
|
19
20
|
):
|
|
20
21
|
"""Implementation of ROBPCA algorithm as described in
|
|
21
22
|
Hubert, Rousseeuw & Vanden Branden (2005) and Hubert, Rousseeuw & Verdonck (2009)
|
|
@@ -33,6 +34,8 @@ class ROBPCA(RobustPCA):
|
|
|
33
34
|
robust estimates. If False, the eigenvectors after projection onto V1 (subspace
|
|
34
35
|
determined by points with OD < cutoff) are used as the final estimates.
|
|
35
36
|
Defaults to True.
|
|
37
|
+
random_seed (int | None, optional):
|
|
38
|
+
Can be used to provide a random seed.
|
|
36
39
|
|
|
37
40
|
References:
|
|
38
41
|
- Hubert, Rousseeuw & Vanden Branden (2005),
|
|
@@ -44,6 +47,7 @@ class ROBPCA(RobustPCA):
|
|
|
44
47
|
self.k_min_var_explained = k_min_var_explained
|
|
45
48
|
self.alpha = alpha
|
|
46
49
|
self.final_MCD_step = final_MCD_step
|
|
50
|
+
self.random_seed = random_seed
|
|
47
51
|
|
|
48
52
|
def fit(self, X: np.ndarray) -> ROBPCA:
|
|
49
53
|
# step 1: singular value decomposition = applying standard PCA
|
|
@@ -52,7 +56,7 @@ class ROBPCA(RobustPCA):
|
|
|
52
56
|
self.location_ = np.mean(X, axis=0)
|
|
53
57
|
loadings = pca.components_.T
|
|
54
58
|
# step 2: stahel donoho outlyingness --> h subset
|
|
55
|
-
outlyingness = stahel_donoho(X)
|
|
59
|
+
outlyingness = stahel_donoho(X, random_seed=self.random_seed)
|
|
56
60
|
h_index = np.argsort(outlyingness)[: int(self.alpha * X.shape[0])]
|
|
57
61
|
h_cov = np.cov(X[h_index], rowvar=False)
|
|
58
62
|
# step 3: project on k-dimensional subspace
|
|
@@ -2,7 +2,12 @@ import numpy as np
|
|
|
2
2
|
from scipy.stats import median_abs_deviation
|
|
3
3
|
|
|
4
4
|
|
|
5
|
-
def stahel_donoho(
|
|
5
|
+
def stahel_donoho(
|
|
6
|
+
X: np.ndarray,
|
|
7
|
+
n_points: int = 2,
|
|
8
|
+
n_dir: int = 250,
|
|
9
|
+
random_seed: int | None = None,
|
|
10
|
+
) -> np.ndarray:
|
|
6
11
|
"""Calculate the degree of outlyingness for multivariate points.
|
|
7
12
|
Based on the algorithm proposed by Stahel (1981) and Donoho (1982).
|
|
8
13
|
|
|
@@ -10,6 +15,7 @@ def stahel_donoho(X: np.ndarray, n_points: int = 2, n_dir: int = 250) -> np.ndar
|
|
|
10
15
|
X (np.ndarray): data matric of shape (n_obs, n_features)
|
|
11
16
|
n_points (int, optional): number of points to determine the hyperplane. Defaults to 2.
|
|
12
17
|
n_dir (int, optional): number of random directions to consider. Defaults to 250.
|
|
18
|
+
random_seed (int | None, optional): can be used to provide a random seed.
|
|
13
19
|
|
|
14
20
|
Returns:
|
|
15
21
|
np.ndarray: single column of outlyingness values
|
|
@@ -23,7 +29,10 @@ def stahel_donoho(X: np.ndarray, n_points: int = 2, n_dir: int = 250) -> np.ndar
|
|
|
23
29
|
"""
|
|
24
30
|
# step 1: get random directions
|
|
25
31
|
D = np.hstack(
|
|
26
|
-
[
|
|
32
|
+
[
|
|
33
|
+
_get_random_direction(X, n_points=n_points, random_seed=random_seed).reshape(-1, 1)
|
|
34
|
+
for _ in range(n_dir)
|
|
35
|
+
]
|
|
27
36
|
) # (n_features, n_dir)
|
|
28
37
|
# step 2: projections
|
|
29
38
|
projections = X @ D # (n_obs, n_dir)
|
|
@@ -37,17 +46,23 @@ def stahel_donoho(X: np.ndarray, n_points: int = 2, n_dir: int = 250) -> np.ndar
|
|
|
37
46
|
)
|
|
38
47
|
|
|
39
48
|
|
|
40
|
-
def _get_random_direction(
|
|
49
|
+
def _get_random_direction(
|
|
50
|
+
X: np.ndarray,
|
|
51
|
+
n_points: int = 2,
|
|
52
|
+
random_seed: int | None = None,
|
|
53
|
+
) -> np.ndarray:
|
|
41
54
|
"""Get direction orthogonal to the hyperplane spanned by random points in X
|
|
42
55
|
|
|
43
56
|
Args:
|
|
44
57
|
X (np.ndarray): data matrix (n x p)
|
|
45
58
|
n_points (int, optional): number of points to determine the hyperplane. Defaults to 2.
|
|
59
|
+
random_seed (int | None, optional): can be used to provide a random seed.
|
|
46
60
|
|
|
47
61
|
Returns:
|
|
48
62
|
np.ndarray: vector of shape (p, ) indicating the direction
|
|
49
63
|
"""
|
|
50
|
-
|
|
64
|
+
rng = np.random.default_rng(random_seed)
|
|
65
|
+
points = X[rng.choice(X.shape[0], n_points, replace=False)]
|
|
51
66
|
|
|
52
67
|
if n_points == 2:
|
|
53
68
|
d = points[0] - points[1]
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|