statgpu 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.
- statgpu/__init__.py +174 -0
- statgpu/_base.py +544 -0
- statgpu/_config.py +127 -0
- statgpu/anova/__init__.py +5 -0
- statgpu/anova/_oneway.py +194 -0
- statgpu/backends/__init__.py +83 -0
- statgpu/backends/_array_ops.py +529 -0
- statgpu/backends/_base.py +184 -0
- statgpu/backends/_cupy.py +453 -0
- statgpu/backends/_factory.py +65 -0
- statgpu/backends/_gpu_inference_cupy.py +214 -0
- statgpu/backends/_gpu_inference_torch.py +422 -0
- statgpu/backends/_numpy.py +324 -0
- statgpu/backends/_torch.py +685 -0
- statgpu/backends/_torch_safe.py +47 -0
- statgpu/backends/_utils.py +423 -0
- statgpu/core/__init__.py +10 -0
- statgpu/core/formula/__init__.py +33 -0
- statgpu/core/formula/_design.py +99 -0
- statgpu/core/formula/_parser.py +191 -0
- statgpu/core/formula/_terms.py +70 -0
- statgpu/core/formula/tests/__init__.py +0 -0
- statgpu/core/formula/tests/test_parser.py +194 -0
- statgpu/covariance/__init__.py +6 -0
- statgpu/covariance/_empirical.py +310 -0
- statgpu/covariance/_shrinkage.py +248 -0
- statgpu/cross_validation/__init__.py +31 -0
- statgpu/cross_validation/_base.py +410 -0
- statgpu/cross_validation/_engine.py +167 -0
- statgpu/diagnostics/__init__.py +7 -0
- statgpu/diagnostics/_regression_diagnostics.py +188 -0
- statgpu/feature_selection/__init__.py +24 -0
- statgpu/feature_selection/_knockoff.py +870 -0
- statgpu/feature_selection/_knockoff_utils.py +1003 -0
- statgpu/feature_selection/_stepwise.py +300 -0
- statgpu/glm_core/__init__.py +81 -0
- statgpu/glm_core/_base.py +202 -0
- statgpu/glm_core/_family.py +362 -0
- statgpu/glm_core/_fused.py +149 -0
- statgpu/glm_core/_gamma.py +111 -0
- statgpu/glm_core/_inverse_gaussian.py +62 -0
- statgpu/glm_core/_irls.py +561 -0
- statgpu/glm_core/_logistic.py +82 -0
- statgpu/glm_core/_negative_binomial.py +68 -0
- statgpu/glm_core/_poisson.py +60 -0
- statgpu/glm_core/_solver_legacy.py +100 -0
- statgpu/glm_core/_squared.py +53 -0
- statgpu/glm_core/_tweedie.py +74 -0
- statgpu/inference/__init__.py +239 -0
- statgpu/inference/_distributions_backend.py +2610 -0
- statgpu/inference/_multiple_testing.py +391 -0
- statgpu/inference/_resampling.py +1400 -0
- statgpu/inference/_results.py +265 -0
- statgpu/linear_model/__init__.py +75 -0
- statgpu/linear_model/_gaussian_inference.py +306 -0
- statgpu/linear_model/_glm_base.py +1261 -0
- statgpu/linear_model/_ordered_logit.py +52 -0
- statgpu/linear_model/_ordered_probit.py +50 -0
- statgpu/linear_model/_stats.py +170 -0
- statgpu/linear_model/cv/__init__.py +13 -0
- statgpu/linear_model/cv/_elasticnet_cv.py +892 -0
- statgpu/linear_model/cv/_lasso_cv.py +253 -0
- statgpu/linear_model/cv/_logistic_cv.py +895 -0
- statgpu/linear_model/cv/_ridge_cv.py +1160 -0
- statgpu/linear_model/legacy/__init__.py +1 -0
- statgpu/linear_model/legacy/_distributions_legacy_gpu.py +340 -0
- statgpu/linear_model/legacy/_elasticnet_legacy.py +936 -0
- statgpu/linear_model/legacy/_lasso_legacy.py +4876 -0
- statgpu/linear_model/legacy/_penalized_legacy.py +1174 -0
- statgpu/linear_model/legacy/_ridge_legacy.py +863 -0
- statgpu/linear_model/legacy/_solver_legacy.py +104 -0
- statgpu/linear_model/penalized/__init__.py +25 -0
- statgpu/linear_model/penalized/_base.py +437 -0
- statgpu/linear_model/penalized/_fit_mixin.py +1877 -0
- statgpu/linear_model/penalized/_inference_mixin.py +1179 -0
- statgpu/linear_model/penalized/_penalized_cv.py +2699 -0
- statgpu/linear_model/penalized/_penalized_gamma.py +86 -0
- statgpu/linear_model/penalized/_penalized_inverse_gaussian.py +62 -0
- statgpu/linear_model/penalized/_penalized_linear.py +236 -0
- statgpu/linear_model/penalized/_penalized_logistic.py +100 -0
- statgpu/linear_model/penalized/_penalized_negative_binomial.py +65 -0
- statgpu/linear_model/penalized/_penalized_poisson.py +62 -0
- statgpu/linear_model/penalized/_penalized_tweedie.py +65 -0
- statgpu/linear_model/penalized/_predict_mixin.py +182 -0
- statgpu/linear_model/wrappers/__init__.py +31 -0
- statgpu/linear_model/wrappers/_adaptive_lasso.py +63 -0
- statgpu/linear_model/wrappers/_elasticnet.py +75 -0
- statgpu/linear_model/wrappers/_gamma.py +67 -0
- statgpu/linear_model/wrappers/_inverse_gaussian.py +47 -0
- statgpu/linear_model/wrappers/_lasso.py +2124 -0
- statgpu/linear_model/wrappers/_linear.py +1127 -0
- statgpu/linear_model/wrappers/_logistic.py +1435 -0
- statgpu/linear_model/wrappers/_mcp.py +58 -0
- statgpu/linear_model/wrappers/_negative_binomial.py +58 -0
- statgpu/linear_model/wrappers/_poisson.py +48 -0
- statgpu/linear_model/wrappers/_ridge.py +166 -0
- statgpu/linear_model/wrappers/_scad.py +58 -0
- statgpu/linear_model/wrappers/_tweedie.py +57 -0
- statgpu/metrics/__init__.py +21 -0
- statgpu/metrics/_classification.py +591 -0
- statgpu/nonparametric/__init__.py +50 -0
- statgpu/nonparametric/kernel_methods/__init__.py +25 -0
- statgpu/nonparametric/kernel_methods/_kernels.py +246 -0
- statgpu/nonparametric/kernel_methods/_krr.py +234 -0
- statgpu/nonparametric/kernel_methods/_krr_cv.py +380 -0
- statgpu/nonparametric/kernel_smoothing/__init__.py +39 -0
- statgpu/nonparametric/kernel_smoothing/_bandwidth_selection.py +1083 -0
- statgpu/nonparametric/kernel_smoothing/_kde.py +761 -0
- statgpu/nonparametric/kernel_smoothing/_kernel_common.py +348 -0
- statgpu/nonparametric/kernel_smoothing/_kernel_regression.py +748 -0
- statgpu/nonparametric/splines/__init__.py +5 -0
- statgpu/nonparametric/splines/_bspline_basis.py +336 -0
- statgpu/nonparametric/splines/_penalized.py +349 -0
- statgpu/panel/__init__.py +19 -0
- statgpu/panel/_covariance.py +140 -0
- statgpu/panel/_fixed_effects.py +420 -0
- statgpu/panel/_random_effects.py +385 -0
- statgpu/panel/_utils.py +482 -0
- statgpu/penalties/__init__.py +139 -0
- statgpu/penalties/_adaptive_l1.py +313 -0
- statgpu/penalties/_base.py +261 -0
- statgpu/penalties/_categories.py +39 -0
- statgpu/penalties/_elasticnet.py +98 -0
- statgpu/penalties/_group_lasso.py +678 -0
- statgpu/penalties/_group_mcp.py +553 -0
- statgpu/penalties/_group_scad.py +605 -0
- statgpu/penalties/_l1.py +107 -0
- statgpu/penalties/_l2.py +77 -0
- statgpu/penalties/_mcp.py +237 -0
- statgpu/penalties/_scad.py +260 -0
- statgpu/semiparametric/__init__.py +5 -0
- statgpu/semiparametric/_gam.py +401 -0
- statgpu/solvers/__init__.py +24 -0
- statgpu/solvers/_admm.py +241 -0
- statgpu/solvers/_constants.py +15 -0
- statgpu/solvers/_convergence.py +6 -0
- statgpu/solvers/_fista.py +436 -0
- statgpu/solvers/_fista_bb.py +513 -0
- statgpu/solvers/_fista_lla.py +541 -0
- statgpu/solvers/_lbfgs.py +206 -0
- statgpu/solvers/_newton.py +149 -0
- statgpu/solvers/_utils.py +277 -0
- statgpu/survival/__init__.py +14 -0
- statgpu/survival/_cox.py +3974 -0
- statgpu/survival/_cox_breslow_triton_kernel.py +106 -0
- statgpu/survival/_cox_cv.py +1159 -0
- statgpu/survival/_cox_efron_cuda.py +1280 -0
- statgpu/survival/_cox_efron_triton.py +359 -0
- statgpu/unsupervised/__init__.py +29 -0
- statgpu/unsupervised/_agglomerative.py +307 -0
- statgpu/unsupervised/_dbscan.py +263 -0
- statgpu/unsupervised/_dbscan_cpu.pyx +125 -0
- statgpu/unsupervised/_gmm.py +332 -0
- statgpu/unsupervised/_incremental_pca.py +176 -0
- statgpu/unsupervised/_kmeans.py +261 -0
- statgpu/unsupervised/_minibatch_kmeans.py +299 -0
- statgpu/unsupervised/_minibatch_nmf.py +252 -0
- statgpu/unsupervised/_nmf.py +190 -0
- statgpu/unsupervised/_pca.py +189 -0
- statgpu/unsupervised/_truncated_svd.py +132 -0
- statgpu/unsupervised/_tsne.py +192 -0
- statgpu/unsupervised/_umap.py +224 -0
- statgpu/unsupervised/_utils.py +134 -0
- statgpu-0.1.0.dist-info/METADATA +245 -0
- statgpu-0.1.0.dist-info/RECORD +168 -0
- statgpu-0.1.0.dist-info/WHEEL +5 -0
- statgpu-0.1.0.dist-info/licenses/LICENSE +199 -0
- statgpu-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Elastic Net penalty implementation.
|
|
3
|
+
|
|
4
|
+
P(w) = α * l1_ratio * ||w||₁ + (α/2) * (1 - l1_ratio) * ||w||²₂
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
__all__ = ["ElasticNetPenalty"]
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
from typing import Optional
|
|
11
|
+
from statgpu.backends._array_ops import _xp
|
|
12
|
+
import numpy as np
|
|
13
|
+
from statgpu.penalties._base import Penalty
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class ElasticNetPenalty(Penalty):
|
|
17
|
+
"""
|
|
18
|
+
Elastic Net penalty: P(w) = α * l1_ratio * ||w||₁ + (α/2) * (1-l1_ratio) * ||w||²₂
|
|
19
|
+
|
|
20
|
+
Combines L1 (sparsity) and L2 (grouping effect) penalties.
|
|
21
|
+
- l1_ratio = 1: Pure L1 (Lasso)
|
|
22
|
+
- l1_ratio = 0: Pure L2 (Ridge)
|
|
23
|
+
- 0 < l1_ratio < 1: Combination
|
|
24
|
+
|
|
25
|
+
The proximal operator combines soft thresholding with L2 scaling:
|
|
26
|
+
prox(w) = soft_threshold(w, α*l1_ratio*step) / (1 + α*(1-l1_ratio)*step)
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
name = "elasticnet"
|
|
30
|
+
is_convex = True
|
|
31
|
+
|
|
32
|
+
def __init__(self, alpha: float = 1.0, l1_ratio: float = 0.5):
|
|
33
|
+
"""
|
|
34
|
+
Parameters
|
|
35
|
+
----------
|
|
36
|
+
alpha : float, default=1.0
|
|
37
|
+
Regularization strength.
|
|
38
|
+
l1_ratio : float, default=0.5
|
|
39
|
+
Mixing parameter between L1 and L2 penalties.
|
|
40
|
+
- l1_ratio = 1: L1 only (Lasso)
|
|
41
|
+
- l1_ratio = 0: L2 only (Ridge)
|
|
42
|
+
- 0 < l1_ratio < 1: Combined
|
|
43
|
+
"""
|
|
44
|
+
self.alpha = alpha
|
|
45
|
+
if not (0.0 <= l1_ratio <= 1.0):
|
|
46
|
+
raise ValueError(f"l1_ratio must be in [0, 1], got {l1_ratio}")
|
|
47
|
+
self.l1_ratio = l1_ratio
|
|
48
|
+
|
|
49
|
+
def value(self, coef):
|
|
50
|
+
"""P(w) = α*l1_ratio*||w||₁ + (α/2)*(1-l1_ratio)*||w||²₂"""
|
|
51
|
+
xp = _xp(coef)
|
|
52
|
+
l1 = self.alpha * self.l1_ratio * float(xp.sum(xp.abs(coef)))
|
|
53
|
+
l2 = 0.5 * self.alpha * (1 - self.l1_ratio) * float(xp.sum(coef ** 2))
|
|
54
|
+
return l1 + l2
|
|
55
|
+
|
|
56
|
+
def gradient(self, coef):
|
|
57
|
+
"""∇P(w) = α*l1_ratio*sign(w) + α*(1-l1_ratio)*w"""
|
|
58
|
+
xp = _xp(coef)
|
|
59
|
+
grad_l1 = self.alpha * self.l1_ratio * xp.sign(coef)
|
|
60
|
+
grad_l2 = self.alpha * (1 - self.l1_ratio) * coef
|
|
61
|
+
return grad_l1 + grad_l2
|
|
62
|
+
|
|
63
|
+
def proximal(
|
|
64
|
+
self,
|
|
65
|
+
w: np.ndarray,
|
|
66
|
+
step: float,
|
|
67
|
+
backend: str = "numpy"
|
|
68
|
+
) -> np.ndarray:
|
|
69
|
+
"""
|
|
70
|
+
Soft thresholding + L2 scaling.
|
|
71
|
+
|
|
72
|
+
prox(w) = sign(w) * max(|w| - α*l1_ratio*step, 0) / (1 + α*(1-l1_ratio)*step)
|
|
73
|
+
|
|
74
|
+
Parameters
|
|
75
|
+
----------
|
|
76
|
+
w : array
|
|
77
|
+
Input array.
|
|
78
|
+
step : float
|
|
79
|
+
Step size.
|
|
80
|
+
backend : str
|
|
81
|
+
Backend: 'numpy', 'cupy', or 'torch'.
|
|
82
|
+
|
|
83
|
+
Returns
|
|
84
|
+
-------
|
|
85
|
+
array
|
|
86
|
+
Proximal result.
|
|
87
|
+
"""
|
|
88
|
+
thresh = self.alpha * self.l1_ratio * step
|
|
89
|
+
l2_scale = 1.0 + self.alpha * (1 - self.l1_ratio) * step
|
|
90
|
+
|
|
91
|
+
from statgpu.backends._array_ops import _soft_threshold
|
|
92
|
+
return _soft_threshold(w, thresh) / l2_scale
|
|
93
|
+
|
|
94
|
+
def get_params(self) -> dict:
|
|
95
|
+
params = super().get_params()
|
|
96
|
+
params["alpha"] = self.alpha
|
|
97
|
+
params["l1_ratio"] = self.l1_ratio
|
|
98
|
+
return params
|