Calculates the diffusion coefficient of a trajectory by means of the linear fitting of the TA-MSD.
Type
Default
Details
trajs
ndarray
t_lags
list
None
Time lags used for the TA-MSD.
Returns
np.array
Diffusion coefficient of the given trajectory.
Here we show an example for the calculation of the diffusion coefficient of a 2D Brownian motion trajectory. We create trajectories from displacements of variance \(\sigma =1\), which results in a diffusion coefficient \(D=0.5\).
pos = np.cumsum(np.random.randn(1070, 100, 2), axis =1)D = msd_analysis().get_diff_coeff(pos)plt.hist(D, bins =30);plt.axvline(np.mean(D), c ='k', label =f'Mean of predictions = {np.round(np.mean(D), 2)}') plt.axvline(0.5, c ='r', ls ='--', label ='Expected')plt.legend()
Calculates the diffusion coefficient of a trajectory by means of the linear fitting of the TA-MSD.
Type
Default
Details
trajs
ndarray
t_lags
list
None
Time lags used for the TA-MSD.
Returns
np.array
Diffusion coefficient of the given trajectory.
To showcase this function, we generate fractional brownian motion trajectories with \(\alpha = 0.5\) and calculate their exponent:
trajs, _ = models_phenom().single_state(N =1001, T =100, alphas =0.5, dim =2)alpha = msd_analysis().get_exponent(trajs.transpose(1,0,2))plt.hist(alpha, bins =30);plt.axvline(np.mean(alpha), c ='k', label =f'Mean of predictions = {np.round(np.mean(alpha), 2)}')plt.axvline(0.5, c ='r', ls ='--', label ='Expected')plt.legend()
Computes the changes points a multistate trajectory based on the Convex Hull approach proposed in PRE 96 (022144), 2017.
Type
Default
Details
trajs
np.array
NxT matrix containing N trajectories of length T.
tau
int
10
Time window over which the CH is calculated.
metric
{‘area’, ‘volume’}
volume
Calculate change points w.r.t. area or volume of CH.
Returns
list
Change points of the given trajectory.
We showcase the use of the convex hull in a Brownian motion trajectory with two distinct diffusion coefficients, one 10 times the other:
# Generate trajectories and plot change pointsT =100; on =40; off =60; tau =5traj = np.random.randn(T, 2)traj[on:off, :] = traj[on:off, :]*10traj = traj.cumsum(0)plt.axvline(on-tau, c ='k')plt.axvline(off-tau, c ='k', label ='True change points')# Calculate variable Sd Sd = np.zeros(traj.shape[0]-2*tau)for k inrange(traj.shape[0]-2*tau): Sd[k] = ConvexHull(traj[k:(k+2*tau)], ).volume # Compute change points both with volume and areaCPs = CH_changepoints([traj], tau = tau)[0].flatten()-tauCPs_a = CH_changepoints([traj], tau = tau, metric ='area')[0].flatten()-tau# Plot everythinglabel_cp ='CH Volume'for cp in CPs: plt.axvline(cp, c ='g', alpha =0.8, ls ='--', label = label_cp) label_cp =''label_cp ='CH Area'for cp in CPs_a: plt.axvline(cp, alpha =0.8, ls ='--', c ='orange', label = label_cp) label_cp =''plt.plot(Sd, '-o')plt.axhline(Sd.mean(), label ='CH Volume mean', c ='g',)plt.legend()plt.xlabel('n'); plt.ylabel(r'$S_d(n)$')
Text(0, 0.5, '$S_d(n)$')
Cramér-Rao lower bound
Expressions of the CRLB for the estimation of the diffusion coefficient for trajectories without noise, for dimension 1 and 2, at different trajectory lengths
Calculates the bound for S(D)/D, i.e. ratio between the standard deviation and the expected value of D This holds for x->0 only (i.e. no noise)! See PRE 85, 061916 (2012) for full equation.
Type
Default
Details
T
int
Length of the trajectory
dim
int
1
Dimension of the trajectoy
Returns
float
Cramér-Rao bound
Here we show how for Brownian motion trajectories, the MSD with is optimal and converges to the CRLB.
#eval: falseTs = np.arange(10, 100) # Trajectory lengthsDs = np.arange(0.5, 2.1, 0.5) # We will test on different diffusion coefficients# Saving the standard deviationsstd_ds = np.zeros((len(Ds), len(Ts)))for idxD, D inenumerate(tqdm(Ds)):# Generate the BM trajectories with given parameters trajs = (np.sqrt(2*D)*np.random.randn(int(1e4), Ts[-1], 1)).cumsum(1) for idxL, T inenumerate(tqdm(Ts)): std_ds[idxD, idxL] = np.array(msd_analysis().get_diff_coeff(trajs = trajs[:, :T], t_lags = [1,2])).std()