https://github.com/sponsors/robcaulk
"""
- version = "3.7.129"
+ version = "3.7.130"
_SQRT_2: Final[float] = np.sqrt(2.0)
self._optuna_train_params: dict[str, dict[str, Any]] = {}
self._optuna_label_params: dict[str, dict[str, Any]] = {}
self._optuna_label_candle_pool_full_cache: dict[int, list[int]] = {}
+ self._optuna_label_shuffle_rng = random.Random(self._optuna_config.get("seed"))
self.init_optuna_label_candle_pool()
self._optuna_label_candle: dict[str, int] = {}
self._optuna_label_candles: dict[str, int] = {}
if len(optuna_label_candle_pool_full) == 0:
raise RuntimeError("Failed to initialize optuna label candle pool full")
self._optuna_label_candle_pool = optuna_label_candle_pool_full
- random.shuffle(self._optuna_label_candle_pool)
+ self._optuna_label_shuffle_rng.shuffle(self._optuna_label_candle_pool)
if len(self._optuna_label_candle_pool) == 0:
raise RuntimeError("Failed to initialize optuna label candle pool")
- set(self._optuna_label_candle.values())
)
if len(optuna_label_available_candles) > 0:
- self._optuna_label_candle_pool.extend(optuna_label_available_candles)
- random.shuffle(self._optuna_label_candle_pool)
+ self._optuna_label_candle_pool.extend(
+ sorted(optuna_label_available_candles)
+ )
+ self._optuna_label_shuffle_rng.shuffle(self._optuna_label_candle_pool)
def fit(
self, data_dictionary: dict[str, Any], dk: FreqaiDataKitchen, **kwargs
return (ranks - 1) / (n - 1)
+def _impute_weights(
+ weights: NDArray[np.floating],
+ *,
+ finite_mask: NDArray[np.bool_] | None = None,
+ default_weight: float = DEFAULT_EXTREMA_WEIGHT,
+) -> NDArray[np.floating]:
+ weights = weights.astype(float, copy=False)
+ if finite_mask is None:
+ finite_mask = np.isfinite(weights)
+
+ if not finite_mask.any():
+ return np.full_like(weights, default_weight, dtype=float)
+
+ median_weight = np.nanmedian(weights[finite_mask])
+ if not np.isfinite(median_weight):
+ median_weight = default_weight
+
+ weights_out = weights.astype(float, copy=True)
+ weights_out[~finite_mask] = median_weight
+ return weights_out
+
+
def normalize_weights(
weights: NDArray[np.floating],
# Phase 1: Standardization
if weights.size == 0:
return weights
- weights_out = np.full_like(weights, DEFAULT_EXTREMA_WEIGHT, dtype=float)
-
weights_finite_mask = np.isfinite(weights)
if not weights_finite_mask.any():
- return weights_out
+ return np.full_like(weights, DEFAULT_EXTREMA_WEIGHT, dtype=float)
+
+ weights = _impute_weights(
+ weights,
+ finite_mask=weights_finite_mask,
+ default_weight=DEFAULT_EXTREMA_WEIGHT,
+ )
# Phase 1: Standardization
standardized_weights = standardize_weights(
- weights[weights_finite_mask],
+ weights,
method=standardization,
robust_quantiles=robust_quantiles,
mmad_scaling_factor=mmad_scaling_factor,
normalized_weights
)
- weights_out[weights_finite_mask] = normalized_weights
- weights_out[~np.isfinite(weights_out)] = DEFAULT_EXTREMA_WEIGHT
- return weights_out
+ if not np.isfinite(normalized_weights).all():
+ return np.full_like(weights, DEFAULT_EXTREMA_WEIGHT, dtype=float)
+
+ return normalized_weights
def calculate_extrema_weights(