예제 #1
0
def create_df_preflop_hand_distrib(save=False):
    """returns df containing the hand rank distribution for all combinations of preflop hands"""
    global df_preflop_hand_distrib

    print "\n--------------- start create_df_preflop_hand_distrib"
    print 'all preflop hands = \nstart = {}\nend = {}\nnb of elements = {}'.format(
        all_preflop_hands[:10], all_preflop_hands[-10:],
        len(all_preflop_hands))

    print "\nhand rank distribution for all preflop hands"
    t0 = timer()
    preflop_hand_rank_distrib = np.zeros([1 + len(all_preflop_hands), 7462],
                                         dtype=np.int32)

    sys.stdout.write('\rk=%3d / %3d' % (1, 1 + len(all_preflop_hands)))
    sys.stdout.flush()
    N = C(52, 7)
    h1_array_all_seven_cards = create_array_all_seven_fast()
    h1_rank7 = EvalSeven.get_seven_rank_fast(
        h1_array_all_seven_cards, keys.CARD_FLUSH_KEY, keys.CARD_FACE_KEY,
        keys.CARD_SUIT, keys.SUIT_MASK, keys.SUIT_BIT_SHIFT,
        EvalSeven.flush_rank, EvalSeven.face_rank, EvalSeven.flush_suit)
    preflop_hand_rank_distrib[0, :] = np.bincount(h1_rank7)

    for k, hand in enumerate(all_preflop_hands):
        sys.stdout.write('\rk = %3d / %3d' %
                         (2 + k, 1 + len(all_preflop_hands)))
        sys.stdout.flush()
        hand_no = hand_str_to_no(hand)
        N = C(52 - 2, 5)
        array_five_cards = all_n_cards_given_m_cards_fast(5, hand_no, N)
        h1_cards = np.ones([N, 2], dtype=np.int32) * hand_no
        h1_array_all_seven_cards = np.concatenate((h1_cards, array_five_cards),
                                                  axis=1)
        h1_array_all_seven_cards = np.array(h1_array_all_seven_cards,
                                            dtype=np.int32)
        h1_rank7 = EvalSeven.get_seven_rank_fast(
            h1_array_all_seven_cards, keys.CARD_FLUSH_KEY, keys.CARD_FACE_KEY,
            keys.CARD_SUIT, keys.SUIT_MASK, keys.SUIT_BIT_SHIFT,
            EvalSeven.flush_rank, EvalSeven.face_rank, EvalSeven.flush_suit)
        preflop_hand_rank_distrib[1 + k, :] = np.bincount(h1_rank7)

    df = pd.DataFrame(data=preflop_hand_rank_distrib.T,
                      columns=['NoHand'] + all_preflop_hands,
                      index=np.arange(7462))
    t1 = timer()
    print '\ndf_preflop_hand_distrib time = {:8.4f} s'.format(t1 - t0)
    if (save):
        df.to_pickle(os.path.join('Tables', 'df_preflop_hand_distrib.pd'))
        print '{} saved to disk as {}'.format(
            'df_preflop_hand_distrib',
            os.path.join('Tables', 'df_preflop_hand_distrib.pd'))
        df.to_csv(os.path.join('Tables', 'df_preflop_hand_distrib.csv'),
                  index=False)
        print '{} saved to disk as {}'.format(
            'df_preflop_hand_distrib',
            os.path.join('Tables', 'df_preflop_hand_distrib.csv'))

    print "--------------- end create_df_preflop_hand_distrib"
    return df
