tradefloor 0.11.0

A reproducible evaluation environment for financial AI agents.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
//! INVARIANTS 6.1–6.8, as property tests.
//!
//! These are deliberately NOT parity tests. The vectors already hold the port
//! to the reference implementation bit for bit; what these assert is that the MODEL is
//! right — that Okun's law has the sign economics gives it, that the Fed cuts
//! into a collapse. A port can be perfectly faithful to a broken model, and
//! these are the tests that would notice.

#![cfg(test)]

use super::*;
use crate::rng::Rng;
use super::central_bank::{CORPORATE_SPREAD_FLOOR, MORTGAGE_SPREAD_FLOOR};

/// A generator with no noise. Every normal here has mean zero, so silencing
/// them leaves the deterministic channel — which is what a property test
/// about economics should be looking at.
struct Silent(f64);
impl Rng for Silent {
    fn next_f64(&mut self) -> f64 {
        self.0
    }
    fn next_normal(&mut self) -> f64 {
        0.0
    }
}

fn economy() -> EconomyState {
    create_initial_economy_state(&InitialEconomyOptions::default())
}

/// A month-start day, where the monthly channels fire.
fn month_step(e: &EconomyState, volatility: f64) -> EconomyState {
    update_economy_daily(
        e,
        &DailyInputs {
            volatility,
            game_day: DAYS_PER_MONTH,
            ..Default::default()
        },
        &mut Silent(0.5),
    )
}

fn meeting(e: &EconomyState, cb: &CentralBankState) -> MeetingOutcome {
    let mut cb = *cb;
    cb.next_meeting_date = -1;
    update_central_bank(&cb, e, 1000, &mut Silent(0.5))
}

// ── 6.1 Taylor rule ───────────────────────────────────────────────────────

#[test]
fn the_taylor_rule_reads_every_one_of_its_inputs() {
    // The point of decision D1. Each input is moved on its own and must change
    // the outcome; a rule blind to one of them passes a test that only ever
    // moves inflation.
    let base = economy();
    let cb = create_initial_central_bank_state(0);
    let rate = |e: &EconomyState, cb: &CentralBankState| meeting(e, cb).economy.federal_funds_rate;

    let baseline = rate(&base, &cb);

    let mut hot = base.clone();
    hot.inflation_rate = 6.0;
    assert!(
        rate(&hot, &cb) > baseline,
        "high inflation must raise the rate"
    );

    let mut slack = base.clone();
    slack.unemployment_rate = 9.0;
    slack.inflation_rate = 2.5;
    assert!(
        rate(&slack, &cb) < baseline,
        "a slack labour market must lower the rate — a Taylor rule blind to \
         unemployment is exactly what D1 rejected"
    );

    // `hawkish_dovish_score` is real state that the deployed WASM formula
    // never read. The term shifts the rate by at most 0.1, so the test has to
    // straddle a decision threshold for it to be observable at all — the same
    // reason the parity sweep needed a fine `federalFundsRate` grid.
    let mut near = base.clone();
    near.inflation_rate = 3.2;
    near.unemployment_rate = 4.5;
    near.federal_funds_rate = 2.15;
    let mut dovish = cb;
    dovish.hawkish_dovish_score = -1.0;
    let mut hawkish = cb;
    hawkish.hawkish_dovish_score = 1.0;
    assert!(
        rate(&near, &hawkish) > rate(&near, &dovish),
        "policy momentum must move the decision when rateDiff sits on a threshold"
    );
}

#[test]
fn the_fed_cuts_into_a_collapse_whatever_inflation_is_doing() {
    // The dual mandate: employment wins when the economy is collapsing. A
    // pure inflation targeter would hike here.
    let mut e = economy();
    e.gdp_growth = -3.0;
    e.unemployment_rate = 11.0;
    e.inflation_rate = 5.0;
    e.federal_funds_rate = 4.0;
    let cb = create_initial_central_bank_state(0);

    let out = meeting(&e, &cb);
    assert!(
        out.economy.federal_funds_rate < e.federal_funds_rate,
        "deep recession must cut, got {} from {}",
        out.economy.federal_funds_rate,
        e.federal_funds_rate
    );
    assert_eq!(out.decision, Some(Decision::EmergencyCut));
}

// ── 6.2 Phillips curve ────────────────────────────────────────────────────

