def kscomp(mu_sigma,trc_func,t_series,p_series,num_bins,*faux_args): '''Compare a trace's histogramed fds to a normal distribution mu_sigma = a tuple of the parameters mu and sigma trc_func = Use etrace or atrace to act on p_series and t_series t_series = The time series corrisponding to p_series p_series = The data set which will be traced num_bins = The number of bins used to histogram the fds faux_args = Auxilary arguments for trc_func Returns ------- ks_2samp: KS static and a p-value in a tuple mkr_trc: An array of bin positions for cnt_trc and cnt_norm cnt_trc: An array of bin values for trace's histogramed fds cnt_norm: An array of bin values for a scaled normal distribution ''' mu,sigma = mu_sigma trc = trc_func('price',t_series,p_series,mu,sigma,*faux_args) fds_trc = np.array(ufuncs.get_fds(trc)) cnt_trc,mkr_trc = np.histogram(fds_trc,num_bins) mkr_trc=mkr_trc[1:] std = np.sqrt(np.mean((t_series-t_series.shift(1))[1:].values)) cnt_norm = normpdf(mkr_trc,0,std) cnt_norm = cnt_norm*max(cnt_trc)/max(cnt_norm) return ks_2samp(cnt_norm, cnt_trc),mkr_trc,cnt_trc,cnt_norm
def makeplots(a,b): dat = func('price',t_series,p_series,a,b,*func_auxargs) fig, ax = plt.subplots(figsize=(10,2)) ax.set_title('Data After Trace') ax.set_ylabel('wiener',fontsize=13) ax.set_xlabel('time',fontsize=13) ax.set_xlim(min(t_series),max(t_series)) ax.plot(t_series,dat) dat_fds = np.array(ufuncs.get_fds(dat)) dat_cnt,dat_mkr = np.histogram(dat_fds,bar_num) norm_hist = np.histogram(dat_fds,bar_num)[0] cnt_norm = normpdf(dat_mkr,0,np.std(dat_fds)) cnt_norm = cnt_norm*max(dat_cnt)/max(cnt_norm) ksstat,pval = ks_2samp(cnt_norm, dat_cnt) print 'KS test using scaled fds of traced dat and a fit gaussian:' print 'Statistic value =',ksstat print 'Two sided p-value =',pval fig,ax1 = plt.subplots(figsize=(10,4)) ax1.set_title('Finite Difference Series Comparison') ax1.set_ylabel('count',fontsize=13) ax1.set_xlabel('difference',fontsize=13) wdth = float(max(dat_mkr)-min(dat_mkr))/bar_num plt.bar(dat_mkr[:-1],dat_cnt,label='traced fds (scaled by dt)',width = wdth,align='center') plt.plot(dat_mkr,cnt_norm,label='fit gaussian',color='m') ax1.set_xlim(min(dat_mkr)-wdth,max(dat_mkr)+wdth) plt.legend(loc='best') plt.show()