1"""Model C engine: feasibility, determinism, and the pre-registered qualitative predictions in the Bayesian control."""2import numpy as np3import pytest45from age.markets.designs import bilateral_bargaining, continuous_double_auction, walrasian_call6from age.markets.simulation import MarketConfig, run_market789def test_designs_match_feasibly():10 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'),11 dict(agent=2, side='sell', qty=1.0, limit=11.0, type='limit')]12 t, p, info = walrasian_call(orders, 9.5)13 assert sum(x['qty'] for x in t) == pytest.approx(0.5) and 9.0 <= p <= 10.014 rng = np.random.default_rng(0)15 t, p, info = continuous_double_auction(orders, 9.5, rng)16 assert sum(x['qty'] for x in t) == pytest.approx(0.5)17 t, p, info = bilateral_bargaining(orders, 9.5, rng)18 assert all(9.0 <= x['price'] <= 10.0 for x in t)192021def test_run_is_deterministic_and_ledger_closed():22 a = run_market(MarketConfig(T=30, seed=5, design='cda'))23 b = run_market(MarketConfig(T=30, seed=5, design='cda'))24 assert np.allclose(a['prices'], b['prices'])25 fin = a['final']26 assert np.isclose(fin['holdings'].sum(), a['cfg'].S + a['cfg'].mm_inventory_max / 2) # investors + market maker27 assert fin['bank_equity'] >= -1e-9282930def _mean(key, **kw):31 return np.nanmean([run_market(MarketConfig(T=100, seed=s, **kw))[key] for s in range(1, 9)])323334def test_h2_correlation_rises_with_delegation_and_falls_with_diversity():35 c0 = _mean('of_corr', design='walrasian', delegation_share=0.0)36 c1 = _mean('of_corr', design='walrasian', delegation_share=1.0, M=1)37 c4 = _mean('of_corr', design='walrasian', delegation_share=1.0, M=4)38 assert c1 > c0 + 0.239 assert c4 < c1 - 0.1404142def test_h4_leverage_creates_defaults_and_forced_sales():43 d0 = _mean('defaults', design='cda', delegation_share=0.5, max_leverage=0.0)44 d1 = _mean('defaults', design='cda', delegation_share=0.5, max_leverage=0.5)45 f1 = _mean('forced', design='cda', delegation_share=0.5, max_leverage=0.5)46 assert d0 == 0.0 and d1 > 0.0 and f1 > 0.0474849def test_no_lookahead_in_observations():50 """Expected dividends used at t depend only on signals about t+1, never on realised u[t+1] directly."""51 out = run_market(MarketConfig(T=20, seed=2, delegation_share=1.0, M=1, sig_A=1e-9))52 # with a perfect model signal, the price at t should move with the realised innovation at t+1 (information), but53 # with a private noisy signal it should not be perfectly correlated: just check the mechanism is wired.54 assert np.isfinite(out['vol'])55