def ensemble_net_histogram_plot(param=0.5, sig=0, NTraj=20): ### change here to load a different network model net_file = '.\Models\\fbm_reg_ensemble_10_steps_' + np.str( NTraj) + '_traj.h5' reg_model = load_model(net_file) Hnet = np.zeros([200, 1]) Hmsd = np.zeros([200, 1]) for k in range(200): X = np.zeros([1, NTraj, 9, 1]) Y = np.zeros([1, NTraj, 9, 1]) Xmsd = np.zeros([2, NTraj, 10]) for i in range(NTraj): H = np.random.uniform(low=param - 0.05, high=param + 0.05) x, y, t = fbm_diffusion(n=100, H=H, T=4) nx = sig * np.random.randn(len(x), ) ny = sig * np.random.randn(len(x), ) x = x + nx y = y + ny Xmsd[0, i, :] = x[25:35] Xmsd[1, i, :] = y[25:35] dx = np.diff(x[25:35], axis=0) dy = np.diff(y[25:35], axis=0) X[0, i, :, 0] = autocorr(dx / (1e-10 + np.std(dx))) Y[0, i, :, 0] = autocorr(dy / (1e-10 + np.std(dy))) tVec, MSD_mat = matrix_of_MSDs(Xmsd) emsd = np.mean(MSD_mat, axis=0) a, b = curve_fit(curve_func, np.log(tVec), np.log(1e-10 + emsd.ravel())) Hx = reg_model.predict(X) Hy = reg_model.predict(Y) Hnet[k, 0] = np.mean([Hx, Hy]) Hmsd[k, 0] = a[1] / 2 plt.figure() plt.hist(Hmsd, alpha=0.5) plt.hist(Hnet, alpha=0.5) plt.legend(['Ensemble MSD estimation (10 steps)', 'Net estimation']) plt.xlabel('H') plt.xlim([0, 1]) plt.ylabel('Counts') plt.title('Comparison to Ensemble MSD (10 step tracks)') return Hnet, Hmsd
def predict_1D(x, stepsActual, reg_model): if len(x) < stepsActual: return 0 dx = (np.diff(x[:stepsActual], axis=0)[:, 0]) dx = autocorr((dx - np.mean(dx)) / (np.std(dx) + 1e-10)) dx = np.reshape(dx, [1, np.size(dx), 1]) pX = reg_model.predict(dx) return pX
def plotacf(ax, lc, peaks=True, detrend_order=2, acf_kwargs={}, pk_kwargs={}): lag, power = utils.autocorr( lc.t, lc.flux - utils.trend(lc.t, lc.flux, detrend_order)) pks, wts, _ = utils.candidates(lag, power) ax.plot(lag, power, label='ACF', **acf_kwargs) if peaks and (len(pks) > 0): if 'alpha' in pk_kwargs: wts = pk_kwargs['alpha'] * np.ones_like(w) else: wts /= max(wts) for i, (p, w) in enumerate(zip(pks, wts)): if i == 0: ax.axvline(p, alpha=w, **pk_kwargs, label='candidate periods') ax.axvline(p, alpha=w, **pk_kwargs) return ax
def MT_net_on_file(file): f = scipy.io.loadmat(file) for k in f.keys(): if k[0] == '_': continue varName = k data = f[varName] NAxes = (np.shape(data)[1] - 1) numTraj = len(np.unique(data[:, NAxes])) # supplied networks can recive up to 150 short trajectories (with increments of 10) if numTraj > 150: data = data[:1500, :] numTraj = 150 # supplied networks run on a set number of trajectories (10,20,30,...140,150) if numTraj % 10 > 0: numTraj = numTraj - numTraj % 10 data = data[:numTraj * 10, :] Xmat = np.zeros([NAxes, numTraj, 9, 1]) for i in range(numTraj): for j in range(NAxes): x = data[np.argwhere(data[:, NAxes] == i + 1), j] dx = np.diff(x, axis=0) dx = autocorr(dx[:, 0] / (1e-10 + np.std(dx[:, 0]))) Xmat[j, i, :, 0] = dx ### change here to load a different network model net_file = '.\Models\\fbm_reg_ensemble_10_steps_' + np.str( numTraj) + '_traj.h5' reg_model = load_model(net_file) prediction = reg_model.predict(Xmat) return prediction
def ensemble_net_estimation_rmse(param=0.5, sig=0): numTraj = np.arange(10, 151, 10) rmseMSD = np.zeros([len(numTraj), 500], dtype=np.float64) rmseNET = np.zeros([len(numTraj), 500], dtype=np.float64) HMSDVec = np.zeros([500, len(numTraj)]) HNetVec = np.zeros([500, len(numTraj)]) for nt, nt_ind in zip(numTraj, range(len(numTraj))): net_file = '.\Models\\fbm_reg_ensemble_10_steps_' + np.str( nt) + '_traj.h5' reg_model = load_model(net_file) for k in range(500): Xnet = np.zeros([1, nt, 9, 1]) Ynet = np.zeros([1, nt, 9, 1]) Xmsd = np.zeros([2, nt, 10]) for i in range(nt): H = np.random.uniform(low=param - 0.05, high=param + 0.05) x, y, t = fbm_diffusion(n=100, H=H, T=4) x = x + sig * np.random.randn(len(x), ) # add noise y = y + sig * np.random.randn(len(x), ) dx = np.diff(x[25:35], axis=0) dy = np.diff(y[25:35], axis=0) Xnet[0, i, :, 0] = autocorr(dx / (1e-10 + np.std(dx))) Ynet[0, i, :, 0] = autocorr(dy / (1e-10 + np.std(dy))) Xmsd[0, i, :] = x[25:35] Xmsd[1, i, :] = y[25:35] tVec, MSD_mat = matrix_of_MSDs(Xmsd) emsd = np.mean(MSD_mat, axis=0) a, b = curve_fit(curve_func, np.log(tVec), np.log(1e-10 + emsd.ravel())) HMSDVec[k, nt_ind] = a[1] / 2 Hx = reg_model.predict(Xnet) Hy = reg_model.predict(Ynet) HNetVec[k, nt_ind] = np.mean([Hx, Hy]) rmseMSD[nt_ind, ] = np.sqrt(np.mean((HMSDVec[:, nt_ind] - param)**2)) rmseNET[nt_ind, ] = np.sqrt(np.mean((HNetVec[:, nt_ind] - param)**2)) plt.plot(numTraj, np.mean(rmseMSD, axis=1)) plt.plot(numTraj, np.mean(rmseNET, axis=1)) plt.xlabel('Number of trajectories used in ensemble MSD') plt.title('RMSE') plt.ylim([0, 0.25]) plt.xlim([10, 150]) plt.xticks(np.arange(10, 151, 20)) plt.ylabel('RMSE') plt.legend(['RMSE ensemble MSD', 'RMSE MT network']) EMSD_err = np.std(HMSDVec, axis=0) EMSD_plot = np.mean(HMSDVec, axis=0) net_plot = np.mean(HNetVec, axis=0) net_err = np.std(HNetVec, axis=0) # Create the plot object _, ax = plt.subplots() ax.plot(numTraj, EMSD_plot, lw=1, color='b', alpha=1, label='MSD estimation') ax.plot(numTraj, net_plot, lw=1, color='r', alpha=1, label='Network estimation') # Shade the confidence interval ax.fill_between(numTraj, EMSD_plot - EMSD_err, EMSD_plot + EMSD_err, color='b', alpha=0.3) ax.fill_between(numTraj, net_plot - net_err, net_plot + net_err, color='r', alpha=0.3) ax.set_title('Value convergence') ax.set_xlabel('Number of trajectories used in ensemble MSD') ax.set_ylabel('H') ax.set_ylim([param - 0.3, param + 0.3]) ax.set_xlim([10, 150]) ax.set_xticks(np.arange(10, 151, 20)) ax.legend(loc='best') return rmseMSD, rmseNET, HMSDVec, HNetVec
def AR_est_YW(x, order, rxx=None): r"""Determine the autoregressive (AR) model of a random process x using the Yule Walker equations. The AR model takes this convention: .. math:: x(n) = a(1)x(n-1) + a(2)x(n-2) + \dots + a(p)x(n-p) + e(n) where e(n) is a zero-mean white noise process with variance sig_sq, and p is the order of the AR model. This method returns the a_i and sigma The orthogonality property of minimum mean square error estimates states that .. math:: E\{e(n)x^{*}(n-k)\} = 0 \quad 1\leq k\leq p Inserting the definition of the error signal into the equations above yields the Yule Walker system of equations: .. math:: R_{xx}(k) = \sum_{i=1}^{p}a(i)R_{xx}(k-i) \quad1\leq k\leq p Similarly, the variance of the error process is .. math:: E\{e(n)e^{*}(n)\} = E\{e(n)x^{*}(n)\} = R_{xx}(0)-\sum_{i=1}^{p}a(i)R^{*}(i) Parameters ---------- x : ndarray The sampled autoregressive random process order : int The order p of the AR system rxx : ndarray (optional) An optional, possibly unbiased estimate of the autocorrelation of x Returns ------- ak, sig_sq : The estimated AR coefficients and innovations variance """ if rxx is not None and type(rxx) == np.ndarray: r_m = rxx[:order + 1] else: r_m = utils.autocorr(x)[:order + 1] Tm = linalg.toeplitz(r_m[:order]) y = r_m[1:] print(Tm.shape) print(y.shape) ak = linalg.solve(Tm, y) sigma_v = r_m[0].real - np.dot(r_m[1:].conj(), ak).real return ak, sigma_v
def AR_est_LD(x, order, rxx=None): r"""Levinson-Durbin algorithm for solving the Hermitian Toeplitz system of Yule-Walker equations in the AR estimation problem .. math:: T^{(p)}a^{(p)} = \gamma^{(p+1)} where .. math:: :nowrap: \begin{align*} T^{(p)} &= \begin{pmatrix} R_{0} & R_{1}^{*} & \cdots & R_{p-1}^{*}\\ R_{1} & R_{0} & \cdots & R_{p-2}^{*}\\ \vdots & \vdots & \ddots & \vdots\\ R_{p-1}^{*} & R_{p-2}^{*} & \cdots & R_{0} \end{pmatrix}\\ a^{(p)} &=\begin{pmatrix} a_1 & a_2 & \cdots a_p \end{pmatrix}^{T}\\ \gamma^{(p+1)}&=\begin{pmatrix}R_1 & R_2 & \cdots & R_p \end{pmatrix}^{T} \end{align*} and :math:`R_k` is the autocorrelation of the kth lag Parameters ---------- x : ndarray the zero-mean stochastic process order : int the AR model order--IE the rank of the system. rxx : ndarray, optional (at least) order+1 samples of the autocorrelation sequence Returns ------- ak, sig_sq The AR coefficients for 1 <= k <= p, and the variance of the driving white noise process """ if rxx is not None and type(rxx) == np.ndarray: rxx_m = rxx[:order + 1] else: rxx_m = utils.autocorr(x)[:order + 1] w = np.zeros((order + 1, ), rxx_m.dtype) # intialize the recursion with the R[0]w[1]=r[1] solution (p=1) b = rxx_m[0].real w_k = rxx_m[1] / b w[1] = w_k p = 2 while p <= order: b *= 1 - (w_k * w_k.conj()).real w_k = (rxx_m[p] - (w[1:p] * rxx_m[1:p][::-1]).sum()) / b # update w_k from k=1,2,...,p-1 # with a correction from w*_i i=p-1,p-2,...,1 w[1:p] = w[1:p] - w_k * w[1:p][::-1].conj() w[p] = w_k p += 1 b *= 1 - (w_k * w_k.conj()).real return w[1:], b
def autocorrelation(data, name, maxlag=100, format='png', suffix='-acf', path='./', fontmap={ 1: 10, 2: 8, 3: 6, 4: 5, 5: 4 }, verbose=1): """ Generate bar plot of a series, usually autocorrelation or autocovariance. :Arguments: data: array or list A trace from an MCMC sample. name: string The name of the object. format (optional): string Graphic output format (defaults to png). suffix (optional): string Filename suffix. path (optional): string Specifies location for saving plots (defaults to local directory). """ # If there is just one data series, wrap it in a list if rank(data) == 1: data = [data] # Number of plots per page rows = min(len(data), 4) for i, values in enumerate(data): if verbose > 0: print 'Plotting', name + suffix if not i % rows: # Generate new figure figure(figsize=(10, 6)) # New subplot subplot(rows, 1, i - (rows * (i / rows)) + 1) x = arange(maxlag) y = [autocorr(values, lag=i) for i in x] bar(x, y) # Set axis bounds ylim(-1.0, 1.0) xlim(0, len(y)) # Plot options ylabel(name, fontsize='x-small') tlabels = gca().get_yticklabels() setp(tlabels, 'fontsize', fontmap[rows]) tlabels = gca().get_xticklabels() setp(tlabels, 'fontsize', fontmap[rows]) # Save to file if not (i + 1) % rows or i == len(values) - 1: # Label X-axis on last subplot xlabel('Lag', fontsize='x-small') if not os.path.exists(path): os.mkdir(path) if rows > 4: # Append plot number to suffix, if there will be more than one suffix += '_%i' % i savefig("%s%s%s.%s" % (path, name, suffix, format))