robpy 0.1.0__py3-none-any.whl

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.

Potentially problematic release.


This version of robpy might be problematic. Click here for more details.

Files changed (55) hide show
  1. robpy/__init__.py +0 -0
  2. robpy/covariance/__init__.py +6 -0
  3. robpy/covariance/base.py +82 -0
  4. robpy/covariance/cellmcd.py +621 -0
  5. robpy/covariance/initial_ddcw.py +133 -0
  6. robpy/covariance/kendall.py +21 -0
  7. robpy/covariance/mcd.py +470 -0
  8. robpy/covariance/ogk.py +95 -0
  9. robpy/covariance/utils/alter_covariance.py +31 -0
  10. robpy/covariance/utils/cellmcd_utils.py +38 -0
  11. robpy/covariance/utils/cellmcd_visualization_utils.py +96 -0
  12. robpy/covariance/wrapping.py +72 -0
  13. robpy/datasets/__init__.py +4 -0
  14. robpy/datasets/base.py +389 -0
  15. robpy/datasets/data/animals.csv +66 -0
  16. robpy/datasets/data/glass.csv +181 -0
  17. robpy/datasets/data/stars.csv +48 -0
  18. robpy/datasets/data/telephone.csv +25 -0
  19. robpy/datasets/data/topgear.csv +298 -0
  20. robpy/datasets/descr/animals.rst +26 -0
  21. robpy/datasets/descr/glass.rst +20 -0
  22. robpy/datasets/descr/stars.rst +28 -0
  23. robpy/datasets/descr/telephone.rst +23 -0
  24. robpy/datasets/descr/topgear.rst +51 -0
  25. robpy/outliers/__init__.py +4 -0
  26. robpy/outliers/ddc.py +296 -0
  27. robpy/pca/__init__.py +3 -0
  28. robpy/pca/base.py +97 -0
  29. robpy/pca/robpca.py +89 -0
  30. robpy/pca/spca.py +48 -0
  31. robpy/preprocessing/__init__.py +3 -0
  32. robpy/preprocessing/data_cleaner.py +214 -0
  33. robpy/preprocessing/scaling.py +57 -0
  34. robpy/preprocessing/transfo.py +591 -0
  35. robpy/preprocessing/utils.py +63 -0
  36. robpy/regression/__init__.py +3 -0
  37. robpy/regression/base.py +116 -0
  38. robpy/regression/lts.py +408 -0
  39. robpy/regression/mm_estimator.py +57 -0
  40. robpy/regression/s_estimator.py +188 -0
  41. robpy/univariate/__init__.py +9 -0
  42. robpy/univariate/base.py +53 -0
  43. robpy/univariate/mcd.py +84 -0
  44. robpy/univariate/onestep_m.py +143 -0
  45. robpy/univariate/qn.py +112 -0
  46. robpy/univariate/tau.py +63 -0
  47. robpy/utils/distance.py +24 -0
  48. robpy/utils/general.py +31 -0
  49. robpy/utils/logging.py +22 -0
  50. robpy/utils/median.py +62 -0
  51. robpy/utils/outlyingness.py +61 -0
  52. robpy/utils/rho.py +35 -0
  53. robpy-0.1.0.dist-info/METADATA +31 -0
  54. robpy-0.1.0.dist-info/RECORD +55 -0
  55. robpy-0.1.0.dist-info/WHEEL +4 -0