#[test]
fn slack_above_nairu_lowers_inflation_and_tightness_raises_it() {
    let mut slack = economy();
    slack.unemployment_rate = 9.0;
    slack.structural_unemployment = 4.5;
    let mut tight = economy();
    tight.unemployment_rate = 2.8;
    tight.structural_unemployment = 4.5;

    let slack_out = month_step(&slack, 0.0);
    let tight_out = month_step(&tight, 0.0);
    assert!(
        slack_out.inflation_rate < tight_out.inflation_rate,
        "a slack labour market must be less inflationary: {} vs {}",
        slack_out.inflation_rate,
        tight_out.inflation_rate
    );
}

// ── 6.3 Okun's law ────────────────────────────────────────────────────────

#[test]
fn gdp_below_trend_raises_unemployment() {
    let mut weak = economy();
    weak.gdp_growth = -2.0;
    let mut strong = economy();
    strong.gdp_growth = 4.0;

    assert!(
        month_step(&weak, 0.0).unemployment_rate > month_step(&strong, 0.0).unemployment_rate,
        "weak growth must raise unemployment relative to strong growth"
    );
}

// ── 6.4 Prime rate ────────────────────────────────────────────────────────

#[test]
fn prime_is_always_exactly_three_points_above_the_policy_rate() {
    for fed in [0.0, 0.1, 2.5, 5.5, 8.0] {
        let mut e = economy();
        e.federal_funds_rate = fed;
        e.inflation_rate = 2.0;
        let out = meeting(&e, &create_initial_central_bank_state(0)).economy;
        assert_eq!(out.prime_rate, out.federal_funds_rate + 3.0);
    }
    let e = economy();
    assert_eq!(e.prime_rate, e.federal_funds_rate + 3.0);
}

// ── 6.5 Spreads ───────────────────────────────────────────────────────────

#[test]
fn mortgage_and_corporate_spreads_stay_positive_and_bounded() {
    for vix in [10.0, 18.0, 30.0, 55.0, 80.0] {
        for phase in [
            CyclePhase::Expansion,
            CyclePhase::Peak,
            CyclePhase::Contraction,
            CyclePhase::Trough,
            CyclePhase::Recovery,
        ] {
            let mut e = economy();
            e.vix = vix;
            e.cycle_phase = phase;
            let out = meeting(&e, &create_initial_central_bank_state(0)).economy;

            let m_spread = out.mortgage_rate_30y - out.treasury_yield_10y;
            assert!(
                (0.5 - 1e-12..=3.5 + 1e-12).contains(&m_spread),
                "mortgage spread {m_spread} outside [0.5, 3.5] at vix {vix} in {phase:?}"
            );

            let c_spread = out.corporate_bond_yield - out.treasury_yield_10y;
            assert!(
                c_spread >= 0.8 - 1e-12,
                "corporate spread {c_spread} below its floor at vix {vix} in {phase:?}"
            );
        }
    }
}

#[test]
fn credit_spreads_widen_in_a_downturn() {
    // The cycle multipliers exist so credit stress is a phase property, not
    // only a VIX property.
    let spread_in = |phase| {
        let mut e = economy();
        e.cycle_phase = phase;
        e.vix = 25.0;
        let out = meeting(&e, &create_initial_central_bank_state(0)).economy;
        out.corporate_bond_yield - out.treasury_yield_10y
    };
    assert!(spread_in(CyclePhase::Trough) > spread_in(CyclePhase::Contraction));
    assert!(spread_in(CyclePhase::Contraction) > spread_in(CyclePhase::Expansion));
}

// ── 6.7 / 6.8 Clamps ──────────────────────────────────────────────────────

