Mean squared displacement
Question: I am investigating the motion of a single micrometer sized particle in a low
density plasma. With a so called long distance microscope I have recorded the
motion of a particle (2726 images, fps=60 Hz).

The x,y data in mm are available here: <http://pastebin.com/qdMsaUHD>

With mathematica I got the following log-log plot for the mean squared
displacement (MSD):

[![enter image description
here](http://i.stack.imgur.com/CKiCb.png)](http://i.stack.imgur.com/CKiCb.png)

**I am new to Python and have searched for examples on how to read in the 2D
coordinates from a file, calculate and display the MSD (mean and standard
deviation).**

I have seen that there are some answers which show how to calculate the MSD
but I am not able to adapt them to my data.

I would appreciate very much if somebody could show me the full Python source
code (with all imports) which is necessary to solve my problem.

That will be for me a great opportunity and a very concrete problem to start
also programming with Python. Thank you very much in advance for your help.

I tried the following from one of the answers ([Computing the mean square
displacement of a 2d random walk in
Python](http://stackoverflow.com/questions/26472653/computing-the-mean-square-
displacement-of-a-2d-random-walk-in-
python/27709260#comment60361901_27709260)), but the code produced errors.

    
    
    import numpy as np
    import matplotlib.pyplot as plt
    
    data= [[49.136926889715, 48.4423791821561],
    [48.8104534783146, 51.0491783022365],
    [48.5231487166892, 53.3485202014],
    [48.2320069851565, 55.2569539728078],
    [47.8817794028032, 56.993296770262],
    [47.381875792142, 58.179721166033],
    ...
    [45.3409434914228, 49.0259838546922]]
    
    def compute_MSD(path):
       totalsize=len(path)
       msd=[]
       for i in range(totalsize-1):
           j=i+1
           msd.append(np.sum((path[0:-j]-path[j::])**2)/float(totalsize-j))
    
       msd=np.array(msd)
       return msd
    
    result=compute_MSD(data)
    
    plt.plot(result)
    plt.show()
    


Answer: My graphs look slightly different than yours which likely stems from a
misunderstanding of exactly what it is you are calculating, however I believe
this addresses the basic idea.

    
    
    plasma = [[0.09296720430107527, 0.09280376344086022],
    [0.09230113636363636, 0.09769886363636364],
    [0.09130555555555556, 0.10198777777777777],
    ...
    
    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    
    df = pd.DataFrame(plasma, columns=['x', 'y'])
    df['time'] = np.arange(0,len(df)) / 60.0
    df['dist'] = np.sqrt(df['x']**2 + df['y']**2)
    df['MSD'] = pd.rolling_mean((np.abs(df['dist'])**2), len(df), min_periods=1)
    

The above is my interpretation of what you mean by MSD. I am using distance
from the origin, i.e. `sqrt(x^2 + y^2)` and then applying the following
[definition](http://mathworld.wolfram.com/MeanSquareDisplacement.html)

Then you can create a plot using [matplotlib](http://matplotlib.org/) as
follows

    
    
    plt.loglog(df['time'], df['MSD'], 'o')
    plt.xlabel('t (sec)')
    plt.ylabel('MSD')
    plt.show()
    

[![enter image description
here](http://i.stack.imgur.com/K6gVg.png)](http://i.stack.imgur.com/K6gVg.png)

If you want to touch up / refine the graphs an excellent place to get a handle
on matplotlib is the tutorial section,
[here](http://matplotlib.org/users/pyplot_tutorial.html).

