From 59bb390b72faa7be68ea999d5e4e3854cd9c44fd Mon Sep 17 00:00:00 2001 From: Marc Mintel Date: Mon, 16 Nov 2026 05:41:41 +0100 Subject: [PATCH] =?UTF-8?q?POSTERIOR=20S1=20(engine::glaube):=20P(Fold)=20?= =?UTF-8?q?aus=20der=20Band-Masse=20unter=20dem=20call=5Ffloor,=20Board-Te?= =?UTF-8?q?rm=20in=20Logit;=20Varianten=20fold:,=20nurfold.=20Aus=20bit?= =?UTF-8?q?gleich;=20Pilot=20w=3D1=20verliert=20(STAND=20=C2=A719)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- src/engine/called_equity.rs | 12 +- src/engine/glaube.rs | 231 ++++++++++++++++++++++++++++++++++++ src/engine/mod.rs | 1 + src/exploit/call_floor.rs | 2 +- src/exploit/table_fold.rs | 5 + 5 files changed, 249 insertions(+), 2 deletions(-) create mode 100644 src/engine/glaube.rs diff --git a/src/engine/called_equity.rs b/src/engine/called_equity.rs index e53f6e51..7f046a56 100644 --- a/src/engine/called_equity.rs +++ b/src/engine/called_equity.rs @@ -57,7 +57,17 @@ fn simulate(spot: &Spot, notch: i32, ratio: f64) -> Option { let mut spec = range_spec(spot, v); let fold_only = v.profile.is_some_and(|p| p.fold_only); let call = if fold_only { top_call_continuation } else { call_continuation }; - spec.continues.push(call(v, spot.street, ratio, spot.to_call > 0.0)); + let mut c = call(v, spot.street, ratio, spot.to_call > 0.0); + if super::glaube::called_on() { + // POSTERIOR=fold: the range that calls is 1 - the fold the fold branch uses. + let goes_on = 1.0 - super::glaube::table_fold_of(spot, v, ratio, spot.to_call > 0.0); + if c.top_share.is_some() { + c.top_share = Some(goes_on); + } else { + c.continue_share = Some(goes_on); + } + } + spec.continues.push(c); range_filter(&spec, rules, spot.variant, &spot.board) }) .collect(); diff --git a/src/engine/glaube.rs b/src/engine/glaube.rs new file mode 100644 index 00000000..c6c36f60 --- /dev/null +++ b/src/engine/glaube.rs @@ -0,0 +1,231 @@ +//! One belief about an opponent (`POSTERIOR`, off by default = bit-identical), PLAN ยง8.11 #4, 5, 8, 15, 16. +//! +//! Stage S1 (`POSTERIOR=fold`): P(fold) to a bet is the range mass below the continue boundary, the +//! same boundary and the same number that the called branch plays against (`exploit::call_floor`). +//! Today the fold chance is a formula without the board (`exploit::fold_chance`), and the call floor +//! takes its width from that formula without the street read (`table_fold` uses the street read), +//! so the fold branch and the called branch disagree. +//! +//! The read says HOW MUCH a villain folds (its average over boards), the board says WHERE: in logit, +//! +//! `logit P(fold) = logit f_read + w * (logit m_board - logit m_ref)` +//! +//! * `f_read`: the fold chance of the read at this pressure (`fold_chance_on` / the c-bet read), the +//! villain's average; +//! * `m_board`: the share of his preflop band (`range_model::range_spec` lo..hi, hero's cards and the +//! board removed) below the calling floor (`call_floor`: rung by street, size notch and style; draws +//! from a gutshot continue before the river) on THIS board; +//! * `m_ref`: the same share averaged over `REF_BOARDS` random boards of the same length, so the term +//! is zero on an average board and the read keeps its level; +//! * `w`: the board weight (`POSTERIOR=fold:`, default 1). +//! +//! Only the preflop band enters `m_board`: the pool folds MORE on later streets (NL2 flop 0,51, turn +//! 0,55, river 0,63), so the narrowing of a calling range is not the dominant effect, and the read's +//! street level already carries it. Board texture is what the formula misses. + +use crate::card::{deck, Card}; +use crate::exploit::call_floor::CALL_RUNG; +use crate::exploit::fold_chance::{fold_base_cbet, fold_chance_on, sized_fold, MAX_FOLD}; +use crate::exploit::made_ladder::rung; +use crate::exploit::range_model::range_spec; +use crate::exploit::size_notch::size_notch; +use crate::exploit::style_rung::style_rung; +use crate::game::{line, Spot, Street, Villain}; +use crate::preflop::hand_percentile::percentile; +use crate::read::draw::Draw; +use crate::read::glance::glance; +use crate::read::made::Made; +use crate::rng::Rng; +use crate::variant::{Rules, Variant}; +use std::cell::RefCell; +use std::collections::HashMap; + +/// Random boards per reference share. +pub const REF_BOARDS: usize = 48; +const REF_SEED: u64 = 0x6_1A0B_E000; +/// Band edges are rounded to this step for the reference cache. +const BAND_STEP: f64 = 0.05; +/// Shares are kept inside `[EPS, 1 - EPS]` before the logit. +const EPS: f64 = 0.01; + +/// The switch: which stages are on and the board weight of S1. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Mode { + pub fold: bool, + pub board_weight: f64, + /// `nurfold`: the board term only in the fold branch, the called branch keeps `call_floor`. + pub fold_only_branch: bool, +} + +thread_local! { + static OVERRIDE: std::cell::Cell>> = const { std::cell::Cell::new(None) }; + static MASS: RefCell> = RefCell::new(HashMap::new()); +} + +/// `POSTERIOR=fold` or `fold:` (comma list of stages; unknown stages are ignored). Else off. +pub fn mode() -> Option { + use std::sync::OnceLock; + static M: OnceLock> = OnceLock::new(); + OVERRIDE.with(|o| o.get()).unwrap_or_else(|| *M.get_or_init(|| std::env::var("POSTERIOR").ok().and_then(|s| parse(&s)))) +} + +fn parse(s: &str) -> Option { + let mut m = Mode { fold: false, board_weight: 1.0, fold_only_branch: false }; + for t in s.split(',').map(str::trim) { + if t == "fold" { + m.fold = true; + } else if t == "nurfold" { + m.fold_only_branch = true; + } else if let Some(w) = t.strip_prefix("fold:").and_then(|w| w.parse::().ok()).filter(|w| w.is_finite()) { + m.fold = true; + m.board_weight = w; + } + } + m.fold.then_some(m) +} + +/// Force the switch for this thread (`Some(None)` = off), `None` = the environment. Tests. +pub fn set_override(mode: Option>) { + OVERRIDE.with(|o| o.set(mode)); +} + +/// Is S1 on? +pub fn fold_on() -> bool { + mode().is_some_and(|m| m.fold) +} + +/// Does the called branch play against `1 - fold` (S1 without `nurfold`)? +pub fn called_on() -> bool { + mode().is_some_and(|m| m.fold && !m.fold_only_branch) +} + +fn logit(p: f64) -> f64 { + let p = p.clamp(EPS, 1.0 - EPS); + (p / (1.0 - p)).ln() +} + +fn sigmoid(x: f64) -> f64 { + 1.0 / (1.0 + (-x).exp()) +} + +/// The calling floor of `villain` against pressure `ratio` on `street` (`call_floor::call_continuation`). +fn floor(villain: &Villain, street: Street, ratio: f64) -> (Made, bool) { + let slot = (street as usize).clamp(1, 3) - 1; + (rung(CALL_RUNG[slot], size_notch(Some(ratio)) + style_rung(villain)), street != Street::River) +} + +/// Share of the two-card hands with preflop percentile in `[lo, hi]`, outside `dead`, that fall +/// below the floor on `board`. `None` for other hand sizes or an empty band. +fn below_share(rules: &Rules, variant: Variant, lo: f64, hi: f64, board: &[Card], dead: &[Card], made: Made, draws: bool) -> Option { + if rules.hole_cards != 2 { + return None; + } + let pool = deck::without_in(rules.deck, dead); + let (mut all, mut below) = (0usize, 0usize); + for i in 0..pool.len() { + for j in i + 1..pool.len() { + let hand = [pool[i], pool[j]]; + let p = percentile(variant, &hand); + if p < lo || p > hi { + continue; + } + let Ok((m, d)) = glance(rules, &hand, board) else { continue }; + all += 1; + if !(m >= made || (draws && d >= Draw::Gutshot)) { + below += 1; + } + } + } + (all > 0).then(|| below as f64 / all as f64) +} + +fn cached(key: String, f: impl FnOnce() -> Option) -> Option { + if let Some(x) = MASS.with(|m| m.borrow().get(&key).copied()) { + return Some(x); + } + let x = f()?; + MASS.with(|m| { + let mut m = m.borrow_mut(); + if m.len() >= 4096 { + m.clear(); + } + m.insert(key, x); + }); + Some(x) +} + +/// The board-average share below the floor for this band and street (random boards, fixed seed). +fn reference(rules: &Rules, variant: Variant, lo: f64, hi: f64, board_len: usize, made: Made, draws: bool) -> Option { + let key = format!("ref|{variant:?}|{lo:.2}|{hi:.2}|{board_len}|{made:?}|{draws}"); + cached(key, || { + let full = deck::without_in(rules.deck, &[]); + let mut rng = Rng::seeded(REF_SEED ^ board_len as u64); + let mut sum = 0.0; + let mut n = 0usize; + for _ in 0..REF_BOARDS { + let (board, next) = deck::draw(&full, board_len, rng)?; + rng = next; + if let Some(x) = below_share(rules, variant, lo, hi, &board, &board, made, draws) { + sum += x; + n += 1; + } + } + (n > 0).then(|| sum / n as f64) + }) +} + +/// P(`villain` folds) to pressure `ratio`, S1 (see the module text). Falls back to the read alone +/// preflop, without a usable band, or for other hand sizes. +pub fn fold(spot: &Spot, villain: &Villain, ratio: f64, facing_raise: bool, read_fold: f64) -> f64 { + let Some(m) = mode().filter(|m| m.fold) else { return read_fold }; + if spot.street == Street::Preflop || spot.board.len() < 3 || !(0.0..1.0).contains(&read_fold) || read_fold <= 0.0 { + return read_fold; + } + let rules = spot.rules(); + let spec = range_spec(spot, villain); + let round = |x: f64| ((x / BAND_STEP).round() * BAND_STEP).clamp(0.0, 1.0); + let (lo, hi) = (round(spec.lo), round(spec.hi)); + if hi <= lo { + return read_fold; + } + let (made, draws) = floor(villain, spot.street, ratio); + let dead: Vec = spot.hero_cards.iter().chain(&spot.board).copied().collect(); + let key = format!("brett|{:?}|{lo:.2}|{hi:.2}|{:?}|{:?}|{made:?}|{draws}", spot.variant, spot.board, spot.hero_cards); + let board = cached(key, || below_share(&rules, spot.variant, lo, hi, &spot.board, &dead, made, draws)); + let reference = reference(&rules, spot.variant, lo, hi, spot.board.len(), made, draws); + let _ = facing_raise; + match (board, reference) { + (Some(b), Some(r)) => sigmoid(logit(read_fold) + m.board_weight * (logit(b) - logit(r))).clamp(0.0, MAX_FOLD), + _ => read_fold, + } +} + +/// The read's fold chance as `table_fold` prices it (street read, c-bet read on a c-bet), then S1. +pub fn table_fold_of(spot: &Spot, villain: &Villain, ratio: f64, facing_raise: bool) -> f64 { + let p = villain.profile.as_ref(); + let read = if line::cbet_spot(&spot.history, spot.hero_seat, spot.street) { + sized_fold(fold_base_cbet(p), ratio, facing_raise) + } else { + fold_chance_on(p, ratio, facing_raise, Some(spot.street)) + }; + fold(spot, villain, ratio, facing_raise, read) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn parses_the_stages() { + assert_eq!(parse("fold"), Some(Mode { fold: true, board_weight: 1.0, fold_only_branch: false })); + assert_eq!(parse("fold:0.5,bluff"), Some(Mode { fold: true, board_weight: 0.5, fold_only_branch: false })); + assert_eq!(parse("fold:0,nurfold"), Some(Mode { fold: true, board_weight: 0.0, fold_only_branch: true })); + assert_eq!(parse("0"), None); + assert_eq!(parse(""), None); + } + + #[test] + fn an_average_board_keeps_the_read() { + assert!((sigmoid(logit(0.45)) - 0.45).abs() < 1e-12); + } +} diff --git a/src/engine/mod.rs b/src/engine/mod.rs index e9f77832..1459e051 100644 --- a/src/engine/mod.rs +++ b/src/engine/mod.rs @@ -12,6 +12,7 @@ pub mod effective; pub mod flop_spieler; pub mod fortsetzung; pub mod frist; +pub mod glaube; pub mod from_plan; pub mod multiway; pub mod omaha_combos; diff --git a/src/exploit/call_floor.rs b/src/exploit/call_floor.rs index f7d6d711..fc6ebdf8 100644 --- a/src/exploit/call_floor.rs +++ b/src/exploit/call_floor.rs @@ -7,7 +7,7 @@ use crate::game::{Street, Villain}; /// Rung of the weakest hand that calls a bet, per street (flop, turn, river) on /// `made_ladder`: bottom pair on flop and turn, middle pair on the river. -const CALL_RUNG: [i32; 3] = [1, 1, 2]; +pub const CALL_RUNG: [i32; 3] = [1, 1, 2]; /// What a villain continues with when hero bets `ratio` pots on `street`: the calling /// floor moved by the size (`size_notch`) and the villain's style (`style_rung`); draws diff --git a/src/exploit/table_fold.rs b/src/exploit/table_fold.rs index fee11447..05e0c3ea 100644 --- a/src/exploit/table_fold.rs +++ b/src/exploit/table_fold.rs @@ -23,7 +23,12 @@ pub fn table_fold(spot: &Spot, to: f64, scale: f64) -> f64 { } // Hero's continuation bet is priced by the fold-to-c-bet read (`fold_base_cbet`). let cbet = line::cbet_spot(&spot.history, spot.hero_seat, spot.street); + let glaube = crate::engine::glaube::fold_on(); live.map(|v| { + if glaube { + // POSTERIOR=fold: the same number the called branch plays against (`engine::glaube`). + return (crate::engine::glaube::table_fold_of(spot, v, ratio, facing_raise) * scale).clamp(0.0, MAX_FOLD); + } let p = v.profile.as_ref(); let f = if cbet { sized_fold(fold_base_cbet(p), ratio, facing_raise)