#[test]
fn the_bounded_series_stay_bounded_under_sustained_stress() {
    // Driven with an extreme generator rather than a silent one, so the noise
    // terms push hard against every clamp for 400 consecutive days.
    struct Extreme(f64);
    impl Rng for Extreme {
        fn next_f64(&mut self) -> f64 {
            self.0
        }
        fn next_normal(&mut self) -> f64 {
            self.0 * 40.0
        }
    }

    for sign in [1.0f64, -1.0] {
        let mut e = economy();
        let mut cb = create_initial_central_bank_state(0);
        for day in 0..400i64 {
            e = update_economy_daily(
                &e,
                &DailyInputs {
                    volatility: 0.9,
                    market_return_pct: sign * 5.0,
                    game_day: day,
                    ..Default::default()
                },
                &mut Extreme(sign),
            );
            e = check_cycle_transition(
                &e,
                &mut Extreme(if sign > 0.0 { 0.0 } else { 0.99 }),
                0.0,
            );
            let out = update_central_bank(&cb, &e, day * 24 * 60, &mut Extreme(sign));
            cb = out.central_bank;
            e = out.economy;

            for (label, value, lo, hi) in [
                ("unemployment", e.unemployment_rate, 2.5, 15.0),
                ("fed rate", e.federal_funds_rate, 0.0, 8.0),
                ("vix", e.vix, 10.0, 80.0),
                ("10Y", e.treasury_yield_10y, 0.5, 12.0),
                ("inflation", e.inflation_rate, -1.0, 6.0),
                ("fear/greed", e.fear_greed_index, 0.0, 100.0),
                ("recession prob", e.recession_probability, 0.05, 0.95),
            ] {
                assert!(
                    (lo..=hi).contains(&value),
                    "day {day} (sign {sign}): {label} {value} escaped [{lo}, {hi}]"
                );
            }
        }
    }
}

#[test]
fn qe_engages_only_at_the_zero_bound_in_a_contraction() {
    let mut e = economy();
    e.federal_funds_rate = 0.1;
    e.cycle_phase = CyclePhase::Contraction;
    e.inflation_rate = 0.5;
    e.unemployment_rate = 9.0;
    let cb = create_initial_central_bank_state(0);

    let out = meeting(&e, &cb);
    assert!(
        out.central_bank.qe_active,
        "QE must engage at the zero bound in contraction"
    );
    assert!(
        out.economy.qe_pe_boost > 0.0,
        "QE must feed the PE boost the market module reads"
    );

    // Not in an expansion, however low the rate.
    let mut calm = e.clone();
    calm.cycle_phase = CyclePhase::Expansion;
    let out = meeting(&calm, &cb);
    assert!(!out.central_bank.qe_active);
    assert_eq!(out.economy.qe_pe_boost, 0.0);
}

// ── Cycle ─────────────────────────────────────────────────────────────────

#[test]
fn a_phase_cannot_end_before_its_minimum_duration() {
    for phase in [
        CyclePhase::Expansion,
        CyclePhase::Peak,
        CyclePhase::Contraction,
        CyclePhase::Trough,
        CyclePhase::Recovery,
    ] {
        let mut e = economy();
        e.cycle_phase = phase;
        e.months_in_current_phase = phase_characteristics(phase).min_months - 0.001;
        let (p, _) = get_cycle_transition_probability(&e, 0.0);
        assert_eq!(p, 0.0, "{phase:?} offered a transition before its minimum");
        // A generator that always fires must still not move it.
        assert_eq!(
            check_cycle_transition(&e, &mut Silent(0.0), 0.0).cycle_phase,
            phase
        );
    }
}

#[test]
fn the_daily_transition_hazard_is_capped_at_thirty_percent() {
    for phase in [
        CyclePhase::Expansion,
        CyclePhase::Peak,
        CyclePhase::Contraction,
        CyclePhase::Trough,
        CyclePhase::Recovery,
    ] {
        let mut e = economy();
        e.cycle_phase = phase;
        e.months_in_current_phase = 600.0;
        e.inflation_rate = 9.0;
        e.federal_funds_rate = 8.0;
        e.treasury_yield_2y = 9.0;
        e.treasury_yield_10y = 1.0;
        e.market_pe = Some(60.0);
        let (p, _) = get_cycle_transition_probability(&e, 0.0);
        assert!(
            (0.0..=0.3).contains(&p),
            "{phase:?} hazard {p} escaped [0, 0.3]"
        );
        // Read per month the same cap bounds a MONTHLY rate, so the daily
        // probability it admits is thirty times smaller. Asserted at the
        // bound rather than at 0.3, which every value under the monthly
        // reading would satisfy whether or not the conversion happened.
        let (per_month, _) = get_cycle_transition_probability(&e, 1.0);
        assert!(
            (0.0..=0.01).contains(&per_month),
            "{phase:?} hazard {per_month} escaped [0, 0.01] read per month"
        );
        assert_eq!(
            per_month,
            p / 30.0,
            "{phase:?} read per month is not the monthly rate over 30"
        );
    }
}

