Skip to content

Commit 3c5c1b2

Browse files
АнгелинаАнгелина
authored andcommitted
feat: Add general interface StatisticalProcedure
* added base StatisticalProcedure protocol * replaced NDArray[np.float64] with np.ndarray
1 parent 9fa4aad commit 3c5c1b2

File tree

7 files changed

+26
-15
lines changed

7 files changed

+26
-15
lines changed

src/procedures/semiparametric/nm_semi_param_algorithms/g_estimation_convolution.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
BOHMAN_DELTA_DEFAULT_VALUE: float = 0.0001
1212
X_DATA_DEFAULT_VALUE: List[float] = [1.0]
1313

14-
1514
class NMEstimationDensityInvMT:
1615
"""Estimation of mixing density function g (xi density function) of NM mixture
1716
represented in canonical form Y = xi + sigma*N.
@@ -34,8 +33,8 @@ class ParamsAnnotation(TypedDict, total=False):
3433
bohman_n: int
3534
bohman_delta: float
3635

37-
def __init__(self, sample: Optional[_typing.NDArray[np.float64]] = None, **kwargs: Unpack[ParamsAnnotation]):
38-
self.sample: _typing.NDArray[np.float64] = np.array([]) if sample is None else sample
36+
def __init__(self, sample: Optional[np.ndarray] = None, **kwargs: Unpack[ParamsAnnotation]):
37+
self.sample: np.ndarray = np.array([]) if sample is None else sample
3938
self.n: int = len(self.sample)
4039
(
4140
self.x_data,
@@ -93,7 +92,7 @@ def cdf(self, X: np.ndarray) -> np.ndarray:
9392

9493
return F_x.real
9594

96-
def compute(self, sample: _typing.NDArray[np.float64]) -> EstimateResult:
95+
def compute(self, sample: np.ndarray) -> EstimateResult:
9796
inv = self.BohmanA(N=self.bohman_n, delta=self.bohman_delta)
9897
inv.fit(self.characteristic_function_xi)
9998
x_data_array = np.array(self.x_data, dtype=np.float64)

src/procedures/semiparametric/nv_semi_param_algorithms/g_estimation_given_mu.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ class ParamsAnnotation(TypedDict, total=False):
4949
integration_tolerance: float
5050
integration_limit: int
5151

52-
def __init__(self, sample: Optional[_typing.NDArray[np.float64]] = None, **kwargs: Unpack[ParamsAnnotation]):
52+
def __init__(self, sample: Optional[np.ndarray] = None, **kwargs: Unpack[ParamsAnnotation]):
5353
self.x_powers: Dict[float, np.ndarray] = {}
5454
self.second_u_integrals: np.ndarray
5555
self.first_u_integrals: np.ndarray
5656
self.gamma_grid: np.ndarray
5757
self.v_grid: np.ndarray
58-
self.sample: _typing.NDArray[np.float64] = np.array([]) if sample is None else sample
58+
self.sample: np.ndarray = np.array([]) if sample is None else sample
5959
self.n: int = len(self.sample)
6060
(
6161
self.gmm,
@@ -173,6 +173,6 @@ def compute_integrals_for_x(self, x: float) -> float:
173173
total = np.sum(first_integral + second_integral) / self.denominator
174174
return max(0.0, total.real)
175175

176-
def compute(self, sample: _typing.NDArray[np.float64]) -> EstimateResult:
176+
def compute(self, sample: np.ndarray) -> EstimateResult:
177177
y_data = [self.compute_integrals_for_x(x) for x in self.x_data]
178178
return EstimateResult(list_value=y_data, success=True)

src/procedures/semiparametric/nv_semi_param_algorithms/g_estimation_post_widder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def p_x_estimation(self, x: float) -> float:
7777
)
7878
return result.real
7979

80-
def compute(self, sample: np._typing.NDArray) -> EstimateResult:
80+
def compute(self, sample: np.ndarray) -> EstimateResult:
8181
"""Estimate g(x)
8282
8383
Args:

