Spaces:
Running
Running
Sync from GitHub (tests passed)
Browse files
app/features.py
CHANGED
|
@@ -15,6 +15,12 @@ from typing import Optional
|
|
| 15 |
|
| 16 |
import numpy as np
|
| 17 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
from sqlalchemy import func
|
| 19 |
from sqlalchemy.orm import Session
|
| 20 |
|
|
@@ -130,7 +136,7 @@ def load_sentiment_data(
|
|
| 130 |
|
| 131 |
def compute_returns(prices: pd.Series, periods: int = 1) -> pd.Series:
|
| 132 |
"""Compute percentage returns."""
|
| 133 |
-
return prices.pct_change(periods)
|
| 134 |
|
| 135 |
|
| 136 |
def compute_sma(prices: pd.Series, window: int) -> pd.Series:
|
|
|
|
| 15 |
|
| 16 |
import numpy as np
|
| 17 |
import pandas as pd
|
| 18 |
+
|
| 19 |
+
# Suppress silent downcasting FutureWarnings globally for this module
|
| 20 |
+
try:
|
| 21 |
+
pd.set_option('future.no_silent_downcasting', True)
|
| 22 |
+
except Exception:
|
| 23 |
+
pass
|
| 24 |
from sqlalchemy import func
|
| 25 |
from sqlalchemy.orm import Session
|
| 26 |
|
|
|
|
| 136 |
|
| 137 |
def compute_returns(prices: pd.Series, periods: int = 1) -> pd.Series:
|
| 138 |
"""Compute percentage returns."""
|
| 139 |
+
return prices.pct_change(periods, fill_method=None)
|
| 140 |
|
| 141 |
|
| 142 |
def compute_sma(prices: pd.Series, window: int) -> pd.Series:
|
deep_learning/data/feature_store.py
CHANGED
|
@@ -311,7 +311,7 @@ def build_tft_dataframe(
|
|
| 311 |
|
| 312 |
# ---- 6. Target: next-day simple return ----
|
| 313 |
close = price_df["close"]
|
| 314 |
-
target_ret = close.pct_change().shift(-1)
|
| 315 |
target_ret.name = "target"
|
| 316 |
|
| 317 |
# ---- Assemble master DataFrame ----
|
|
|
|
| 311 |
|
| 312 |
# ---- 6. Target: next-day simple return ----
|
| 313 |
close = price_df["close"]
|
| 314 |
+
target_ret = close.pct_change(fill_method=None).shift(-1)
|
| 315 |
target_ret.name = "target"
|
| 316 |
|
| 317 |
# ---- Assemble master DataFrame ----
|
deep_learning/data/lme_warehouse.py
CHANGED
|
@@ -163,7 +163,7 @@ def compute_lme_features(
|
|
| 163 |
for w in windows:
|
| 164 |
change = stock.diff(w)
|
| 165 |
features[f"lme_stock_change_{w}d"] = change
|
| 166 |
-
pct = stock.pct_change(w)
|
| 167 |
features[f"lme_stock_pct_change_{w}d"] = pct
|
| 168 |
|
| 169 |
features["lme_depletion_rate"] = stock.diff(depletion_window) / depletion_window
|
|
@@ -207,7 +207,7 @@ def compute_proxy_lme_features(
|
|
| 207 |
|
| 208 |
features["proxy_vol_zscore"] = (vol - vol_ma20) / vol_std20
|
| 209 |
features["proxy_vol_spike"] = (features["proxy_vol_zscore"] > 2.0).astype(np.float32)
|
| 210 |
-
features["proxy_vol_price_interaction"] = features["proxy_vol_zscore"] * close.pct_change()
|
| 211 |
|
| 212 |
spread_5_20 = close.rolling(5).mean() - close.rolling(20).mean()
|
| 213 |
features["proxy_momentum_spread"] = spread_5_20 / close.rolling(20).std().replace(0, np.nan)
|
|
|
|
| 163 |
for w in windows:
|
| 164 |
change = stock.diff(w)
|
| 165 |
features[f"lme_stock_change_{w}d"] = change
|
| 166 |
+
pct = stock.pct_change(w, fill_method=None)
|
| 167 |
features[f"lme_stock_pct_change_{w}d"] = pct
|
| 168 |
|
| 169 |
features["lme_depletion_rate"] = stock.diff(depletion_window) / depletion_window
|
|
|
|
| 207 |
|
| 208 |
features["proxy_vol_zscore"] = (vol - vol_ma20) / vol_std20
|
| 209 |
features["proxy_vol_spike"] = (features["proxy_vol_zscore"] > 2.0).astype(np.float32)
|
| 210 |
+
features["proxy_vol_price_interaction"] = features["proxy_vol_zscore"] * close.pct_change(fill_method=None)
|
| 211 |
|
| 212 |
spread_5_20 = close.rolling(5).mean() - close.rolling(20).mean()
|
| 213 |
features["proxy_momentum_spread"] = spread_5_20 / close.rolling(20).std().replace(0, np.nan)
|
deep_learning/inference/predictor.py
CHANGED
|
@@ -31,11 +31,25 @@ warnings.filterwarnings(
|
|
| 31 |
category=UserWarning,
|
| 32 |
module="sklearn",
|
| 33 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
from deep_learning.config import TFTASROConfig, get_tft_config
|
| 36 |
|
| 37 |
logger = logging.getLogger(__name__)
|
| 38 |
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
class TFTPredictor:
|
| 41 |
"""
|
|
|
|
| 31 |
category=UserWarning,
|
| 32 |
module="sklearn",
|
| 33 |
)
|
| 34 |
+
warnings.filterwarnings(
|
| 35 |
+
"ignore",
|
| 36 |
+
message=".*is an instance of `nn.Module` and is already saved during checkpointing.*",
|
| 37 |
+
category=UserWarning,
|
| 38 |
+
module="lightning.pytorch.utilities",
|
| 39 |
+
)
|
| 40 |
+
warnings.filterwarnings(
|
| 41 |
+
"ignore",
|
| 42 |
+
category=DeprecationWarning,
|
| 43 |
+
module="lightning.pytorch.utilities",
|
| 44 |
+
)
|
| 45 |
|
| 46 |
from deep_learning.config import TFTASROConfig, get_tft_config
|
| 47 |
|
| 48 |
logger = logging.getLogger(__name__)
|
| 49 |
|
| 50 |
+
# Suppress PyTorch Lightning promotional tips ("litlogger", "litmodels")
|
| 51 |
+
logging.getLogger("lightning.pytorch.utilities.rank_zero").setLevel(logging.WARNING)
|
| 52 |
+
|
| 53 |
|
| 54 |
class TFTPredictor:
|
| 55 |
"""
|
deep_learning/models/tft_copper.py
CHANGED
|
@@ -150,6 +150,8 @@ def create_tft_model(
|
|
| 150 |
log_val_interval=1,
|
| 151 |
)
|
| 152 |
|
|
|
|
|
|
|
| 153 |
n_params = sum(p.numel() for p in model.parameters())
|
| 154 |
n_trainable = sum(p.numel() for p in model.parameters() if p.requires_grad)
|
| 155 |
logger.info("TFT model created: %d total params, %d trainable", n_params, n_trainable)
|
|
|
|
| 150 |
log_val_interval=1,
|
| 151 |
)
|
| 152 |
|
| 153 |
+
model.save_hyperparameters(ignore=['loss', 'logging_metrics'])
|
| 154 |
+
|
| 155 |
n_params = sum(p.numel() for p in model.parameters())
|
| 156 |
n_trainable = sum(p.numel() for p in model.parameters() if p.requires_grad)
|
| 157 |
logger.info("TFT model created: %d total params, %d trainable", n_params, n_trainable)
|