예제 #2
0
def create_df_preflop_hand_distrib(save=False):
	"""returns df containing the hand rank distribution for all combinations of preflop hands"""
	global df_preflop_hand_distrib

	print "\n--------------- start create_df_preflop_hand_distrib"
	print 'all preflop hands = \nstart = {}\nend = {}\nnb of elements = {}'.format(all_preflop_hands[:10], all_preflop_hands[-10:], len(all_preflop_hands))

	print "\nhand rank distribution for all preflop hands"
	t0 = timer()
	preflop_hand_rank_distrib = np.zeros([1+len(all_preflop_hands), 7462], dtype=np.int32)

	sys.stdout.write('\rk=%3d / %3d' % (1, 1+len(all_preflop_hands)))
	sys.stdout.flush()
	N = C(52, 7)
	h1_array_all_seven_cards = create_array_all_seven_fast()
	h1_rank7 = EvalSeven.get_seven_rank_fast(h1_array_all_seven_cards, keys.CARD_FLUSH_KEY, keys.CARD_FACE_KEY, keys.CARD_SUIT, keys.SUIT_MASK, keys.SUIT_BIT_SHIFT, EvalSeven.flush_rank, EvalSeven.face_rank, EvalSeven.flush_suit)
	preflop_hand_rank_distrib[0, :] = np.bincount(h1_rank7)

	for k, hand in enumerate(all_preflop_hands):
		sys.stdout.write('\rk = %3d / %3d' % (2+k, 1+len(all_preflop_hands)))
		sys.stdout.flush()
		hand_no = hand_str_to_no(hand)
		N = C(52-2, 5)
		array_five_cards = all_n_cards_given_m_cards_fast(5, hand_no, N)
		h1_cards = np.ones([N, 2], dtype=np.int32)*hand_no
		h1_array_all_seven_cards = np.concatenate((h1_cards, array_five_cards), axis=1)
		h1_array_all_seven_cards = np.array(h1_array_all_seven_cards, dtype=np.int32)
		h1_rank7 = EvalSeven.get_seven_rank_fast(h1_array_all_seven_cards, keys.CARD_FLUSH_KEY, keys.CARD_FACE_KEY, keys.CARD_SUIT, keys.SUIT_MASK, keys.SUIT_BIT_SHIFT, EvalSeven.flush_rank, EvalSeven.face_rank, EvalSeven.flush_suit)
		preflop_hand_rank_distrib[1+k, :] = np.bincount(h1_rank7)

	df = pd.DataFrame(data=preflop_hand_rank_distrib.T, columns=['NoHand']+all_preflop_hands, index=np.arange(7462))
	t1 = timer()
	print '\ndf_preflop_hand_distrib time = {:8.4f} s'.format(t1-t0)
	if (save):
		df.to_pickle(os.path.join('Tables', 'df_preflop_hand_distrib.pd'))
		print '{} saved to disk as {}'.format('df_preflop_hand_distrib', os.path.join('Tables', 'df_preflop_hand_distrib.pd'))
		df.to_csv(os.path.join('Tables', 'df_preflop_hand_distrib.csv'), index=False)
		print '{} saved to disk as {}'.format('df_preflop_hand_distrib', os.path.join('Tables', 'df_preflop_hand_distrib.csv'))

	print "--------------- end create_df_preflop_hand_distrib"
	return df
예제 #3
0
def create_df_hand_seven():
	"""create summary dataframe for all seven card hands"""
	hand_type = EvalFive.hand_type

	uniq, idx = np.unique(hand_type, return_index=True)
	sorted_idx = np.sort(idx)[::-1]
	uniq_hand_type = hand_type[sorted_idx]
	min_rank_5 = sorted_idx
	max_rank_5 = np.r_[hand_type.size, sorted_idx[:-1]]-1
	nb_rank_5 = max_rank_5-min_rank_5+1

	array_all_seven_cards = create_array_all_seven_fast()
	all_hand_rank = EvalSeven.get_seven_rank_fast(array_all_seven_cards, keys.CARD_FLUSH_KEY, keys.CARD_FACE_KEY, keys.CARD_SUIT, keys.SUIT_MASK, keys.SUIT_BIT_SHIFT, EvalSeven.flush_rank, EvalSeven.face_rank, EvalSeven.flush_suit)

	bins = np.r_[sorted_idx[::-1], max_rank_5[0]]
	nb_occurence = np.histogram(all_hand_rank, bins)[0][::-1]
	proba = 1.0*nb_occurence/np.sum(nb_occurence)
	odd = 1.0/proba
	cum_proba = np.cumsum(proba)

	bins = np.r_[sorted_idx[::-1], max_rank_5[0]+1][::-1]
	digitized = np.digitize(all_hand_rank, bins)
	min_rank_7 = np.zeros(uniq.size, dtype=np.int32)
	max_rank_7 = np.zeros(uniq.size, dtype=np.int32)
	nb_rank_7 = np.zeros(uniq.size, dtype=np.int32)
	for k in range(1, uniq.size+1):
		subset = all_hand_rank[digitized==k]
		min_rank_7[k-1] = subset.min()
		max_rank_7[k-1] = subset.max()
		nb_rank_7[k-1] = np.unique(subset).size

	proba_exact = map(lambda x : Rational(x, array_all_seven_cards.shape[0]), nb_occurence)

	df = pd.DataFrame(data=np.array([uniq_hand_type, nb_rank_5, nb_rank_7, min_rank_5, min_rank_7, max_rank_5, max_rank_7, nb_occurence, proba_exact, odd, proba, cum_proba]).T,
			columns=['HandType', 'NbHands_5', 'NbHands_7', 'MinRank_5', 'MinRank_7', 'MaxRank_5', 'MaxRank_7', 'NbOccurence', 'ProbaExact', 'Odd', 'ProbaApprox', 'CumulativeProba'])
	# df.loc[uniq.size] = ['All', nb_rank_5.sum(), nb_rank_7.sum(), min_rank_5.min(), min_rank_7.min(), max_rank_5.max(), max_rank_7.max(), array_all_seven_cards.shape[0], 1.0, 1.0, 1.0, np.nan]

	s = pd.Series(data=['All', nb_rank_5.sum(), nb_rank_7.sum(), min_rank_5.min(), min_rank_7.min(), max_rank_5.max(), max_rank_7.max(), array_all_seven_cards.shape[0], 1.0, 1.0, 1.0, np.nan],
			index=['HandType', 'NbHands_5', 'NbHands_7', 'MinRank_5', 'MinRank_7', 'MaxRank_5', 'MaxRank_7', 'NbOccurence', 'ProbaExact', 'Odd', 'ProbaApprox', 'CumulativeProba'], name=uniq.size)
	df = df.append(s)

	df.to_pickle(os.path.join('Tables', 'df_hand_seven.pd'))
	return df
