def corrplot(trace, maxlags=100): trace = trace[:, 0] plt.acorr(trace - np.mean(trace), normed=True, maxlags=maxlags) plt.xlim([0, maxlags]) plt.show()
def autocorrelation_plot(x, l): '''plots the autocorrelation of x to lag = l''' plt.acorr(x - np.mean(x), maxlags=l, normed=True, usevlines=False) plt.xlim((0, 100)) plt.ylabel('Autocorrelation') plt.xlabel('Lag') plt.show()
def acr_plot(data, filename=None, title="ACR Plot", xlabel="Lag #", ylabel="Correlation"): """Generates an ACF plot, demeaned and normalised. Arguments: data -- list of data points Keyword arguments: filename -- filename to write graph to (None plots to screen) title -- graph title xlabel -- label on x-axis ylabel -- label on y-axis """ plt.cla() plt.acorr(data, detrend=mlab.detrend_mean, usevlines=True, maxlags=None, normed=True, lw=2) plt.title(title) plt.xlabel(xlabel) plt.ylabel(ylabel) if filename is not None: plt.savefig(filename) else: plt.show()
def studies_autocorre(df, src, dst): #df=df.set_index('datetime') df = df.loc[((df['src'] == src) & (df['dst'] == dst))] taille = list() #print("src=",src," dst=",dst," Taille=",len(df)) if len(df) > 0: for i in range(len(df)): k = i + 1 taille.append(k) df['taille'] = taille df = df.set_index('taille') #ts=df['pdr'] ts = df['mean_rssi'] plt.acorr(ts) plt.legend(loc='best') plt.xlabel('Lag') plt.ylabel('Autocorrelation') #title1='Time Series PDR '+str(src)+"==>"+str(dst) title1 = 'Time Series RSSI ' + str(src) + "==>" + str(dst) plt.title(title1) #title="C:\\Users\WSN-LINK\\Documents\\TEST\\SOCALE\\CHOOSE\\PDR\\PDR_AUTO_LINK_"+str(src)+"==="+str(dst)+".png" title = "C:\\Users\WSN-LINK\\Documents\\TEST\\SOCALE\\CHOOSE\\RSSI\\RSSI_AUTO_LINK_" + str( src) + "===" + str(dst) + ".png" plt.savefig(title, format='png', bbox_inches='tight', pad_inches=0) plt.clf()
def acorr(trace): if len(trace) > 50: plt.acorr(trace, normed=True, detrend=mlab.detrend_mean, maxlags=50) plt.xticks([]) plt.yticks([]) l,r,b,t = plt.axis() plt.axis([-10, r, -.1, 1.1])
def residuals_autocorrelation(residuals, window): f = plt.figure(4) plt.xlabel('Time lag') plt.ylabel('Autocorrelation') plt.acorr(residuals, maxlags=window) plt.savefig('/Users/gwren/Downloads/11_naive_residuals_acor.eps', format='eps') f.show()
def patch_correlations(timeSteps, writeStep, dT, p): infile = path + 'output/' + 'w0_post.pvp' w = rw.PVReadWeights(infile) # opens file and reads params!! print 'numPatches = ' + str(w.numPatches) + ' numWeights = ' + str(w.numWeights) + ' patchSize = ' + str(w.patchSize) w.print_params() w.just_rewind() T = int(timeSteps*dT)/writeStep weights = np.zeros((T,w.patchSize),dtype=np.float32) # read the first record (which is always at time 0.5) r = w.next_record() # read header and next record (returns numWeights array) # from now on, we read weights written every writeStep output = open(path + 'output/p' + str(p) +'.dat','a') n = 0 try: while True: r = w.next_record() # read header and next record (returns numWeights array) m = 0 print str(n+1) + ': ', for k in range(p*w.patchSize,(p+1)*w.patchSize): weights[n,m] = r[k] m += 1 print r[k], output.write(str(r[k]) + ' ') output.write('\n') print n+=1 #s = raw_input('--> ') except: print "Finished reading, read ", n+1, "records" output.close() # plot weights evolution sym = np.array(['r','b','g','r','b','g','r','b','g','r','b','g','r','b','g','r']) fig = plt.figure(1) plt.subplot(2,1,1) for k in range(w.patchSize): plt.plot(np.arange(T), weights[:,k], '-o', color=sym[k]) plt.xlabel('Time') plt.ylabel('Weights') plt.title('Weights Evolution') plt.hold(True) plt.draw() # compute and plot correlations plt.subplot(2,1,2) for k in range(w.patchSize): #plt.acorr(weights[:,k], normed=True, maxlags=3,linestyle = 'solid', color = sym[k]) plt.acorr(weights[:,k], normed=True, maxlags=3,usevlines=False, color = sym[k]) plt.xlabel('Time') plt.ylabel('Corr') plt.title('Weights Autocorrelations') plt.hold(True) plt.draw()
def Autocrr(residuals): plot.acorr(residuals, maxlags=9) plot.title('Autocorrelação dos resíduos do Ticker') plot.xlabel('Lag') plot.ylabel('Autocorrelação') plot.show()
def plot_autocorr( sol, save=False, draw=True, save_as_png=False, dpi=None, ignore=subplots_to_ignore, ): """ Plots autocorrelations """ ext = ['png' if save_as_png else 'pdf'][0] MDL = sol.MDL keys = [k for k in sol.var_dict.keys() if k not in ignore] for (i, k) in enumerate(keys): vect = old_div((MDL.trace(k)[:].size), (len(MDL.trace(k)[:]))) if vect > 1: keys[i] = [k + "%d" % n for n in range(1, vect + 1)] keys = list(flatten(keys)) ncols = 2 nrows = int(ceil(len(keys) * 1.0 / ncols)) fig, ax = plt.subplots(nrows, ncols, figsize=(10, nrows * 2)) plt.ticklabel_format(style='sci', axis='both', scilimits=(0, 0)) for (a, k) in zip(ax.flat, keys): if k[-1] not in ["%d" % d for d in range(1, 8)] or k == "R0": data = sorted(MDL.trace(k)[:].ravel()) else: data = sorted(MDL.trace(k[:-1])[:][:, int(k[-1]) - 1].ravel()) plt.sca(a) plt.gca().get_yaxis().get_major_formatter().set_useOffset(False) plt.gca().get_xaxis().get_major_formatter().set_useOffset(False) plt.yticks(fontsize=12) plt.xticks(fontsize=12) plt.ylabel(k, fontsize=12) to_thin = old_div(len(data), 50) if to_thin != 0: plt.xlabel("Lags / %d" % to_thin, fontsize=12) else: plt.xlabel("Lags", fontsize=12) max_lags = None if len(data) > 50: data = data[::to_thin] plt.acorr(data, usevlines=True, maxlags=max_lags, detrend=plt.mlab.detrend_mean) plt.grid(None) fig.tight_layout() for a in ax.flat[ax.size - 1:len(keys) - 1:-1]: a.set_visible(False) if save: fn = 'AC-%s-%s.%s' % (sol.model_type_str, sol.filename, ext) save_figure(fig, subfolder='Autocorrelations', fname=fn, dpi=dpi) plt.close(fig) if draw: return fig else: return None
def temperature_noise(temps): fig=plt.figure(figsize=(15,10)) fig.suptitle('Autocorrelation of Temperature During Hold') plt.figure() plt.acorr(temps,usevlines=False,maxlags=10,color='blue') #ax3.autocorrelation_plot(temps['2017-10-25 08']['Ambient'],color='green') #ax4.autocorrelation_plot(temps['2017-10-25 08']['T2'],color='black') print('Autocorrelation Plot') plt.show()
def do_acf2(): # Autocorrelation Function,ACF dataset = np.array(loadData()) x = dataset[:, 0] plt.acorr(x, usevlines=True, normed=True, maxlags=100) plt.grid(True) plt.show() return
def acorr(trace): if len(trace) > 50: plt.acorr(trace, normed=True, detrend=mlab.detrend_mean, maxlags=50) plt.xticks([]) plt.yticks([]) l, r, b, t = plt.axis() plt.axis([-10, r, -.1, 1.1])
def graphAutoCorr(self, variable, Maxlags=20, Normed=True, Usevlines=True): plt.acorr(variable - np.mean(variable), maxlags=Maxlags, normed=Normed, usevlines=Usevlines) plt.xlabel("Lag") plt.ylabel("Auto Correlation") plt.title("Auto Correlation Plot") plt.xlim([0, Maxlags]) plt.show()
def create_autocorrelation_plots(self): for column in self.df: plt.figure() plt.acorr(self.df[column], maxlags=min(1000, self.df.shape[0]), usevlines=False, linestyle="-", label=column) plt.legend(loc="best") plt.xlabel("Lag") plt.ylabel("Autocorrelation")
def autocorr(array, name): array = array[~np.isnan(array)] array = array.tolist() fig = plt.figure() plt.acorr(array, maxlags=None, lw = 2) plt.xlim([0,len(array)]) # plt.ylim([-1,1]) plt.legend([name], loc = 'best') fig.savefig('Election_13/acorr/'+name+'.png', bbox_inches='tight') plt.clf() fig.clf() plt.close(fig)
def plot_autocorr(self, dim=0, maxlags=100, color='black', alpha=1, label=None): plt.acorr(np.array(self.history['state'])[:, dim], maxlags=maxlags, color=color, alpha=alpha, label=label) plt.xlabel('Lag') plt.xlim(-1, maxlags + 1) plt.ylabel('Autocorrelation')
def acf_plot(self, chain, lags, thin=1, save_as=None, fmt='eps'): trace = self.trace['parameters'][chain] freeparams = self.free_parameters() for i, parameter in enumerate(freeparams): series = [a[i] for a in trace] series += -np.mean(series) series = series[::thin] plt.acorr(series, maxlags=lags, color='b') plt.title("Autocorrelation Plot for free parameter %s" % parameter) plt.xlabel("Lag") plt.ylabel("ACF") if save_as is not None: plt.savefig(save_as + str(i) + '.' + fmt, format=fmt, dpi=1000) plt.show()
def plot_autocorr(sol, save=False, draw=True, save_as_png=False, dpi=None, ignore=default_ignore, ): """ Plots autocorrelations """ ext = ['png' if save_as_png else 'pdf'][0] MDL = sol.MDL keys = [k for k in sol.var_dict.keys() if k not in ignore] for (i, k) in enumerate(keys): vect = old_div((MDL.trace(k)[:].size),(len(MDL.trace(k)[:]))) if vect > 1: keys[i] = [k+"%d"%n for n in range(1,vect+1)] keys = list(flatten(keys)) ncols = 2 nrows = int(ceil(len(keys)*1.0 / ncols)) fig, ax = plt.subplots(nrows, ncols, figsize=(10,nrows*2)) plt.ticklabel_format(style='sci', axis='both', scilimits=(0,0)) for (a, k) in zip(ax.flat, keys): if k[-1] not in ["%d"%d for d in range(1,8)] or k =="R0": data = sorted(MDL.trace(k)[:].ravel()) else: data = sorted(MDL.trace(k[:-1])[:][:,int(k[-1])-1].ravel()) plt.sca(a) plt.gca().get_yaxis().get_major_formatter().set_useOffset(False) plt.gca().get_xaxis().get_major_formatter().set_useOffset(False) plt.yticks(fontsize=12) plt.xticks(fontsize=12) plt.ylabel(k, fontsize=12) to_thin = old_div(len(data),50) if to_thin != 0: plt.xlabel("Lags / %d"%to_thin, fontsize=12) else: plt.xlabel("Lags", fontsize=12) max_lags = None if len(data) > 50: data= data[::to_thin] plt.acorr(data, usevlines=True, maxlags=max_lags, detrend=plt.mlab.detrend_mean) plt.grid(None) fig.tight_layout() for a in ax.flat[ax.size - 1:len(keys) - 1:-1]: a.set_visible(False) if save: fn = 'AC-%s-%s.%s'%(sol.model_type_str,sol.filename,ext) save_figure(fig, subfolder='Autocorrelations', fname=fn, dpi=dpi) plt.close(fig) if draw: return fig else: return None
def PlotCorrelationAndSaveFig(yy,mm,dd,numDays,numPts): #yy,mm,dd,numDays = 2011,4,1,15 gm = GliderModel(myDataDir+'/RiskMap.shelf',romsDataDir) u,v,time1,depth,lat,lon = gm.GetRomsData(yy,mm,dd,numDays) u_mean, v_mean = np.mean(u,axis=1), np.mean(v,axis=1) s_mean = np.sqrt(u_mean * u_mean + v_mean * v_mean) # Now let us look at the correlation in this variability over time. (tMax,lyMax,lxMax) = s_mean.shape u_mean = np.where(np.isnan(u_mean),0,u_mean) v_mean = np.where(np.isnan(v_mean),0,v_mean) s_mean = np.where(np.isnan(s_mean),0,s_mean) fig = plt.figure() numPts = 10 plt.title('Plot of Auto-correlations in ocean current predictions for \n%d days from %04d-%02d-%02d'%(numDays,yy,mm,dd)) ax1 = fig.add_subplot(311) ax1.grid(True) ax1.yaxis.label.set_text('Auto-Correlation\nCurrent u m/s') x_pts,y_pts = np.arange(0.1,lxMax-0.1,numPts),np.arange(0.1,lyMax-0.1,numPts) X,Y = np.array(np.floor(x_pts),int),np.array(np.floor(y_pts),int) for x in X: for y in Y: #plt.acorr(s_mean[:,x,y],usevlines=True,detrend=mlab.detrend_mean,normed=True,maxlags=None) lags,cVec,linecols,lines = plt.acorr(u_mean[:,y,x],usevlines=False,normed=True,maxlags=None,lw=2) plt.xlim((0,max(lags))) ax2 = fig.add_subplot(312,sharex=ax1) ax2.grid(True) ax2.yaxis.label.set_text('Auto-Correlation\ncurrent v m/s') for x in X: for y in Y: #plt.acorr(s_mean[:,x,y],usevlines=True,detrend=mlab.detrend_mean,normed=True,maxlags=None) lags,cVec,linecols,lines = plt.acorr(v_mean[:,y,x],usevlines=False,normed=True,maxlags=None,lw=2) plt.xlim((0,max(lags))) ax3 = fig.add_subplot(313,sharex=ax1) ax3.grid(True) ax3.yaxis.label.set_text('Auto-Correlation\ncurrent mag. m/s') for x in X: for y in Y: #plt.acorr(s_mean[:,x,y],usevlines=True,detrend=mlab.detrend_mean,normed=True,maxlags=None) lags,cVec,linecols,lines = plt.acorr(s_mean[:,y,x],usevlines=False,normed=True,maxlags=None,lw=2) plt.xlim((0,max(lags))) ax1.xaxis.label.set_text('hour') ax2.xaxis.label.set_text('hour') ax3.xaxis.label.set_text('hour') plt.savefig('AutoCorrelations_%04d%02d%02d_%d.pdf'%(yy,mm,dd,numDays),pad_inches='tight',transparent=True)
def GetVectorCorrelationPlot(gm,lat,lon,qty1,qty2,yy,mm,dd,numDays,numPts): # Here, we are going to treat [qty1 qty2] (tMax1,lyMax1,lxMax1) = qty1.shape (tMax2,lyMax2,lxMax2) = qty2.shape qty1 = np.where(np.isnan(qty1),0,qty1) qty2 = np.where(np.isnan(qty2),0,qty2) lat_pts, lon_pts = gm.lat_pts, gm.lon_pts x_pts, y_pts = [], [] for i in range(0,len(lat_pts)): x,y = gm.LatLonToRomsXY(lat_pts[i],lon_pts[i],lat,lon) x_pts.append(x) y_pts.append(y) X,Y = np.array(np.floor(x_pts),int),np.array(np.floor(y_pts),int) cumVec = np.zeros((numPts**2,numDays *24 *2 -1)) tLags = np.zeros((numPts**2,numDays *24 *2 -1)) i=0 fig = plt.figure() for y in Y: for x in X: lags,cVec,linecols,lines = plt.acorr(np.dot(qty1[:,y,x],qty2[:,y,x]),usevlines=False,normed=True,maxlags=None,lw=2) tLags[i] = lags cumVec[i] += cVec i=i+1 plt.close() return tLags,cumVec,X,Y,lxMax,lyMax
def GetCorrelationPlot(gm,lat,lon,qty,yy,mm,dd,numDays,numPts): # Now let us look at the correlation in this variability over time. (tMax,lyMax,lxMax) = qty.shape qty = np.where(np.isnan(qty),0,qty) #x_pts,y_pts = gm.x_pts,gm.y_pts lat_pts,lon_pts = gm.lat_pts,gm.lon_pts x_pts,y_pts=[],[] for i in range(0,len(lat_pts)): x,y = gm.LatLonToRomsXY(lat_pts[i],lon_pts[i],lat,lon) x_pts.append(x) y_pts.append(y) #np.linspace(0.1,lxMax-0.1,numPts),np.linspace(0.1,lyMax-0.1,numPts) X,Y = np.array(np.floor(x_pts),int),np.array(np.floor(y_pts),int) cumVec = np.zeros((numPts**2,numDays *24 *2 -1)) tLags = np.zeros((numPts**2,numDays *24 *2 -1)) i=0 fig = plt.figure() for y in Y: for x in X: lags,cVec,linecols,lines = plt.acorr(qty[:,y,x],usevlines=False,normed=True,maxlags=None,lw=2) tLags[i] = lags cumVec[i] += cVec i=i+1 plt.close() return tLags,cumVec,X,Y,lxMax,lyMax
def plot_autocorr(self, name, acorrFac=10.0, doShow=False): """ Plot the autocorrelation functions of the traces for a parameter. If the parameter is array-value then autocorrelation plots for each of the parameter's elements will be plotted. :param name: The parameter name. """ if not self._samples.has_key(name): print "WARNING: sampler does not have", name return else: print "Plotting autocorrelation function (this make take a while)" fig = plt.figure() traces = self._samples[name] # Get the sampled parameter values mtrace = np.mean(traces, axis=0) ntrace = traces.shape[1] acorr = self.autocorr_timescale(traces) for i in range(ntrace): sp = plt.subplot(ntrace, 1, i + 1) lags, acf, not_needed1, not_needed2 = plt.acorr( traces[:, i] - mtrace[i], maxlags=traces.shape[0] - 1, lw=2) sp.set_xlim(-0.5, acorrFac * acorr[i]) sp.set_ylim(-0.01, 1.01) sp.axhline(y=0.5, c='k', linestyle='--') sp.axvline(x=acorr[i], c='r', linestyle='--') sp.set_ylabel("par %d autocorr" % (i)) if i == ntrace - 1: sp.set_xlabel("lag") plt.suptitle(name) if doShow: plt.show()
def GetCorrelationPlot(gm, lat, lon, qty, yy, mm, dd, numDays, numPts): # Now let us look at the correlation in this variability over time. (tMax, lyMax, lxMax) = qty.shape qty = np.where(np.isnan(qty), 0, qty) #x_pts,y_pts = gm.x_pts,gm.y_pts lat_pts, lon_pts = gm.lat_pts, gm.lon_pts x_pts, y_pts = [], [] for i in range(0, len(lat_pts)): x, y = gm.LatLonToRomsXY(lat_pts[i], lon_pts[i], lat, lon) x_pts.append(x) y_pts.append(y) #np.linspace(0.1,lxMax-0.1,numPts),np.linspace(0.1,lyMax-0.1,numPts) X, Y = np.array(np.floor(x_pts), int), np.array(np.floor(y_pts), int) cumVec = np.zeros((numPts**2, numDays * 24 * 2 - 1)) tLags = np.zeros((numPts**2, numDays * 24 * 2 - 1)) i = 0 fig = plt.figure() for y in Y: for x in X: lags, cVec, linecols, lines = plt.acorr(qty[:, y, x], usevlines=False, normed=True, maxlags=None, lw=2) tLags[i] = lags cumVec[i] += cVec i = i + 1 plt.close() return tLags, cumVec, X, Y, lxMax, lyMax
def plot_autocorr(self, name, acorrFac = 10.0, doShow=False): """ Plot the autocorrelation functions of the traces for a parameter. If the parameter is array-value then autocorrelation plots for each of the parameter's elements will be plotted. :param name: The parameter name. """ if not self.samples.has_key(name): print "WARNING: sampler does not have", name return else: print "Plotting autocorrelation function (this make take a while)" fig = plt.figure() traces = self.samples[name] # Get the sampled parameter values mtrace = np.mean(traces, axis=0) ntrace = traces.shape[1] acorr = self.autocorr_timescale(traces) for i in range(ntrace): sp = plt.subplot(ntrace, 1, i+1) lags, acf, not_needed1, not_needed2 = plt.acorr(traces[:, i] - mtrace[i], maxlags=traces.shape[0]-1, lw=2) sp.set_xlim(-0.5, acorrFac * acorr[i]) sp.set_ylim(-0.01, 1.01) sp.axhline(y=0.5, c='k', linestyle='--') sp.axvline(x=acorr[i], c='r', linestyle='--') sp.set_ylabel("par %d autocorr" % (i)) if i == ntrace-1: sp.set_xlabel("lag") plt.suptitle(name) if doShow: plt.show()
def GetVectorCorrelationPlot(gm, lat, lon, qty1, qty2, yy, mm, dd, numDays, numPts): # Here, we are going to treat [qty1 qty2] (tMax1, lyMax1, lxMax1) = qty1.shape (tMax2, lyMax2, lxMax2) = qty2.shape qty1 = np.where(np.isnan(qty1), 0, qty1) qty2 = np.where(np.isnan(qty2), 0, qty2) lat_pts, lon_pts = gm.lat_pts, gm.lon_pts x_pts, y_pts = [], [] for i in range(0, len(lat_pts)): x, y = gm.LatLonToRomsXY(lat_pts[i], lon_pts[i], lat, lon) x_pts.append(x) y_pts.append(y) X, Y = np.array(np.floor(x_pts), int), np.array(np.floor(y_pts), int) cumVec = np.zeros((numPts**2, numDays * 24 * 2 - 1)) tLags = np.zeros((numPts**2, numDays * 24 * 2 - 1)) i = 0 fig = plt.figure() for y in Y: for x in X: lags, cVec, linecols, lines = plt.acorr(np.dot( qty1[:, y, x], qty2[:, y, x]), usevlines=False, normed=True, maxlags=None, lw=2) tLags[i] = lags cumVec[i] += cVec i = i + 1 plt.close() return tLags, cumVec, X, Y, lxMax, lyMax
def autocorrelation(M, plot=True, fit=False): Nlags = int(np.floor((M.Ux.shape[2] - 1) / 2.)) # Compute the temporal autocorrelation function at each position, rho(x,y) lag = np.zeros((M.Uy.shape[0], M.Uy.shape[1], Nlags * 2 + 1)) rho = np.zeros((M.Uy.shape[0], M.Uy.shape[1], Nlags * 2 + 1)) Dt = np.mean(np.diff(M.t)) for x in range(0, M.Uy.shape[0]): for y in range(0, M.Uy.shape[1]): A = plt.acorr(M.Uy[x, y, ...], maxlags=Nlags) a = list(A[0]) b = list(A[1]) for i in range(len(a)): a[i] = float(a[i]) * Dt # convert lag[frame] into lag[sec] lag[x][y][i] = a[i] rho[x][y][i] = b[i] print x rho_mean = np.mean(rho, axis=(0, 1)) #spatial average of autocorrelation function # Convert 3d numpy array to 1d array for plotting lag = list(A[0]) print lag # Convert lag[frame] into lag[] for t in range(len(lag)): lag[t] = float(lag[t]) * Dt # Plot the spatially averaged autocorrelation function if plot: fig1 = plt.figure() ax = fig1.add_subplot(1, 1, 1) plt.axis([-3, 3, 0, 1]) plt.plot(lag, rho_mean) plt.xlabel('$\\tau=t-t\'$ [s]') plt.ylabel('$\\rho(\\tau)$') # Fit the spatially averaged autocorrelation function with exponential if fit: a = lag[int(Nlags):] X = [np.mean(a[i]) for i in range(0, int(Nlags / 2))] b = rho_mean[int(Nlags):] Y = [np.mean(b[i]) for i in range(0, int(Nlags / 2))] popt, pcov = curve_fit(func1, X, Y, bounds=([0, 0, 0], [2, 100, 0.5])) x = np.arange(0., 1., 0.0001) y = func1(x, *popt) plt.plot(x, y, 'r--', label='fit') fit_param = list() for item in popt: fit_param.append(str(round(item, 3))) fit_eq = '$y=a*exp(-b*x)+c$: $a$=' + fit_param[0] + ', $b$=' + fit_param[1] + ', $c$=' + fit_param[2] ax.text(-2.1, 0.8, fit_eq, fontsize=10) tau_half = np.log(2) / float(fit_param[1]) print 'tau1/2 = ' + str(tau_half) + ' [s]' print 'fit parameters: a*exp(-b*x)+c:' print popt return lag, rho_mean
def ACFplot(timeseries, maxlags=36, title=None, z1=None, z2=None): """ This function takes a Pandas Series object and plots its autocorrelation function. The plot is in the 'R style' found in the timeseries module; as such it shows a basic, positive ACF of the series (as opposed to the signal processing literature which typically shows an ACF centered at zero). ARGUMENTS: [required] timeseries (Pandas Series object) [optional] maxlags (integer): number of lags to plot, up to length(timeseries) name (string): Heading for series ex: name = "Closing price of AAPL" will provide the following plot labels title = "ACF of Timeseries Clossing price of AAPL" ylab = "Autocorrelation" xlab = "Lag" **Note: only the title is mutable z1, z2 (numeric): z values for horizontal p-value boundaries. Defaults to 95% and 99%. """ # Exception Handling try: if maxlags > len(timeseries.index): raise InvalidLengthError if maxlags < 0: raise NegativeValueError if not title: title = timeseries.name lags, c, line, b = plt.acorr(timeseries, color='b') plt.xlim(xmin=1, xmax=maxlags) plt.ylim(ymin=min(-.2, min(c[c != -1]) - .05), ymax=max(c[c != 1]) + .05) n = len(timeseries) # from pandas source "https://github.com/pandas-dev/pandas/" -> plotting/_misc.py if not z1: z1 = 1.959963984540054 if not z2: z2 = 2.5758293035489004 plt.axhline(y=z2 / np.sqrt(n - 1), linestyle='--', color='grey') plt.axhline(y=z1 / np.sqrt(n - 1), color='grey') plt.axhline(y=0.0, color='black') plt.axhline(y=-z1 / np.sqrt(n - 1), color='grey') plt.axhline(y=-z2 / np.sqrt(n - 1), linestyle='--', color='grey') plt.xlabel("Lag") plt.ylabel("Autocorrelation") plt.title("ACF of Timeseries " + title) plt.show() except InvalidLengthError: print(InvalidLengthError().name) except NegativeValueError: print(NegativeValueError().name) except: print("Unknown exception thrown. Examine inputs and usage.")
def autocorr(x): plt.figure(2) plt.grid(True) plt.xlim([0, 30]) plt.title("Funkcja autokorelacji") output_data = plt.acorr(x, maxlags=30, normed=True) auto_coef = output_data[1][30:] plt.show() return auto_coef
def autocorr(x=np.array([1, -9, 4, 7, 9, -7, 4, 1, 8, 5])): plt.figure(2) plt.grid(True) plt.xlim([0, 10]) plt.title("Funkcja autokorelacji") output_data = plt.acorr(x, maxlags=10, normed=True) auto_coef = output_data[1][10:] plt.show() return auto_coef
def sample_data(df,column_name,percentage,choice=True,plot=False,pause=False): valid_index = df[column_name].dropna().index.values # http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.choice.html#numpy.random.choice if choice: sampled_index = np.random.choice(valid_index,size=len(valid_index)*(percentage/100),replace=False) else: sampled_index = [x for x in valid_index if np.random.rand() < percentage/100] # Validation part with the ACF plot if plot: fig = plt.figure() ax1 = fig.add_subplot(211) maxlags=100 plt.acorr(df[column_name].ix[valid_index].dropna().values, detrend=mlab.detrend_linear, maxlags=maxlags, lw=2.0,color='m') # plt.acorr(df[column_name].ix[valid_index].values, # detrend=mlab.detrend_linear, # maxlags=maxlags, # lw=1.5,color='Orange',linestyle='--') # autocorrelation_plot(df[column_name].ix[valid_index]) plt.ylim([-0.25,0.25]) plt.xlim([-maxlags,maxlags]) plt.grid(True) plt.xlabel('Full data') ax2 = fig.add_subplot(212) plt.acorr(df[column_name].ix[sampled_index].dropna().values, detrend=mlab.detrend_linear, maxlags=maxlags, lw=2.0,color='m') # plt.acorr(df[column_name].ix[sampled_index].values, # detrend=mlab.detrend_linear, # maxlags=maxlags, # lw=1.5,color='Orange',linestyle='--') # autocorrelation_plot(df[column_name].ix[sampled_index]) plt.ylim([-0.25,0.25]) plt.xlim([-maxlags,maxlags]) plt.grid(True) plt.xlabel('Sampled data: {} %'.format(percentage)) plt.tight_layout() print('Number of samples before/after sampling: {}/{}'.format(len(valid_index),len(sampled_index))) if pause: input('Press any key to continue.') return df.ix[sampled_index]
def plot_parameter(self, name, pindex=0, doShow=False): """ Simultaneously plots the trace, histogram, and autocorrelation of this parameter's values. If the parameter is array-valued, then the user must specify the index of the array to plot, as these are all 1-d plots on a single plotting window. :param name: The name of the parameter that the plots are made for. :param pindex: If the parameter is array-valued, then this is the index of the array that the plots are made for. """ if not self.samples.has_key(name): print "WARNING: sampler does not have", name return else: print "Plotting parameter summary" fig = plt.figure() traces = self.samples[name] plot_title = name if traces.ndim > 1: # Parameter is array valued, grab the column corresponding to pindex if traces.ndim > 2: # Parameter values are at least matrix-valued, reshape to a vector traces = traces.reshape(traces.shape[0], np.prod(traces.shape[1:])) traces = traces[:, pindex] plot_title = name + ", element " + str(pindex) # First plot the trace plt.subplot(211) plt.plot(traces, '.', markersize=2) plt.xlim(0, traces.size) plt.xlabel("Iteration") plt.ylabel("Value") plt.title(plot_title) # Now add the histogram of values to the trace plot axes pdf, bin_edges = np.histogram(traces, bins=25) bin_edges = bin_edges[0:pdf.size] # Stretch the PDF so that it is readable on the trace plot when plotted horizontally pdf = pdf / float(pdf.max()) * 0.34 * traces.size # Add the histogram to the plot plt.barh(bin_edges, pdf, height=bin_edges[1] - bin_edges[0], alpha=0.75) # Finally, plot the autocorrelation function of the trace plt.subplot(212) centered_trace = traces - traces.mean() lags, acf, not_needed1, not_needed2 = plt.acorr(centered_trace, maxlags=traces.size - 1, lw=2) acf = acf[acf.size / 2:] plt.ylabel("ACF") plt.xlabel("Lag") # Compute the autocorrelation timescale, and then reset the x-axis limits accordingly # acf_timescale = self.autocorr_timescale(traces) plt.xlim(0, traces.size / 10.0) if doShow: plt.show()
def findRecordLen(segment, maxLen=1000): "Find record length in given string, via autocorrelation" # Turn string into array of byte with zero mean arr = np.array([float(ord(c)) for c in segment]) arrNorm = arr - np.mean(arr) (lags, c, line, b) = plt.acorr(arrNorm, maxlags=maxLen) return int(c[maxLen + 1:].argmax()) + 1
def plot_acorr(dfcol, maxlags, newfig=False, index=None): """ plot the autocorrelation function """ if newfig: plt.figure(figsize=(12,8)) bins, values,_,_ = plt.acorr(dfcol - dfcol.mean(), maxlags=maxlags, usevlines=False) plt.xlim([0, bins[-1]]) plt.grid() if index is not None: xticks = plt.gca().get_xticks() plt.gca().set_xticklabels([str(index[t]) for t in xticks])
def correlation(data, mode, size): """Plot the autocorrelation function for `data`.""" if len(data) > 1: # Lift any integers to floats, so division won't truncate float_data = map(float, data) # Normalise our data, ready for autocorrelation. Begin by subtracting # the mean, so that our data becomes centred about zero. centred_data = [x - np.mean(float_data) for x in float_data] # Shrink the data such that it has a unit norm: we calculate the norm # (`ord=1` for L1 norm, `ord=2` for L2 norm, etc.), then divide each # element by this value (or an appropriate epsilon, if the norm is 0). normed_data = np.divide(centred_data, max(np.linalg.norm(centred_data, ord=2), sys.float_info.epsilon)) plt.acorr(normed_data, maxlags=None, usevlines=True, normed=True, lw=2) plt.title("Autocorrelation, sample size %s, %s" % (size, mode))
def acorr(self): '''generate some descriptive statistice from event list Returns ------- slope : float slope of a linear regression to positive lags. I expect a significantly negative slope for interesting autocorrelations functions. ''' lags, c, line, lines = plt.acorr(self.hist, detrend = mlab.detrend_mean, maxlags = 500) linregress = scipy.stats.linregress(lags[lags > 0], c[lags > 0]) return linregress[0]
def plot_acorr(dfcol, maxlags, newfig=False, index=None): """ plot the autocorrelation function """ if newfig: plt.figure(figsize=(12, 8)) bins, values, _, _ = plt.acorr(dfcol - dfcol.mean(), maxlags=maxlags, usevlines=False) plt.xlim([0, bins[-1]]) plt.grid() if index is not None: xticks = plt.gca().get_xticks() plt.gca().set_xticklabels([str(index[t]) for t in xticks])
def recurrent_connections_simulation_competitivehebb(M, delta, update_function, update_name, save_name): num_neurons = 500 num_timesteps = 1000 num_inputs = 2 learning_rate = 0.1 weights = np.random.rand(num_neurons, num_inputs) for timestep in range(num_timesteps): current_input = utils.generate_correlated_input() weights = utils.competitive_hebb_update(weights, current_input, learning_rate, M, delta) weights_diff = np.reshape(weights[:,0] - weights[:,1], (num_neurons, 1)) plt.figure() plt.title(r"Ocular Dominance Map, %s" % update_name) plt.imshow(np.transpose(weights_diff[:50,:]), cmap="gray") plt.savefig("../figures/map_%s" % save_name) plt.close() plt.acorr(weights_diff[:,0], maxlags=25) plt.title("autocorrelation %s." % update_name) plt.savefig("../figures/autocorrelation_%s" % save_name) plt.close()
def simple_autocorrelation_plot(hourly_data, save, n_lags, y_lim=[-0.05, 0.3]): print( "\n\nsimple_autocorrelation_plot: assuming data has been normalized to '1', subtracting 1 from each value.\nAlso, requiring that values were originally positive." ) #y = [d.value - 1 for d in hourly_data if d.value > 0.] #x = [d.datetime for d in hourly_data if d.value > 0.] y = [d.value - 1 for d in hourly_data] x = [d.datetime for d in hourly_data] fig, ax = plt.subplots() ax.plot(x, y, 'o', label='demand') fig = plt.gcf() plt.show() fig.savefig("plots/" + save + "_values.png") plt.acorr(y, maxlags=n_lags) plt.ylim(y_lim[0], y_lim[1]) fig = plt.gcf() plt.show() fig.savefig("plots/" + save + "_autocorr.png")
def plotcorrWTimeAxis(x, maxlags, dt, skip=1): tL = -maxlags * dt * skip tR = -tL ac = _plt.acorr(x[::skip], maxlags=maxlags, usevlines=False) _plt.close() # data is in ac[1] tps = _N.arange(tL, tR + dt, skip*dt) # time points _plt.plot(tps, ac[1]) _plt.grid() _plt.ylim(-1, 1) _plt.axhline(y=0, ls="--") return tps, ac[1]
def plot_acorr(hist, bins): lag, acorr, temp1, temp2 = plt.acorr(hist, normed = True, detrend = mlab.detrend_linear, maxlags = None) lag = np.array(lag, dtype = np.float)*0.032 ind = (lag >=0) fig = plt.figure() ax = fig.add_subplot(111) ax.plot(lag[ind], acorr[ind]) ax.set_ylabel('Autocorrelation coefficient') ax.set_xlabel('Time scale [s]') ax.set_ylim(-0.03, +0.03) ax.set_xlim(0., 60.) pd.plotfile(fig, 'acorr') return fig, ax
def acorrs(mARp, mdn, realDat=False): fx = mARp.fx if not realDat: fx = mARp.x _plt.acorr(fx.flatten(), usevlines=False, maxlags=300, color="red", linestyle="-", marker=".", ms=0, lw=2) _plt.acorr(mdn.flatten(), usevlines=False, maxlags=300, color="black", linestyle="-", marker=".", ms=0, lw=2) _plt.savefig("acorrs") _plt.close()
def plot_autocorellation(args, log_folder, n, dataset): results_folder = log_folder + "/" + dataset filename = os.path.join(results_folder, args.log_filename + ".npy") try: logging.info(f"Loading {filename}") states = np.load(filename) except: logging.error( f"{filename}.npy was not found. Surely means filter did not finish" ) raise FileNotFoundError # load all states meas = states[:, 46:49] if os.path.exists(os.path.join(results_folder, "vio_states.npy")): vio_states = np.load(os.path.join(results_folder, "vio_states.npy")) ref_disp = vio_states[:, 15:18] else: logging.error( "vio_states.npy was not found. you shoud create it with plot_state.py before... sorry :(" ) raise FileNotFoundError fig = plt.figure("autocorellation " + dataset) meas_err = meas - ref_disp meas_err_update = meas_err[~np.isnan(meas_err).any(axis=1)] logging.warning("We assume update frequency at 20hz for autocorrelation") for i in range(3): plt.subplot(3, 1, i + 1) plt.acorr(meas_err_update[:, i], maxlags=100, lw=2, usevlines=False, label=n) locs, labels = plt.xticks() # Get locations and labels for (l, t) in zip(locs, labels): t.set_text(str(l / 20.0) + "s") plt.xticks(locs, labels) # Set locations and labels plt.xlim(left=0) plt.grid() plt.legend()
"""Plot the autocorrelation of the specified column """ import sys import csv import matplotlib.pyplot as plot import loaders tsv_filename = sys.argv[1] col = int(sys.argv[2]) data = loaders.data_from_tsv(tsv_filename, [col])[0] scatter = plot.acorr(data, linewidth=3, normed=True) plot.savefig(sys.argv[3])
# Plot results fig2 = plt.figure() # Plot original timeseries plt.subplot(221) plt.plot(x, y, color='k') plt.xlabel(u"Время") plt.ylabel(u"Значение") #plt.title("Original timeseries") plt.grid(True) # ACF plot if f_use_symmetric_ACF: # Symmetric ACF plt.subplot(222) plt.acorr(y, maxlags=None, color='k') plt.xlabel(u"Время") plt.ylabel(u"АКФ") #plt.title("Timeseries ACF") plt.grid(True) else: # Asymmetric ACF autocorrelation_plot(y, ax=plt.subplot(222), color='k') plt.xlabel(u'Шаг') plt.ylabel(u'АКФ') plt.title('') # Spectrum plot plt.subplot(223) plt.plot(freq, np.log(spectrum), color='k') plt.xlabel(u"Частота")
print(['Quantiles (0.25, 0.5, 0.75)', g.quantile(q=[0.25, 0.5, 0.75])]) print(['Correlations', g.corr()]) # try some graphs g.plot() #g.boxplot() g.hist(bins=20) #%% # pyplot apps gy = g.ix[1:,0] gp = g.ix[1:,1] #%% #acf_gy = pl.acorr(gy, maxlags=48) acf_gp = pl.acorr(gp, maxlags=60) #pl.xcorr(gy, gp, maxlags=12) #acf_gp = np.correlate(gp, gp, mode='same') #acf = acf[12:] phi = 0.991 acf_arma = 0.87*phi**abs(acf_gp[0]) pl.bar(acf_gp[0], acf_gp[1]) pl.plot(acf_gp[0], acf_arma, 'r*') #pl.plot(acf_gp[0], 0.95**acf_gp[0], 'r*') #%% # Example import matplotlib.pyplot as plt import numpy as np
print "Finished UnPickling" burnIn = 1000 endPoint = 100000 lag = 100 acorrOnly = False if acorrOnly: # plt.figure(tight_layout=True) plt.subplot(211) plt.title("Autocorrelation of samples") plt.ylabel("Correlation") plt.xlim((-1, 101)) plt.acorr(samples.T[0][burnIn:endPoint] - np.mean(samples.T[0][burnIn:endPoint]), maxlags=100) # pri/nt samples.T[0][burnIn:] plt.subplot(212) plt.xlabel("Lag") plt.ylabel("Correlation") plt.xlim((-1, 101)) plt.acorr(samples.T[1][burnIn:endPoint] - np.mean(samples.T[1][burnIn:endPoint]), maxlags=100) # print samples.T[1][burnIn:] plt.show() else: from EMComparisonToy import EMSpread EMmeans = EMSpread(numPoints) plt.figure(tight_layout=True) plt.title("log $p(\\theta|x)$ and 100 EM estimates, {} points ".format(numPoints))
from scipy.interpolate import interp1d x = 2*np.sin(np.arange(100)/10.0) x += np.random.randn(len(x)) plt.clf() plt.subplot(2,1,1) plt.plot(x, '-s') plt.ylabel('x', fontsize=20) plt.grid(True) plt.xlabel('Time') plt.subplot(2,1,2) c = plt.acorr(x, usevlines=True, normed=True, maxlags=50, lw=2) plt.grid(True) plt.axhline(0, color='black', lw=2) plt.axhline(1/np.exp(1), color='red') plt.ylabel('Autocorrelation') plt.xlim(xmin=0,xmax=100) plt.xlabel('Lag') plt.savefig('/home/tomer/my_books/python_in_hydrology/images/corr_1.png') lags = c[0] auto_corr = c[1] print(lags) auto_corr = auto_corr[lags>=0] lags = lags[lags>=0] n = sum(auto_corr>np.exp(-1)) f = interp1d([auto_corr[n], auto_corr[n-1]], [lags[n], lags[n-1]])
#etacorr = np.correlate(eta, eta, mode='same') #auto correlate eta with eta?, unnormalized, what are x and y axes? #etadiff = [x-y for x in eta for y in eta if y <20 and y >-5 and x <20 and x >-5] #NOPE! #eta =[] #print len(eta), len(etacorr) #etadiff => want pairwise difference #s=[eta,eta] #NOPE!!! #s=list(itertools.product(eta,eta)) #this does the right thing BUT EATS ALL MEMORY YET AGAIN! #print s xlab="Eta1" ylab="Eta2" tle = filename.strip('.hepmc') fig = figure() ax = fig.add_subplot(111) plt.acorr(eta, usevlines=False)#, bins=100) #autocorrelation #plt.xcorr(eta, eta)#, bins=100) #cross correlation #plt.scatter(eta, eta,c=etacorr, cmap='jet')#this is meaningless really #plt.hist(s) #xlim(-5,20) #ylim(-5,20) xlabel(xlab) ylabel(ylab) title(tle) plt.savefig(pdf,format='pdf') #plt.show() close() ''' xlab="eta1" ylab="eta2"
# Add titles etc. plt.title("Histogram") plt.xlabel("value") plt.ylabel("count") # Plot a zoomed view in the next cell. Again, check docs and explore plt.subplot('223') # Tighten the layout so everything fits plt.tight_layout() plt.plot(data["sample_900"]) plt.xlim(50,200) plt.title("Zoomed") # Finally, lets do an autocorrelation plt.subplot('224') # Tighten the layout so everything fits plt.tight_layout() plt.acorr(data["sample_900"]) plt.title("autocorrelation") # Show the figure plt.show()
p3 = plot.hist( E2_data, bins=250, align='mid' ) ax2 = plot.twinx() density = gaussian_kde(E2_data) x2 = linspace(-10, 70, 5000) p4 = plot.plot( x2, density(x2), 'k-', linewidth=2 ) plot.ylabel( "PDF", fontsize=axis_label_fontsize) #ax.ticklabel_format(style='sci', axis='x', scilimits=(0,0) ) #ax.ticklabel_format(style='sci', axis='y', scilimits=(0,0) ) plot.figure(4) p5 = plot.acorr(E2_data) plot.figure(3) plot.xlabel( " E3 ", fontsize=axis_label_fontsize) plot.ylabel( "Output Samples", fontsize=axis_label_fontsize) p5 = plot.hist( E3_data, bins=50, align='mid' ) ax3 = plot.twinx() density = gaussian_kde(E3_data) x3 = linspace(-10, 70, 5000) p6 = plot.plot( x3, density(x3), 'k-', linewidth=2 )
def do_XRB(): sname = 'XTE 1550-564' data_file = data_dir + 'LC_B_3.35-12.99keV_1div128s_total.fits' data = fits.open(data_file)[1].data tsecs = data['TIME'] flux = data['RATE'] dt = tsecs[1:] - tsecs[:-1] gap = np.where(dt > 1)[0] tsecs = tsecs[gap[0]+1:gap[1]][:40000] flux = flux[gap[0]+1:gap[1]][:40000] tsecs0 = tsecs.copy() flux0 = flux.copy() ndown_sample = 4000 idx = np.random.permutation(len(flux0))[:ndown_sample] idx.sort() tsecs = tsecs[idx] logflux = np.log(flux[idx]) ferr = np.sqrt(flux[idx]) logf_err = ferr / flux[idx] # # high-frequency sampling lightcurve # high_cutoff = 10000 # tsecs_high = tsecs[:high_cutoff] # logflux_high = np.log(flux[:high_cutoff]) # ferr_high = np.sqrt(flux[:high_cutoff]) # logferr_high = ferr_high / flux[:high_cutoff] # # ndown_sample_high = 1000 # idx_high = np.random.permutation(len(logflux_high))[:ndown_sample_high] # idx_high.sort() # # # middle-frequency sampling lightcurve # tsecs_mid = tsecs[high_cutoff:] # logflux_mid = np.log(flux[high_cutoff:]) # ferr_mid = np.sqrt(flux[high_cutoff:]) # logf_err_mid = ferr_mid / flux[high_cutoff:] # # logf_err = np.sqrt(0.00018002985939372774 / 2.0 / np.median(dt)) # eyeballed from periodogram # # logf_err = np.ones(len(tsecs)) * logf_err # # ndown_sample_mid = 4000 - ndown_sample_high # idx_mid = np.random.permutation(len(logflux_mid))[:ndown_sample_mid] # idx_mid.sort() # # tsecs = np.concatenate((tsecs_high[idx_high], tsecs_mid[idx_mid])) # logflux = np.concatenate((logflux_high[idx_high], logflux_mid[idx_mid])) # logf_err = np.concatenate((logferr_high[idx_high], logf_err_mid[idx_mid])) # idx = np.concatenate((idx_high, idx_mid)) plt.plot(tsecs0, np.log(flux0)) plt.errorbar(tsecs, logflux, yerr=logf_err) print 'Measurement errors are', np.mean(logf_err) / np.std(logflux) * 100, ' % of observed standard deviation.' print 'Mean time spacing:', np.mean(tsecs[1:] - tsecs[:-1]) # print 'Mean time spacing for high-frequency sampling:', np.mean(tsecs_high[idx_high[1:]]-tsecs_high[idx_high[:-1]]) # print 'Mean time spacing for low-frequency sampling:', np.mean(tsecs_mid[idx_mid[1:]]-tsecs_mid[idx_mid[:-1]]) plt.show() plt.clf() plt.plot(tsecs, logflux) plt.show() plt.hist(logflux, bins=100, normed=True) plt.xlabel('log Flux') print 'Standard deviation in lightcurve:', np.std(logflux) print 'Typical measurement error:', np.mean(logf_err) plt.show() plt.clf() assert np.all(np.isfinite(tsecs)) assert np.all(np.isfinite(logflux)) assert np.all(np.isfinite(logf_err)) dt_idx = tsecs[1:] - tsecs[:-1] assert np.all(dt_idx > 0) load_pickle = True if load_pickle: carma_sample = cPickle.load(open(data_dir + 'xte1550_p5q4.pickle', 'rb')) else: carma_sample = make_sampler_plots(tsecs, logflux, logf_err, 7, 'xte1550_', sname, njobs=7) plt.subplot(111) pgram, freq = plt.psd(np.log(flux0), 512, 2.0 / np.median(dt), detrend=detrend_mean) plt.clf() ax = plt.subplot(111) print 'Getting bounds on PSD...' psd_low, psd_hi, psd_mid, frequencies = carma_sample.plot_power_spectrum(percentile=95.0, sp=ax, doShow=False, color='SkyBlue', nsamples=5000) psd_mle = cm.power_spectrum(frequencies, carma_sample.mle['sigma'], carma_sample.mle['ar_coefs'], ma_coefs=np.atleast_1d(carma_sample.mle['ma_coefs'])) ax.loglog(freq / 2, pgram, 'o', color='DarkOrange') nyquist_freq = np.mean(0.5 / dt_idx) nyquist_idx = np.where(frequencies <= nyquist_freq)[0] ax.loglog(frequencies, psd_mle, '--b', lw=2) # noise_level = 2.0 * np.mean(dt_idx) * np.mean(logf_err ** 2) noise_level0 = 0.00018002985939372774 / 2.0 # scale the noise level seen in the PSD noise_level = noise_level0 * (0.5 / np.median(dt)) / nyquist_freq ax.loglog(frequencies[nyquist_idx], np.ones(len(nyquist_idx)) * noise_level, color='grey', lw=2) # ax.loglog(frequencies, np.ones(len(frequencies)) * noise_level0) ax.set_ylim(bottom=noise_level0 / 10.0) ax.annotate("Measurement Noise Level", (3.0 * ax.get_xlim()[0], noise_level / 1.5)) ax.set_xlabel('Frequency [Hz]') ax.set_ylabel('Power Spectral Density [fraction$^2$ Hz$^{-1}$]') plt.title(sname) plt.savefig(base_dir + 'plots/xte1550_psd.eps') # plot the standardized residuals and compare with the standard normal plt.clf() kfilter, mu = carma_sample.makeKalmanFilter('map') kfilter.Filter() kmean = np.asarray(kfilter.GetMean()) kvar = np.asarray(kfilter.GetVar()) standardized_residuals = (carma_sample.y - mu - kmean) / np.sqrt(kvar) plt.hist(standardized_residuals, bins=100, normed=True, color='SkyBlue', histtype='stepfilled') plt.xlabel('Standardized Residuals') plt.ylabel('Probability Distribution') xlim = plt.xlim() xvalues = np.linspace(xlim[0], xlim[1], num=100) expected_pdf = np.exp(-0.5 * xvalues ** 2) / np.sqrt(2.0 * np.pi) plt.plot(xvalues, expected_pdf, 'k', lw=3) plt.title(sname) plt.savefig(base_dir + 'plots/xte1550_resid_dist.eps') # plot the autocorrelation function of the residuals and compare with the 95% confidence intervals for white # noise plt.clf() maxlag = 50 wnoise_upper = 1.96 / np.sqrt(carma_sample.time.size) wnoise_lower = -1.96 / np.sqrt(carma_sample.time.size) plt.fill_between([0, maxlag], wnoise_upper, wnoise_lower, facecolor='grey') lags, acf, not_needed1, not_needed2 = plt.acorr(standardized_residuals, maxlags=maxlag, lw=3) plt.xlim(0, maxlag) plt.ylim(-0.2, 0.2) plt.xlabel('Time Lag') plt.ylabel('ACF of Residuals') plt.savefig(base_dir + 'plots/xte1550_resid_acf.eps') # plot the autocorrelation function of the squared residuals and compare with the 95% confidence intervals for # white noise plt.clf() squared_residuals = standardized_residuals ** 2 wnoise_upper = 1.96 / np.sqrt(carma_sample.time.size) wnoise_lower = -1.96 / np.sqrt(carma_sample.time.size) plt.fill_between([0, maxlag], wnoise_upper, wnoise_lower, facecolor='grey') lags, acf, not_needed1, not_needed2 = plt.acorr(squared_residuals - squared_residuals.mean(), maxlags=maxlag, lw=3) plt.xlim(0, maxlag) plt.ylim(-0.2, 0.2) plt.xlabel('Time Lag') plt.ylabel('ACF of Sqrd. Resid.') plt.savefig(base_dir + 'plots/xte1550_sqrres_acf.eps') if not load_pickle: pfile = open(data_dir + 'xte1550_nonoise.pickle', 'wb') cPickle.dump(carma_sample, pfile) pfile.close()
idra = k_stability_analysis(forwardjump) le = np.shape(idra) print le eg = le[1] eg = eg / 2 eg = 1000 print "eg = ", eg for i in range(eg): #numpat incon = idra[0:, i] #print incon[(len(le) / 2.):] #print type(incon) boxer = plt.acorr(incon, usevlines=False, normed = True, maxlags=50) boxer = boxer[1] #print "boxer = ", boxer #print "type boxer = ", type(boxer) the = boxer[(len(boxer) / 2):] if i == 0: huk = np.zeros((len(the))) huk = np.add(huk, boxer[len(boxer)/2:]) print "pre huk = ", huk huk = huk / eg print "huk = ", huk a = sys.argv[3]
"""收盘价变化率初探 当然做实时量化交易我们最感兴趣的还是每秒的变化率。那么我们来看看股价变 化有什么样的分布。 """ ## 下面我们可以看到,Series对象的很多操作可以链式在一行完成,非常方便: data_trading_hour["Close"].diff().plot.hist() ## plt.savefig("plots/price-change-histogram.png") plt.show() change = data_trading_hour["Close"].diff()/data_trading_hour["Close"] change.plot() ## plt.savefig("plots/price-change-plot.png") plt.show() ## 看看前一秒变化率和后一秒变化率之间的关系。 change.shift(1).corr(change) change.shift(2).corr(change) ## 系统性的看看相差时间长度和相关性变化之间的差距: plt.acorr(change[1:], lw = 2) ## plt.savefig("plots/price-change-auto-correlation.png") plt.show() ## 哇,没想到相关程度这么大,那我们在后面好好利用这样的性质做做文章。
import numpy as np from matplotlib import pyplot energies = np.load('energy_left3.npy')#25 ions, long # energies = np.load('energy_left5ms_5ions.npy');energies = energies[0] #5 ions long pyplot.subplot(311) pyplot.title('5 ions energy dynamics') pyplot.plot(energies) pyplot.xlim(0,1000) pyplot.subplot(312) pyplot.title('energy histogarm') pyplot.hist(energies, 100) pyplot.subplot(313) pyplot.title('autocorrelation') pyplot.acorr(energies, maxlags = 1000) pyplot.xlim(0,1000) pyplot.tight_layout() pyplot.show()