Files
pokerfreak/tests/equity_reference.rs
T
2026-10-28 11:48:44 +01:00

159 lines
6.0 KiB
Rust

//! Monte Carlo equity against published reference values and exact enumeration.
//!
//! Sources:
//! - Preflop matchups: Wikipedia "Texas hold 'em starting hands" (head-to-head table) and
//! PokerStove / ProPokerTools Odds Oracle results: AA vs KK ~ 81.9 / 17.8 (+ ~0.3 tie),
//! AKo vs QQ ~ 43 / 57, AKs vs 22 ~ 50 / 50, AA vs 3 random hands ~ 64.2 %.
//! - Flush draw: "rule of 4 and 2" (9 outs, two cards to come: 1 - 38/47 * 37/46 = 35.0 %).
//! - Exact spots: complete enumeration with `hand::showdown::best` (independent of the sampler).
use pokerfreak::card::{deck, Card};
use pokerfreak::equity::estimate::Estimate;
use pokerfreak::equity::monte_carlo::estimate;
use pokerfreak::equity::query::Query;
use pokerfreak::equity::villain_range::VillainRange;
use pokerfreak::hand::showdown::best;
use pokerfreak::rng::Rng;
use pokerfreak::variant::{rules, HoleUsage, Variant};
fn cs(text: &str) -> Vec<Card> {
Card::parse_list(text).expect("cards")
}
fn run(variant: Variant, hero: &str, board: &str, villains: &[VillainRange], trials: u32) -> Estimate {
let (hero, board) = (cs(hero), cs(board));
let query = Query { rules: rules(variant), hero: &hero, board: &board, villains, trials };
estimate(&query, Rng::seeded(2024)).expect("estimate").0
}
fn versus(variant: Variant, hero: &str, villain: &str, board: &str, trials: u32) -> Estimate {
run(variant, hero, board, &[VillainRange::Exact(cs(villain))], trials)
}
/// Exact hero share against one known hand by enumerating every board completion.
fn exact_share(usage: HoleUsage, hero: &str, villain: &str, board: &str) -> f64 {
let (hero, villain, board) = (cs(hero), cs(villain), cs(board));
let dead: Vec<Card> = hero.iter().chain(&villain).chain(&board).copied().collect();
let pool = deck::without(&dead);
let missing = 5 - board.len();
let mut total = 0.0;
let mut count = 0.0;
let mut visit = |extra: &[Card]| {
let full: Vec<Card> = board.iter().chain(extra).copied().collect();
let (h, v) = (best(usage, &hero, &full).unwrap(), best(usage, &villain, &full).unwrap());
total += if h > v { 1.0 } else if h == v { 0.5 } else { 0.0 };
count += 1.0;
};
match missing {
1 => pool.iter().for_each(|a| visit(&[*a])),
2 => {
for i in 0..pool.len() {
for j in i + 1..pool.len() {
visit(&[pool[i], pool[j]]);
}
}
}
_ => visit(&[]),
}
total / count
}
fn close(actual: f64, expected: f64, tolerance: f64) {
assert!((actual - expected).abs() <= tolerance, "expected {expected} +- {tolerance}, got {actual}");
}
#[test]
fn aces_versus_kings_preflop_is_about_82_percent() {
let e = versus(Variant::Nlhe, "Ah As", "Kd Kc", "", 30_000);
close(e.share, 0.82, 0.015);
}
#[test]
fn ace_king_offsuit_versus_queens_is_about_43_percent() {
let e = versus(Variant::Nlhe, "Ah Kd", "Qc Qs", "", 30_000);
close(e.share, 0.43, 0.015);
}
#[test]
fn ace_king_suited_versus_deuces_is_a_coin_flip() {
let e = versus(Variant::Nlhe, "Ah Kh", "2c 2d", "", 30_000);
close(e.share, 0.50, 0.02);
}
#[test]
fn river_royal_flush_wins_every_trial() {
let e = versus(Variant::Nlhe, "Ah Kh", "2c 3d", "Qh Jh Th 7c 8d", 500);
assert_eq!((e.win, e.tie, e.share), (1.0, 0.0, 1.0));
}
#[test]
fn river_dominated_hand_loses_every_trial() {
let e = versus(Variant::Nlhe, "2c 3d", "Ah Kh", "Qh Jh Th 7c 8d", 500);
assert_eq!((e.win, e.tie, e.share), (0.0, 0.0, 0.0));
}
#[test]
fn river_board_plays_splits_the_pot_exactly() {
let e = versus(Variant::Nlhe, "2c 3c", "4d 5d", "Ah Kd Qc Jh Ts", 500);
assert_eq!((e.win, e.tie, e.share), (0.0, 1.0, 0.5));
}
#[test]
fn river_three_way_tie_gives_a_third() {
let villains = [VillainRange::Exact(cs("4d 5d")), VillainRange::Exact(cs("6d 7d"))];
let e = run(Variant::Nlhe, "2c 3c", "Ah Kd Qc Jh Ts", &villains, 200);
close(e.share, 1.0 / 3.0, 1e-12);
}
#[test]
fn flop_nut_flush_draw_versus_overpair_matches_the_nine_out_rule() {
let spot = ("Kh 4h", "Ac Ad", "Qs 2h 9h");
let e = versus(Variant::Nlhe, spot.0, spot.1, spot.2, 30_000);
close(e.share, 0.35, 0.03);
close(e.share, exact_share(HoleUsage::BestFiveOfAll, spot.0, spot.1, spot.2), 0.015);
}
#[test]
fn aces_against_three_random_hands_is_about_64_percent() {
let villains = [VillainRange::Any, VillainRange::Any, VillainRange::Any];
let e = run(Variant::Nlhe, "Ah As", "", &villains, 30_000);
close(e.share, 0.642, 0.02);
}
#[test]
fn ace_king_offsuit_against_one_random_hand_is_about_65_percent() {
// PokerStove: AKo vs random hand = 65.3 %.
let e = run(Variant::Nlhe, "Ah Kd", "", &[VillainRange::Any], 30_000);
close(e.share, 0.653, 0.015);
}
#[test]
fn plo5_river_nut_flush_versus_lower_flush_is_certain() {
// Both flush; hero holds Ah 4h, villain 8h 7h: ace-high flush wins.
let e = versus(Variant::Plo5, "Ah 4h Kc Qc Jd", "8h 7h Ac Qd Jc", "2h 5h 9h Kd 3c", 300);
assert_eq!((e.win, e.share), (1.0, 1.0));
}
#[test]
fn plo5_river_board_flush_does_not_play_without_two_suited_hole_cards() {
// Board is five hearts. Hero holds one heart, villain two hearts: villain makes the flush.
let e = versus(Variant::Plo5, "Ah Ks Kd Qc 2c", "9h 8h 4s 3d 3c", "Kh Qh Jh 7h 5h", 300);
assert_eq!(e.share, 0.0);
}
#[test]
fn plo5_turn_spot_matches_exact_enumeration() {
let (hero, villain, board) = ("As Ah Kd Kc Qs", "9h 9c 5d 4s 2h", "Jc 7d 3h 2s");
let exact = exact_share(HoleUsage::ExactlyTwoHole, hero, villain, board);
let e = versus(Variant::Plo5, hero, villain, board, 20_000);
close(e.share, exact, 0.015);
}
#[test]
fn plo5_flop_versus_random_hands_is_dominated_by_the_nut_draw() {
// Sanity bound (no published table for PLO5 flops): AAKKQ with a nut-flush draw plus top
// overpair-ish holdings is a clear favourite over one random five-card hand.
let villains = [VillainRange::Any];
let e = run(Variant::Plo5, "As Ks Ah Kh Qd", "Qs 7s 2c", &villains, 6_000);
assert!(e.share > 0.6 && e.share < 0.95, "got {}", e.share);
}