def getE8Bracket(model, year): bracket = np.repeat(-1, 63) for region in range(4): s1 = getE8SeedBottom(year, model) s2 = getE8SeedTop(year, model) region_bracket = fixRegionalBits(s1) region_bracket_2 = fixRegionalBits(s2) region_bracket[region_bracket_2 != -1] = region_bracket_2[ region_bracket_2 != -1] region_bracket[-1] = -1 bracket[region * 15:region * 15 + 15] = region_bracket # ... r1_probs = [getP(model, year, i) for i in range(8)] counts = [np.random.binomial(4, r1_prob) for r1_prob in r1_probs] positions = [ np.random.choice([0, 1, 2, 3], count, replace=False) for count in counts ] for bit, pos in enumerate(positions): for region in range(4): if bracket[region * 15 + bit] != -1: continue if region in pos: bracket[region * 15 + bit] = 1 else: bracket[region * 15 + bit] = 0 return fillEmptySpaces(bracket, model, year)
def generateBracket(model, year): fmt = model.get('format', 'TTT') bracket = [] # random.seed() endModel = 'None' if 'endModel' in model: endModel = model['endModel'] e8Seeds = [] if endModel == 'E8': for i in range(4): e8Seeds.append(getE8SeedTop(year)) e8Seeds.append(getE8SeedBottom(year)) else: e8Seeds = [-1, -1, -1, -1, -1, -1, -1, -1] f4Seeds = [] if endModel == 'F4_1': for i in range(4): f4Seeds.append(getF4SeedTogether(year)) elif endModel == 'F4_2': for i in range(4): f4Seeds.append(getF4SeedSplit(year)) else: f4Seeds = [-1, -1, -1, -1] ncgSeeds = [-1, -1] if 'Rev' in endModel: champion = getChampion(year) runnerUp = getRunnerUp(year) champRegion = int(floor(random.random() * 4)) champHalf = champRegion / 2 ruRegion = int(floor(random.random() * 2)) if champHalf == 0: ncgSeeds = [champion, runnerUp] else: ncgSeeds = [runnerUp, champion] ffrRegion = 1 - ruRegion if champRegion < 2: ruRegion += 2 ffrRegion += 2 ffcRegion = 1 - champRegion else: ffcRegion = 5 - champRegion f4Seeds[champRegion] = champion f4Seeds[ruRegion] = runnerUp else: champRegion = -1 ruRegion = -1 if endModel == 'Rev_4': f4Seeds[ffcRegion] = getF4SeedTogether(year) f4Seeds[ffrRegion] = getF4SeedTogether(year) # Loop through regional rounds R64, R32, and S16 for region in range(4): seeds = [1, 16, 8, 9, 5, 12, 4, 13, 6, 11, 3, 14, 7, 10, 2, 15] for roundNum in range(1, 5): numGames = int(len(seeds) / 2) newSeeds = [] for gameNum in range(numGames): s1 = seeds[2 * gameNum] s2 = seeds[2 * gameNum + 1] # Force any fixed F4/E8 seeds to make it through s1Wins = (s1 == f4Seeds[region]) or ( (roundNum < 4) and ((s1 == e8Seeds[2 * region]) or (s1 == e8Seeds[2 * region + 1]))) s2Wins = (s2 == f4Seeds[region]) or ( (roundNum < 4) and ((s2 == e8Seeds[2 * region]) or (s2 == e8Seeds[2 * region + 1]))) if s1Wins: p = 1 elif s2Wins: p = 0 else: p = getP(s1, s2, model, year, roundNum) if random.random() <= p: bracket.append(1 if fmt == 'TTT' else ( 1 if s1 < s2 else 0)) newSeeds.append(s1) else: bracket.append(0 if fmt == 'TTT' else ( 1 if s2 < s1 else 0)) newSeeds.append(s2) seeds = newSeeds f4Seeds[region] = seeds[0] bracket = bracket + [-1, -1, -1] # Round 5: for gameNum in range(2): s1 = f4Seeds[2 * gameNum] s2 = f4Seeds[2 * gameNum + 1] if 'Rev' in endModel: if (2 * gameNum == champRegion) or (2 * gameNum == ruRegion): p = 1 elif (2 * gameNum + 1 == champRegion) or (2 * gameNum + 1 == ruRegion): p = 0 else: p = getP(s1, s2, model, year, 5) else: p = getP(s1, s2, model, year, 5) if random.random() <= p: bracket[60 + gameNum] = 1 ncgSeeds[gameNum] = s1 else: bracket[60 + gameNum] = 0 ncgSeeds[gameNum] = s2 # Round 6: s1 = ncgSeeds[0] s2 = ncgSeeds[1] if 'Rev' in endModel: if champHalf == 0: p = 1 else: p = 0 else: p = getP(s1, s2, model, year, 6) if random.random() <= p: bracket[-1] = 1 else: bracket[-1] = 0 # assert len(bracket) == 63 # assert np.count_nonzero(np.array(bracket) == -1) == 0 return bracket
# print year # pprint(freqs) # print '\n' # Test 3: # Sample many Elite Eight seeds (top half) for each year, # then visually compare the results to the actual distribution. print '--- Elite Eight seeds (top half) ---' for year in range(2019, 2012, -1): seeds = [1, 4, 5, 8, 9, 12, 13, 16] seedCounts = [0 for i in range(len(seeds))] freqs = dict(zip(seeds, seedCounts)) for trialNum in range(nTrials): seed = getE8SeedTop(year) freqs[seed] += 1 # Scale back down to the appropriate total totalCount = 4 * (year - 1985) for seed in seeds: freqs[seed] = freqs[seed] * 1.0 / nTrials * totalCount print year pprint(freqs) print '\n' # Test 4: # Sample many Elite Eight seeds (bottom half) for each year, # then visually compare the results to the actual distribution.
def generateBracket(model, year): bracket = [] regionWinners = [] random.seed() isEliteEightSampleModel = "True" in model['isEliteEightSampleModel'] e8Seeds = [] if isEliteEightSampleModel: for i in range(4): e8Seeds.append(getE8SeedTop(year)) e8Seeds.append(getE8SeedBottom(year)) else: e8Seeds = [-1, -1, -1, -1, -1, -1, -1, -1] genE8seeds = [] # Loop through regional rounds R64, R32, and S16 for region in range(4): seeds = [1, 16, 8, 9, 5, 12, 4, 13, 6, 11, 3, 14, 7, 10, 2, 15] for roundNum in range(1, 4): # Don't do Elite Eight games yet numGames = int(len(seeds) / 2) newSeeds = [] for gameNum in range(numGames): s1 = seeds[2 * gameNum] s2 = seeds[2 * gameNum + 1] # Force the region winner to make it through if (s1 == e8Seeds[2 * region]) or (s1 == e8Seeds[2 * region + 1]): p = 1 elif (s2 == e8Seeds[2 * region]) or (s2 == e8Seeds[2 * region + 1]): p = 0 else: p = getP(s1, s2, model, year, roundNum) if random.random() <= p: bracket.append(1) newSeeds.append(s1) else: bracket.append(0) newSeeds.append(s2) seeds = newSeeds for seed in seeds: genE8seeds.append(seed) e8Seeds = genE8seeds # Round 4: f4seeds = [] for gameNum in range(4): s1 = e8Seeds[2 * gameNum] s2 = e8Seeds[2 * gameNum + 1] p = getP(s1, s2, model, year, 4) if random.random() <= p: bracket.append(1) f4seeds.append(s1) else: bracket.append(0) f4seeds.append(s2) # Round 5: semiFinalists = [] for gameNum in range(2): s1 = f4seeds[2 * gameNum] s2 = f4seeds[2 * gameNum + 1] p = getP(s1, s2, model, year, 5) if random.random() <= p: bracket.append(1) semiFinalists.append(s1) else: bracket.append(0) semiFinalists.append(s2) # Round 6: s1 = semiFinalists[0] s2 = semiFinalists[1] p = getP(s1, s2, model, year, 6) if random.random() <= p: bracket.append(1) else: bracket.append(0) return bracket