How can I improve the performance of the following code which calculates the mean squared displacement for coordinates stored in data? Here for each time difference dn
all possible distance combinations are taken into account:
---- distance combinations between points
dn=1: 1 and 2, 2 and 3, 3 and 4, ... n-1 and n
dn=2: 1 and 3, 2 and 4, 3 and 5, ... n-2 and n-1
dn=3: 1 and 4, 2 and 5, 3 and 6, ... n-3 and n-2
...
and so on ...
data = Import["data.txt"]; (*data file: http://goo.gl/Fmm9fZ*)
msd = Array[0 &, {n - 1, n - 1}];
xSquared = Array[0 &, {n - 1, n - 1}];
ySquared = Array[0 &, {n - 1, n - 1}];
Table[
dx = data[[n + dn, 1]] - data[[n, 1]];
dy = data[[n + dn, 2]] - data[[n, 2]];
msd[[dn, n]] = dx^2 + dy^2;
xSquared[[dn, n]] = dx^2;
ySquared[[dn, n]] = dy^2,
{dn, 1, n - 1},
{n, 1, n - dn}
]; // AbsoluteTiming
Is something else possible?
Later on the mean of the arrays msd
, ySquared
and ySquared
is calculated taking into account that of each dn
the relevant length is n-dn
.
Table[
msdMean[[dn]] = Mean[msd[[dn, 1 ;; n - dn]]];
msdStdDev[[dn]] = StandardDeviation[msd[[dn, 1 ;; n - dn]]];
xSquaredMean[[dn]] = Mean[xSquared[[dn, 1 ;; n - dn]]];
xSquaredStdDev[[dn]] =
StandardDeviation[xSquared[[dn, 1 ;; n - dn]]];
ySquaredMean[[dn]] = Mean[ySquared[[dn, 1 ;; n - dn]]];
ySquaredStdDev[[dn]] =
StandardDeviation[ySquared[[dn, 1 ;; n - dn]]],
{dn, 1, n - 2}
];
My notebook which I used to calculate and plot them is here: http://goo.gl/ztPqxN
Update for FFT solution:
https://stackoverflow.com/questions/34222272/computing-mean-square-displacement-using-python-and-fft
FFT-Code in Python:
https://github.com/soft-matter/trackpy/blob/master/trackpy/motion.py
the MSD should be done by looking at non-overlapping windows of a given time and then averaging those. There was some recent work (github.com/soft-matter/trackpy/pull/337) to make msd computations faster.
How to get errors (standard deviation of mean value data set):
- see github.com/soft-matter/trackpy/pull/352 for discussion of computing errors on the msd.
UPDATE:
Please use for your code my data file: http://goo.gl/Fmm9fZ
I am very much interested that the numerical results of my code can be reproduced and that for the whole set of calculations I did (three mean value data sets and their errors) the performance is compared.
data
... but very slow $\endgroup$