Jul-03-2024, 11:48 AM
Hi everybody,
I have some questions regarding a code experiment I would like to adapt.
This code was done to stimulate a blackjack experiment. Basically, a deck of card was created and we calculated the distribution of the sum of cards chosen.
The code is done with remplacement of cards. I would like to do the same basically but with no replacement.
The first function calculates the distribution of probabilities for the sum of cards chosen. The second determines whether it is a winning game or not. And the last one determines at each stage of the game the probability to win depending on the number of cards that are still to be drawn.
This is the original code :
Roxane
I have some questions regarding a code experiment I would like to adapt.
This code was done to stimulate a blackjack experiment. Basically, a deck of card was created and we calculated the distribution of the sum of cards chosen.
The code is done with remplacement of cards. I would like to do the same basically but with no replacement.
The first function calculates the distribution of probabilities for the sum of cards chosen. The second determines whether it is a winning game or not. And the last one determines at each stage of the game the probability to win depending on the number of cards that are still to be drawn.
This is the original code :
#Import useful packages import numpy as np import pandas as pd def gen_distribution(thegame): nmax = thegame['nmax'] xs, counts = np.unique(thegame['deck'], return_counts=True) ps = counts / sum(counts) allpsum = [pd.Series(ps, index=xs)] for n in range(2, nmax + 1): sums = np.arange(min(xs) * n, max(xs) * n + 1) psum = pd.Series(np.zeros(len(sums)), index=sums) psum0 = allpsum[n - 2] for kx, x in enumerate(xs): for x0 in psum0.index.values: psum[x + x0] += psum0[x0] * ps[kx] allpsum.append(psum) return allpsum def dowin(cs, thegame): ub, lb = thegame['ublb'] badnow = (sum(cs) >= ub) or (sum(cs) <= lb) outcome = 0 if badnow: if thegame['isbust'] or (not thegame['isbust'] and len(cs) == thegame['nmax']): outcome = -1 else: if len(cs) == thegame['nmax']: outcome = 1 return outcome def gen_pwin(thegame): nmax = thegame['nmax'] ub, lb = thegame['ublb'] isbust = thegame['isbust'] xs, counts = np.unique(thegame['deck'], return_counts=True) ps = counts / sum(counts) allpsum = gen_distribution(thegame) allpwin = [[] for _ in range(nmax + 1)] nmore = 0 N = nmax sums = np.arange(min(xs) * N, max(xs) * N + 1) pwin = [int(k < ub and k > lb) for k in sums] allpwin[nmore] = pd.Series(pwin, index=sums) for nmore in range(1, nmax + 1): N = nmax - nmore sums = np.arange(min(xs) * N, max(xs) * N + 1) pwin = [] for s in sums: if isbust: if s >= ub or s <= lb: pwin.append(0) continue pw = 0 for kx, xnext in enumerate(xs): pw += ps[kx] * allpwin[nmore - 1][s + xnext] pwin.append(pw) else: psum = allpsum[nmore - 1] thisp = max(min(sum([psum[smr] for smr in psum.index.values if s + smr < ub and s + smr > lb]), 1), 0) pwin.append(thisp) allpwin[nmore] = pd.Series(pwin, index=sums) thegame['allpwin'] = allpwin returnThank you in advance

Roxane