src/procedures/semiparametric/nvm_semi_param_algorithms/g_estimation_given_mu.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ class ParamsAnnotation(TypedDict, total=False):
5151
integration_tolerance: float
5252
integration_limit: int
5353

54-
def __init__(self, sample: Optional[_typing.NDArray[np.float64]] = None, **kwargs: Unpack[ParamsAnnotation]):
54+
def __init__(self, sample: Optional[np.ndarray] = None, **kwargs: Unpack[ParamsAnnotation]):
5555
self.x_powers: Dict[float, np.ndarray] = {}
5656
self.second_u_integrals: np.ndarray
5757
self.first_u_integrals: np.ndarray
5858
self.gamma_grid: np.ndarray
5959
self.v_grid: np.ndarray
60-
self.sample: _typing.NDArray[np.float64] = np.array([]) if sample is None else sample
60+
self.sample: np.ndarray= np.array([]) if sample is None else sample
6161
self.n: int = len(self.sample)
6262
(
6363
self.mu,
@@ -179,6 +179,6 @@ def compute_integrals_for_x(self, x: float) -> float:
179179
total = np.sum(first_integral + second_integral) / self.denominator
180180
return max(0.0, total.real)
181181

182-
def compute(self, sample: _typing.NDArray[np.float64]) -> EstimateResult:
182+
def compute(self, sample: np.ndarray) -> EstimateResult:
183183
y_data = [self.compute_integrals_for_x(x) for x in self.x_data]
184184
return EstimateResult(list_value=y_data, success=True)

src/procedures/semiparametric/nvm_semi_param_algorithms/g_estimation_given_mu_rqmc_based.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ class ParamsAnnotation(TypedDict, total=False):
5252
integration_tolerance: float
5353
integration_limit: int
5454

55-
def __init__(self, sample: Optional[_typing.NDArray[np.float64]] = None, **kwargs: Unpack[ParamsAnnotation]):
55+
def __init__(self, sample: Optional[np.ndarray] = None, **kwargs: Unpack[ParamsAnnotation]):
5656
self.x_powers: Dict[float, np.ndarray] = {}
5757
self.second_u_integrals: np.ndarray
5858
self.first_u_integrals: np.ndarray
5959
self.gamma_grid: np.ndarray
6060
self.v_grid: np.ndarray
61-
self.sample: _typing.NDArray[np.float64] = np.array([]) if sample is None else sample
61+
self.sample: np.ndarray = np.array([]) if sample is None else sample
6262
self.n: int = len(self.sample)
6363
(
6464
self.mu,
@@ -171,6 +171,6 @@ def compute_integrals_for_x(self, x: float) -> float:
171171
total = (first_integral + second_integral) / self.denominator
172172
return max(0.0, total.real)
173173

174-
def compute(self, sample: np._typing.NDArray) -> EstimateResult:
174+
def compute(self, sample: np.ndarray) -> EstimateResult:
175175
y_data = [self.compute_integrals_for_x(x) for x in self.x_data]
176176
return EstimateResult(list_value=y_data, success=True)

src/procedures/semiparametric/nvm_semi_param_algorithms/mu_estimation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def __w(self, p: float, sample: np._typing.NDArray) -> float:
8484
y += e * self.omega(x)
8585
return y
8686

87-
def compute(self, sample: np._typing.NDArray) -> EstimateResult:
87+
def compute(self, sample: np.ndarray) -> EstimateResult:
8888
"""Root of this function is an estimation of mu
8989
9090
Args:
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from typing import Protocol
2+
import numpy as np
3+
4+
from src.estimators.estimate_result import EstimateResult
5+
6+
class StatisticalProcedure(Protocol):
7+
"""Protocol for statistical algorithms that compute estimates from data.
8+
9+
"""
10+
def compute(self, sample: np.ndarray) -> EstimateResult:
11+
pass
12+

0 commit comments

Comments
 (0)