SPB Git

spb/wp27_uqo

Public
3.0 KB · 62 lines python
1import numpy as np2import pandas as pd3import pytest45from market_agents.experiments.config import RunConfig6from market_agents.experiments.population import build_population7from market_agents.experiments.runner import run_config8from market_agents.utils.rng import make_streams91011def test_end_to_end_classical_100_agents(tmp_path):12    cfg = RunConfig(name="t", seed=3, n_ticks=120, warmup=20)13    res = run_config(cfg, out_dir=tmp_path / "run")14    assert len(res.ticks) == 120 and res.manifest["n_trades"] > 5015    assert (tmp_path / "run" / "manifest.json").exists() and (tmp_path / "run" / "ticks.parquet").exists()16    # accounting: total wealth change equals dividends paid minus fees (mark-to-market at the same price)17    assert res.agents["final_wealth"].notna().all()181920def test_determinism_across_runs():21    cfg = RunConfig(name="t", seed=11, n_ticks=100)22    a, b = run_config(cfg), run_config(cfg)23    pd.testing.assert_frame_equal(a.ticks, b.ticks)24    pd.testing.assert_frame_equal(a.trades, b.trades)252627def test_common_random_numbers_across_llm_share():28    """Matched pairs: fundamentals and news paths are identical across llm_share treatments."""29    base = RunConfig(name="t", seed=5, n_ticks=80)30    tr = base.with_overrides(**{"population.llm_share": 0.2, "population.llm.provider_mix": {"mock": 1.0}})31    a, b = run_config(base), run_config(tr)32    assert np.allclose(a.ticks["full_value"], b.ticks["full_value"])33    sa, sb = build_population(base, make_streams(5)), build_population(tr, make_streams(5))34    # non-LLM slots keep their endowments and latencies35    for x, y in zip(sa, sb):36        assert x.cash == y.cash and x.latency == y.latency37    assert sum(s.policy_name == "llm" for s in sb) == 20383940def test_mock_llm_run_reports_shares_and_validity():41    cfg = RunConfig(name="t", seed=7, n_ticks=120, population={"llm_share": 0.5, "llm": {"provider_mix": {"mock": 1.0}, "decision_interval": 10}})42    res = run_config(cfg, mock_kwargs={"invalid_rate": 0.2})43    m = res.manifest44    assert m["llm_share_traders"] == pytest.approx(0.5, abs=0.06)   # market makers are never replaced45    assert 0 < m["llm_share_orders"] < 1 and m["n_llm_decisions"] > 10046    assert 0.6 < m["llm_valid_rate"] < 0.9547    assert set(res.llm["provider"]) == {"mock"}484950def test_zero_trade_edge_case():51    cfg = RunConfig(name="t", seed=1, n_ticks=30, population={"mix": {"market_maker": 1.0}, "activity": {"market_maker": 0.0}})52    res = run_config(cfg)53    assert res.manifest["n_trades"] == 0 and len(res.ticks) == 30 and res.ticks["mid"].notna().all()545556def test_shocks_are_logged_and_applied():57    cfg = RunConfig(name="t", seed=2, n_ticks=60, shocks=[{"time": 10, "kind": "fundamental_jump", "size": 30.0},58                                                          {"time": 20, "kind": "liquidity"}, {"time": 30, "kind": "placebo"}])59    res = run_config(cfg)60    assert [s["shock"] for s in res.manifest["shocks"]] == ["fundamental_jump", "liquidity", "placebo"]61    assert res.ticks.loc[res.ticks.t == 10, "x"].iloc[0] > res.ticks.loc[res.ticks.t == 9, "x"].iloc[0] + 2562
Files · d09ab0c71293588caf435dcaebf6e4bc03a129ff
Menu

Theme