def getNumFreq(lead): inLead=lt.inLead(lead) changes=lt.numChanges(inLead, lead) print np.mean(changes) h,b=np.histogram(changes, max(changes)-min(changes)) hcorr=h/float(len(changes)) return hcorr,b
def getLastLeadFreq(lead, bins): inLead = lt.inLead(lead) s = lt.lastChange(inLead) h, b = np.histogram(s, bins) binw = arcs.getBinWidth(bins) hcorr = h / (binw * len(s)) return hcorr, b
def getLastLeadFreq(lead, bins): inLead=lt.inLead(lead) s=lt.lastChange(inLead) h,b=np.histogram(s, bins) binw=arcs.getBinWidth(bins) hcorr=h/(binw*len(s)) return hcorr, b
def getNumFreq(lead): inLead = lt.inLead(lead) changes = lt.numChanges(inLead, lead) print np.mean(changes) h, b = np.histogram(changes, max(changes) - min(changes)) hcorr = h / float(len(changes)) return hcorr, b
def plotBathtub(sport): if sport=='NBA': scope=2880 seasons='2002-2010' bins=NBAbins() else: scope=3600 seasons='2000-2009' if sport=='NHL': bins=NHLbins() elif sport=='CFB': bins=NFLbins() elif sport=='NFL': bins=NFLbins() binw=getBinWidth(bins) step=120.0 data=lt.getData(sport) lead=lt.Lead(data, sport) inLead=lt.inLead(lead) s=lt.lastChange(inLead) #m,s=lt.maxLeadTime(lead) '''rw_lead=rwk.Lead(sport) rw_inLead=rwk.inLead(rw_lead) rw=rwk.lastChange(rw_inLead)''' sm_lead=sm.Lead(sport) sm_inLead=sm.inLead(sm_lead) sr=sm.lastChange(sm_inLead) msr,sr=lt.maxLeadTime(sm_lead) fontSize=18 ax=plt.gca() ax.tick_params(labelsize=fontSize) h,b=np.histogram(s, bins) hcorr=h/(binw*len(s)) plt.scatter(b[:len(bins)-1], hcorr, c='blue',marker='o',label=sport+' games') hr,br=np.histogram(sr,bins) hrcorr=hr/(binw*len(sr)) plt.plot(br[:len(bins)-1],hrcorr,color='DarkTurquoise',linewidth=2,label='Inhomogeneous Poisson process') #ubiased RW '''rwstep=20.0 rwbins=scope/rwstep h,b=np.histogram(rw, rwbins) hcorr=h/(rwstep*len(rw)) plt.plot(b[:rwbins], hcorr, c='purple',linewidth=2.5,label='Homogenous Poisson process') ''' x=np.array(range(scope)) #arcsine law y=1/(np.pi*(x*(scope+1-x))**(0.5)) plt.plot(x,y,color='FireBrick',linewidth=2,label='Arcsine law') plt.xlim(xmin=0,xmax=scope) plt.ylim(ymin=0,ymax=0.0027) plt.legend(prop={'size':fontSize}) plt.xlabel('Game clock time, t (seconds)', fontsize=fontSize) plt.ylabel('Probability of maximum lead change', fontsize=fontSize) plt.subplots_adjust(left=0.16, right=0.95, top=0.95, bottom=0.13) #plt.savefig(sport+'varBinnedUpdate.pdf') #plt.close() plt.show()
def getSafeAvg(lead, sport): inLead=lt.inLead(lead) lastChange=lt.lastChangeMessy(inLead) safe=lt.assignSafety(lastChange, sport) z_tuples=lt.getZ(lead, safe, sport) df=pd.DataFrame(z_tuples) z1,ave_q=binAverage(df) return z1,ave_q
def getSafeAvg(lead, sport): inLead = lt.inLead(lead) lastChange = lt.lastChangeMessy(inLead) safe = lt.assignSafety(lastChange, sport) z_tuples = lt.getZ(lead, safe, sport) df = pd.DataFrame(z_tuples) z1, ave_q = binAverage(df) return z1, ave_q
def plotWScoringRate(sport): if sport=='NBA': scope=2880 seasons='2002-2010' bins=arcs.NBAbins() else: scope=3600 seasons='2000-2009' if sport=='NHL': bins=arcs.NHLbins() elif sport=='CFB': bins=arcs.NFLbins() elif sport=='NFL': bins=arcs.NFLbins() binw=arcs.getBinWidth(bins) step=120.0 data=lt.getData(sport) lead=lt.Lead(data, sport) inLead=lt.inLead(lead) s=lt.lastChange(inLead) #m,s=lt.maxLeadTime(lead) '''rw_lead=rwk.Lead(sport) rw_inLead=rwk.inLead(rw_lead) rw=rwk.lastChange(rw_inLead)''' sm_lead=sm.Lead(sport) sm_inLead=sm.inLead(sm_lead) sr=sm.lastChange(sm_inLead) #msr,sr=lt.maxLeadTime(sm_lead) ev_prob=pd.DataFrame.from_csv('/Users/Ish/Documents/SafeLeads/Results/'+sport+'_res/'+sport+'_eventProb.csv',\ header=None) fontSize=18 window=10 #f, (ax1, ax2) = plt.subplots(2, sharex=True) f=plt.figure() gs=gridspec.GridSpec(2,1,height_ratios=[2,3]) #1 used to be 3 ax1=plt.subplot(gs[0,:]) ax2=plt.subplot(gs[1:,:], sharex=ax1) smoothEvProb=movingaverage(ev_prob[1],window) avg=np.mean(smoothEvProb) #y_formatter = matplotlib.ticker.ScalarFormatter(useOffset=-100) #ax1.yaxis.set_major_formatter(y_formatter) ax1.plot(smoothEvProb, linewidth=1.3) ax1.hlines(avg, 0,scope, colors='red', linewidth=2) yticks = ax1.yaxis.get_major_ticks() yticks[0].label1.set_visible(False) ax1.set_ylim(ymin=0,ymax=max(smoothEvProb)+0.005) ax1.set_ylabel('Pr(scoring event)',fontsize=fontSize, labelpad=25)#25 for NBA ax1.tick_params(labelsize=fontSize) ax2.tick_params(labelsize=fontSize) h,b=np.histogram(s, bins) hcorr=h/(binw*len(s)) ax2.scatter(b[:len(bins)-1], hcorr, c='blue',marker='o',label=sport+' games') hr,br=np.histogram(sr,bins) hrcorr=hr/(binw*len(sr)) ax2.plot(br[:len(bins)-1],hrcorr,color='DarkTurquoise',linewidth=2,label='Inhomogeneous Poisson process') #ubiased RW '''rwstep=20.0 rwbins=scope/rwstep h,b=np.histogram(rw, rwbins) hcorr=h/(rwstep*len(rw)) plt.plot(b[:rwbins], hcorr, c='purple',linewidth=2.5,label='Homogenous Poisson process') ''' x=np.array(range(scope)) #arcsine law y=1/(np.pi*(x*(scope+1-x))**(0.5)) ax2.plot(x,y,color='FireBrick',linewidth=2,label='Arcsine law') ax2.set_xlim(xmin=0,xmax=scope) ax2.set_ylim(ymin=0,ymax=0.0027) ax2.legend(prop={'size':fontSize}) ax2.set_xlabel('Game clock time, t (seconds)', fontsize=fontSize) ax2.set_ylabel('Pr(last lead change)', fontsize=fontSize) f.subplots_adjust(left=0.16, right=0.95, top=0.95, bottom=0.11, hspace=0.00001) #16,13 # Fine-tune figure; make subplots close to each other and hide x ticks for # all but bottom plot. plt.setp([a.get_xticklabels() for a in f.axes[:-1]], visible=False) plt.show()
def PropInLead(sport): #portion of time first team to score is in the lead data=lt.getData(sport) lead=lt.Lead(data,sport) inLead=lt.inLead(lead) rands=np.random.rand(len(inLead)) mult=-1+2*(rands<0.5) matr=(np.tile(mult,(len(inLead[0]),1))).T rand_inLead=np.multiply(matr, inLead) props=np.sum(rand_inLead<0, axis=1) sm_lead=sm.Lead(sport) sm_inLead=sm.inLead(sm_lead) rands=np.random.rand(len(sm_inLead)) mult=-1+2*(rands<0.5) matr=(np.tile(mult,(len(sm_inLead[0]),1))).T sm_randInLead=np.multiply(matr,sm_inLead) sm_props=np.sum(sm_randInLead<0,axis=1) rw_lead=rw.Lead(sport) rw_inLead=rw.inLead(rw_lead) rands=np.random.rand(len(rw_inLead)) mult=-1+2*(rands<0.5) matr=(np.tile(mult,(len(rw_inLead[0]),1))).T rw_randInLead=np.multiply(matr, rw_inLead) rw_props=np.sum(rw_randInLead<0,axis=1) if sport=='NBA': scope=2880 bins=arcs.NBAbins() else: scope=3600 if sport=='NHL': bins=arcs.NHLbins() else: bins=arcs.NFLbins() binw=arcs.getBinWidth(bins) fontSize=18 ax=plt.gca() ax.tick_params(labelsize=fontSize) h,b=np.histogram(props, bins) hcorr=h/(binw*len(props)) plt.scatter(b[:len(bins)-1], hcorr, c='blue',marker='o',label=sport+' games') hr,br=np.histogram(sm_props,bins) hrcorr=hr/(binw*len(sm_props)) plt.plot(br[:len(bins)-1],hrcorr,color='DarkTurquoise', linewidth=2,label='Inhomogeneous Poisson process') #Unbiased rw #rwstep=20.0 #rwbins=scope/rwstep #hw,bw=np.histogram(rw_props,rwbins) #hwcorr=hw/(rwstep*len(rw_props)) #plt.plot(bw[:rwbins],hwcorr,color='DarkSalmon', linewidth=2,label='Homogeneous Poisson process') #arcsine law x=np.array(range(scope)) y=1/(np.pi*(x*(scope+1-x))**(0.5)) plt.plot(x,y,color='FireBrick',linewidth=2,label='Arcsine law') plt.xlim(xmin=0,xmax=scope) plt.ylim(ymin=0,ymax=0.0027) plt.legend(prop={'size':fontSize}) plt.xlabel('Number of seconds a team is in the lead', fontsize=fontSize) plt.ylabel('Relative frequency', fontsize=fontSize) plt.subplots_adjust(left=0.16, right=0.95, top=0.95, bottom=0.13) #plt.savefig(sport+'varBinnedUpdate.pdf') #plt.close() plt.show()
def plotWScoringRate(sport): if sport == 'NBA': scope = 2880 seasons = '2002-2010' bins = arcs.NBAbins() else: scope = 3600 seasons = '2000-2009' if sport == 'NHL': bins = arcs.NHLbins() elif sport == 'CFB': bins = arcs.NFLbins() elif sport == 'NFL': bins = arcs.NFLbins() binw = arcs.getBinWidth(bins) step = 120.0 data = lt.getData(sport) lead = lt.Lead(data, sport) inLead = lt.inLead(lead) s = lt.lastChange(inLead) #m,s=lt.maxLeadTime(lead) '''rw_lead=rwk.Lead(sport) rw_inLead=rwk.inLead(rw_lead) rw=rwk.lastChange(rw_inLead)''' sm_lead = sm.Lead(sport) sm_inLead = sm.inLead(sm_lead) sr = sm.lastChange(sm_inLead) #msr,sr=lt.maxLeadTime(sm_lead) ev_prob=pd.DataFrame.from_csv('/Users/Ish/Documents/SafeLeads/Results/'+sport+'_res/'+sport+'_eventProb.csv',\ header=None) fontSize = 18 window = 10 #f, (ax1, ax2) = plt.subplots(2, sharex=True) f = plt.figure() gs = gridspec.GridSpec(2, 1, height_ratios=[2, 3]) #1 used to be 3 ax1 = plt.subplot(gs[0, :]) ax2 = plt.subplot(gs[1:, :], sharex=ax1) smoothEvProb = movingaverage(ev_prob[1], window) avg = np.mean(smoothEvProb) #y_formatter = matplotlib.ticker.ScalarFormatter(useOffset=-100) #ax1.yaxis.set_major_formatter(y_formatter) ax1.plot(smoothEvProb, linewidth=1.3) ax1.hlines(avg, 0, scope, colors='red', linewidth=2) yticks = ax1.yaxis.get_major_ticks() yticks[0].label1.set_visible(False) ax1.set_ylim(ymin=0, ymax=max(smoothEvProb) + 0.005) ax1.set_ylabel('Pr(scoring event)', fontsize=fontSize, labelpad=25) #25 for NBA ax1.tick_params(labelsize=fontSize) ax2.tick_params(labelsize=fontSize) h, b = np.histogram(s, bins) hcorr = h / (binw * len(s)) ax2.scatter(b[:len(bins) - 1], hcorr, c='blue', marker='o', label=sport + ' games') hr, br = np.histogram(sr, bins) hrcorr = hr / (binw * len(sr)) ax2.plot(br[:len(bins) - 1], hrcorr, color='DarkTurquoise', linewidth=2, label='Inhomogeneous Poisson process') #ubiased RW '''rwstep=20.0 rwbins=scope/rwstep h,b=np.histogram(rw, rwbins) hcorr=h/(rwstep*len(rw)) plt.plot(b[:rwbins], hcorr, c='purple',linewidth=2.5,label='Homogenous Poisson process') ''' x = np.array(range(scope)) #arcsine law y = 1 / (np.pi * (x * (scope + 1 - x))**(0.5)) ax2.plot(x, y, color='FireBrick', linewidth=2, label='Arcsine law') ax2.set_xlim(xmin=0, xmax=scope) ax2.set_ylim(ymin=0, ymax=0.0027) ax2.legend(prop={'size': fontSize}) ax2.set_xlabel('Game clock time, t (seconds)', fontsize=fontSize) ax2.set_ylabel('Pr(last lead change)', fontsize=fontSize) f.subplots_adjust(left=0.16, right=0.95, top=0.95, bottom=0.11, hspace=0.00001) #16,13 # Fine-tune figure; make subplots close to each other and hide x ticks for # all but bottom plot. plt.setp([a.get_xticklabels() for a in f.axes[:-1]], visible=False) plt.show()
def PropInLead(sport): #portion of time first team to score is in the lead data = lt.getData(sport) lead = lt.Lead(data, sport) inLead = lt.inLead(lead) rands = np.random.rand(len(inLead)) mult = -1 + 2 * (rands < 0.5) matr = (np.tile(mult, (len(inLead[0]), 1))).T rand_inLead = np.multiply(matr, inLead) props = np.sum(rand_inLead < 0, axis=1) sm_lead = sm.Lead(sport) sm_inLead = sm.inLead(sm_lead) rands = np.random.rand(len(sm_inLead)) mult = -1 + 2 * (rands < 0.5) matr = (np.tile(mult, (len(sm_inLead[0]), 1))).T sm_randInLead = np.multiply(matr, sm_inLead) sm_props = np.sum(sm_randInLead < 0, axis=1) rw_lead = rw.Lead(sport) rw_inLead = rw.inLead(rw_lead) rands = np.random.rand(len(rw_inLead)) mult = -1 + 2 * (rands < 0.5) matr = (np.tile(mult, (len(rw_inLead[0]), 1))).T rw_randInLead = np.multiply(matr, rw_inLead) rw_props = np.sum(rw_randInLead < 0, axis=1) if sport == 'NBA': scope = 2880 bins = arcs.NBAbins() else: scope = 3600 if sport == 'NHL': bins = arcs.NHLbins() else: bins = arcs.NFLbins() binw = arcs.getBinWidth(bins) fontSize = 18 ax = plt.gca() ax.tick_params(labelsize=fontSize) h, b = np.histogram(props, bins) hcorr = h / (binw * len(props)) plt.scatter(b[:len(bins) - 1], hcorr, c='blue', marker='o', label=sport + ' games') hr, br = np.histogram(sm_props, bins) hrcorr = hr / (binw * len(sm_props)) plt.plot(br[:len(bins) - 1], hrcorr, color='DarkTurquoise', linewidth=2, label='Inhomogeneous Poisson process') #Unbiased rw #rwstep=20.0 #rwbins=scope/rwstep #hw,bw=np.histogram(rw_props,rwbins) #hwcorr=hw/(rwstep*len(rw_props)) #plt.plot(bw[:rwbins],hwcorr,color='DarkSalmon', linewidth=2,label='Homogeneous Poisson process') #arcsine law x = np.array(range(scope)) y = 1 / (np.pi * (x * (scope + 1 - x))**(0.5)) plt.plot(x, y, color='FireBrick', linewidth=2, label='Arcsine law') plt.xlim(xmin=0, xmax=scope) plt.ylim(ymin=0, ymax=0.0027) plt.legend(prop={'size': fontSize}) plt.xlabel('Number of seconds a team is in the lead', fontsize=fontSize) plt.ylabel('Relative frequency', fontsize=fontSize) plt.subplots_adjust(left=0.16, right=0.95, top=0.95, bottom=0.13) #plt.savefig(sport+'varBinnedUpdate.pdf') #plt.close() plt.show()
def plotBathtub(sport): if sport == 'NBA': scope = 2880 seasons = '2002-2010' bins = NBAbins() else: scope = 3600 seasons = '2000-2009' if sport == 'NHL': bins = NHLbins() elif sport == 'CFB': bins = NFLbins() elif sport == 'NFL': bins = NFLbins() binw = getBinWidth(bins) step = 120.0 data = lt.getData(sport) lead = lt.Lead(data, sport) inLead = lt.inLead(lead) s = lt.lastChange(inLead) #m,s=lt.maxLeadTime(lead) '''rw_lead=rwk.Lead(sport) rw_inLead=rwk.inLead(rw_lead) rw=rwk.lastChange(rw_inLead)''' sm_lead = sm.Lead(sport) sm_inLead = sm.inLead(sm_lead) sr = sm.lastChange(sm_inLead) msr, sr = lt.maxLeadTime(sm_lead) fontSize = 18 ax = plt.gca() ax.tick_params(labelsize=fontSize) h, b = np.histogram(s, bins) hcorr = h / (binw * len(s)) plt.scatter(b[:len(bins) - 1], hcorr, c='blue', marker='o', label=sport + ' games') hr, br = np.histogram(sr, bins) hrcorr = hr / (binw * len(sr)) plt.plot(br[:len(bins) - 1], hrcorr, color='DarkTurquoise', linewidth=2, label='Inhomogeneous Poisson process') #ubiased RW '''rwstep=20.0 rwbins=scope/rwstep h,b=np.histogram(rw, rwbins) hcorr=h/(rwstep*len(rw)) plt.plot(b[:rwbins], hcorr, c='purple',linewidth=2.5,label='Homogenous Poisson process') ''' x = np.array(range(scope)) #arcsine law y = 1 / (np.pi * (x * (scope + 1 - x))**(0.5)) plt.plot(x, y, color='FireBrick', linewidth=2, label='Arcsine law') plt.xlim(xmin=0, xmax=scope) plt.ylim(ymin=0, ymax=0.0027) plt.legend(prop={'size': fontSize}) plt.xlabel('Game clock time, t (seconds)', fontsize=fontSize) plt.ylabel('Probability of maximum lead change', fontsize=fontSize) plt.subplots_adjust(left=0.16, right=0.95, top=0.95, bottom=0.13) #plt.savefig(sport+'varBinnedUpdate.pdf') #plt.close() plt.show()