def fftsurr(x): """ Compute an FFT phase randomized surrogate of x """ z = fft(x) a = 2.*pi*1j phase = a*rand(len(x)) z = z*exp(phase) return inverse_fft(z).real
def fftsurr(x, detrend=detrend_none, window=window_none): """ Compute an FFT phase randomized surrogate of x """ x = window(detrend(x)) z = fft(x) a = 2.*pi*1j phase = a*rand(len(x)) z = z*exp(phase) return inverse_fft(z).real
def fftsurr(x, detrend=detrend_none, window=window_none): """ Compute an FFT phase randomized surrogate of x """ x = window(detrend(x)) z = fft(x) a = 2. * pi * 1j phase = a * rand(len(x)) z = z * exp(phase) return inverse_fft(z).real
def exp_safe(x): """Compute exponentials which safely underflow to zero. Slow but convenient to use. Note that NumArray will introduce proper floating point exception handling with access to the underlying hardware.""" if type(x) is ArrayType: return exp(clip(x,exp_safe_MIN,exp_safe_MAX)) else: return math.exp(x)
def exp_safe(x): """Compute exponentials which safely underflow to zero. Slow but convenient to use. Note that NumArray will introduce proper floating point exception handling with access to the underlying hardware.""" if type(x) is ArrayType: return exp(clip(x, exp_safe_MIN, exp_safe_MAX)) else: return math.exp(x)
def bivariate_normal(X, Y, sigmax=1.0, sigmay=1.0, mux=0.0, muy=0.0, sigmaxy=0.0): """ Bivariate gaussan distribution for equal shape X, Y http://mathworld.wolfram.com/BivariateNormalDistribution.html """ Xmu = X-mux Ymu = Y-muy rho = sigmaxy/(sigmax*sigmay) z = Xmu**2/sigmax**2 + Ymu**2/sigmay - 2*rho*Xmu*Ymu/(sigmax*sigmay) return 1.0/(2*pi*sigmax*sigmay*(1-rho**2)) * exp( -z/(2*(1-rho**2)))
def levypdf(x, gamma, alpha): "Returm the levy pdf evaluated at x for params gamma, alpha" N = len(x) if N % 2 != 0: raise ValueError, 'x must be an event length array; try\n' + \ 'x = linspace(minx, maxx, N), where N is even' dx = x[1] - x[0] f = 1 / (N * dx) * arange(-N / 2, N / 2, Float) ind = concatenate([arange(N / 2, N, Int), arange(N / 2, Int)]) df = f[1] - f[0] cfl = exp(-gamma * absolute(2 * pi * f)**alpha) px = fft(take(cfl, ind) * df).astype(Float) return take(px, ind)
def levypdf(x, gamma, alpha): "Returm the levy pdf evaluated at x for params gamma, alpha" N = len(x) if N%2 != 0: raise ValueError, 'x must be an event length array; try\n' + \ 'x = linspace(minx, maxx, N), where N is even' dx = x[1]-x[0] f = 1/(N*dx)*arange(-N/2, N/2, Float) ind = concatenate([arange(N/2, N, Int), arange(N/2,Int)]) df = f[1]-f[0] cfl = exp(-gamma*absolute(2*pi*f)**alpha) px = fft(take(cfl,ind)*df).astype(Float) return take(px, ind)
def pyxplothist(graph, x_sample, Nbins=80, bin_range=None, norm=False, bars=0, lw=0, lt=0, color=(0,0,0), xlogscale=False, ylogscale=False, y_given=None, title=False): """ Plots a histogram of 'x_sample'. Arguments: 'x_sampe' - data 'Nbins' - number of bins 'bin_range' - intervall to be divided into 'Nbins' equidistant parts 'norm' - normalization of histogram (comparison with density function) 'bars' - style parameter: bars (bars=1) or steps (bars=0) 'lw', 'lt' - linewidth, linetype 'title' - title 'color' - color of histogram (r,g,b) 'xlogscale' - takes bins with logarithmically constant width, makes only sense, when 'xaxistype' of 'graph' is set to 'log' 'ylogscale' - activates necessary changes, if y-axis is logarithmic, makes only sense, when 'yaxistype' of 'graph' is set to 'log' 'ygiven' - if you already know the y-values """ steps = 1-bars # determine max. x-value for the bins x_max = max(x_sample) if bin_range != None: x_max = bin_range[1] # changes due to logarithmic x-axis: # take log of the data, check if data or range is <= zero if xlogscale: if min(x_sample) > 0: x_sample = log(x_sample) if bin_range != None: if bin_range[0] > 0 and bin_range[1] > 0: bin_range = (log(bin_range[0]),log(bin_range[1])) else: print "Given range includes values <= 0!" print "Ignoring given range..." bin_range = None else: print "Data is smaller than zero, no logarithmic x-axis possible!" print "Continuing linearly..." xlogscale = False if y_given != None: x_l = x_sample x_hist = y_given Nbins = len(x_l) else: ## histogram of 'x_sample': gives number 'x_hist' of sample points within ## 'Nbins' different of bins within 'bin_range', where the bins are ## specified by their left edge coordinate 'x_l' x_hist, x_l = histogram(x_sample, bins=Nbins, range=bin_range, normed=norm) # if logarithmic x-axis: exponentiate bin positions and data if xlogscale: x_l = exp(x_l) x_sample = exp(x_sample) y_min = 0 # offset for logarithmic y-axis if ylogscale: x_hist = x_hist+1e-16 y_min = 1e-16 # plot histogram manually for i in xrange(Nbins): # horzontal lines if i < Nbins-1: graph.pyxplot(([x_l[i],x_l[i+1]],[x_hist[i],x_hist[i]]), style="l", color=color, lt=lt, lw=lw, title=False) else: graph.pyxplot(([x_l[i],x_max],[x_hist[i],x_hist[i]]), style="l", color=color, lt=lt, lw=lw, title=False) # vertical lines if i != Nbins-1: graph.pyxplot(([x_l[i+1],x_l[i+1]],[x_hist[i],x_hist[i+1]]), style="l", color=color, lt=lt, lw=lw, title=False) if i == 0: graph.pyxplot(([x_l[i],x_l[i]],[x_hist[i],y_min]), style="l", color=color, lt=lt, lw=lw, title=title) if i == Nbins-1: graph.pyxplot(([x_max,x_max],[x_hist[i],y_min]), style="l", color=color, lt=lt, lw=lw, title=False)
def normpdf(x, *args): "Return the normal pdf evaluated at x; args provides mu, sigma" mu, sigma = args return 1/(numerix.mlab.sqrt(2*pi)*sigma)*exp(-0.5 * (1/sigma*(x - mu))**2)
def normpdf(x, *args): "Return the normal pdf evaluated at x; args provides mu, sigma" mu, sigma = args return 1 / (numerix.mlab.sqrt(2 * pi) * sigma) * exp(-0.5 * (1 / sigma * (x - mu))**2)