Skip to content

Instantly share code, notes, and snippets.

View JeffSackmann's full-sized avatar

Jeff Sackmann JeffSackmann

View GitHub Profile
@JeffSackmann
JeffSackmann / reseeder.py
Created September 3, 2012 13:44
reseeder takes a list of players (perhaps an existing draw) and generates a (new) draw according to non-strict seeding rules.
import random
from operator import itemgetter
def insertSeedOrNonseed(ndraw, seeds, nonseeds, sj, nj):
if sj < len(seeds):
ndraw.append(seeds[sj])
sj += 1
else:
ndraw.append(nonseeds[nj])
nj += 1
@JeffSackmann
JeffSackmann / fiveSetProb.py
Created September 1, 2012 17:42
Given probability of winning a best-of-three-set match and the assumption that sets are independent, output the probability of winning a best-of-five-set match
## given probability of winning a best-of-three-set match and the assumption that sets are independent,
## output the probability of winning a best-of-five-set match
##One way to find the probability of winning an n-set match is to start with the probability of winning
##a single set. If we have an estimated probability of winning a best-of-three, e.g. from betting odds,
##we need to work backwards to get the probability of winning a single set.
##
##If x is p(set win), the probability of winning a three-setter is:
## x^2 + 2(x^2)(1-x)
## x^2 is the p(winning in straight sets)
@JeffSackmann
JeffSackmann / wpaCalc.py
Created February 7, 2011 22:24
Find win expectancy and volatility given inning, out, base, run situation.
## Find win expectancy and volatility given inning, out, base, run situation.
## no. of runs that score with HR in diff. base situations
baseHr = {1: 1,
2: 2,
3: 2,
4: 3,
5: 2,
6: 3,
7: 3,
@JeffSackmann
JeffSackmann / aussie_draw_2011.csv
Created January 16, 2011 16:30
run simulations of a single-elimination tournament
Nadal 1 12390
Daniel 564
Sweeting Q 486
Gimeno-Traver 844
Tomic W 239
Chardy 960
Falla 540
Lopez F 31 1310
Isner 20 1850
Serra 711
@JeffSackmann
JeffSackmann / generatePitchingMarcels.py
Created January 14, 2011 18:34
Generate a full season's worth of pitching Marcel projections from past years' stats
## Generate a full season's worth of pitching Marcel projections from past years' stats
from createTuple import createTuple ## gist: 778481
from writeMatrixCSV import writeMatrixCSV ## gist: 778484
def makePitTable(r):
for stat in ['AB', 'H', 'D', 'T', 'HR', 'SO', 'BB', 'SF', 'HP', 'CI', 'IPouts', 'R']:
if stat in r: pass
else: r[stat] = 0
ab = 0.9*r['IPouts'] + r['H']
@JeffSackmann
JeffSackmann / generateBattingMarcels.py
Created January 14, 2011 18:32
Generate a full season's worth of batting Marcel projections from past years' stats
## Generate a full season's worth of batting Marcel projections from past years' stats
from createTuple import createTuple ## gist: 778481
from writeMatrixCSV import writeMatrixCSV ## gist: 778484
def makeBatTable(r):
for stat in ['AB', 'H', 'D', 'T', 'HR', 'SO', 'BB', 'SF', 'HP', 'CI']:
if stat in r: pass
else: r[stat] = 0
if r['AB'] == 0:
@JeffSackmann
JeffSackmann / writeMatrixCSV.py
Created January 13, 2011 20:01
turn a 2-d python matrix (list of lists) into a .csv file
## turn a 2-d python matrix (list of lists) into a .csv file
## see also gist: 778481 to reverse the process
def writeMatrixCSV(mx, fname):
## mx is a 2-d python matrix
## fname is the desired output filename
f = open(fname, 'w')
for r in mx:
tx = ''
@JeffSackmann
JeffSackmann / createTuple.py
Created January 13, 2011 19:59
turn a .csv file into a 2-dimensional python matrix (an array of arrays)
# turn a .csv file into a 2-dimensional python matrix (a list of lists)
# this will not make you happy if cells have commas in them, however escaped they may be
# reverse the process (go from matrix to .csv) with gist: 778484
def fixText(text):
row = []
z = text.find(',')
if z == 0: row.append('')
@JeffSackmann
JeffSackmann / tennisMatchProbability.py
Created January 13, 2011 15:56
calculates probability of winning a tennis match from any given score dependent on the skill levels of the two players
## calculates probability of winning a tennis match from any given score dependent on the skill levels
## of the two players
## requires functions in other gists:
## gameProb: https://gist.github.com/768862
## setGeneral: https://gist.github.com/776986
## tiebreakProb: https://gist.github.com/776875
def fact(x):
if x in [0, 1]: return 1
@JeffSackmann
JeffSackmann / tennisSetProbability.py
Created January 12, 2011 21:58
calculate the probability of the current server winning a 6-game, tiebreak set, given prob. of server winning any given service point (s) or return point (u), and the current game score (v, w)
## calculate the probability of the current server winning
## a 6-game, tiebreak set, given prob. of server winning any
## given service point (s) or return point (u), and the current
## game score (v, w)
## some results:
## http://summerofjeff.wordpress.com/2010/12/02/single-set-win-expectancy-tables/
def fact(x):
if x in [0, 1]: return 1