352 lines
17 KiB
Rust
352 lines
17 KiB
Rust
//! The stake's player pool as an exploit source (switch `GEGNER_POOL=<d2_priors.json>`, default off).
|
||
//!
|
||
//! Best practice at micro stakes: against an opponent with few hands, play the POOL of the stake, not a
|
||
//! GTO-shaped blind default. The pool is the empirical-Bayes table of `lesen/priors.py`
|
||
//! (`messung/gedaechtnis/d2_priors.json`): per stake (`stufen`) and read a Beta prior `mu, kappa`, plus
|
||
//! the held-out cross-validation against the core's anchors (`cv_delta_heute = [mean, lo, hi]`, log-loss
|
||
//! gain per chance). Only reads that are
|
||
//!
|
||
//! * **passive axes** (fold rates, `PASSIVE`): the uniform-median prior of `sim::memory` masked the
|
||
//! aggressive outliers (maniac −32, `mem-prior-loser`), so aggression, pfr and vpip never come from
|
||
//! the pool, and
|
||
//! * **proven at that stake**: the CV lower bound beats the anchor (`lo > 0`) with at least
|
||
//! `MIN_PLAYERS` players behind it
|
||
//!
|
||
//! enter. The pool is a read with its own confidence `kappa / (kappa + S)` (S = the core's prior strength
|
||
//! for that read); the individual read takes over as its sample grows: the pool replaces the core's
|
||
//! anchor inside the individual posterior (`r' = r + (1 − c)(mu − anchor)`), and the combined
|
||
//! confidence is `1 − (1 − c)(1 − c_pool)`.
|
||
//!
|
||
//! **Aggressor guard.** Before the pool applies, a villain whose own sample already reads aggressive
|
||
//! (`aggression ≥ AGGRESSOR_AGGRESSION` with `aggression_confidence ≥ AGGRESSOR_CONFIDENCE`, or
|
||
//! `pfr ≥ AGGRESSOR_PFR`) keeps only his own reads: a maniac must never be priced as the passive pool.
|
||
//!
|
||
//! The stake comes from `GEGNER_POOL_STUFE` (forced, for the ladder arms) or from the big blind
|
||
//! (0.02 → `NL0.01-0.02`, 0.10 → `NL0.05-0.1`); no stake, no pool (bit-identical).
|
||
|
||
use crate::exploit::fold_chance::DEFAULT_FOLD_TO_BET;
|
||
use crate::exploit::profile::Profile;
|
||
use crate::exploit::sample::{FOLD_TO_THREE_BET_ANCHOR, PRIOR_HANDS};
|
||
use crate::json::value::Value;
|
||
|
||
/// Fold-rate reads the pool may supply (wire names of `lesen/zaehler.py`).
|
||
pub const PASSIVE: [&str; 7] =
|
||
["foldToBet", "foldToCbet", "flopFoldToBet", "turnFoldToBet", "riverFoldToBet", "foldToThreeBet", "foldToSteal"];
|
||
/// A stake's read counts only with this many players behind its fit.
|
||
pub const MIN_PLAYERS: f64 = 50.0;
|
||
/// Aggressor guard: own aggression at least this (the nemesis-fix line, `classify`'s 0.45) ...
|
||
pub const AGGRESSOR_AGGRESSION: f64 = 0.45;
|
||
/// ... read with at least this aggression confidence,
|
||
pub const AGGRESSOR_CONFIDENCE: f64 = 0.25;
|
||
/// or a preflop raise rate at least this.
|
||
pub const AGGRESSOR_PFR: f64 = 0.30;
|
||
/// Prior strength of the per-line reads in the core (`decode_villain` LINE_PRIOR / STREET_PRIOR).
|
||
const LINE_STRENGTH: f64 = 15.0;
|
||
/// Fold-to-steal prior strength in the core.
|
||
const STEAL_STRENGTH: f64 = 30.0;
|
||
|
||
/// One proven pool read.
|
||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||
pub struct PoolRead {
|
||
pub mu: f64,
|
||
pub kappa: f64,
|
||
}
|
||
|
||
impl PoolRead {
|
||
/// The pool's own confidence against the core's prior strength `s`.
|
||
pub fn confidence(&self, s: f64) -> f64 {
|
||
self.kappa / (self.kappa + s)
|
||
}
|
||
}
|
||
|
||
/// The proven passive reads of one stake.
|
||
#[derive(Clone, Debug, Default, PartialEq)]
|
||
pub struct Pool {
|
||
pub stage: String,
|
||
pub reads: Vec<(String, PoolRead)>,
|
||
}
|
||
|
||
impl Pool {
|
||
pub fn get(&self, name: &str) -> Option<PoolRead> {
|
||
self.reads.iter().find(|(n, _)| n == name).map(|(_, r)| *r)
|
||
}
|
||
}
|
||
|
||
fn member<'a>(v: &'a Value, key: &str) -> Option<&'a Value> {
|
||
match v {
|
||
Value::Object(m) => m.iter().find(|(k, _)| k == key).map(|(_, x)| x),
|
||
_ => None,
|
||
}
|
||
}
|
||
|
||
fn number(v: Option<&Value>) -> Option<f64> {
|
||
match v {
|
||
Some(Value::Number(x)) if x.is_finite() => Some(*x),
|
||
_ => None,
|
||
}
|
||
}
|
||
|
||
/// A stake entry's `format`: absent (older tables) or `"cash"`. Tournament and play-money stakes have
|
||
/// their own gates and never enter the cash pool.
|
||
fn is_cash(format: Option<&Value>) -> bool {
|
||
match format {
|
||
None => true,
|
||
Some(Value::String(f)) => f == "cash",
|
||
Some(_) => false,
|
||
}
|
||
}
|
||
|
||
/// The proven passive reads of `stage` from a parsed `d2_priors.json` or per-stake pool export.
|
||
pub fn from_table(table: &Value, stage: &str) -> Pool {
|
||
let mut reads = Vec::new();
|
||
if let Some(all) = member(table, "reads") {
|
||
for name in PASSIVE {
|
||
let Some(s) = member(all, name).and_then(|r| member(r, "stufen")).and_then(|s| member(s, stage)) else {
|
||
continue;
|
||
};
|
||
let (Some(mu), Some(kappa)) = (number(member(s, "mu")), number(member(s, "kappa"))) else { continue };
|
||
let players = number(member(s, "spieler")).unwrap_or(0.0);
|
||
let lo = match member(s, "cv_delta_heute") {
|
||
Some(Value::Array(x)) if x.len() == 3 => number(x.get(1)),
|
||
_ => None,
|
||
};
|
||
// The per-stake export (`archiv/lauf/pool/kandidat.json` / `aktuell.json`) carries its own gate
|
||
// `tor` (Holm p < 0.05 over the stake's reads, delta > 0 against the Rust anchors, finite kappa)
|
||
// and a `format`; only a cash stake feeds this (cash) pool. Without `tor` (the older
|
||
// `d2_priors.json`) the CV lower bound decides as before.
|
||
let proven = match member(s, "tor") {
|
||
Some(Value::Bool(t)) => *t && is_cash(member(s, "format")),
|
||
Some(_) => false,
|
||
None => lo.is_some_and(|lo| lo > 0.0),
|
||
};
|
||
if proven && players >= MIN_PLAYERS && (0.0..=1.0).contains(&mu) && kappa > 0.0 {
|
||
reads.push((name.to_string(), PoolRead { mu, kappa }));
|
||
}
|
||
}
|
||
}
|
||
Pool { stage: stage.to_string(), reads }
|
||
}
|
||
|
||
/// All stakes' pools from `GEGNER_POOL` (read once); empty when the switch is off or the file unusable.
|
||
fn table() -> &'static Option<Value> {
|
||
static T: std::sync::OnceLock<Option<Value>> = std::sync::OnceLock::new();
|
||
T.get_or_init(|| {
|
||
let path = std::env::var("GEGNER_POOL").ok()?;
|
||
let text = std::fs::read_to_string(path.trim()).ok()?;
|
||
crate::json::parse::parse(&text).ok()
|
||
})
|
||
}
|
||
|
||
/// The stake name for a big blind (or the forced `GEGNER_POOL_STUFE`).
|
||
pub fn stage_for(big_blind: f64) -> Option<String> {
|
||
if let Ok(s) = std::env::var("GEGNER_POOL_STUFE") {
|
||
let s = s.trim();
|
||
if !s.is_empty() {
|
||
return Some(s.to_string());
|
||
}
|
||
}
|
||
let near = |x: f64| (big_blind - x).abs() <= 1e-6;
|
||
if near(0.02) {
|
||
Some("NL0.01-0.02".into())
|
||
} else if near(0.10) || near(0.1) {
|
||
Some("NL0.05-0.1".into())
|
||
} else {
|
||
None
|
||
}
|
||
}
|
||
|
||
/// The live pool for this big blind (`None`: switch off, no stake, or nothing proven there).
|
||
pub fn pool_for(big_blind: f64) -> Option<Pool> {
|
||
let table = table().as_ref()?;
|
||
let stage = stage_for(big_blind)?;
|
||
let pool = from_table(table, &stage);
|
||
(!pool.reads.is_empty()).then_some(pool)
|
||
}
|
||
|
||
/// The aggressor guard: the villain's own sample says aggressive.
|
||
pub fn aggressor(p: &Profile) -> bool {
|
||
let aggr = p.aggression.filter(|a| a.is_finite());
|
||
let aconf = p.aggression_confidence.or(p.confidence).filter(|c| c.is_finite()).unwrap_or(0.0);
|
||
aggr.is_some_and(|a| a >= AGGRESSOR_AGGRESSION) && aconf >= AGGRESSOR_CONFIDENCE
|
||
|| p.pfr.filter(|x| x.is_finite()).is_some_and(|x| x >= AGGRESSOR_PFR)
|
||
}
|
||
|
||
/// `p` (or no profile) with the pool's proven fold reads blended in; `None` for an aggressor (guard),
|
||
/// or when nothing changes. The returned profile's `confidence` is the combined one.
|
||
pub fn pooled(pool: &Pool, p: Option<Profile>) -> Option<Profile> {
|
||
let base = p.unwrap_or_default();
|
||
if p.is_some_and(|p| aggressor(&p)) {
|
||
return None;
|
||
}
|
||
let c = base.confidence.filter(|c| c.is_finite()).unwrap_or(0.0).clamp(0.0, 1.0);
|
||
let mut out = base;
|
||
let mut c_pool: f64 = 0.0;
|
||
let mut blend = |slot: &mut Option<f64>, name: &str, anchor: f64, strength: f64| {
|
||
let Some(r) = pool.get(name) else { return };
|
||
let own = slot.filter(|x| x.is_finite());
|
||
*slot = Some(match own {
|
||
Some(x) => (x + (1.0 - c) * (r.mu - anchor)).clamp(0.0, 1.0),
|
||
None => r.mu,
|
||
});
|
||
c_pool = c_pool.max(r.confidence(strength));
|
||
};
|
||
blend(&mut out.fold_to_bet, "foldToBet", DEFAULT_FOLD_TO_BET, PRIOR_HANDS);
|
||
blend(&mut out.fold_to_cbet, "foldToCbet", DEFAULT_FOLD_TO_BET, LINE_STRENGTH);
|
||
blend(&mut out.street_fold_to_bet[0], "flopFoldToBet", DEFAULT_FOLD_TO_BET, LINE_STRENGTH);
|
||
blend(&mut out.street_fold_to_bet[1], "turnFoldToBet", DEFAULT_FOLD_TO_BET, LINE_STRENGTH);
|
||
blend(&mut out.street_fold_to_bet[2], "riverFoldToBet", DEFAULT_FOLD_TO_BET, LINE_STRENGTH);
|
||
blend(&mut out.fold_to_three_bet, "foldToThreeBet", FOLD_TO_THREE_BET_ANCHOR, LINE_STRENGTH);
|
||
blend(&mut out.fold_to_steal, "foldToSteal", 0.65, STEAL_STRENGTH);
|
||
if out == base {
|
||
return None;
|
||
}
|
||
out.confidence = Some(1.0 - (1.0 - c) * (1.0 - c_pool));
|
||
Some(out)
|
||
}
|
||
|
||
/// `POOL_PRIOR` (read once; needs `GEGNER_POOL` and a stake): the pool as the baseline of an unread
|
||
/// or gated villain in the core (`engine::read_gate`), passive axes only.
|
||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||
pub struct PriorMode {
|
||
/// The pool's fold reads, faded by the villain's own sample.
|
||
pub fold: bool,
|
||
/// Raises carry the pool's value share (no population over-bluff prior on a raise).
|
||
pub raise_value: bool,
|
||
}
|
||
|
||
thread_local! {
|
||
static PRIOR_OVERRIDE: std::cell::Cell<Option<Option<PriorMode>>> = const { std::cell::Cell::new(None) };
|
||
}
|
||
|
||
/// `POOL_PRIOR=passiv`: fold reads and raise value; `fold` / `raise`: one of them. Else off.
|
||
pub fn prior_mode() -> Option<PriorMode> {
|
||
static M: std::sync::OnceLock<Option<PriorMode>> = std::sync::OnceLock::new();
|
||
PRIOR_OVERRIDE.with(|o| o.get()).unwrap_or_else(|| {
|
||
*M.get_or_init(|| match std::env::var("POOL_PRIOR").as_deref() {
|
||
Ok("passiv") => Some(PriorMode { fold: true, raise_value: true }),
|
||
Ok("fold") => Some(PriorMode { fold: true, raise_value: false }),
|
||
Ok("raise") => Some(PriorMode { fold: false, raise_value: true }),
|
||
_ => None,
|
||
})
|
||
})
|
||
}
|
||
|
||
/// Force `POOL_PRIOR` on this thread (`Some(None)` = off), `None` = the environment. Tests only.
|
||
pub fn set_prior_override(mode: Option<Option<PriorMode>>) {
|
||
PRIOR_OVERRIDE.with(|o| o.set(mode));
|
||
}
|
||
|
||
/// The pool baseline for an unread or gated villain (`raw` = his ungated profile, if any), or `None`
|
||
/// (blind as before) for an aggressor (guard) or when nothing would change.
|
||
///
|
||
/// * **Fold reads:** `anchor + (1 - c) (mu - anchor)` per proven pool read, `c` the villain's own
|
||
/// confidence. Unread he plays like the stake's pool; as his sample grows the baseline fades back to
|
||
/// the core's blind anchor, since his own read stays gated (an untrusted style). It never becomes his
|
||
/// own fold read (`ANGRIFF_FOLD` lost −330 bb/100 against nemesis-1 that way).
|
||
/// * **Raise value:** the micro pool raises for value (checkRaise 0.052 at NL2, live 14.11. 12 of 13
|
||
/// raises continued, −391 BB), so a raise gets no population over-bluff prior, unless his own
|
||
/// raise-facing-a-bet read (Bayes, anchor `RAISE_TO_BET_POOL`) is wider than the anchor.
|
||
pub fn baseline(pool: &Pool, raw: Option<&Profile>, mode: PriorMode) -> Option<Profile> {
|
||
if raw.is_some_and(aggressor) {
|
||
return None;
|
||
}
|
||
let c = raw.and_then(|p| p.confidence).filter(|c| c.is_finite()).unwrap_or(0.0).clamp(0.0, 1.0);
|
||
let mut out = Profile { pool_baseline: true, ..Profile::default() };
|
||
if mode.fold {
|
||
let fade = |name: &str, anchor: f64| pool.get(name).map(|r| (anchor + (1.0 - c) * (r.mu - anchor)).clamp(0.0, 1.0));
|
||
out.fold_to_bet = fade("foldToBet", DEFAULT_FOLD_TO_BET);
|
||
out.fold_to_cbet = fade("foldToCbet", DEFAULT_FOLD_TO_BET);
|
||
out.street_fold_to_bet = [fade("flopFoldToBet", DEFAULT_FOLD_TO_BET), fade("turnFoldToBet", DEFAULT_FOLD_TO_BET), fade("riverFoldToBet", DEFAULT_FOLD_TO_BET)];
|
||
out.fold_to_three_bet = fade("foldToThreeBet", FOLD_TO_THREE_BET_ANCHOR);
|
||
out.fold_to_steal = fade("foldToSteal", 0.65);
|
||
}
|
||
if mode.raise_value {
|
||
let wide = raw
|
||
.and_then(|p| p.raise_to_bet)
|
||
.filter(|r| r.is_finite())
|
||
.is_some_and(|r| r > crate::exploit::street_continuation::RAISE_TO_BET_POOL);
|
||
out.pool_raise_value = !wide;
|
||
}
|
||
let empty = Profile { pool_baseline: true, ..Profile::default() };
|
||
(out != empty).then_some(out)
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::*;
|
||
|
||
fn table() -> Value {
|
||
crate::json::parse::parse(
|
||
r#"{"reads": {
|
||
"foldToBet": {"stufen": {"NL0.01-0.02": {"mu": 0.55, "kappa": 19, "spieler": 229, "cv_delta_heute": [0.015, 0.005, 0.02]}}},
|
||
"foldToCbet": {"stufen": {"NL0.01-0.02": {"mu": 0.49, "kappa": 8.3, "spieler": 171, "cv_delta_heute": [0.57, 0.40, 0.77]}}},
|
||
"riverFoldToBet": {"stufen": {"NL0.01-0.02": {"mu": 0.63, "kappa": 6, "spieler": 142, "cv_delta_heute": [0.03, -0.03, 0.07]}}},
|
||
"aggression": {"stufen": {"NL0.01-0.02": {"mu": 0.32, "kappa": 25, "spieler": 260, "cv_delta_heute": [0.001, 0.0001, 0.002]}}}
|
||
}}"#,
|
||
)
|
||
.unwrap()
|
||
}
|
||
|
||
/// The per-stake export: `tor` decides (not the CV bound), and only a cash stake enters.
|
||
#[test]
|
||
fn the_export_gate_and_format_decide() {
|
||
let t = crate::json::parse::parse(
|
||
r#"{"reads": {
|
||
"foldToBet": {"stufen": {"NL0.01-0.02": {"mu": 0.554, "kappa": 19, "spieler": 229, "cv_delta_heute": [0.0127, 0.0041, 0.0218], "tor": true, "format": "cash"},
|
||
"uebung": {"mu": 0.50, "kappa": 12, "spieler": 408, "cv_delta_heute": [0.004, 0.001, 0.007], "tor": true, "format": "spielgeld"}}},
|
||
"foldToCbet": {"stufen": {"NL0.01-0.02": {"mu": 0.49, "kappa": 8, "spieler": 171, "cv_delta_heute": [0.01, 0.002, 0.02], "tor": false, "format": "cash"}}}
|
||
}}"#,
|
||
)
|
||
.unwrap();
|
||
let nl2 = from_table(&t, "NL0.01-0.02");
|
||
assert_eq!(nl2.reads.len(), 1, "{nl2:?}");
|
||
assert!(nl2.get("foldToBet").is_some() && nl2.get("foldToCbet").is_none(), "tor=false wins over a positive CV bound");
|
||
assert!(from_table(&t, "uebung").reads.is_empty(), "play money never enters the cash pool");
|
||
}
|
||
|
||
#[test]
|
||
fn the_baseline_fades_to_the_anchor_and_guards_the_aggressor() {
|
||
let pool = from_table(&table(), "NL0.01-0.02");
|
||
let both = PriorMode { fold: true, raise_value: true };
|
||
let unread = baseline(&pool, None, both).unwrap();
|
||
assert_eq!(unread.fold_to_bet, Some(0.55));
|
||
assert!(unread.pool_baseline && unread.pool_raise_value);
|
||
assert_eq!(crate::exploit::style::classify(&unread), crate::exploit::style::Style::Unknown);
|
||
let half = Profile { fold_to_bet: Some(0.80), confidence: Some(0.5), ..Profile::default() };
|
||
let b = baseline(&pool, Some(&half), both).unwrap();
|
||
assert!((b.fold_to_bet.unwrap() - (DEFAULT_FOLD_TO_BET + 0.5 * 0.10)).abs() < 1e-12, "own read stays gated");
|
||
let wide = Profile { raise_to_bet: Some(0.45), ..half };
|
||
assert!(!baseline(&pool, Some(&wide), both).unwrap().pool_raise_value);
|
||
let maniac = Profile { aggression: Some(0.7), aggression_confidence: Some(0.5), ..half };
|
||
assert_eq!(baseline(&pool, Some(&maniac), both), None);
|
||
let only_fold = PriorMode { fold: true, raise_value: false };
|
||
assert!(!baseline(&pool, None, only_fold).unwrap().pool_raise_value);
|
||
assert_eq!(baseline(&Pool::default(), None, only_fold), None);
|
||
}
|
||
|
||
#[test]
|
||
fn only_proven_passive_reads_enter() {
|
||
let pool = from_table(&table(), "NL0.01-0.02");
|
||
assert!(pool.get("foldToBet").is_some() && pool.get("foldToCbet").is_some());
|
||
assert!(pool.get("riverFoldToBet").is_none(), "CV lower bound below zero");
|
||
assert!(pool.get("aggression").is_none(), "never an aggression axis");
|
||
assert!(from_table(&table(), "NL0.05-0.1").reads.is_empty());
|
||
}
|
||
|
||
#[test]
|
||
fn the_individual_read_takes_over_and_the_aggressor_is_guarded() {
|
||
let pool = from_table(&table(), "NL0.01-0.02");
|
||
let none = pooled(&pool, None).unwrap();
|
||
assert_eq!(none.fold_to_bet, Some(0.55));
|
||
let thin = Profile { fold_to_bet: Some(0.30), confidence: Some(0.1), ..Profile::default() };
|
||
let thick = Profile { confidence: Some(0.9), ..thin };
|
||
let (a, b) = (pooled(&pool, Some(thin)).unwrap(), pooled(&pool, Some(thick)).unwrap());
|
||
assert!(a.fold_to_bet.unwrap() > b.fold_to_bet.unwrap(), "pool pull shrinks with the sample");
|
||
assert!((b.fold_to_bet.unwrap() - (0.30 + 0.1 * 0.10)).abs() < 1e-12);
|
||
let maniac = Profile { aggression: Some(0.7), aggression_confidence: Some(0.5), ..thin };
|
||
assert_eq!(pooled(&pool, Some(maniac)), None);
|
||
let raiser = Profile { pfr: Some(0.35), ..thin };
|
||
assert_eq!(pooled(&pool, Some(raiser)), None);
|
||
}
|
||
}
|