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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Multi-Task Learning for Joint Alpha + Volatility + Portfolio Optimization
|
| 2 |
+
|
| 3 |
+
Based on Ong & Herremans 2023 (arxiv:2306.13661):
|
| 4 |
+
"Multi-Task Learning for Time Series Momentum Portfolio Construction"
|
| 5 |
+
|
| 6 |
+
KEY INSIGHT: Jointly optimizing all three tasks simultaneously outperforms
|
| 7 |
+
independent optimization even after 3bps transaction costs.
|
| 8 |
+
|
| 9 |
+
This is THE critical upgrade that separates toy systems from production-grade quant.
|
| 10 |
+
"""
|
| 11 |
+
import numpy as np
|
| 12 |
+
import pandas as pd
|
| 13 |
+
import torch
|
| 14 |
+
import torch.nn as nn
|
| 15 |
+
import torch.nn.functional as F
|
| 16 |
+
from torch.utils.data import Dataset, DataLoader
|
| 17 |
+
from typing import Dict, Tuple, Optional, List
|
| 18 |
+
import warnings
|
| 19 |
+
warnings.filterwarnings('ignore')
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
class MTLSample(Dataset):
|
| 23 |
+
"""Dataset for multi-task learning with sequence input"""
|
| 24 |
+
def __init__(self, X: np.ndarray,
|
| 25 |
+
y_return: np.ndarray,
|
| 26 |
+
y_vol: np.ndarray,
|
| 27 |
+
y_portfolio: Optional[np.ndarray] = None):
|
| 28 |
+
self.X = torch.FloatTensor(X)
|
| 29 |
+
self.y_return = torch.FloatTensor(y_return)
|
| 30 |
+
self.y_vol = torch.FloatTensor(y_vol)
|
| 31 |
+
if y_portfolio is not None:
|
| 32 |
+
self.y_portfolio = torch.FloatTensor(y_portfolio)
|
| 33 |
+
else:
|
| 34 |
+
self.y_portfolio = None
|
| 35 |
+
|
| 36 |
+
def __len__(self):
|
| 37 |
+
return len(self.X)
|
| 38 |
+
|
| 39 |
+
def __getitem__(self, idx):
|
| 40 |
+
out = {
|
| 41 |
+
'X': self.X[idx],
|
| 42 |
+
'return': self.y_return[idx],
|
| 43 |
+
'volatility': self.y_vol[idx]
|
| 44 |
+
}
|
| 45 |
+
if self.y_portfolio is not None:
|
| 46 |
+
out['portfolio'] = self.y_portfolio[idx]
|
| 47 |
+
return out
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
class MultiTaskPortfolioNet(nn.Module):
|
| 51 |
+
"""
|
| 52 |
+
Multi-Task Learning Network for Joint:
|
| 53 |
+
1. Return prediction (alpha generation)
|
| 54 |
+
2. Volatility prediction (risk estimation)
|
| 55 |
+
3. Portfolio weight optimization
|
| 56 |
+
|
| 57 |
+
Architecture (from MTL-TSMOM paper):
|
| 58 |
+
- Shared LSTM encoder (hard parameter sharing)
|
| 59 |
+
- Task-specific FNN heads with different architectures
|
| 60 |
+
- Custom task-specific losses
|
| 61 |
+
|
| 62 |
+
Shared encoder learns common temporal representations.
|
| 63 |
+
Each head learns task-specific transformations.
|
| 64 |
+
"""
|
| 65 |
+
|
| 66 |
+
def __init__(self,
|
| 67 |
+
input_dim: int,
|
| 68 |
+
hidden_dim: int = 128,
|
| 69 |
+
n_lstm_layers: int = 2,
|
| 70 |
+
n_assets: int = 10,
|
| 71 |
+
dropout: float = 0.15,
|
| 72 |
+
use_attention: bool = True):
|
| 73 |
+
super().__init__()
|
| 74 |
+
|
| 75 |
+
self.input_dim = input_dim
|
| 76 |
+
self.hidden_dim = hidden_dim
|
| 77 |
+
self.n_assets = n_assets
|
| 78 |
+
self.use_attention = use_attention
|
| 79 |
+
|
| 80 |
+
# Shared encoder: LSTM with optional attention
|
| 81 |
+
self.lstm = nn.LSTM(
|
| 82 |
+
input_dim, hidden_dim, n_lstm_layers,
|
| 83 |
+
batch_first=True, dropout=dropout if n_lstm_layers > 1 else 0
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
# Optional: Self-attention on LSTM outputs
|
| 87 |
+
if use_attention:
|
| 88 |
+
self.attention = nn.MultiheadAttention(
|
| 89 |
+
hidden_dim, num_heads=4, dropout=dropout, batch_first=True
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
# Shared projection layer
|
| 93 |
+
self.shared_fc = nn.Sequential(
|
| 94 |
+
nn.Linear(hidden_dim, hidden_dim),
|
| 95 |
+
nn.ReLU(),
|
| 96 |
+
nn.Dropout(dropout)
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
# Task 1: Return prediction head (Alpha)
|
| 100 |
+
# Predicts future returns for each asset
|
| 101 |
+
self.return_head = nn.Sequential(
|
| 102 |
+
nn.Linear(hidden_dim, 256),
|
| 103 |
+
nn.ReLU(),
|
| 104 |
+
nn.Dropout(dropout),
|
| 105 |
+
nn.Linear(256, 128),
|
| 106 |
+
nn.ReLU(),
|
| 107 |
+
nn.Linear(128, n_assets) # One return per asset
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
# Task 2: Volatility prediction head (Risk)
|
| 111 |
+
# Predicts realized volatility for each asset
|
| 112 |
+
self.vol_head = nn.Sequential(
|
| 113 |
+
nn.Linear(hidden_dim, 128),
|
| 114 |
+
nn.ReLU(),
|
| 115 |
+
nn.Dropout(dropout),
|
| 116 |
+
nn.Linear(128, 64),
|
| 117 |
+
nn.ReLU(),
|
| 118 |
+
nn.Linear(64, n_assets)
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
# Task 3: Portfolio weight head (Allocation)
|
| 122 |
+
# Directly outputs portfolio weights (long-only, softmax)
|
| 123 |
+
self.portfolio_head = nn.Sequential(
|
| 124 |
+
nn.Linear(hidden_dim, 256),
|
| 125 |
+
nn.ReLU(),
|
| 126 |
+
nn.Dropout(dropout),
|
| 127 |
+
nn.Linear(256, 128),
|
| 128 |
+
nn.ReLU(),
|
| 129 |
+
nn.Linear(128, n_assets),
|
| 130 |
+
nn.Softmax(dim=-1) # Long-only, fully invested
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
# Task 4: Direction prediction (auxiliary)
|
| 134 |
+
# Binary classification: up or down (helps stabilize training)
|
| 135 |
+
self.direction_head = nn.Sequential(
|
| 136 |
+
nn.Linear(hidden_dim, 64),
|
| 137 |
+
nn.ReLU(),
|
| 138 |
+
nn.Linear(64, n_assets),
|
| 139 |
+
nn.Sigmoid()
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
def forward(self, x: torch.Tensor) -> Dict[str, torch.Tensor]:
|
| 143 |
+
"""
|
| 144 |
+
Forward pass.
|
| 145 |
+
|
| 146 |
+
Args:
|
| 147 |
+
x: (batch, seq_len, input_dim)
|
| 148 |
+
|
| 149 |
+
Returns:
|
| 150 |
+
Dict with 'returns', 'volatility', 'portfolio', 'direction'
|
| 151 |
+
"""
|
| 152 |
+
# Shared LSTM encoder
|
| 153 |
+
lstm_out, (h_n, _) = self.lstm(x)
|
| 154 |
+
# h_n: (n_layers, batch, hidden_dim)
|
| 155 |
+
shared = h_n[-1] # (batch, hidden_dim) — last layer final hidden state
|
| 156 |
+
|
| 157 |
+
# Optional attention on sequence outputs
|
| 158 |
+
if self.use_attention:
|
| 159 |
+
attn_out, _ = self.attention(lstm_out, lstm_out, lstm_out)
|
| 160 |
+
# Global average pooling over time
|
| 161 |
+
shared_attn = attn_out.mean(dim=1) # (batch, hidden_dim)
|
| 162 |
+
shared = shared + shared_attn # Residual connection
|
| 163 |
+
|
| 164 |
+
# Shared projection
|
| 165 |
+
shared_repr = self.shared_fc(shared)
|
| 166 |
+
|
| 167 |
+
# Task-specific outputs
|
| 168 |
+
returns = self.return_head(shared_repr) # (batch, n_assets)
|
| 169 |
+
volatility = F.softplus(self.vol_head(shared_repr)) + 1e-6 # Ensure positive
|
| 170 |
+
portfolio = self.portfolio_head(shared_repr) # (batch, n_assets), sums to 1
|
| 171 |
+
direction = self.direction_head(shared_repr) # (batch, n_assets), 0-1
|
| 172 |
+
|
| 173 |
+
return {
|
| 174 |
+
'returns': returns,
|
| 175 |
+
'volatility': volatility,
|
| 176 |
+
'portfolio': portfolio,
|
| 177 |
+
'direction': direction,
|
| 178 |
+
'shared_repr': shared_repr # For analysis
|
| 179 |
+
}
|
| 180 |
+
|
| 181 |
+
|
| 182 |
+
class MTLPortfolioTrainer:
|
| 183 |
+
"""
|
| 184 |
+
Trainer for Multi-Task Portfolio Network.
|
| 185 |
+
|
| 186 |
+
Uses task-specific loss weighting and gradient normalization
|
| 187 |
+
to balance the three tasks.
|
| 188 |
+
|
| 189 |
+
Key innovations from MTL-TSMOM paper:
|
| 190 |
+
1. Negative Sharpe ratio as primary portfolio loss
|
| 191 |
+
2. MSE for return prediction
|
| 192 |
+
3. MSE for volatility prediction
|
| 193 |
+
4. BCE for direction (auxiliary stabilization)
|
| 194 |
+
5. GradNorm for automatic task balancing
|
| 195 |
+
"""
|
| 196 |
+
|
| 197 |
+
def __init__(self, model: MultiTaskPortfolioNet,
|
| 198 |
+
device: str = 'cpu',
|
| 199 |
+
learning_rate: float = 1e-4,
|
| 200 |
+
weight_decay: float = 1e-5,
|
| 201 |
+
max_grad_norm: float = 0.5,
|
| 202 |
+
risk_free_rate: float = 0.04):
|
| 203 |
+
self.model = model.to(device)
|
| 204 |
+
self.device = device
|
| 205 |
+
self.risk_free_rate = risk_free_rate / 252 # Daily
|
| 206 |
+
self.max_grad_norm = max_grad_norm
|
| 207 |
+
|
| 208 |
+
self.optimizer = torch.optim.Adam(
|
| 209 |
+
model.parameters(), lr=learning_rate, weight_decay=weight_decay
|
| 210 |
+
)
|
| 211 |
+
self.scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(
|
| 212 |
+
self.optimizer, patience=10, factor=0.5, verbose=True
|
| 213 |
+
)
|
| 214 |
+
|
| 215 |
+
# Task loss weights (can be learned via GradNorm)
|
| 216 |
+
self.task_weights = {
|
| 217 |
+
'return': 1.0,
|
| 218 |
+
'volatility': 0.5,
|
| 219 |
+
'portfolio': 2.0, # Primary task gets highest weight
|
| 220 |
+
'direction': 0.3
|
| 221 |
+
}
|
| 222 |
+
|
| 223 |
+
self.history = {
|
| 224 |
+
'train_loss': [], 'val_loss': [],
|
| 225 |
+
'return_loss': [], 'vol_loss': [],
|
| 226 |
+
'portfolio_loss': [], 'direction_loss': [],
|
| 227 |
+
'sharpe': [], 'val_sharpe': []
|
| 228 |
+
}
|
| 229 |
+
|
| 230 |
+
def compute_loss(self, outputs: Dict[str, torch.Tensor],
|
| 231 |
+
batch: Dict[str, torch.Tensor],
|
| 232 |
+
actual_returns: Optional[torch.Tensor] = None) -> Dict[str, torch.Tensor]:
|
| 233 |
+
"""
|
| 234 |
+
Compute multi-task loss.
|
| 235 |
+
|
| 236 |
+
Args:
|
| 237 |
+
outputs: Model predictions
|
| 238 |
+
batch: Ground truth batch
|
| 239 |
+
actual_returns: Actual future returns (for Sharpe calculation)
|
| 240 |
+
|
| 241 |
+
Returns:
|
| 242 |
+
Dict of losses
|
| 243 |
+
"""
|
| 244 |
+
losses = {}
|
| 245 |
+
|
| 246 |
+
# Task 1: Return prediction loss (MSE on predicted vs actual returns)
|
| 247 |
+
losses['return'] = F.mse_loss(outputs['returns'], batch['return'])
|
| 248 |
+
|
| 249 |
+
# Task 2: Volatility prediction loss (MSE on predicted vs realized vol)
|
| 250 |
+
losses['volatility'] = F.mse_loss(outputs['volatility'], batch['volatility'])
|
| 251 |
+
|
| 252 |
+
# Task 3: Portfolio loss — NEGATIVE Sharpe ratio
|
| 253 |
+
# We want portfolio weights that maximize risk-adjusted return
|
| 254 |
+
if actual_returns is not None:
|
| 255 |
+
# Portfolio return: sum(w_i * r_i)
|
| 256 |
+
port_return = (outputs['portfolio'] * actual_returns).sum(dim=-1)
|
| 257 |
+
|
| 258 |
+
# Sharpe ratio: mean(excess_return) / std(return)
|
| 259 |
+
# We compute over batch (simulating a holding period)
|
| 260 |
+
mean_return = port_return.mean()
|
| 261 |
+
std_return = port_return.std() + 1e-6
|
| 262 |
+
sharpe = (mean_return - self.risk_free_rate) / std_return
|
| 263 |
+
|
| 264 |
+
# Negative Sharpe (we minimize this → maximize Sharpe)
|
| 265 |
+
losses['portfolio'] = -sharpe
|
| 266 |
+
|
| 267 |
+
# Track for monitoring
|
| 268 |
+
losses['sharpe'] = sharpe.detach()
|
| 269 |
+
else:
|
| 270 |
+
losses['portfolio'] = torch.tensor(0.0, device=self.device)
|
| 271 |
+
losses['sharpe'] = torch.tensor(0.0, device=self.device)
|
| 272 |
+
|
| 273 |
+
# Task 4: Direction prediction (BCE)
|
| 274 |
+
# Convert returns to binary: 1 if return > 0, else 0
|
| 275 |
+
direction_target = (batch['return'] > 0).float()
|
| 276 |
+
losses['direction'] = F.binary_cross_entropy(
|
| 277 |
+
outputs['direction'], direction_target
|
| 278 |
+
)
|
| 279 |
+
|
| 280 |
+
# Total loss with task weighting
|
| 281 |
+
total = sum(
|
| 282 |
+
self.task_weights[task] * losses[task]
|
| 283 |
+
for task in ['return', 'volatility', 'portfolio', 'direction']
|
| 284 |
+
)
|
| 285 |
+
losses['total'] = total
|
| 286 |
+
|
| 287 |
+
return losses
|
| 288 |
+
|
| 289 |
+
def train_epoch(self, dataloader: DataLoader,
|
| 290 |
+
actual_returns: Optional[np.ndarray] = None) -> Dict[str, float]:
|
| 291 |
+
"""Train for one epoch"""
|
| 292 |
+
self.model.train()
|
| 293 |
+
|
| 294 |
+
epoch_losses = {
|
| 295 |
+
'return': 0.0, 'volatility': 0.0,
|
| 296 |
+
'portfolio': 0.0, 'direction': 0.0,
|
| 297 |
+
'total': 0.0, 'sharpe': 0.0
|
| 298 |
+
}
|
| 299 |
+
n_batches = 0
|
| 300 |
+
|
| 301 |
+
for batch in dataloader:
|
| 302 |
+
# Move to device
|
| 303 |
+
X = batch['X'].to(self.device)
|
| 304 |
+
returns_target = batch['return'].to(self.device)
|
| 305 |
+
vol_target = batch['volatility'].to(self.device)
|
| 306 |
+
|
| 307 |
+
# Actual returns for Sharpe (can be same as returns_target or future)
|
| 308 |
+
actual = returns_target if actual_returns is None else \
|
| 309 |
+
torch.FloatTensor(actual_returns[n_batches]).to(self.device)
|
| 310 |
+
|
| 311 |
+
# Forward
|
| 312 |
+
outputs = self.model(X)
|
| 313 |
+
|
| 314 |
+
# Loss
|
| 315 |
+
losses = self.compute_loss(outputs, {
|
| 316 |
+
'return': returns_target,
|
| 317 |
+
'volatility': vol_target
|
| 318 |
+
}, actual)
|
| 319 |
+
|
| 320 |
+
# Backward
|
| 321 |
+
self.optimizer.zero_grad()
|
| 322 |
+
losses['total'].backward()
|
| 323 |
+
|
| 324 |
+
# Gradient clipping (critical for LSTM stability)
|
| 325 |
+
torch.nn.utils.clip_grad_norm_(self.model.parameters(), self.max_grad_norm)
|
| 326 |
+
|
| 327 |
+
self.optimizer.step()
|
| 328 |
+
|
| 329 |
+
# Track
|
| 330 |
+
for key in epoch_losses:
|
| 331 |
+
if key in losses:
|
| 332 |
+
val = losses[key]
|
| 333 |
+
if isinstance(val, torch.Tensor):
|
| 334 |
+
val = val.item()
|
| 335 |
+
epoch_losses[key] += val
|
| 336 |
+
|
| 337 |
+
n_batches += 1
|
| 338 |
+
|
| 339 |
+
# Average
|
| 340 |
+
for key in epoch_losses:
|
| 341 |
+
epoch_losses[key] /= max(n_batches, 1)
|
| 342 |
+
|
| 343 |
+
return epoch_losses
|
| 344 |
+
|
| 345 |
+
def validate(self, dataloader: DataLoader) -> Dict[str, float]:
|
| 346 |
+
"""Validate"""
|
| 347 |
+
self.model.eval()
|
| 348 |
+
|
| 349 |
+
val_losses = {
|
| 350 |
+
'return': 0.0, 'volatility': 0.0,
|
| 351 |
+
'portfolio': 0.0, 'direction': 0.0,
|
| 352 |
+
'total': 0.0
|
| 353 |
+
}
|
| 354 |
+
n_batches = 0
|
| 355 |
+
|
| 356 |
+
portfolio_returns = []
|
| 357 |
+
|
| 358 |
+
with torch.no_grad():
|
| 359 |
+
for batch in dataloader:
|
| 360 |
+
X = batch['X'].to(self.device)
|
| 361 |
+
returns_target = batch['return'].to(self.device)
|
| 362 |
+
vol_target = batch['volatility'].to(self.device)
|
| 363 |
+
|
| 364 |
+
outputs = self.model(X)
|
| 365 |
+
|
| 366 |
+
losses = self.compute_loss(outputs, {
|
| 367 |
+
'return': returns_target,
|
| 368 |
+
'volatility': vol_target
|
| 369 |
+
}, returns_target)
|
| 370 |
+
|
| 371 |
+
for key in val_losses:
|
| 372 |
+
if key in losses:
|
| 373 |
+
val = losses[key]
|
| 374 |
+
if isinstance(val, torch.Tensor):
|
| 375 |
+
val = val.item()
|
| 376 |
+
val_losses[key] += val
|
| 377 |
+
|
| 378 |
+
# Track portfolio returns for validation Sharpe
|
| 379 |
+
port_ret = (outputs['portfolio'] * returns_target).sum(dim=-1)
|
| 380 |
+
portfolio_returns.extend(port_ret.cpu().numpy())
|
| 381 |
+
|
| 382 |
+
n_batches += 1
|
| 383 |
+
|
| 384 |
+
for key in val_losses:
|
| 385 |
+
val_losses[key] /= max(n_batches, 1)
|
| 386 |
+
|
| 387 |
+
# Compute validation Sharpe
|
| 388 |
+
if len(portfolio_returns) > 1:
|
| 389 |
+
port_returns = np.array(portfolio_returns)
|
| 390 |
+
mean_ret = np.mean(port_returns)
|
| 391 |
+
std_ret = np.std(port_returns) + 1e-8
|
| 392 |
+
val_sharpe = (mean_ret - self.risk_free_rate) / std_ret * np.sqrt(252)
|
| 393 |
+
val_losses['sharpe'] = val_sharpe
|
| 394 |
+
|
| 395 |
+
return val_losses
|
| 396 |
+
|
| 397 |
+
def fit(self, train_loader: DataLoader,
|
| 398 |
+
val_loader: Optional[DataLoader] = None,
|
| 399 |
+
epochs: int = 100,
|
| 400 |
+
early_stopping_patience: int = 15) -> Dict:
|
| 401 |
+
"""
|
| 402 |
+
Full training loop.
|
| 403 |
+
|
| 404 |
+
Returns:
|
| 405 |
+
Training history dictionary
|
| 406 |
+
"""
|
| 407 |
+
best_val_loss = float('inf')
|
| 408 |
+
patience_counter = 0
|
| 409 |
+
|
| 410 |
+
print(f"Training MTL Portfolio Net for {epochs} epochs...")
|
| 411 |
+
print(f"Task weights: {self.task_weights}")
|
| 412 |
+
print(f"Device: {self.device}")
|
| 413 |
+
|
| 414 |
+
for epoch in range(epochs):
|
| 415 |
+
# Train
|
| 416 |
+
train_losses = self.train_epoch(train_loader)
|
| 417 |
+
|
| 418 |
+
# Validate
|
| 419 |
+
if val_loader is not None:
|
| 420 |
+
val_losses = self.validate(val_loader)
|
| 421 |
+
val_total = val_losses.get('total', 0)
|
| 422 |
+
|
| 423 |
+
# Learning rate scheduling
|
| 424 |
+
self.scheduler.step(val_total)
|
| 425 |
+
|
| 426 |
+
# Early stopping
|
| 427 |
+
if val_total < best_val_loss:
|
| 428 |
+
best_val_loss = val_total
|
| 429 |
+
patience_counter = 0
|
| 430 |
+
else:
|
| 431 |
+
patience_counter += 1
|
| 432 |
+
|
| 433 |
+
if patience_counter >= early_stopping_patience:
|
| 434 |
+
print(f"Early stopping at epoch {epoch}")
|
| 435 |
+
break
|
| 436 |
+
else:
|
| 437 |
+
val_losses = {}
|
| 438 |
+
|
| 439 |
+
# Record
|
| 440 |
+
for key in ['return', 'volatility', 'portfolio', 'direction', 'total']:
|
| 441 |
+
self.history[f'{key}_loss'].append(train_losses.get(key, 0))
|
| 442 |
+
self.history['sharpe'].append(train_losses.get('sharpe', 0))
|
| 443 |
+
if 'sharpe' in val_losses:
|
| 444 |
+
self.history['val_sharpe'].append(val_losses['sharpe'])
|
| 445 |
+
|
| 446 |
+
# Print
|
| 447 |
+
if epoch % 10 == 0 or epoch == epochs - 1:
|
| 448 |
+
msg = f"Epoch {epoch}: "
|
| 449 |
+
msg += f"train_total={train_losses['total']:.4f} "
|
| 450 |
+
msg += f"return={train_losses['return']:.4f} "
|
| 451 |
+
msg += f"vol={train_losses['volatility']:.4f} "
|
| 452 |
+
msg += f"port={train_losses['portfolio']:.4f} "
|
| 453 |
+
if 'sharpe' in train_losses:
|
| 454 |
+
msg += f"sharpe={train_losses['sharpe']:.4f} "
|
| 455 |
+
if 'sharpe' in val_losses:
|
| 456 |
+
msg += f"val_sharpe={val_losses['sharpe']:.4f}"
|
| 457 |
+
print(msg)
|
| 458 |
+
|
| 459 |
+
return self.history
|
| 460 |
+
|
| 461 |
+
def predict(self, X: np.ndarray) -> Dict[str, np.ndarray]:
|
| 462 |
+
"""Predict all tasks"""
|
| 463 |
+
self.model.eval()
|
| 464 |
+
|
| 465 |
+
X_t = torch.FloatTensor(X).to(self.device)
|
| 466 |
+
|
| 467 |
+
with torch.no_grad():
|
| 468 |
+
outputs = self.model(X_t)
|
| 469 |
+
|
| 470 |
+
return {
|
| 471 |
+
'returns': outputs['returns'].cpu().numpy(),
|
| 472 |
+
'volatility': outputs['volatility'].cpu().numpy(),
|
| 473 |
+
'portfolio': outputs['portfolio'].cpu().numpy(),
|
| 474 |
+
'direction': outputs['direction'].cpu().numpy()
|
| 475 |
+
}
|
| 476 |
+
|
| 477 |
+
|
| 478 |
+
class MTLPortfolioStrategy:
|
| 479 |
+
"""
|
| 480 |
+
End-to-end strategy using MTL Portfolio Net.
|
| 481 |
+
|
| 482 |
+
Unlike the original AlphaForge which runs separate models then combines,
|
| 483 |
+
this trains ONE model that jointly optimizes all tasks.
|
| 484 |
+
|
| 485 |
+
Output is directly usable portfolio weights — no separate optimizer needed!
|
| 486 |
+
"""
|
| 487 |
+
|
| 488 |
+
def __init__(self,
|
| 489 |
+
input_dim: int,
|
| 490 |
+
n_assets: int,
|
| 491 |
+
hidden_dim: int = 128,
|
| 492 |
+
device: str = 'cpu'):
|
| 493 |
+
self.model = MultiTaskPortfolioNet(
|
| 494 |
+
input_dim=input_dim,
|
| 495 |
+
hidden_dim=hidden_dim,
|
| 496 |
+
n_assets=n_assets,
|
| 497 |
+
use_attention=True
|
| 498 |
+
)
|
| 499 |
+
self.trainer = MTLPortfolioTrainer(self.model, device=device)
|
| 500 |
+
self.n_assets = n_assets
|
| 501 |
+
|
| 502 |
+
def prepare_data(self,
|
| 503 |
+
X_train: np.ndarray,
|
| 504 |
+
returns_train: np.ndarray,
|
| 505 |
+
vol_train: np.ndarray,
|
| 506 |
+
X_val: Optional[np.ndarray] = None,
|
| 507 |
+
returns_val: Optional[np.ndarray] = None,
|
| 508 |
+
vol_val: Optional[np.ndarray] = None,
|
| 509 |
+
batch_size: int = 64) -> Tuple[DataLoader, Optional[DataLoader]]:
|
| 510 |
+
"""Prepare data loaders"""
|
| 511 |
+
train_dataset = MTLSample(X_train, returns_train, vol_train)
|
| 512 |
+
train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
|
| 513 |
+
|
| 514 |
+
val_loader = None
|
| 515 |
+
if X_val is not None:
|
| 516 |
+
val_dataset = MTLSample(X_val, returns_val, vol_val)
|
| 517 |
+
val_loader = DataLoader(val_dataset, batch_size=batch_size)
|
| 518 |
+
|
| 519 |
+
return train_loader, val_loader
|
| 520 |
+
|
| 521 |
+
def fit(self, X_train: np.ndarray,
|
| 522 |
+
returns_train: np.ndarray,
|
| 523 |
+
vol_train: np.ndarray,
|
| 524 |
+
X_val: Optional[np.ndarray] = None,
|
| 525 |
+
returns_val: Optional[np.ndarray] = None,
|
| 526 |
+
vol_val: Optional[np.ndarray] = None,
|
| 527 |
+
epochs: int = 100) -> Dict:
|
| 528 |
+
"""Fit the MTL model"""
|
| 529 |
+
train_loader, val_loader = self.prepare_data(
|
| 530 |
+
X_train, returns_train, vol_train,
|
| 531 |
+
X_val, returns_val, vol_val
|
| 532 |
+
)
|
| 533 |
+
|
| 534 |
+
return self.trainer.fit(train_loader, val_loader, epochs=epochs)
|
| 535 |
+
|
| 536 |
+
def generate_portfolio(self, X: np.ndarray) -> Tuple[np.ndarray, Dict]:
|
| 537 |
+
"""
|
| 538 |
+
Generate portfolio weights and predictions.
|
| 539 |
+
|
| 540 |
+
Returns:
|
| 541 |
+
weights: (n_samples, n_assets) — directly usable allocations
|
| 542 |
+
predictions: Dict with returns, volatility, direction predictions
|
| 543 |
+
"""
|
| 544 |
+
predictions = self.trainer.predict(X)
|
| 545 |
+
|
| 546 |
+
weights = predictions['portfolio']
|
| 547 |
+
|
| 548 |
+
# Ensure valid weights
|
| 549 |
+
weights = np.maximum(weights, 0)
|
| 550 |
+
weights = weights / (weights.sum(axis=1, keepdims=True) + 1e-10)
|
| 551 |
+
|
| 552 |
+
return weights, predictions
|
| 553 |
+
|
| 554 |
+
|
| 555 |
+
# Factory function for easy integration
|
| 556 |
+
def create_mtl_strategy(input_dim: int, n_assets: int,
|
| 557 |
+
device: str = 'cpu') -> MTLPortfolioStrategy:
|
| 558 |
+
"""Factory for MTL portfolio strategy"""
|
| 559 |
+
return MTLPortfolioStrategy(input_dim, n_assets, device=device)
|
| 560 |
+
|
| 561 |
+
|
| 562 |
+
if __name__ == '__main__':
|
| 563 |
+
# Test MTL model
|
| 564 |
+
np.random.seed(42)
|
| 565 |
+
torch.manual_seed(42)
|
| 566 |
+
|
| 567 |
+
n_samples = 2000
|
| 568 |
+
seq_len = 60
|
| 569 |
+
n_features = 20
|
| 570 |
+
n_assets = 10
|
| 571 |
+
|
| 572 |
+
# Synthetic data
|
| 573 |
+
X = np.random.randn(n_samples, seq_len, n_features).astype(np.float32)
|
| 574 |
+
|
| 575 |
+
# Target returns (with some structure)
|
| 576 |
+
returns = np.zeros((n_samples, n_assets))
|
| 577 |
+
for i in range(n_assets):
|
| 578 |
+
returns[:, i] = X[:, -1, i % n_features] * 0.1 + np.random.randn(n_samples) * 0.05
|
| 579 |
+
|
| 580 |
+
# Target volatility
|
| 581 |
+
vol = np.abs(returns) * 2 + 0.1
|
| 582 |
+
|
| 583 |
+
# Split
|
| 584 |
+
train_size = 1500
|
| 585 |
+
X_train, X_val = X[:train_size], X[train_size:]
|
| 586 |
+
r_train, r_val = returns[:train_size], returns[train_size:]
|
| 587 |
+
v_train, v_val = vol[:train_size], vol[train_size:]
|
| 588 |
+
|
| 589 |
+
# Create and train
|
| 590 |
+
strategy = MTLPortfolioStrategy(
|
| 591 |
+
input_dim=n_features,
|
| 592 |
+
n_assets=n_assets,
|
| 593 |
+
device='cpu'
|
| 594 |
+
)
|
| 595 |
+
|
| 596 |
+
history = strategy.fit(
|
| 597 |
+
X_train, r_train, v_train,
|
| 598 |
+
X_val, r_val, v_val,
|
| 599 |
+
epochs=20
|
| 600 |
+
)
|
| 601 |
+
|
| 602 |
+
# Generate portfolio
|
| 603 |
+
weights, preds = strategy.generate_portfolio(X_val[:10])
|
| 604 |
+
|
| 605 |
+
print(f"\nSample portfolio weights (first 3):")
|
| 606 |
+
for i in range(min(3, len(weights))):
|
| 607 |
+
print(f" Day {i}: {weights[i].round(3)} (sum={weights[i].sum():.3f})")
|
| 608 |
+
|
| 609 |
+
print(f"\nPredicted returns (first 3):")
|
| 610 |
+
print(preds['returns'][:3].round(4))
|
| 611 |
+
|
| 612 |
+
print(f"\nPredicted volatility (first 3):")
|
| 613 |
+
print(preds['volatility'][:3].round(4))
|