//! Stufe 4 of the synthetic lab, the OFFLINE half: the sim-to-real gap in observable space. //! //! The lab is only as good as its population `P(θ)`. Stufe 1 fits the population's archetype weights to //! real players; this module measures the OTHER direction — what HUD distribution the synthetic //! population *induces*, so it can be held against the real one. The truth→observation link //! (`sim::reads::opponent_profile`, hand-eyed in `tests/sim_opponent_stats`) is here generalised from //! "five fixed styles" to "any drawn ruleset", so a whole drawn population can be run through it and its //! induced vpip / pfr / fold-to-bet / aggression distribution reported. //! //! The gap that remains genuinely LIVE — running the engine against real humans and comparing outcomes //! (the `COINPOKER_KERN` A/B over `betrieb/runner.py`) — needs the live farm and is not buildable here. //! This module builds everything that can be measured offline: does our synthetic field even *look* //! like real players through a HUD? If the induced distribution misses the real one, no amount of //! in-lab EV is trustworthy, and this says so before a single live hand is played. use super::population::Population; use super::reader::{act_with, Reading, Rules}; use super::opponent::{Act, View}; use crate::card::{deck, Card}; use crate::rng::Rng; /// The four HUD numbers a ruleset induces over many random hands — the same axes `exploit::profile` /// and `sim::memory` speak in. #[derive(Clone, Copy, Debug, PartialEq)] pub struct Hud { pub vpip: f64, pub pfr: f64, pub fold_to_bet: f64, pub aggression: f64, } fn hole_and_flop(rng: Rng) -> (Vec, Vec, Rng) { let (cards, rng) = deck::draw(&deck::full(), 5, rng).expect("deck"); (cards[..2].to_vec(), cards[2..5].to_vec(), rng) } /// Measure the HUD a ruleset induces, over `n` random hands at the same canonical spots the oracle /// reads were eyed at (`tests/sim_opponent_stats`): a preflop open, a postflop spot facing a /// two-thirds bet, and a postflop checked-to spot. pub fn induced_hud(rules: Rules, n: usize, seed: u64) -> Hud { let mut rng = Rng::seeded(seed); let (mut vpip, mut pfr, mut faced, mut folds, mut aggr, mut acts) = (0u32, 0u32, 0u32, 0u32, 0u32, 0u32); let preflop_view = View { to_call: 0.5, min_raise_to: Some(2.0), max_raise_to: Some(100.0) }; let facing_view = View { to_call: 4.0, min_raise_to: Some(8.0), max_raise_to: Some(100.0) }; let checked_view = View { to_call: 0.0, min_raise_to: Some(4.0), max_raise_to: Some(100.0) }; for _ in 0..n { let (hole, flop, next) = hole_and_flop(rng); rng = next; let pre = super::reader::strength(&hole, &[], Rng::seeded(rng.below(u64::MAX).0)); let post = super::reader::strength(&hole, &flop, Rng::seeded(rng.below(u64::MAX).0)); let (a, r) = act_with(rules, preflop_view, Reading { strength: pre, pot: 1.0, own_bet: 0.5, preflop: true }, rng); rng = r; match a { Act::RaiseTo(_) => { vpip += 1; pfr += 1; } Act::Call => vpip += 1, _ => {} } let (a, r) = act_with(rules, facing_view, Reading { strength: post, pot: 6.0, own_bet: 0.0, preflop: false }, rng); rng = r; faced += 1; match a { Act::Fold => folds += 1, Act::RaiseTo(_) => { aggr += 1; acts += 1; } _ => acts += 1, } let (a, r) = act_with(rules, checked_view, Reading { strength: post, pot: 6.0, own_bet: 0.0, preflop: false }, rng); rng = r; match a { Act::RaiseTo(_) => { aggr += 1; acts += 1; } _ => acts += 1, } } let f = |x: u32, d: u32| if d == 0 { 0.0 } else { x as f64 / d as f64 }; Hud { vpip: f(vpip, n as u32), pfr: f(pfr, n as u32), fold_to_bet: f(folds, faced), aggression: f(aggr, acts), } } /// Mean and sample sd of each HUD axis across the induced HUDs of `draws` drawn opponents — the /// synthetic field's fingerprint in observable space. #[derive(Clone, Debug)] pub struct HudDistribution { pub draws: usize, pub mean: Hud, pub sd: Hud, } fn mean_sd(v: &[f64]) -> (f64, f64) { let n = v.len().max(1) as f64; let m = v.iter().sum::() / n; let var = v.iter().map(|x| (x - m).powi(2)).sum::() / (n - 1.0).max(1.0); (m, var.sqrt()) } /// The induced-HUD distribution of a whole drawn population: draw `draws` opponents, measure each /// one's induced HUD over `hands_each` hands, and report the per-axis mean and spread. pub fn population_induced_hud( pop: &Population, draws: usize, hands_each: usize, seed: u64, ) -> HudDistribution { let mut rng = Rng::seeded(seed); let (mut vp, mut pf, mut fb, mut ag) = (Vec::new(), Vec::new(), Vec::new(), Vec::new()); for k in 0..draws { let (draw, next) = pop.sample(rng); rng = next; let h = induced_hud(draw.rules, hands_each, seed ^ (k as u64 + 1).wrapping_mul(0x9E37_79B9)); vp.push(h.vpip); pf.push(h.pfr); fb.push(h.fold_to_bet); ag.push(h.aggression); } let (vpm, vps) = mean_sd(&vp); let (pfm, pfs) = mean_sd(&pf); let (fbm, fbs) = mean_sd(&fb); let (agm, ags) = mean_sd(&ag); HudDistribution { draws, mean: Hud { vpip: vpm, pfr: pfm, fold_to_bet: fbm, aggression: agm }, sd: Hud { vpip: vps, pfr: pfs, fold_to_bet: fbs, aggression: ags }, } } #[cfg(test)] mod tests { use super::*; use crate::sim::reader::Style; #[test] fn induced_hud_orders_the_styles_as_expected() { // Fish play nearly everything (high vpip); nits almost nothing. The induced HUD must preserve // that — the truth→observation link is monotone. let fish = induced_hud(Style::Fish.rules(), 4000, 0x5EED_0001); let nit = induced_hud(Style::Nit.rules(), 4000, 0x5EED_0001); println!("fish {:?}\nnit {:?}", fish, nit); assert!(fish.vpip > nit.vpip + 0.1, "fish should be far looser than a nit"); assert!((0.0..=1.0).contains(&fish.aggression)); } #[test] fn population_induced_hud_is_finite_and_bounded() { let pop = Population::micro(); let d = population_induced_hud(&pop, 40, 1500, 0x5EED_0001); println!("induced HUD mean {:?}\n sd {:?}", d.mean, d.sd); for x in [d.mean.vpip, d.mean.pfr, d.mean.fold_to_bet, d.mean.aggression] { assert!((0.0..=1.0).contains(&x)); } assert!(d.sd.vpip >= 0.0); } }