#[test]
fn phases_advance_in_the_documented_order() {
    for (from, to) in [
        (CyclePhase::Expansion, CyclePhase::Peak),
        (CyclePhase::Peak, CyclePhase::Contraction),
        (CyclePhase::Contraction, CyclePhase::Trough),
        (CyclePhase::Trough, CyclePhase::Recovery),
        (CyclePhase::Recovery, CyclePhase::Expansion),
    ] {
        assert_eq!(phase_characteristics(from).next_phase, to);
    }
}

#[test]
fn the_weibull_hazard_changes_character_across_shape_one() {
    // shape > 1: an ageing phase grows fragile. shape < 1: early exits
    // dominate and late ones linger. Both regimes are configured, so both are
    // asserted — this is the property the shape parameter exists for.
    let rising = |m| weibull_hazard(m, 1.8, 36.0);
    assert!(
        rising(60.0) > rising(12.0),
        "shape 1.8 must rise with duration"
    );

    let falling = |m| weibull_hazard(m, 0.7, 12.0);
    assert!(
        falling(24.0) < falling(2.0),
        "shape 0.7 must fall with duration"
    );

    // And the cap holds however long the phase runs.
    for (shape, scale) in [
        (1.8, 36.0),
        (2.0, 6.0),
        (0.7, 12.0),
        (1.5, 4.0),
        (1.3, 12.0),
    ] {
        assert!(weibull_hazard(1e6, shape, scale) <= 0.8);
        assert_eq!(weibull_hazard(0.0, shape, scale), 0.0);
        assert_eq!(weibull_hazard(-5.0, shape, scale), 0.0);
    }
}

// ── crisis_vix_threshold reaches every gate that claims to use it ─────────

#[test]
fn a_preset_that_moves_the_crisis_threshold_leaves_the_dollar_gate_alone() {
    // The regression 0.4.2 shipped, pinned. Issue #50 was right that the
    // dollar gate read a constant where the gold premium read the parameter,
    // and the one-line fix pointed both at `crisis_vix_threshold`. But
    // `pt-v13` and `pt-v14` OVERRIDE that parameter to 30.88, so their dollar
    // gate moved from 25.5 and their trajectories moved with it, in a patch
    // release, against a version policy that forbids exactly that.
    //
    // Nothing caught it because nothing looks here. The known-answer test
    // starts at VIX 19.5 and never crosses 25.5; the two surviving full
    // bit-parity economy trajectories peak at 25.44 and 16.51; the three that
    // do cross were retired at the crisis-gates fork. This test drives the
    // VIX ABOVE the gate deliberately, which is the region none of them
    // sample.
    let mut e = economy();
    e.vix = 28.0;          // above 25.5, below a preset's raised 30.88
    e.gdp_growth = -2.0;

    let run = |crisis: f64, usd: f64| {
        update_economy_daily(
            &e,
            &DailyInputs {
                volatility: 1.0,
                game_day: 1,
                crisis_vix_threshold: crisis,
                usd_crisis_vix_threshold: usd,
                ..Default::default()
            },
            &mut Silent(0.5),
        )
    };

    let base = run(CRISIS_VIX_THRESHOLD, CRISIS_VIX_THRESHOLD);
    // A preset raising ONLY the crisis threshold, as pt-v14 does.
    let raised_crisis = run(30.88325108, CRISIS_VIX_THRESHOLD);
    assert_eq!(
        base.usd_index, raised_crisis.usd_index,
        "moving crisis_vix_threshold moved the dollar. The two gates share a \
         default; they are not the same dial, and a preset that raises one \
         must not silently move the other."
    );
    assert_ne!(
        base.gold_price, raised_crisis.gold_price,
        "moving crisis_vix_threshold did not move gold, so this test is no \
         longer exercising the gate it claims to"
    );

    // And the dollar gate still works when asked for directly.
    let raised_usd = run(CRISIS_VIX_THRESHOLD, 40.0);
    assert_ne!(
        base.usd_index, raised_usd.usd_index,
        "usd_crisis_vix_threshold did not reach the dollar"
    );
}

