Add to_varstore() compatibility shims on DuelingQNetwork and DistributionalDuelingQNetwork so test/example code can rebuild a GpuVarStore snapshot when needed. Delete dead tests that referenced removed DQNAgent, PrioritizedReplayBuffer, and ReplayBufferType. Fix action index references (action_19/21 -> action_28/30) and type annotation issues (sin ambiguity, remainder operator). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
291 lines
9.0 KiB
Rust
291 lines
9.0 KiB
Rust
#![allow(
|
|
clippy::assertions_on_constants,
|
|
clippy::assertions_on_result_states,
|
|
clippy::clone_on_copy,
|
|
clippy::decimal_literal_representation,
|
|
clippy::doc_markdown,
|
|
clippy::empty_line_after_doc_comments,
|
|
clippy::field_reassign_with_default,
|
|
clippy::get_unwrap,
|
|
clippy::identity_op,
|
|
clippy::inconsistent_digit_grouping,
|
|
clippy::indexing_slicing,
|
|
clippy::integer_division,
|
|
clippy::len_zero,
|
|
clippy::let_underscore_must_use,
|
|
clippy::manual_div_ceil,
|
|
clippy::manual_let_else,
|
|
clippy::manual_range_contains,
|
|
clippy::modulo_arithmetic,
|
|
clippy::needless_range_loop,
|
|
clippy::non_ascii_literal,
|
|
clippy::redundant_clone,
|
|
clippy::shadow_reuse,
|
|
clippy::shadow_same,
|
|
clippy::shadow_unrelated,
|
|
clippy::single_match_else,
|
|
clippy::str_to_string,
|
|
clippy::string_slice,
|
|
clippy::tests_outside_test_module,
|
|
clippy::too_many_lines,
|
|
clippy::unnecessary_wraps,
|
|
clippy::unseparated_literal_suffix,
|
|
clippy::use_debug,
|
|
clippy::useless_vec,
|
|
clippy::wildcard_enum_match_arm,
|
|
clippy::else_if_without_else,
|
|
clippy::expect_used,
|
|
clippy::missing_const_for_fn,
|
|
clippy::similar_names,
|
|
clippy::type_complexity,
|
|
clippy::collapsible_else_if,
|
|
clippy::doc_lazy_continuation,
|
|
clippy::items_after_test_module,
|
|
clippy::map_clone,
|
|
clippy::multiple_unsafe_ops_per_block,
|
|
clippy::unwrap_or_default,
|
|
clippy::assign_op_pattern,
|
|
clippy::needless_borrow,
|
|
clippy::println_empty_string,
|
|
clippy::unnecessary_cast,
|
|
clippy::used_underscore_binding,
|
|
clippy::create_dir,
|
|
clippy::implicit_saturating_sub,
|
|
clippy::exit,
|
|
clippy::expect_fun_call,
|
|
clippy::too_many_arguments,
|
|
clippy::unnecessary_map_or,
|
|
clippy::unwrap_used,
|
|
dead_code,
|
|
unused_imports,
|
|
unused_variables,
|
|
clippy::cloned_ref_to_slice_refs,
|
|
clippy::neg_multiply,
|
|
clippy::while_let_loop,
|
|
clippy::bool_assert_comparison,
|
|
clippy::excessive_precision,
|
|
clippy::trivially_copy_pass_by_ref,
|
|
clippy::op_ref,
|
|
clippy::redundant_closure,
|
|
clippy::unnecessary_lazy_evaluations,
|
|
clippy::if_then_some_else_none,
|
|
clippy::unnecessary_to_owned,
|
|
clippy::single_component_path_imports,
|
|
)]
|
|
/// PPO Factored Action Tests - TDD Red Phase
|
|
///
|
|
/// These tests verify the porting of DQN's 45-action factored space to PPO.
|
|
/// Tests written BEFORE implementation to ensure correct behavior.
|
|
|
|
use ml::ppo::{ExposureLevel, FactoredAction, OrderType, Urgency};
|
|
|
|
#[test]
|
|
fn test_factored_action_index_mapping() {
|
|
// Test index <-> (exposure, order, urgency) conversion
|
|
// Formula: index = exposure*9 + order*3 + urgency
|
|
|
|
// Test boundary cases
|
|
let action_0 = FactoredAction::from_index(0).unwrap();
|
|
assert_eq!(action_0.exposure, ExposureLevel::ShortSmall);
|
|
assert_eq!(action_0.order, OrderType::Market);
|
|
assert_eq!(action_0.urgency, Urgency::Patient);
|
|
assert_eq!(action_0.to_index(), 0);
|
|
|
|
let action_62 = FactoredAction::from_index(62).unwrap();
|
|
assert_eq!(action_62.exposure, ExposureLevel::LongFull);
|
|
assert_eq!(action_62.order, OrderType::IoC);
|
|
assert_eq!(action_62.urgency, Urgency::Aggressive);
|
|
assert_eq!(action_62.to_index(), 62);
|
|
|
|
// Test middle case (Flat + Market + Normal)
|
|
// index = 3*9 + 0*3 + 1 = 28
|
|
let action_28 = FactoredAction::from_index(28).unwrap();
|
|
assert_eq!(action_28.exposure, ExposureLevel::Flat);
|
|
assert_eq!(action_28.order, OrderType::Market);
|
|
assert_eq!(action_28.urgency, Urgency::Normal);
|
|
assert_eq!(action_28.to_index(), 28);
|
|
|
|
// Test round-trip for all 45 actions
|
|
for idx in 0..63 {
|
|
let action = FactoredAction::from_index(idx).unwrap();
|
|
assert_eq!(action.to_index(), idx, "Round-trip failed for index {}", idx);
|
|
}
|
|
|
|
// Test out of bounds
|
|
assert!(FactoredAction::from_index(63).is_err());
|
|
assert!(FactoredAction::from_index(100).is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn test_factored_action_exposure_levels() {
|
|
// Test all 7 exposure levels
|
|
|
|
let short_small = FactoredAction::new(
|
|
ExposureLevel::ShortSmall,
|
|
OrderType::Market,
|
|
Urgency::Normal,
|
|
);
|
|
assert_eq!(short_small.target_exposure(), -0.25);
|
|
|
|
let short_full = FactoredAction::new(
|
|
ExposureLevel::ShortFull,
|
|
OrderType::Market,
|
|
Urgency::Normal,
|
|
);
|
|
assert_eq!(short_full.target_exposure(), -1.0);
|
|
|
|
let flat = FactoredAction::new(
|
|
ExposureLevel::Flat,
|
|
OrderType::Market,
|
|
Urgency::Normal,
|
|
);
|
|
assert_eq!(flat.target_exposure(), 0.0);
|
|
|
|
let long_small = FactoredAction::new(
|
|
ExposureLevel::LongSmall,
|
|
OrderType::Market,
|
|
Urgency::Normal,
|
|
);
|
|
assert_eq!(long_small.target_exposure(), 0.25);
|
|
|
|
let long_full = FactoredAction::new(
|
|
ExposureLevel::LongFull,
|
|
OrderType::Market,
|
|
Urgency::Normal,
|
|
);
|
|
assert_eq!(long_full.target_exposure(), 1.0);
|
|
}
|
|
|
|
#[test]
|
|
fn test_factored_action_order_types() {
|
|
// Test 3 order types: Market (0.15%), LimitMaker (0.05%), IoC (0.10%)
|
|
|
|
let market = FactoredAction::new(
|
|
ExposureLevel::Flat,
|
|
OrderType::Market,
|
|
Urgency::Normal,
|
|
);
|
|
assert_eq!(market.transaction_cost(), 0.0015); // 0.15%
|
|
|
|
let limit_maker = FactoredAction::new(
|
|
ExposureLevel::Flat,
|
|
OrderType::LimitMaker,
|
|
Urgency::Normal,
|
|
);
|
|
assert_eq!(limit_maker.transaction_cost(), 0.0005); // 0.05%
|
|
|
|
let ioc = FactoredAction::new(
|
|
ExposureLevel::Flat,
|
|
OrderType::IoC,
|
|
Urgency::Normal,
|
|
);
|
|
assert_eq!(ioc.transaction_cost(), 0.0010); // 0.10%
|
|
}
|
|
|
|
#[test]
|
|
fn test_factored_action_urgency_levels() {
|
|
// Test 3 urgency levels: Patient (0.5x), Normal (1.0x), Aggressive (1.5x)
|
|
|
|
let patient = FactoredAction::new(
|
|
ExposureLevel::Flat,
|
|
OrderType::Market,
|
|
Urgency::Patient,
|
|
);
|
|
assert_eq!(patient.urgency_weight(), 0.5);
|
|
|
|
let normal = FactoredAction::new(
|
|
ExposureLevel::Flat,
|
|
OrderType::Market,
|
|
Urgency::Normal,
|
|
);
|
|
assert_eq!(normal.urgency_weight(), 1.0);
|
|
|
|
let aggressive = FactoredAction::new(
|
|
ExposureLevel::Flat,
|
|
OrderType::Market,
|
|
Urgency::Aggressive,
|
|
);
|
|
assert_eq!(aggressive.urgency_weight(), 1.5);
|
|
}
|
|
|
|
#[test]
|
|
fn test_factored_action_to_position_delta() {
|
|
// Test conversion to position changes
|
|
// Formula: delta = (target_exposure - current_position) * max_position
|
|
|
|
// Test Long100 from Flat position
|
|
let long100 = FactoredAction::new(
|
|
ExposureLevel::LongFull,
|
|
OrderType::Market,
|
|
Urgency::Normal,
|
|
);
|
|
let delta = long100.to_position_delta(0.0, 2.0);
|
|
// target_exposure = 1.0, current = 0.0
|
|
// delta = (1.0 - 0.0) * 2.0 = 2.0
|
|
assert_eq!(delta, 2.0);
|
|
|
|
// Test Flat from Long50 position
|
|
let flat = FactoredAction::new(
|
|
ExposureLevel::Flat,
|
|
OrderType::Market,
|
|
Urgency::Normal,
|
|
);
|
|
let delta = flat.to_position_delta(1.0, 2.0);
|
|
// target_exposure = 0.0, current_position = 1.0
|
|
// target_position = 0.0 * 2.0 = 0.0
|
|
// delta = 0.0 - 1.0 = -1.0
|
|
assert_eq!(delta, -1.0);
|
|
|
|
// Test ShortFull from Flat position
|
|
let short_full = FactoredAction::new(
|
|
ExposureLevel::ShortFull,
|
|
OrderType::Market,
|
|
Urgency::Normal,
|
|
);
|
|
let delta = short_full.to_position_delta(0.0, 2.0);
|
|
// target_exposure = -1.0, current = 0.0
|
|
// delta = (-1.0 * 2.0) - 0.0 = -2.0
|
|
assert_eq!(delta, -2.0);
|
|
|
|
// Test no change (already at target)
|
|
let long_small = FactoredAction::new(
|
|
ExposureLevel::LongSmall,
|
|
OrderType::Market,
|
|
Urgency::Normal,
|
|
);
|
|
let delta = long_small.to_position_delta(0.5, 2.0);
|
|
// target_exposure = 0.25, current = 0.5
|
|
// target_position = 0.25 * 2.0 = 0.5
|
|
// delta = 0.5 - 0.5 = 0.0
|
|
assert_eq!(delta, 0.0);
|
|
}
|
|
|
|
#[test]
|
|
fn test_factored_action_boundary_cases() {
|
|
// Test index 0: ShortSmall + Market + Patient
|
|
let action_0 = FactoredAction::from_index(0).unwrap();
|
|
assert_eq!(action_0.exposure, ExposureLevel::ShortSmall);
|
|
assert_eq!(action_0.order, OrderType::Market);
|
|
assert_eq!(action_0.urgency, Urgency::Patient);
|
|
assert_eq!(action_0.target_exposure(), -0.25);
|
|
assert_eq!(action_0.transaction_cost(), 0.0015);
|
|
assert_eq!(action_0.urgency_weight(), 0.5);
|
|
|
|
// Test index 62: LongFull + IoC + Aggressive
|
|
let action_62 = FactoredAction::from_index(62).unwrap();
|
|
assert_eq!(action_62.exposure, ExposureLevel::LongFull);
|
|
assert_eq!(action_62.order, OrderType::IoC);
|
|
assert_eq!(action_62.urgency, Urgency::Aggressive);
|
|
assert_eq!(action_62.target_exposure(), 1.0);
|
|
assert_eq!(action_62.transaction_cost(), 0.0010);
|
|
assert_eq!(action_62.urgency_weight(), 1.5);
|
|
|
|
// Test specific known index: Flat + LimitMaker + Patient
|
|
// index = 3*9 + 1*3 + 0 = 27 + 3 = 30
|
|
let action_30 = FactoredAction::from_index(30).unwrap();
|
|
assert_eq!(action_30.exposure, ExposureLevel::Flat);
|
|
assert_eq!(action_30.order, OrderType::LimitMaker);
|
|
assert_eq!(action_30.urgency, Urgency::Patient);
|
|
assert_eq!(action_30.to_index(), 30);
|
|
}
|