예제 #4
0
def preflop_two_hand_equity((h1, h2), verbose=False):
	"""returns [h1 nb win, nb ties, h2 nb wins] for all suit combinations for 2 preflop hands h1, h2 input as 'Q6o', '52o'"""
	if (verbose):
		print 'preflop hands : {}'.format((h1, h2))
		t0 = timer()
	two_hand_no, freq = two_hand_to_no(h1, h2)
	equity = np.zeros([two_hand_no.shape[0], 4], dtype=np.int32)
	for k, p in enumerate(two_hand_no):

		N = C(52-4, 5)
		array_five_cards = all_n_cards_given_m_cards_fast(5, p, N)

		h1_cards = np.ones([N, 2], dtype=np.int32)*[p[0], p[1]]
		h1_array_all_seven_cards = np.concatenate((h1_cards, array_five_cards), axis=1)
		h1_rank7 = EvalSeven.get_seven_rank_fast(h1_array_all_seven_cards, keys.CARD_FLUSH_KEY, keys.CARD_FACE_KEY, keys.CARD_SUIT, keys.SUIT_MASK, keys.SUIT_BIT_SHIFT, EvalSeven.flush_rank, EvalSeven.face_rank, EvalSeven.flush_suit)
		h2_cards = np.ones([N, 2], dtype=np.int32)*[p[2], p[3]]
		h2_array_all_seven_cards = np.concatenate((h2_cards, array_five_cards), axis=1)
		h2_rank7 = EvalSeven.get_seven_rank_fast(h2_array_all_seven_cards, keys.CARD_FLUSH_KEY, keys.CARD_FACE_KEY, keys.CARD_SUIT, keys.SUIT_MASK, keys.SUIT_BIT_SHIFT, EvalSeven.flush_rank, EvalSeven.face_rank, EvalSeven.flush_suit)
		diff_rank7 = h1_rank7-h2_rank7
		h1_win, tie, h2_win = (diff_rank7>0).sum(), (diff_rank7==0).sum(), (diff_rank7<0).sum()
		equity[k, :] = np.array([h1_win, tie, h2_win, freq[k]])
		if (verbose):
			print 'suit combination #{}'.format(k)
			print '\t', two_hand_no_to_char(p)
			print '\tfrequency = {}'.format(freq[k])
			print '\t{}\t{}\t{}'.format('h1#wins', '#ties', 'h2#wins')
			print '\t{}\t{}\t{}'.format(h1_win, tie, h2_win)
			total = np.array([h1_win, tie, h2_win]).sum()
			print '\t{:5.3f}%\t{:5.3f}%\t{:5.3f}%'.format(100.0*h1_win/total, 100.0*tie/total, 100.0*h2_win/total)
