//! Omaha-4 starting-hand ranking: structure, table reproducibility and calibration against //! sampled equity. //! //! Regenerate `preflop::plo4_score_table` after changing the `plo4_score` weights: //! `cargo test --release --test preflop_plo4 -- --ignored --nocapture print_table` use rechner::card::Card; use rechner::equity::monte_carlo::estimate; use rechner::equity::query::Query; use rechner::equity::villain_range::VillainRange; use rechner::preflop::hand_percentile::percentile; use rechner::preflop::plo4_score::score; use rechner::preflop::plo4_score_table::{PERCENTILE_BY_SCORE, SCORE_MIN}; use rechner::rng::Rng; use rechner::variant::{rules, Variant}; fn hand(text: &str) -> Vec { Card::parse_list(text).expect("valid cards") } fn pct(text: &str) -> f64 { percentile(Variant::Plo4, &hand(text)) } /// Score histogram over all 270 725 four-card hands. fn histogram() -> (i32, Vec, u64) { let all: Vec = (0..52).filter_map(Card::from_index).collect(); let mut by_score: std::collections::BTreeMap = Default::default(); let mut total = 0; for a in 0..52 { for b in a + 1..52 { for c in b + 1..52 { for d in c + 1..52 { let s = score(&[all[a], all[b], all[c], all[d]]).expect("valid"); *by_score.entry(s).or_default() += 1; total += 1; } } } } let min = *by_score.keys().next().expect("scores"); let max = *by_score.keys().last().expect("scores"); let counts = (min..=max).map(|s| by_score.get(&s).copied().unwrap_or(0)).collect(); (min, counts, total) } #[test] #[ignore] fn print_table() { let (min, counts, total) = histogram(); let mut above = 0u64; let mut entries: Vec = counts .iter() .rev() .map(|n| { let p = (above as f64 + *n as f64 / 2.0) / total as f64; above += n; p }) .collect(); entries.reverse(); println!("pub const SCORE_MIN: i32 = {min};"); println!("pub const PERCENTILE_BY_SCORE: [f64; {}] = [", entries.len()); for row in entries.chunks(10) { let line: Vec = row.iter().map(|p| format!("{p:.5}")).collect(); println!(" {},", line.join(", ")); } println!("];"); } #[test] fn the_table_reproduces_from_the_score_distribution() { let (min, counts, total) = histogram(); assert_eq!(total, 270_725); assert_eq!(min, SCORE_MIN); assert_eq!(counts.len(), PERCENTILE_BY_SCORE.len()); let mut above = 0u64; for (i, n) in counts.iter().enumerate().rev() { let expected = (above as f64 + *n as f64 / 2.0) / total as f64; assert!((PERCENTILE_BY_SCORE[i] - expected).abs() < 6e-6, "score {}", min + i as i32); above += n; } } #[test] fn only_four_distinct_cards_are_ranked() { assert_eq!(pct("Ah Kd"), 1.0); assert_eq!(pct("Ah Kd Qc"), 1.0); assert_eq!(pct("Ah Kd Qc Js Th"), 1.0); assert_eq!(pct("Ah Ah Qc Js"), 1.0); assert!(pct("Ah Kd Qc Js") < 1.0); } #[test] fn percentile_is_monotone_in_the_score() { assert!(PERCENTILE_BY_SCORE.windows(2).all(|w| w[0] >= w[1])); } #[test] fn double_suited_big_pairs_beat_rainbow_junk() { assert!(pct("Ah As Kh Ks") < 0.01, "{}", pct("Ah As Kh Ks")); assert!(pct("2c 7d 4h 9s") > 0.8); } #[test] fn four_card_structure_is_valued() { assert!(pct("Ah Kh Qd Jd") < pct("Ah Kd Qc Js"), "double suited"); assert!(pct("Jh Td 9c 8s") < pct("Jh 8d 5c 2s"), "rundown"); assert!(pct("Ah Ad Kh Kd") < pct("Ah Ad Ac Kh"), "trips"); assert!(pct("Ah Ad Ac Kh") < pct("Ah Ad Ac As"), "quads"); assert!(pct("Ah Ad Kh Kd") < pct("Ah Ad Kc Qs"), "double suited pairs"); } #[test] fn four_card_rundowns_and_broadways_rank_high_without_a_fifth_card() { // A five-card table would see these as incomplete hands; here a rainbow rundown is a // top-third hand and a double-suited one clearly better. assert!(pct("Jh Td 9c 8s") < 0.35, "{}", pct("Jh Td 9c 8s")); assert!(pct("Jh Td 9h 8d") < pct("Jh Td 9c 8s")); assert!(pct("Ah Kd Qc Js") < 0.10, "{}", pct("Ah Kd Qc Js")); } /// Equity against one random Omaha hand on a random board, deterministic seed. fn equity(hole: &[Card], seed: u64) -> f64 { let query = Query { rules: rules(Variant::Plo4), hero: hole, board: &[], villains: &[VillainRange::Any], trials: 4_000, }; estimate(&query, Rng::seeded(seed)).expect("estimate").0.share } /// Deterministic pseudo-random four-card hands (LCG), independent of the crate's Rng. fn sample_hands(count: usize) -> Vec> { let mut state: u64 = 0x9E37_79B9_7F4A_7C15; let mut next = move || { state = state.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407); (state >> 33) as u8 }; (0..count) .map(|_| { let mut picked: Vec = Vec::new(); while picked.len() < 4 { let c = next() % 52; if !picked.contains(&c) { picked.push(c); } } picked.into_iter().filter_map(Card::from_index).collect() }) .collect() } fn ranks(values: &[f64]) -> Vec { let mut order: Vec = (0..values.len()).collect(); order.sort_by(|a, b| values[*a].total_cmp(&values[*b])); let mut out = vec![0.0; values.len()]; for (rank, i) in order.into_iter().enumerate() { out[i] = rank as f64; } out } #[test] fn the_ranking_agrees_with_sampled_equity() { let hands = sample_hands(250); let strength: Vec = hands.iter().map(|h| 1.0 - percentile(Variant::Plo4, h)).collect(); let share: Vec = hands.iter().enumerate().map(|(i, h)| equity(h, 100 + i as u64)).collect(); let (a, b) = (ranks(&strength), ranks(&share)); let n = a.len() as f64; let (ma, mb) = (a.iter().sum::() / n, b.iter().sum::() / n); let cov: f64 = a.iter().zip(&b).map(|(x, y)| (x - ma) * (y - mb)).sum(); let var = |v: &[f64], m: f64| v.iter().map(|x| (x - m) * (x - m)).sum::(); if std::env::var("PLO4_DEBUG").is_ok() { let mut rows: Vec<(f64, usize)> = (0..hands.len()).map(|i| ((a[i] - b[i]) / n, i)).collect(); rows.sort_by(|x, y| x.0.total_cmp(&y.0)); for (d, i) in rows.iter().take(8).chain(rows.iter().rev().take(8)) { let t: Vec = hands[*i].iter().map(|c| format!("{}{}", c.rank.value(), c.suit as usize)).collect(); eprintln!("{d:+.2} eq {:.3} pct {:.3} {}", share[*i], percentile(Variant::Plo4, &hands[*i]), t.join(" ")); } } let spearman = cov / (var(&a, ma) * var(&b, mb)).sqrt(); assert!(spearman > 0.85, "rank correlation with equity {spearman}"); }