Skip to content

Instantly share code, notes, and snippets.

@axs
Last active May 17, 2018 22:30
Show Gist options
  • Save axs/fe6045e1be1bf5d1e446 to your computer and use it in GitHub Desktop.
Save axs/fe6045e1be1bf5d1e446 to your computer and use it in GitHub Desktop.
import numpy as np
from fractions import Fraction
import pandas as pd
from odo import odo
def dec2percent (dec):
"""dec2percent(2)
"""
return np.reciprocal(float(dec))
def fractional2percent (fractional):
"""fractional2percent(Fraction(3,1))
"""
return float(fractional.denominator) /(fractional.denominator + fractional.numerator)
def moneyline2percent(moneyline):
percent = 0.0
if np.sign(moneyline) < 0:
percent = (-1*moneyline) / ( -1.0*moneyline+100 )
else:
percent = 100 / (moneyline + 100.0)
return percent
def percent2dec (percent):
"""percent2dec(.5)
"""
return np.reciprocal(percent)
def percent2fractional (percent):
return Fraction(np.reciprocal(percent)-1).limit_denominator(100)
def percent2moneyline(percent):
if percent > .5:
return (percent / (1 - percent) ) * -100
else:
return ( (1 - percent) / percent ) * 100
if __name__ == '__main__':
"""
parimutuel betting example with odds, payouts for win, place, show
"""
df = pd.DataFrame({
'betters': ['fred', 'barney' ,'wilma', 'tom' ,'john' ,'jerry','tom','mo','larry','eddie']
,'bets' : [100 ,200, 100, 50 ,20 ,30 ,100,20,40,60]
,'horse' : ['cigar' , 'brownie' , 'neptune', 'neptune','cigar','delroy','cigar','delroy','neptune','neptune']
,'type' : ['win', 'win', 'win' ,'win', 'win', 'win' , 'place', 'place', 'place','place']
})
pdf = df[df.type=='place']
sdf = df[df.type=='show']
wdf = df[df.type=='win']
bets = df[['bets','betters','horse','type']]
x= wdf.groupby('horse',as_index=False)['bets'].sum()
wdf= wdf.merge(x,on=['horse'])
wdf['pcttotal'] = wdf.bets_x / wdf.bets_y
wdf['payout'] = wdf.bets_x.sum() * wdf.pcttotal
wdf = wdf.rename(columns={'bets_x':'bets','bets_y': 'totamtperhorse'})
wdf['decodds'] = wdf.bets.sum() / wdf.totamtperhorse
wdf['percent'] = wdf.decodds.apply(np.reciprocal)
wdf['moneyline'] = wdf.percent.apply(percent2moneyline)
wdf['fractional'] = wdf.percent.apply(percent2fractional)
wdf['fractional']=wdf.fractional.apply(str)
tote = wdf[['horse','fractional','decodds','percent','moneyline','totamtperhorse']].drop_duplicates()
def placeOrshow (pdf,*args):
"""placeOrshow(pdf,'cigar','neptune')
"""
pool = pdf.bets.sum()
z= pdf.groupby('horse',as_index=False)['bets'].sum()
pdf= pdf.merge(z,on=['horse'])
pdf= pdf[pdf.horse.isin(args)]
profit = pool - pdf.bets_x.sum()
pdf['pcttotal'] = pdf.bets_x / pdf.bets_y
split = len(args) - len(set(args).difference(set(pdf.horse)) )
pdf['payout'] = profit / split * pdf.pcttotal + pdf.bets_x
return [pool,pdf]
odo(tote,'sqlite:///bettingwindow.db::toteboard')
odo(bets,'sqlite:///bettingwindow.db::bets')
print placeOrshow(pdf,'cigar','neptune')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment