Files
pokerfreak/tests/short_deck_equity.rs
T
mmintel 04d5251acf Welt ohne fremden Namen: Pruefordner vollstaendig mitkopieren, Engine nur im eigenen Baum
Die Welt nannte 'pokerfreak' nur noch in zwei Harness-Zeilen - der Rest waren veraltete
Rust-Pruefungen im Pruefordner, die der Aufbau nie auffrischte.
2026-11-01 23:37:11 +01:00

212 lines
9.1 KiB
Rust

//! Short deck equity: Monte Carlo against exact enumeration of the 36-card deck.
//!
//! Reference values come from exact enumeration over the remaining 32 cards
//! (C(32,5) = 201 376 boards per heads-up matchup), evaluated with the short deck rules
//! (`judge`: flush over full house, ace low in A-6-7-8-9). Tolerance for the Monte Carlo
//! estimate: 0.006 (about 5 standard errors at 200 000 trials; the seed is fixed anyway).
//!
//! Published short deck figures could not be verified offline, so none is asserted as exact.
//! Recorded exact values (this enumeration): AA vs KK 74.26 %, AKs vs QQ 54.28 %, AKo vs JTs 49.35 %.
//! The bands below only guard against gross errors. Shape check from general knowledge: AKs is a
//! slight favourite over QQ in short deck (hold'em: about 46 %), pair over pair is a smaller
//! favourite than in hold'em (AA vs KK 81.9 %, PokerStove).
use rechner::card::{deck, Card};
use rechner::equity::estimate::Estimate;
use rechner::equity::monte_carlo::estimate;
use rechner::equity::query::Query;
use rechner::equity::villain_range::VillainRange;
use rechner::fault::Fault;
use rechner::hand::showdown::judge;
use rechner::rng::Rng;
use rechner::variant::{rules, Rules, Variant};
fn cs(text: &str) -> Vec<Card> {
Card::parse_list(text).expect("cards")
}
/// Hero's exact share against one known hand over every possible board.
fn exact_share(rules: &Rules, hero: &[Card], villain: &[Card]) -> f64 {
let dead: Vec<Card> = hero.iter().chain(villain).copied().collect();
let pool = deck::without_in(rules.deck, &dead);
let n = pool.len();
let (mut total, mut shares) = (0u64, 0.0f64);
let mut score = |board: [Card; 5]| {
let h = judge(rules, hero, &board).expect("hero");
let v = judge(rules, villain, &board).expect("villain");
total += 1;
shares += match h.cmp(&v) {
std::cmp::Ordering::Greater => 1.0,
std::cmp::Ordering::Equal => 0.5,
std::cmp::Ordering::Less => 0.0,
};
};
for a in 0..n {
for b in a + 1..n {
for c in b + 1..n {
for d in c + 1..n {
for e in d + 1..n {
score([pool[a], pool[b], pool[c], pool[d], pool[e]]);
}
}
}
}
}
shares / total as f64
}
fn mc(rules: Rules, hero: &str, board: &str, villains: &[VillainRange], trials: u32) -> Result<Estimate, Fault> {
let (hero, board) = (cs(hero), cs(board));
let query = Query { rules, hero: &hero, board: &board, villains, trials };
estimate(&query, Rng::seeded(2025)).map(|(e, _)| e)
}
fn matchup(hero: &str, villain: &str) -> (f64, f64) {
let short = rules(Variant::ShortDeck);
let exact = exact_share(&short, &cs(hero), &cs(villain));
let sampled = mc(short, hero, "", &[VillainRange::Exact(cs(villain))], 200_000).expect("estimate").share;
(exact, sampled)
}
fn agrees(hero: &str, villain: &str) -> f64 {
let (exact, sampled) = matchup(hero, villain);
println!("{hero} vs {villain}: exact {exact:.5}, sampled {sampled:.5}");
assert!((exact - sampled).abs() < 0.006, "{hero} vs {villain}: exact {exact:.4}, sampled {sampled:.4}");
exact
}
#[test]
fn aces_against_kings() {
let exact = agrees("Ah Ad", "Kc Ks");
assert!((0.70..0.80).contains(&exact), "plausibility band: {exact}");
}
#[test]
fn ace_king_suited_against_queens() {
let exact = agrees("Ah Kh", "Qc Qs");
assert!((0.50..0.58).contains(&exact), "plausibility band: {exact}");
}
#[test]
fn ace_king_offsuit_against_jack_ten_suited() {
let exact = agrees("Ah Kd", "Jc Tc");
assert!((0.45..0.55).contains(&exact), "plausibility band: {exact}");
}
#[test]
fn the_two_matchup_sides_add_up_to_one() {
let short = rules(Variant::ShortDeck);
let (a, b) = (cs("Ah Ad"), cs("Kc Ks"));
let sum = exact_share(&short, &a, &b) + exact_share(&short, &b, &a);
assert!((sum - 1.0).abs() < 1e-12, "{sum}");
}
#[test]
fn exact_enumeration_on_a_four_card_board_is_a_hand_countable_value() {
// Hero Ah Kh vs Qc Qd on Ac 7d 6s Ts: 28 rivers left in the 36-card deck.
// Hero has a pair of aces, villain a pair of queens; only Qh or Qs (2 of 28 rivers) turn it.
let short = rules(Variant::ShortDeck);
let (hero, villain) = (cs("Ah Kh"), cs("Qc Qd"));
let board = cs("Ac 7d 6s Ts");
let pool = deck::without_in(short.deck, &[hero.clone(), villain.clone(), board.clone()].concat());
assert_eq!(pool.len(), 36 - 8);
let share: f64 = pool
.iter()
.map(|river| {
let full = [board.clone(), vec![*river]].concat();
let (h, v) = (judge(&short, &hero, &full).expect("h"), judge(&short, &villain, &full).expect("v"));
f64::from(u8::from(h > v)) + 0.5 * f64::from(u8::from(h == v))
})
.sum::<f64>()
/ pool.len() as f64;
let sampled =
mc(short, "Ah Kh", "Ac 7d 6s Ts", &[VillainRange::Exact(cs("Qc Qd"))], 20_000).expect("estimate").share;
assert!((share - sampled).abs() < 0.02, "exact {share}, sampled {sampled}");
// Queens need Qh or Qs (2 outs) to pass aces; a river king or ace does not hurt hero.
assert!((share - 26.0 / 28.0).abs() < 1e-12, "{share}");
}
#[test]
fn burns_do_not_change_equity_because_burned_cards_are_unseen_random_cards() {
let short = rules(Variant::ShortDeck);
let none = Rules { burns: 0, ..short };
let villains = [VillainRange::Any, VillainRange::Any];
let with = mc(short, "Ah Ad", "", &villains, 5_000).expect("with burns");
let without = mc(none, "Ah Ad", "", &villains, 5_000).expect("without burns");
assert_eq!(with, without);
}
#[test]
fn short_deck_runs_are_reproducible_for_one_seed() {
let short = rules(Variant::ShortDeck);
let a = mc(short, "Ah 9h", "Ac 7d 6s", &[VillainRange::Any], 4_000).expect("a");
let b = mc(short, "Ah 9h", "Ac 7d 6s", &[VillainRange::Any], 4_000).expect("b");
assert_eq!(a, b);
}
#[test]
fn fourteen_two_card_players_fit_the_short_deck_fifteen_do_not() {
// 14 x 2 + 5 board + 3 burns = 36
let short = rules(Variant::ShortDeck);
let thirteen: Vec<VillainRange> = (0..13).map(|_| VillainRange::Any).collect();
assert!(mc(short, "Ah Ad", "", &thirteen, 50).is_ok());
let fourteen: Vec<VillainRange> = (0..14).map(|_| VillainRange::Any).collect();
assert!(matches!(mc(short, "Ah Ad", "", &fourteen, 50), Err(Fault::Exhausted(_))));
}
#[test]
fn known_hands_count_towards_the_short_deck_capacity() {
let short = rules(Variant::ShortDeck);
let mut villains: Vec<VillainRange> = (0..12).map(|_| VillainRange::Any).collect();
villains.push(VillainRange::Exact(cs("Kc Ks")));
assert!(mc(short, "Ah Ad", "", &villains, 50).is_ok());
villains.push(VillainRange::Exact(cs("Qc Qs")));
assert!(matches!(mc(short, "Ah Ad", "", &villains, 50), Err(Fault::Exhausted(_))));
}
#[test]
fn the_full_deck_capacity_is_unchanged() {
// hold'em: 2 + 29 x 2 + 5 = 65 > 52 fails, 2 + 21 x 2 + 5 = 49 still runs (no burn reserve)
let nlhe = rules(Variant::Nlhe);
let twenty_one: Vec<VillainRange> = (0..21).map(|_| VillainRange::Any).collect();
assert!(mc(nlhe, "Ah Ad", "", &twenty_one, 20).is_ok());
}
#[test]
fn cards_outside_the_short_deck_are_invalid_input() {
let short = rules(Variant::ShortDeck);
let any = [VillainRange::Any];
assert!(matches!(mc(short, "Ah 5h", "", &any, 50), Err(Fault::Invalid { .. })));
assert!(matches!(mc(short, "Ah Kh", "2c 7d 8s", &any, 50), Err(Fault::Invalid { .. })));
assert!(matches!(
mc(short, "Ah Kh", "", &[VillainRange::Exact(cs("Qc 3d"))], 50),
Err(Fault::Invalid { .. })
));
}
#[test]
fn short_deck_samples_never_deal_a_card_below_six() {
// A deal with a wheel board would be a fault (judge rejects it), so 20 000 clean trials
// against 5 random hands prove that nothing below six is ever dealt.
let short = rules(Variant::ShortDeck);
let villains: Vec<VillainRange> = (0..5).map(|_| VillainRange::Any).collect();
let e = mc(short, "Ah Ad", "", &villains, 20_000).expect("clean run");
assert_eq!(e.trials, 20_000);
}
#[test]
fn holdem_results_are_bit_identical_to_before_for_the_same_seed() {
// Golden values recorded on the unchanged code before the short deck work (seed 77, 3000 trials).
let golden = |variant, hero: &str, board: &str, n: usize| {
let (h, b) = (cs(hero), cs(board));
let vs: Vec<VillainRange> = (0..n).map(|_| VillainRange::Any).collect();
let query = Query { rules: rules(variant), hero: &h, board: &b, villains: &vs, trials: 3000 };
estimate(&query, Rng::seeded(77)).expect("estimate").0
};
let a = golden(Variant::Nlhe, "Ah Kd", "", 2);
assert_eq!((a.win, a.tie, a.share, a.std_error), (0.4683333333333333, 0.014666666666666666, 0.4751111111111111, 0.009053331466858876));
let b = golden(Variant::Nlhe, "Qs Qc", "7d 8h 2c", 1);
assert_eq!((b.win, b.tie, b.share, b.std_error), (0.8013333333333333, 0.0003333333333333333, 0.8015, 0.007281653638800784));
let c = golden(Variant::Plo5, "Ah Kh Qd Jc 9s", "Th 5h 2h", 2);
assert_eq!((c.win, c.tie, c.share, c.std_error), (0.8133333333333334, 0.0, 0.8133333333333334, 0.007115074291118128));
}