robpy/utils/general.py ADDED
@@ -0,0 +1,31 @@
1
+ import numpy as np
2
+
3
+
4
+ def inverse_submatrix(A: np.ndarray, A_inv: np.ndarray, indices: np.array) -> np.ndarray:
5
+ """Given a matrix A and its inverse A_inv, this function calculates the inverse
6
+ of the submatrix of A consisting of the rows and columns in indices.
7
+
8
+ Arguments:
9
+ A (np.ndarray): the matrix of interest
10
+ A_inv (np.ndarray): the inverse of the matrix of interest
11
+ indices (np.array): the indices corresponding to the submatrix of interest
12
+ """
13
+
14
+ p = A.shape[1]
15
+ n_submatrix = len(indices)
16
+ indices_neg = np.setdiff1d(np.arange(p), indices)
17
+ result = np.zeros([n_submatrix, n_submatrix])
18
+
19
+ if n_submatrix < p and n_submatrix > p / 2.0: # in this scenario it useful to use the trick
20
+ result = (
21
+ A_inv[np.ix_(indices, indices)]
22
+ - A_inv[np.ix_(indices, indices_neg)]
23
+ @ np.linalg.inv(A_inv[np.ix_(indices_neg, indices_neg)])
24
+ @ A_inv[np.ix_(indices_neg, indices)]
25
+ )
26
+ elif n_submatrix < p and n_submatrix <= p / 2.0: # don't use the trick
27
+ result = np.linalg.inv(A[np.ix_(indices, indices)])
28
+ else: # submatrix is the original matrix
29
+ result = A_inv
30
+
31
+ return result
robpy/utils/logging.py ADDED
@@ -0,0 +1,22 @@
1
+ import logging
2
+
3
+
4
+ def get_logger(name: str, level: int = logging.INFO) -> logging.Logger:
5
+ """
6
+ Get a logger with the specified name and level.
7
+
8
+ Parameters:
9
+ name: The name of the logger.
10
+ level: The logging level.
11
+
12
+ Returns:
13
+ logger: A logger object
14
+ """
15
+ logger = logging.getLogger(name)
16
+ logger.setLevel(level)
17
+ if logger.hasHandlers():
18
+ logger.handlers.clear()
19
+ handler = logging.StreamHandler()
20
+ handler.setLevel(level)
21
+ logger.addHandler(handler)
22
+ return logger
robpy/utils/median.py ADDED
@@ -0,0 +1,62 @@
1
+ import numpy as np
2
+
3
+
4
+ def l1median(X: np.ndarray) -> float:
5
+ """
6
+ Implementation of the L1-median
7
+
8
+ Args:
9
+ X (np.ndarray): Data to compute the L1-median on.
10
+
11
+ References:
12
+ Fritz, H. and Filzmoser, P. and Croux, C. (2012)
13
+ A comparison of algorithms for the multivariate L1-median.
14
+ Computational Statistics 27, 393–410
15
+ """
16
+ epsilon = 1e-7
17
+ mu_0 = np.mean(X, axis=0)
18
+ while True:
19
+ centered_X = X - mu_0
20
+ d = np.sqrt(np.sum(centered_X * centered_X, axis=1))
21
+ W = 1 / d
22
+ mu_1 = np.sum((X * W[:, np.newaxis]).T / np.sum(W), axis=1)
23
+ if np.sum(np.abs(mu_0 - mu_1)) < epsilon:
24
+ return mu_1
25
+ else:
26
+ mu_0 = mu_1
27
+
28
+
29
+ def weighted_median(X: np.ndarray, weights: np.ndarray) -> float:
30
+ """
31
+ Computes a weighted median.
32
+
33
+ References:
34
+ Time-efficient algorithms for two highly robust estimators
35
+ of scale, Christophe Croux and Peter J. Rousseeuw (1992)
36
+ """
37
+ n = len(X)
38
+ wrest = 0
39
+ wtotal = np.sum(weights)
40
+ Xcand = X
41
+ while True:
42
+ k = np.ceil(n / 2).astype("int")
43
+ if n > 1:
44
+ trial = np.partition(X, k - 1)[
45
+ :k
46
+ ].max() # k^th order statistic, I think this can be programmed better...
47
+ else:
48
+ return Xcand[0]
49
+ wleft = np.sum(weights[X < trial])
50
+ wmid = np.sum(weights[X == trial])
51
+ if (2 * (wrest + wleft)) > wtotal:
52
+ Xcand = X[X < trial]
53
+ weightscand = weights[X < trial]
54
+ elif (2 * (wrest + wleft + wmid)) > wtotal:
55
+ return trial
56
+ else:
57
+ Xcand = X[X > trial]
58
+ weightscand = weights[X > trial]
59
+ wrest = wrest + wleft + wmid
60
+ X = Xcand
61
+ weights = weightscand
62
+ n = len(X)
@@ -0,0 +1,61 @@
1
+ import numpy as np
2
+ from scipy.stats import median_abs_deviation
3
+
4
+
5
+ def stahel_donoho(X: np.ndarray, n_points: int = 2, n_dir: int = 250) -> np.ndarray:
6
+ """Calculate the degree of outlyingness for multivariate points.
7
+ Based on the algorithm proposed by Stahel (1981) and Donoho (1982).
8
+
9
+ Args:
10
+ X (np.ndarray): data matric of shape (n_obs, n_features)
11
+ n_points (int, optional): number of points to determine the hyperplane. Defaults to 2.
12
+ n_dir (int, optional): number of random directions to consider. Defaults to 250.
13
+
14
+ Returns:
15
+ np.ndarray: single column of outlyingness values
16
+
17
+ References:
18
+ Stahel W.A. (1981). Robuste Schatzungen: infinitesimale Optimalitat und Schatzungen von
19
+ Kovarianzmatrizen. PhD Thesis, ETH Zurich.
20
+
21
+ Donoho D.L. (1982). Breakdown properties of multivariate location estimators.
22
+ Ph.D. Qualifying paper, Dept. Statistics, Harvard University, Boston.
23
+ """
24
+ # step 1: get random directions
25
+ D = np.hstack(
26
+ [_get_random_direction(X, n_points=n_points).reshape(-1, 1) for _ in range(n_dir)]
27
+ ) # (n_features, n_dir)
28
+ # step 2: projections
29
+ projections = X @ D # (n_obs, n_dir)
30
+
31
+ # step 3: outlyingness
32
+ # to do: let scale and loc estimators be passed as arguments
33
+ return np.max(
34
+ np.abs(projections - np.median(projections, axis=0))
35
+ / median_abs_deviation(projections, axis=0),
36
+ axis=1,
37
+ )
38
+
39
+
40
+ def _get_random_direction(X: np.ndarray, n_points: int = 2) -> np.ndarray:
41
+ """Get direction orthogonal to the hyperplane spanned by random points in X
42
+
43
+ Args:
44
+ X (np.ndarray): data matrix (n x p)
45
+ n_points (int, optional): number of points to determine the hyperplane. Defaults to 2.
46
+
47
+ Returns:
48
+ np.ndarray: vector of shape (p, ) indicating the direction
49
+ """
50
+ points = X[np.random.choice(X.shape[0], n_points, replace=False)]
51
+
52
+ if n_points == 2:
53
+ d = points[0] - points[1]
54
+ else:
55
+ # vectors spanning hyperplane
56
+ vectors = points - points[0]
57
+ # direction perpendicular to hyperplane
58
+ # U of SVD gives orthogonal basis, last vector is perpendicular to hyperplane
59
+ d = np.linalg.svd(vectors)[-1][-1]
60
+
61
+ return d / np.linalg.norm(d)
robpy/utils/rho.py ADDED
@@ -0,0 +1,35 @@
1
+ import numpy as np
2
+
3
+
4
+ class BaseRho:
5
+ def rho(self, X: np.ndarray) -> np.ndarray:
6
+ raise NotImplementedError
7
+
8
+ def psi(self, X: np.ndarray) -> np.ndarray:
9
+ raise NotImplementedError
10
+
11
+
12
+ class TukeyBisquare(BaseRho):
13
+ def __init__(self, c: float = 1.56):
14
+ self.c = c
15
+
16
+ def rho(self, X: np.ndarray) -> np.ndarray:
17
+ return np.where(
18
+ np.abs(X) <= self.c,
19
+ (X**2 / 2) - (X**4 / (2 * self.c**2)) + (X**6 / (6 * self.c**4)),
20
+ self.c**2 / 6,
21
+ )
22
+
23
+ def psi(self, X: np.ndarray) -> np.ndarray:
24
+ return np.where(np.abs(X) <= self.c, X * (1 - (X / self.c) ** 2) ** 2, 0)
25
+
26
+
27
+ class Huber(BaseRho):
28
+ def __init__(self, b: float = 1.5):
29
+ self.b = b
30
+
31
+ def rho(self, X: np.ndarray) -> np.ndarray:
32
+ return np.where(np.abs(X) <= self.b, X**2 / 2, self.b * np.abs(X) - self.b**2 / 2)
33
+
34
+ def psi(self, X: np.ndarray) -> np.ndarray:
35
+ return np.where(np.abs(X) <= self.b, X, self.b * np.sign(X))
@@ -0,0 +1,31 @@
1
+ Metadata-Version: 2.1
2
+ Name: robpy
3
+ Version: 0.1.0
4
+ Summary:
5
+ Author: Thomas Servotte
6
+ Author-email: servottethomas@gmail.com
7
+ Requires-Python: >=3.10,<4.0
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: Programming Language :: Python :: 3.10
10
+ Classifier: Programming Language :: Python :: 3.11
11
+ Classifier: Programming Language :: Python :: 3.12
12
+ Requires-Dist: matplotlib (>=3.8.2,<4.0.0)
13
+ Requires-Dist: scikit-learn (>=1.3)
14
+ Requires-Dist: scipy (>=1.11.4,<2.0.0)
15
+ Requires-Dist: seaborn (>=0.13.2,<0.14.0)
16
+ Requires-Dist: statsmodels (>=0.14.1,<0.15.0)
17
+ Requires-Dist: tqdm (>=4.66.1,<5.0.0)
18
+ Description-Content-Type: text/x-rst
19
+
20
+ Robpy
21
+ =========
22
+
23
+ **Robpy** is a Python module for robust statistical methods built on top of scikit-learn and
24
+ SciPy.
25
+
26
+ 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.
27
+ The project is currently maintained by a the same team and distributed under MIT license.
28
+
29
+
30
+
31
+
@@ -0,0 +1,55 @@
1
+ robpy/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ robpy/covariance/__init__.py,sha256=404640oJ0yW0NO1BmMwi3mHUgu3rO6NBeEm3cmSylLI,333
3
+ robpy/covariance/base.py,sha256=WgaiH9fWPzo8XHfR046AOfx8jxLBsBHbCcLa7S05qeA,2876
4
+ robpy/covariance/cellmcd.py,sha256=ZGvBnVyZtAG3LdNKie43SjZqTBqH4jLwEOtR0VuXblQ,25256
5
+ robpy/covariance/initial_ddcw.py,sha256=JOoIEtXrns-uOU6Oglh0EdAO5aI_k8aE-9o3TXnkwPo,5615
6
+ robpy/covariance/kendall.py,sha256=VVccyOq5x31xepehibXmVmIa7xohWlOmq-BGSt2HKc0,798
7
+ robpy/covariance/mcd.py,sha256=9JAiY8fuFt06guX25m3y7tJaSLHBnRaQQEN8s3JwIPc,19610
8
+ robpy/covariance/ogk.py,sha256=jtNcYIOglDPqLl-ZbWoSstE9XYYFkhNkbcFhAwpwU0M,4383
9
+ robpy/covariance/utils/alter_covariance.py,sha256=4zF4WagWcUqrv21uSU4YMqk2YcBGzsAz2TxwsayCCno,1059
10
+ robpy/covariance/utils/cellmcd_utils.py,sha256=VxImuVlsjvzLG6m9tMuiX7RE7KVqohFqEV2ub8sijRU,1501
11
+ robpy/covariance/utils/cellmcd_visualization_utils.py,sha256=em-bRhVVr21ZinUDHSmC_TFuCei0E_kyJZtiN2y2rAg,3651
12
+ robpy/covariance/wrapping.py,sha256=d4iRM1dSO-u_ZtDMc-NWICTgZX8FbbHNJAHQUcQiOgo,2522
13
+ robpy/datasets/__init__.py,sha256=sNmflU3BwlojKVJxGRUEGUdRw8pMAkqsSkserwjQkBU,187
14
+ robpy/datasets/base.py,sha256=qF6Iy-SjunEdTZ4tpU4HTEvH2pbFKcDH4EWFYKS_3bY,11118
15
+ robpy/datasets/data/animals.csv,sha256=dnXl7Ggx97t5aZh68aG90cZ4JCTHZ1rTR0AIYNrgbjM,657
16
+ robpy/datasets/data/glass.csv,sha256=zjw2qbC9FtPwzAv5Fb9D02qlwPffSRv3ea8ElylQ5sc,693144
17
+ robpy/datasets/data/stars.csv,sha256=XxYtDx2XU70z3MDf_xR3EKf1EoBQwUkRIhIyUltS7Ig,525
18
+ robpy/datasets/data/telephone.csv,sha256=MhP2axiRYQWTuy2SCuHUf9pkXczZz2IFBnJ9C-0cEu4,227
19
+ robpy/datasets/data/topgear.csv,sha256=Fa7VJOEZ1p9KPkzhEz54MlePyz3vzYAigZi90z5DfA0,74762
20
+ robpy/datasets/descr/animals.rst,sha256=Aqj0ejEpXAj9LuRpeCnKq8tta-F9mjTiIAUvpMm-F3M,918
21
+ robpy/datasets/descr/glass.rst,sha256=AW5_SpkBMYOHenq_cD09zCTJfsB2gGyjGBLpEiqWMKE,673
22
+ robpy/datasets/descr/stars.rst,sha256=Fwk_-moBhsTOjF3iZDHQJ34-qXyKqdQt_yuwNRULtkg,1531
23
+ robpy/datasets/descr/telephone.rst,sha256=w_v9qw9vTwEZHF7EJW3VLAzDW7xWan5trhZSqwQgadg,647
24
+ robpy/datasets/descr/topgear.rst,sha256=TC3HLH3gVRBPrfACebzbJu5DOT2VMcjStp0BjQxNG8U,2887
25
+ robpy/outliers/__init__.py,sha256=t2r0Wa1WMQvLvBcyBdbsY6bzjhI64tFIERScuS8l-CU,115
26
+ robpy/outliers/ddc.py,sha256=dU4gTayV8sf_0nhUhLPn1-7SAKrzr7CF8WBoVEs35gk,12201
27
+ robpy/pca/__init__.py,sha256=j8ZhPJqxafDG-VdVH6wthzc-u8lAKs7HKN_RFmH94rM,135
28
+ robpy/pca/base.py,sha256=b4gU015jaTF7j5AWL4qjMV-dpWaD2AE-8xhKMsHYlL8,3638
29
+ robpy/pca/robpca.py,sha256=XHghim3-9P_wW53Z42ga86TdBWnExWt1kddRkXuPL20,4196
30
+ robpy/pca/spca.py,sha256=sNapgMLuioEvwpdco1sma4ZtMPUrKmNB91MyxKtLYJA,1899
31
+ robpy/preprocessing/__init__.py,sha256=niJIoHl25y1OwaUIStb0pKbuL-nI4xLJAJXOeqg0Ju4,173
32
+ robpy/preprocessing/data_cleaner.py,sha256=2vdgbmoR7HZD7h1bbppUPTzXbPPprPx3m2s4zT6vfCw,8074
33
+ robpy/preprocessing/scaling.py,sha256=84ktYf-lM58NIxDgX9Z-noqy3CTEcQ2AWmHlK9WQ33I,1838
34
+ robpy/preprocessing/transfo.py,sha256=jedvDBllwmMmw-8pCC-XdLUEGbEOXNMAEedFLTHGXjY,22675
35
+ robpy/preprocessing/utils.py,sha256=xsxx0ATyqsCWeVWAl-7bDGKj8w96UOrf6B-QJ0H5zw4,2052
36
+ robpy/regression/__init__.py,sha256=EBo9IYgS-UmJoZ5yeacRZ3OBrHL1brnLaArhoZc5dVo,156
37
+ robpy/regression/base.py,sha256=j5lQBTnuCoaSJzT64UxgXa5dBoAMgaakLzAmLCN5PsU,4170
38
+ robpy/regression/lts.py,sha256=5SMe5M6OFA-FKPceyuhWjShvjIZtOVRQZvxyABNlBvA,16932
39
+ robpy/regression/mm_estimator.py,sha256=AKMR9wyVb3AoRF-h3x-DfuGcgshcEOBfCeAS3kGMXQ8,1925
40
+ robpy/regression/s_estimator.py,sha256=N0PKScLqFWjNNZUmj8QeP8PlrjEUDTtBCGkuRb2pS98,7889
41
+ robpy/univariate/__init__.py,sha256=F8qTK1ZQece41r2y6uDHtFmfV5nzXc6I93PStZ5JTfM,330
42
+ robpy/univariate/base.py,sha256=7G7YfF86cNV0UhQsI47H32Rwl4ADvmq3QmyOEYJPyb8,1725
43
+ robpy/univariate/mcd.py,sha256=1_9U43PIU8an_sjrUFwzSOXA6sdHo4BuALOmF5n9MsY,3497
44
+ robpy/univariate/onestep_m.py,sha256=tVh6Rvwqg1PixaPuslQZtdyujPFWYvNDmcBxtrcihoA,4945
45
+ robpy/univariate/qn.py,sha256=iUYp765pLbegxnSGUVv6djrVySb0HA3FFYrw4HgR8XE,3610
46
+ robpy/univariate/tau.py,sha256=gt6S68PYGW22b1d8ka6YBXl-BGqL3blZCGNP09FXcaA,2060
47
+ robpy/utils/distance.py,sha256=wLlyHnBGuv38gHkz4QSkFTckgxDVhcnLpljakjMPqdw,791
48
+ robpy/utils/general.py,sha256=qmyE5qcnscQpKrmOXPKc1XsrZRbcHay_YHz5xeup2jg,1207
49
+ robpy/utils/logging.py,sha256=4dYUnCcOqnYVu1s3EhbpJ8lasRBC6Qxrigj-giup73k,532
50
+ robpy/utils/median.py,sha256=RxS3yxXhCj9VQYv5msbJ9--voF3R0130oDtY0KrjYfE,1809
51
+ robpy/utils/outlyingness.py,sha256=DnroqqKDy2WMbLxwkHu5YFBf38H8fys-5Cz0QireAHM,2270
52
+ robpy/utils/rho.py,sha256=cOxCFgUX3uHPZIPOn7aMbwxI6KGyxxx24y-_YwwiFuo,987
53
+ robpy-0.1.0.dist-info/METADATA,sha256=cyF3Kc7cMTDl1BlXeT41bfmVdQKx_Pryd3bRswalmag,1007
54
+ robpy-0.1.0.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
55
+ robpy-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: poetry-core 1.9.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any