#[test]
fn moving_the_crisis_threshold_moves_both_the_gold_and_the_dollar_gate() {
    // The gold crisis premium read the parameter and the USD safe-haven drift
    // read the constant, so an embedder who moved `crisis_vix_threshold` got
    // one gate at their level and one still at 25.5. The two describe the same
    // regime and the default hid the split, because there the two agree.
    //
    // Asserts on BOTH series from one parameter move, which is the thing the
    // old tests could not do: they only ever ran at the default.
    let mut e = economy();
    e.vix = 28.0;          // above the default gate, below a raised one
    e.gdp_growth = -2.0;   // the gold premium also needs a contraction

    let run = |threshold: f64, e: &EconomyState| {
        update_economy_daily(
            e,
            &DailyInputs {
                volatility: 1.0,
                game_day: 1,
                crisis_vix_threshold: threshold,
                // Both, deliberately: this test asks whether a caller moving
                // the whole crisis regime reaches both series, which is issue
                // #50's actual question. The test above asks the narrower one.
                usd_crisis_vix_threshold: threshold,
                ..Default::default()
            },
            &mut Silent(0.5),
        )
    };

    let at_default = run(CRISIS_VIX_THRESHOLD, &e);
    let raised = run(40.0, &e);

    assert_ne!(
        at_default.gold_price, raised.gold_price,
        "gold ignored crisis_vix_threshold"
    );
    assert_ne!(
        at_default.usd_index, raised.usd_index,
        "the dollar ignored crisis_vix_threshold; it read the constant"
    );
}

// ── 6.5, extended past the moment of assignment ───────────────────────────

/// Run the deterministic channel for 2000 days and return the worst spread
/// each credit instrument reaches, at a given floor gain.
fn worst_spreads(gain: f64) -> (f64, f64) {
    let mut e = economy();
    e = meeting(&e, &create_initial_central_bank_state(0)).economy;
    let mut worst_corp = e.corporate_bond_yield - e.treasury_yield_10y;
    let mut worst_mort = e.mortgage_rate_30y - e.treasury_yield_10y;
    for day in 1..=2000i64 {
        e = update_economy_daily(
            &e,
            &DailyInputs {
                volatility: 1.0,
                game_day: day,
                daily_credit_floor_gain: gain,
                ..Default::default()
            },
            &mut Silent(0.5),
        );
        let c = e.corporate_bond_yield - e.treasury_yield_10y;
        let m = e.mortgage_rate_30y - e.treasury_yield_10y;
        if c < worst_corp { worst_corp = c; }
        if m < worst_mort { worst_mort = m; }
    }
    (worst_corp, worst_mort)
}

#[test]
fn the_daily_step_still_lets_the_corporate_spread_invert_while_the_dial_is_off() {
    // The defect, pinned so it cannot be fixed by accident. 6.5's own test
    // measures `meeting(&e, ...).economy`, so it only ever inspected the
    // instant the yields are computed. `update_economy_daily` moves
    // `treasury_yield_10y` on every day and never writes either credit yield,
    // so between periodic meetings the spread drifts wherever the benchmark
    // takes it.
    //
    // This is asserted rather than fixed outright because that function is
    // preset-independent: flooring unconditionally would move the economy
    // trajectory of every shipped preset, and the version policy requires a
    // trajectory change to arrive as a NEW preset. So the correction ships as
    // `daily_credit_floor_gain`, inert, and this test records what the model
    // does until a preset turns it on.
    let (corp, _mort) = worst_spreads(0.0);
    assert!(
        corp < CORPORATE_SPREAD_FLOOR,
        "the corporate spread no longer inverts with the dial off ({corp}). If that \
         was deliberate it is a trajectory change for every preset; see the version \
         policy in RELEASING.md."
    );
}

#[test]
fn the_floor_gain_holds_both_credit_spreads_above_their_floors() {
    // And the remedy, gated. At 1.0 both floors are enforced against whatever
    // the treasury has become, on every day rather than only at a meeting.
    let (corp, mort) = worst_spreads(1.0);
    assert!(
        corp >= CORPORATE_SPREAD_FLOOR - 1e-12,
        "corporate spread fell to {corp} with the floor gain at 1.0"
    );
    assert!(
        mort >= MORTGAGE_SPREAD_FLOOR - 1e-12,
        "mortgage spread fell to {mort} with the floor gain at 1.0"
    );
}