//! ICM-lite: harville equities and the bubble. Same spots, cash against bubble. use rechner::card::Card; use rechner::engine::decide::{decide, decide_with}; use rechner::game::{ActionKind, ActionRecord, Choice, SeenAction, Spot, Street, Villain}; use rechner::icm::harville::equities; use rechner::icm::lage::Turnierlage; use rechner::icm::risk::{call_premium, required_equity}; use rechner::variant::Variant; const RANKS: &str = "AKQJT98765432"; fn close(a: f64, b: f64) -> bool { (a - b).abs() < 1e-9 } #[test] fn equities_pay_out_exactly_the_prize_pool() { let e = equities(&[10.0, 20.0, 30.0, 40.0], &[50.0, 30.0, 20.0]); assert!(close(e.iter().sum::(), 100.0), "{e:?}"); assert!(e.windows(2).all(|w| w[0] < w[1]), "bigger stack, bigger equity: {e:?}"); } #[test] fn equal_stacks_are_worth_the_same_and_one_winner_takes_first() { let e = equities(&[5.0; 4], &[50.0, 30.0, 20.0]); assert!(e.iter().all(|x| close(*x, 25.0)), "{e:?}"); let winner_takes_all = equities(&[1.0, 3.0], &[100.0]); assert!(close(winner_takes_all[1], 75.0) && close(winner_takes_all[0], 25.0)); } #[test] fn a_chip_is_worth_less_than_it_costs_to_lose_one() { // The core of ICM: the equity gained by doubling up is less than the equity lost by busting. let (fold, win, lose) = ( equities(&[10.0, 10.0, 20.0, 20.0], &[50.0, 30.0, 20.0])[0], equities(&[20.0, 10.0, 20.0, 20.0], &[50.0, 30.0, 20.0])[0], equities(&[1e-9, 10.0, 20.0, 20.0], &[50.0, 30.0, 20.0])[0], ); assert!(win - fold < fold - lose); } #[test] fn the_required_equity_exceeds_the_chip_break_even_on_the_bubble() { let needed = required_equity(&[20.0, 20.0], &[50.0, 30.0, 20.0], 10.0, 1e-9, 10.0, 11.5).expect("priced"); assert!(needed > 10.0 / 21.5, "{needed}"); } #[test] fn winner_takes_all_has_no_premium() { // Winner-takes-all: money is linear in chips, so money EV equals chip EV. let needed = required_equity(&[20.0, 20.0], &[100.0], 10.0, 1e-9, 10.0, 11.5).expect("priced"); assert!((needed - 10.0 / 21.5).abs() < 1e-6, "{needed}"); } fn record(seat: u8, action: SeenAction, amount: f64) -> ActionRecord { ActionRecord { street: Street::Preflop, seat, action, amount: Some(amount) } } /// Heads-up pot, hero in the big blind (seat 1) facing the small blind's (seat 2) jam of `depth` bb. fn facing_jam(hole: &str, depth: f64) -> Spot { Spot { variant: Variant::Nlhe, big_blind: 1.0, seats: 2, hero_seat: 1, button_seat: Some(2), hero_cards: Card::parse_list(hole).expect("cards"), board: vec![], street: Street::Preflop, pot: depth + 1.0, to_call: depth - 1.0, hero_stack: depth - 1.0, min_raise_to: None, max_raise_to: None, legal: vec![ActionKind::Fold, ActionKind::Call], allowed_sizes: None, villains: vec![Villain { seat: 2, stack: depth, folded: false, profile: None }], seed: 7, history: vec![ record(2, SeenAction::PostSmallBlind, 0.5), record(1, SeenAction::PostBigBlind, 1.0), record(2, SeenAction::AllIn, depth), ], } } fn bubble(depth: f64) -> Turnierlage { Turnierlage { payouts: vec![50.0, 30.0, 20.0], stacks: vec![depth, depth, 2.0 * depth, 2.0 * depth], hero: 0, ante: 0.0, level: 1, } } fn holes() -> Vec { let r: Vec = RANKS.chars().collect(); let mut out = Vec::new(); for (i, a) in r.iter().enumerate() { for b in &r[i..] { if a == b { out.push(format!("{a}s {b}h")); } else { out.push(format!("{a}s {b}s")); out.push(format!("{a}s {b}h")); } } } out } fn calls(choice: Choice) -> bool { matches!(choice, Choice::Call) } #[test] fn bubble_spots_call_narrower_than_cash_at_every_depth() { let mut printed = String::new(); for depth in [5.0, 10.0, 15.0, 25.0] { let (mut cash_calls, mut bubble_calls, mut wider) = (0, 0, 0); let lage = bubble(depth); for hole in holes() { let spot = facing_jam(&hole, depth); let cash = calls(decide(&spot).choice); let money = calls(decide_with(&spot, Some(&lage)).choice); cash_calls += usize::from(cash); bubble_calls += usize::from(money); wider += usize::from(money && !cash); } printed += &format!("{depth} bb: Cash {cash_calls} von 169 Haenden, Bubble {bubble_calls}\n"); assert_eq!(wider, 0, "{depth} bb: the bubble never calls a hand cash folds"); assert!(bubble_calls < cash_calls, "{depth} bb: {bubble_calls} >= {cash_calls}"); } println!("{printed}"); } #[test] fn without_a_situation_nothing_changes() { for hole in ["As Ah", "7s 2h", "Ks Qs"] { let spot = facing_jam(hole, 12.0); assert_eq!(decide(&spot), decide_with(&spot, None)); } } #[test] fn an_unusable_situation_abstains() { let spot = facing_jam("As Ah", 12.0); for lage in [ Turnierlage { payouts: vec![], ..bubble(12.0) }, Turnierlage { hero: 9, ..bubble(12.0) }, Turnierlage { stacks: vec![12.0, f64::NAN], ..bubble(12.0) }, ] { assert_eq!(decide_with(&spot, Some(&lage)).choice, Choice::Abstain); } } #[test] fn no_bet_to_face_costs_no_premium() { let mut spot = facing_jam("As Ah", 12.0); spot.to_call = 0.0; assert_eq!(call_premium(&spot, &bubble(12.0)), Some(0.0)); } #[test] fn only_facing_an_aggressor_costs_a_premium() { // Facing the small blind's all-in (an aggressor in the history): the bubble asks a premium. let defence = facing_jam("Ks Qs", 12.0); assert!(call_premium(&defence, &bubble(12.0)).unwrap() > 0.0); // Same chips to put in, but hero is first in (no aggressor): the call premium is zero - the ICM // there is the open-jam widening, not a narrowing. Without this the defence narrowing bled into // the open-jam and dropped it below the ICM-Nash push (8.11.). let open = Spot { history: vec![record(1, SeenAction::PostBigBlind, 1.0)], ..defence }; assert_eq!(call_premium(&open, &bubble(12.0)), Some(0.0)); } #[test] fn the_defence_width_factor_is_concave_and_floored() { use rechner::icm::risk::{width_factor, MIN_WIDTH}; assert_eq!(width_factor(0.0), 1.0); // no premium, no narrowing (continuous at zero) // Strictly decreasing in the premium. assert!(width_factor(0.05) > width_factor(0.10) && width_factor(0.10) > width_factor(0.15)); // Concave (square root): the first slice of premium narrows MORE than a later, equal slice. assert!(width_factor(0.0) - width_factor(0.05) > width_factor(0.05) - width_factor(0.10)); // Calibrated to the ICM-Nash defence: ~0.47 at a soft bubble premium, ~0.28 at a hard one. assert!((width_factor(0.0797) - 0.466).abs() < 0.02, "{}", width_factor(0.0797)); assert!((width_factor(0.1452) - 0.280).abs() < 0.02, "{}", width_factor(0.1452)); // Never below the floor, even at an extreme premium. assert_eq!(width_factor(1.0), MIN_WIDTH); }