|
16 | 16 | from ml4t.models.latent_factors.ipca import _normalize_theta_y |
17 | 17 | from ml4t.models.latent_factors.rp_pca import _risk_premium_matrix |
18 | 18 | from ml4t.models.portfolio import runtime as portfolio_runtime |
| 19 | +from ml4t.models.portfolio.components import CrossSectionalAttention, MacroGraphAttention |
19 | 20 | from ml4t.models.portfolio.losses import compute_net_portfolio_returns, softmin_sharpe |
20 | | -from ml4t.models.portfolio.postprocessors import normalize_cross_sectional_weights |
| 21 | +from ml4t.models.portfolio.postprocessors import ( |
| 22 | + apply_turnover_limit, |
| 23 | + normalize_cross_sectional_weights, |
| 24 | +) |
21 | 25 |
|
22 | 26 |
|
23 | 27 | def test_rp_pca_default_matches_lettau_pelger_signal_matrix() -> None: |
@@ -326,6 +330,91 @@ def fake_validation( |
326 | 330 | ) |
327 | 331 |
|
328 | 332 |
|
| 333 | +def test_cross_attention_lag_longer_than_the_window_does_not_wrap() -> None: |
| 334 | + torch.manual_seed(0) |
| 335 | + window = torch.randn(1, 3, 2, 4) |
| 336 | + mask = torch.ones(1, 3, 2) |
| 337 | + matched = CrossSectionalAttention(d_model=4, n_heads=1, dropout=0.0, lag=3) |
| 338 | + longer = CrossSectionalAttention(d_model=4, n_heads=1, dropout=0.0, lag=8) |
| 339 | + longer.load_state_dict(matched.state_dict()) |
| 340 | + matched.eval() |
| 341 | + longer.eval() |
| 342 | + |
| 343 | + matched_output = matched(window, mask) |
| 344 | + longer_output = longer(window, mask) |
| 345 | + |
| 346 | + assert matched_output.shape == window.shape |
| 347 | + assert torch.isfinite(longer_output).all() |
| 348 | + torch.testing.assert_close(longer_output, matched_output) |
| 349 | + |
| 350 | + |
| 351 | +def test_graph_attention_stays_finite_when_a_node_has_no_neighbors() -> None: |
| 352 | + torch.manual_seed(0) |
| 353 | + layer = MacroGraphAttention( |
| 354 | + d_model=4, |
| 355 | + n_heads=1, |
| 356 | + dropout=0.0, |
| 357 | + adjacency_mask=torch.ones(2, 2, dtype=torch.bool), |
| 358 | + ) |
| 359 | + layer.eval() |
| 360 | + features = torch.randn(1, 2, 2, 4, requires_grad=True) |
| 361 | + mask = torch.ones(1, 2, 2) |
| 362 | + |
| 363 | + output = layer(features, mask) |
| 364 | + output.sum().backward() |
| 365 | + |
| 366 | + assert torch.isfinite(output).all() |
| 367 | + assert features.grad is not None |
| 368 | + assert torch.isfinite(features.grad).all() |
| 369 | + |
| 370 | + |
| 371 | +def test_graph_attention_ignores_a_masked_only_neighbor() -> None: |
| 372 | + layer = MacroGraphAttention( |
| 373 | + d_model=4, |
| 374 | + n_heads=1, |
| 375 | + dropout=0.0, |
| 376 | + adjacency_mask=torch.tensor([[True, False], [True, True]]), |
| 377 | + ) |
| 378 | + layer.eval() |
| 379 | + base = torch.randn(1, 1, 2, 4) |
| 380 | + shifted = base.clone() |
| 381 | + shifted[..., 1, :] += 10.0 |
| 382 | + mask = torch.tensor([[[1.0, 0.0]]]) |
| 383 | + |
| 384 | + base_output = layer(base, mask) |
| 385 | + shifted_output = layer(shifted, mask) |
| 386 | + |
| 387 | + assert torch.isfinite(base_output).all() |
| 388 | + torch.testing.assert_close(base_output[:, :, 0, :], shifted_output[:, :, 0, :]) |
| 389 | + |
| 390 | + |
| 391 | +def test_zero_turnover_limit_holds_the_previous_book() -> None: |
| 392 | + weights = np.array([[[0.4, -0.4], [0.2, -0.1]]], dtype=np.float64) |
| 393 | + previous = np.array([[0.5, -0.5]], dtype=np.float64) |
| 394 | + |
| 395 | + actual = apply_turnover_limit( |
| 396 | + weights, |
| 397 | + previous_weights=previous, |
| 398 | + mask=np.ones_like(weights, dtype=bool), |
| 399 | + turnover_limit=0.0, |
| 400 | + ) |
| 401 | + |
| 402 | + np.testing.assert_allclose(actual[0, 0], previous[0]) |
| 403 | + np.testing.assert_allclose(actual[0, 1], previous[0]) |
| 404 | + |
| 405 | + |
| 406 | +def test_negative_turnover_limit_is_rejected() -> None: |
| 407 | + weights = np.ones((1, 1, 2), dtype=np.float64) |
| 408 | + |
| 409 | + with pytest.raises(ValueError, match="turnover_limit"): |
| 410 | + apply_turnover_limit( |
| 411 | + weights, |
| 412 | + previous_weights=np.zeros((1, 2)), |
| 413 | + mask=np.ones_like(weights, dtype=bool), |
| 414 | + turnover_limit=-0.1, |
| 415 | + ) |
| 416 | + |
| 417 | + |
329 | 418 | def test_portfolio_constraints_are_satisfied_jointly() -> None: |
330 | 419 | raw = np.array([[[10.0, 1.0, -1.0, -2.0]]]) |
331 | 420 | mask = np.ones_like(raw, dtype=bool) |
|
0 commit comments