예제 #5
0
def create_df_hand_seven():
    """create summary dataframe for all seven card hands"""
    hand_type = EvalFive.hand_type

    uniq, idx = np.unique(hand_type, return_index=True)
    sorted_idx = np.sort(idx)[::-1]
    uniq_hand_type = hand_type[sorted_idx]
    min_rank_5 = sorted_idx
    max_rank_5 = np.r_[hand_type.size, sorted_idx[:-1]] - 1
    nb_rank_5 = max_rank_5 - min_rank_5 + 1

    array_all_seven_cards = create_array_all_seven_fast()
    all_hand_rank = EvalSeven.get_seven_rank_fast(
        array_all_seven_cards, keys.CARD_FLUSH_KEY, keys.CARD_FACE_KEY,
        keys.CARD_SUIT, keys.SUIT_MASK, keys.SUIT_BIT_SHIFT,
        EvalSeven.flush_rank, EvalSeven.face_rank, EvalSeven.flush_suit)

    bins = np.r_[sorted_idx[::-1], max_rank_5[0]]
    nb_occurence = np.histogram(all_hand_rank, bins)[0][::-1]
    proba = 1.0 * nb_occurence / np.sum(nb_occurence)
    odd = 1.0 / proba
    cum_proba = np.cumsum(proba)

    bins = np.r_[sorted_idx[::-1], max_rank_5[0] + 1][::-1]
    digitized = np.digitize(all_hand_rank, bins)
    min_rank_7 = np.zeros(uniq.size, dtype=np.int32)
    max_rank_7 = np.zeros(uniq.size, dtype=np.int32)
    nb_rank_7 = np.zeros(uniq.size, dtype=np.int32)
    for k in range(1, uniq.size + 1):
        subset = all_hand_rank[digitized == k]
        min_rank_7[k - 1] = subset.min()
        max_rank_7[k - 1] = subset.max()
        nb_rank_7[k - 1] = np.unique(subset).size

    proba_exact = map(lambda x: Rational(x, array_all_seven_cards.shape[0]),
                      nb_occurence)

    df = pd.DataFrame(data=np.array([
        uniq_hand_type, nb_rank_5, nb_rank_7, min_rank_5, min_rank_7,
        max_rank_5, max_rank_7, nb_occurence, proba_exact, odd, proba,
        cum_proba
    ]).T,
                      columns=[
                          'HandType', 'NbHands_5', 'NbHands_7', 'MinRank_5',
                          'MinRank_7', 'MaxRank_5', 'MaxRank_7', 'NbOccurence',
                          'ProbaExact', 'Odd', 'ProbaApprox', 'CumulativeProba'
                      ])
    # df.loc[uniq.size] = ['All', nb_rank_5.sum(), nb_rank_7.sum(), min_rank_5.min(), min_rank_7.min(), max_rank_5.max(), max_rank_7.max(), array_all_seven_cards.shape[0], 1.0, 1.0, 1.0, np.nan]

    s = pd.Series(data=[
        'All',
        nb_rank_5.sum(),
        nb_rank_7.sum(),
        min_rank_5.min(),
        min_rank_7.min(),
        max_rank_5.max(),
        max_rank_7.max(), array_all_seven_cards.shape[0], 1.0, 1.0, 1.0, np.nan
    ],
                  index=[
                      'HandType', 'NbHands_5', 'NbHands_7', 'MinRank_5',
                      'MinRank_7', 'MaxRank_5', 'MaxRank_7', 'NbOccurence',
                      'ProbaExact', 'Odd', 'ProbaApprox', 'CumulativeProba'
                  ],
                  name=uniq.size)
    df = df.append(s)

    df.to_pickle(os.path.join('Tables', 'df_hand_seven.pd'))
    return df
예제 #6
0
    """returns [h1 nb win, nb ties, h2 nb wins] for all suit combinations for 2 preflop hands h1, h2 input as 'Q6o', '52o'"""
    if (verbose):
        print 'preflop hands : {}'.format((h1, h2))
        t0 = timer()
    two_hand_no, freq = two_hand_to_no(h1, h2)
    equity = np.zeros([two_hand_no.shape[0], 4], dtype=np.int32)
    for k, p in enumerate(two_hand_no):

        N = C(52 - 4, 5)
        array_five_cards = all_n_cards_given_m_cards_fast(5, p, N)

        h1_cards = np.ones([N, 2], dtype=np.int32) * [p[0], p[1]]
        h1_array_all_seven_cards = np.concatenate((h1_cards, array_five_cards),
                                                  axis=1)
        h1_rank7 = EvalSeven.get_seven_rank_fast(
            h1_array_all_seven_cards, keys.CARD_FLUSH_KEY, keys.CARD_FACE_KEY,
            keys.CARD_SUIT, keys.SUIT_MASK, keys.SUIT_BIT_SHIFT,
            EvalSeven.flush_rank, EvalSeven.face_rank, EvalSeven.flush_suit)
        h2_cards = np.ones([N, 2], dtype=np.int32) * [p[2], p[3]]
        h2_array_all_seven_cards = np.concatenate((h2_cards, array_five_cards),
                                                  axis=1)
        h2_rank7 = EvalSeven.get_seven_rank_fast(
            h2_array_all_seven_cards, keys.CARD_FLUSH_KEY, keys.CARD_FACE_KEY,
            keys.CARD_SUIT, keys.SUIT_MASK, keys.SUIT_BIT_SHIFT,
            EvalSeven.flush_rank, EvalSeven.face_rank, EvalSeven.flush_suit)
        diff_rank7 = h1_rank7 - h2_rank7
        h1_win, tie, h2_win = (diff_rank7 > 0).sum(), (
            diff_rank7 == 0).sum(), (diff_rank7 < 0).sum()
        equity[k, :] = np.array([h1_win, tie, h2_win, freq[k]])
        if (verbose):
            print 'suit combination #{}'.format(k)
            print '\t', two_hand_no_to_char(p)