"""Model C engine: feasibility, determinism, and the pre-registered qualitative predictions in the Bayesian control.""" import numpy as np import pytest from age.markets.designs import bilateral_bargaining, continuous_double_auction, walrasian_call from age.markets.simulation import MarketConfig, run_market def test_designs_match_feasibly(): orders = [dict(agent=0, side='buy', qty=1.0, limit=10.0, type='limit'), dict(agent=1, side='sell', qty=0.5, limit=9.0, type='limit'), dict(agent=2, side='sell', qty=1.0, limit=11.0, type='limit')] t, p, info = walrasian_call(orders, 9.5) assert sum(x['qty'] for x in t) == pytest.approx(0.5) and 9.0 <= p <= 10.0 rng = np.random.default_rng(0) t, p, info = continuous_double_auction(orders, 9.5, rng) assert sum(x['qty'] for x in t) == pytest.approx(0.5) t, p, info = bilateral_bargaining(orders, 9.5, rng) assert all(9.0 <= x['price'] <= 10.0 for x in t) def test_run_is_deterministic_and_ledger_closed(): a = run_market(MarketConfig(T=30, seed=5, design='cda')) b = run_market(MarketConfig(T=30, seed=5, design='cda')) assert np.allclose(a['prices'], b['prices']) fin = a['final'] assert np.isclose(fin['holdings'].sum(), a['cfg'].S + a['cfg'].mm_inventory_max / 2) # investors + market maker assert fin['bank_equity'] >= -1e-9 def _mean(key, **kw): return np.nanmean([run_market(MarketConfig(T=100, seed=s, **kw))[key] for s in range(1, 9)]) def test_h2_correlation_rises_with_delegation_and_falls_with_diversity(): c0 = _mean('of_corr', design='walrasian', delegation_share=0.0) c1 = _mean('of_corr', design='walrasian', delegation_share=1.0, M=1) c4 = _mean('of_corr', design='walrasian', delegation_share=1.0, M=4) assert c1 > c0 + 0.2 assert c4 < c1 - 0.1 def test_h4_leverage_creates_defaults_and_forced_sales(): d0 = _mean('defaults', design='cda', delegation_share=0.5, max_leverage=0.0) d1 = _mean('defaults', design='cda', delegation_share=0.5, max_leverage=0.5) f1 = _mean('forced', design='cda', delegation_share=0.5, max_leverage=0.5) assert d0 == 0.0 and d1 > 0.0 and f1 > 0.0 def test_no_lookahead_in_observations(): """Expected dividends used at t depend only on signals about t+1, never on realised u[t+1] directly.""" out = run_market(MarketConfig(T=20, seed=2, delegation_share=1.0, M=1, sig_A=1e-9)) # with a perfect model signal, the price at t should move with the realised innovation at t+1 (information), but # with a private noisy signal it should not be perfectly correlated: just check the mechanism is wired. assert np.isfinite(out['vol'])