refactor(ml): extract shared items from dqn/ to top-level modules

Move mixed_precision, xavier_init, portfolio_tracker, action_space,
order_router from dqn/ to ml/src/ root. Extract TradingAction enum
from dqn/agent.rs to standalone trading_action module. Re-export
from dqn/mod.rs for backward compatibility.

These modules are shared infrastructure used by PPO, trainers,
hyperopt, and ensemble — not DQN-specific.

Test results: 2757 passed, 0 failed, 25 ignored (unchanged).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
jgrusewski
2026-03-07 22:21:18 +01:00
parent e440de4c6b
commit 5c18f17a01
9 changed files with 57 additions and 43 deletions

View File

@@ -24,38 +24,7 @@ use super::network::{QNetwork, QNetworkConfig};
use super::{Experience, ReplayBuffer, ReplayBufferConfig};
use crate::MLError;
/// Trading actions available to the `DQN` agent
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum TradingAction {
/// Buy signal
Buy = 0,
/// Sell signal
Sell = 1,
/// Hold/Do nothing
Hold = 2,
}
impl TradingAction {
/// Convert action to integer index
pub fn to_int(self) -> u8 {
self as u8
}
/// Convert integer index to action
pub fn from_int(val: u8) -> Option<Self> {
match val {
0 => Some(TradingAction::Buy),
1 => Some(TradingAction::Sell),
2 => Some(TradingAction::Hold),
_ => None,
}
}
/// Get all possible actions
pub fn all() -> [TradingAction; 3] {
[TradingAction::Buy, TradingAction::Sell, TradingAction::Hold]
}
}
pub use crate::trading_action::TradingAction;
/// Trading state representation for `DQN`
#[derive(Debug, Clone, Serialize, Deserialize)]

View File

@@ -4,9 +4,15 @@
//! with all 6 components: Double Q-learning, Dueling Networks, Prioritized Experience Replay,
//! Multi-step Learning, Distributional RL (C51), and Noisy Networks.
// Shared modules moved to crate root — re-export for backward compatibility
pub use crate::action_space;
pub use crate::order_router;
pub use crate::mixed_precision;
pub use crate::xavier_init;
pub use crate::portfolio_tracker;
pub use crate::trading_action::TradingAction;
// Original DQN components
pub mod action_space; // Factored action space (5 DQN exposure actions + deterministic order routing)
pub mod order_router; // Deterministic order routing: exposure → (order_type, urgency) from microstructure
pub mod count_bonus; // UCB count-based exploration bonus for action diversity
pub mod agent;
pub mod attention; // Multi-head self-attention for temporal pattern recognition (Wave 26 P1.2)
@@ -19,7 +25,6 @@ pub mod hindsight_replay; // Hindsight Experience Replay for 5-10x data efficien
pub mod logging; // Optimized logging utilities for ML training (Wave 30)
pub mod network;
pub mod nstep_buffer; // N-step experience buffer for multi-step returns (Wave 2.2)
pub mod portfolio_tracker;
pub mod regime_conditional; // Regime-conditional Q-networks (3 heads: Trending, Ranging, Volatile)
pub mod replay_buffer;
pub mod residual; // Residual/skip connections for better gradient flow (Wave 26 P0.4)
@@ -29,7 +34,6 @@ pub mod logit_clipping; // Bug #12: Clip logits before softmax to prevent satura
pub mod target_update; // Target network update strategies (Polyak averaging, hard updates)
pub mod trade_executor; // Risk controls and execution simulation
pub mod trainable_adapter; // UnifiedTrainable trait implementation
pub mod xavier_init; // Xavier/Glorot weight initialization
// Wave 16 Portfolio Features
pub mod entropy_regularization; // Entropy-based policy diversity
@@ -72,7 +76,7 @@ pub mod performance_validation;
// Re-export core DQN types for public usage
pub use action_space::{ExposureLevel, FactoredAction, OrderType, Urgency};
pub use order_router::OrderRouter;
pub use agent::{AgentMetrics, DQNAgent, TradingAction, TradingState};
pub use agent::{AgentMetrics, DQNAgent, TradingState};
pub use dqn::{GradientResult, DQN, DQNConfig};
pub use experience::{Experience, ExperienceBatch};
pub use nstep_buffer::NStepBuffer;
@@ -112,9 +116,6 @@ pub use logit_clipping::{clip_logits, clip_logits_default, softmax_with_clipping
// Re-export GAE components (Wave 26 P1.9)
pub use gae::{GAECalculator, GAEConfig};
// Wave 26 P2.1: Mixed precision (AMP) for 2x speedup
pub mod mixed_precision;
// Wave 26 P2.3: Ensemble Q-network for uncertainty estimation
pub mod ensemble_network;

View File

@@ -780,6 +780,14 @@ pub const PRECISION_FACTOR: i64 = 100_000_000;
/// Maximum inference latency target in microseconds
pub const MAX_INFERENCE_LATENCY_US: u64 = 100;
// ========== SHARED INFRASTRUCTURE (extracted from dqn/) ==========
pub mod action_space; // Factored action space (5 DQN exposure actions + deterministic order routing)
pub mod mixed_precision; // AMP utilities for 2x speedup (FP16/BF16)
pub mod order_router; // Deterministic order routing: exposure -> (order_type, urgency) from microstructure
pub mod portfolio_tracker; // Portfolio state tracking for training
pub mod trading_action; // TradingAction enum (Buy/Sell/Hold)
pub mod xavier_init; // Xavier/Glorot weight initialization
// ========== CORE ML MODULES ==========
// Core ML modules
pub mod backtesting; // Backtesting framework for barrier optimization

View File

@@ -5,7 +5,7 @@
//! spread and volatility conditions. This separates the alpha model
//! (what exposure?) from execution (how to route?).
use super::action_space::{ExposureLevel, FactoredAction, OrderType, Urgency};
use crate::action_space::{ExposureLevel, FactoredAction, OrderType, Urgency};
/// Deterministic order routing based on market microstructure.
/// DQN selects exposure level (0-4), OrderRouter selects order type + urgency.

View File

@@ -8,8 +8,8 @@
//! The PortfolioTracker is used by DQNTrainer to provide portfolio features
//! to the reward function, enabling P&L-based reward calculations.
use super::action_space::FactoredAction;
use super::agent::TradingAction;
use crate::action_space::FactoredAction;
use crate::trading_action::TradingAction;
use tracing::{debug, warn};
/// Trade action enum with quantities for TradeExecutor

View File

@@ -0,0 +1,36 @@
//! Trading action enum shared across DQN, PPO, ensemble, and other ML modules.
use serde::{Deserialize, Serialize};
/// Trading actions available to the `DQN` agent
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum TradingAction {
/// Buy signal
Buy = 0,
/// Sell signal
Sell = 1,
/// Hold/Do nothing
Hold = 2,
}
impl TradingAction {
/// Convert action to integer index
pub fn to_int(self) -> u8 {
self as u8
}
/// Convert integer index to action
pub fn from_int(val: u8) -> Option<Self> {
match val {
0 => Some(TradingAction::Buy),
1 => Some(TradingAction::Sell),
2 => Some(TradingAction::Hold),
_ => None,
}
}
/// Get all possible actions
pub fn all() -> [TradingAction; 3] {
[TradingAction::Buy, TradingAction::Sell, TradingAction::Hold]
}
}