Averages and fluctuations#

Formulae for averaging#

Note: this section was taken from ref 179.

When analyzing a MD trajectory averages x and fluctuations

(487)#(Δx)212 = [xx]212

of a quantity x are to be computed. The variance σx of a series of Nx values, {xi}, can be computed from

(488)#σx = i=1Nxxi2  1Nx(i=1Nxxi)2

Unfortunately this formula is numerically not very accurate, especially when σx12 is small compared to the values of xi. The following (equivalent) expression is numerically more accurate

(489)#σx = i=1Nx[xix]2

with

(490)#x = 1Nxi=1Nxxi

Using (488) and (490) one has to go through the series of xi values twice, once to determine x and again to compute σx, whereas (487) requires only one sequential scan of the series {xi}. However, one may cast (488) in another form, containing partial sums, which allows for a sequential update algorithm. Define the partial sum

(491)#Xn,m = i=nmxi

and the partial variance

(492)#σn,m = i=nm[xiXn,mmn+1]2

It can be shown that

(493)#Xn,m+k = Xn,m+Xm+1,m+k

and

(494)#σn,m+k=σn,m+σm+1,m+k+[ Xn,mmn+1Xn,m+km+kn+1 ]2  (mn+1)(m+kn+1)k

For n=1 one finds

(495)#σ1,m+k = σ1,m+σm+1,m+k + [ X1,mmX1,m+km+k ]2 m(m+k)k

and for n=1 and k=1 (494) becomes

(496)#σ1,m+1=σ1,m+[X1,mmX1,m+1m+1]2m(m+1)=σ1,m+[ X1,mmxm+1 ]2m(m+1)

where we have used the relation

(497)#X1,m+1 = X1,m+xm+1

Using formulae (496) and (497) the average

(498)#x = X1,NxNx

and the fluctuation

(499)#(Δx)212=[σ1,NxNx]12

can be obtained by one sweep through the data.

Implementation#

In GROMACS the instantaneous energies E(m) are stored in the energy file, along with the values of σ1,m and X1,m. Although the steps are counted from 0, for the energy and fluctuations steps are counted from 1. This means that the equations presented here are the ones that are implemented. We give somewhat lengthy derivations in this section to simplify checking of code and equations later on.

Part of a Simulation#

It is not uncommon to perform a simulation where the first part, e.g. 100 ps, is taken as equilibration. However, the averages and fluctuations as printed in the log file are computed over the whole simulation. The equilibration time, which is now part of the simulation, may in such a case invalidate the averages and fluctuations, because these numbers are now dominated by the initial drift towards equilibrium.

Using (493) and (494) the average and standard deviation over part of the trajectory can be computed as:

(500)#Xm+1,m+k=X1,m+kX1,mσm+1,m+k=σ1,m+kσ1,m[ X1,mmX1,m+km+k ]2 m(m+k)k

or, more generally (with p1 and qp):

(501)#Xp,q=X1,qX1,p1σp,q=σ1,qσ1,p1[ X1,p1p1X1,qq ]2 (p1)qqp+1

Note that implementation of this is not entirely trivial, since energies are not stored every time step of the simulation. We therefore have to construct X1,p1 and σ1,p1 from the information at time p using (496) and (497):

(502)#X1,p1=X1,pxpσ1,p1=σ1,p[ X1,p1(p1)xp ]2(p1)p

Combining two simulations#

Another frequently occurring problem is, that the fluctuations of two simulations must be combined. Consider the following example: we have two simulations (A) of n and (B) of m steps, in which the second simulation is a continuation of the first. However, the second simulation starts numbering from 1 instead of from n+1. For the partial sum this is no problem, we have to add X1,nA from run A:

(503)#X1,n+mAB = X1,nA+X1,mB

When we want to compute the partial variance from the two components we have to make a correction Δσ:

(504)#σ1,n+mAB = σ1,nA+σ1,mB+Δσ

if we define xiAB as the combined and renumbered set of data points we can write:

(505)#σ1,n+mAB = i=1n+m[xiABX1,n+mABn+m]2

and thus

(506)#i=1n+m[xiABX1,n+mABn+m]2 = i=1n[xiAX1,nAn]2+i=1m[xiBX1,mBm]2+Δσ

or

(507)#i=1n+m[(xiAB)22xiABX1,n+mABn+m+(X1,n+mABn+m)2]i=1n[(xiA)22xiAX1,nAn+(X1,nAn)2]i=1m[(xiB)22xiBX1,mBm+(X1,mBm)2]=Δσ

all the xi2 terms drop out, and the terms independent of the summation counter i can be simplified:

(508)#(X1,n+mAB)2n+m(X1,nA)2n(X1,mB)2m2X1,n+mABn+mi=1n+mxiAB+2X1,nAni=1nxiA+2X1,mBmi=1mxiB=Δσ

we recognize the three partial sums on the second line and use (503) to obtain:

(509)#Δσ = (mX1,nAnX1,mB)2nm(n+m)

if we check this by inserting m=1 we get back (496)

Summing energy terms#

The gmx energy program can also sum energy terms into one, e.g. potential + kinetic = total. For the partial averages this is again easy if we have S energy components s:

(510)#Xm,nS = i=mns=1Sxis = s=1Si=mnxis = s=1SXm,ns

For the fluctuations it is less trivial again, considering for example that the fluctuation in potential and kinetic energy should cancel. Nevertheless we can try the same approach as before by writing:

(511)#σm,nS = s=1Sσm,ns+Δσ

if we fill in (492):

(512)#i=mn[(s=1Sxis)Xm,nSmn+1]2 = s=1Si=mn[(xis)Xm,nsmn+1]2+Δσ

which we can expand to:

(513)# i=mn[s=1S(xis)2+(Xm,nSmn+1)22(Xm,nSmn+1s=1Sxis+s=1Ss=s+1Sxisxis)]s=1Si=mn[(xis)22Xm,nsmn+1xis+(Xm,nsmn+1)2] = Δσ

the terms with (xis)2 cancel, so that we can simplify to:

(514)# (Xm,nS)2mn+12Xm,nSmn+1i=mns=1Sxis2i=mns=1Ss=s+1Sxisxis s=1Si=mn[2Xm,nsmn+1xis+(Xm,nsmn+1)2] = Δσ

or

(515)#(Xm,nS)2mn+12i=mns=1Ss=s+1Sxisxis+s=1S(Xm,ns)2mn+1 = Δσ

If we now expand the first term using (510) we obtain:

(516)#(s=1SXm,ns)2mn+12i=mns=1Ss=s+1Sxisxis+s=1S(Xm,ns)2mn+1 = Δσ

which we can reformulate to:

(517)#2[s=1Ss=s+1SXm,nsXm,ns+i=mns=1Ss=s+1Sxisxis] = Δσ

or

(518)#2[s=1SXm,nss=s+1SXm,ns+s=1Si=mnxiss=s+1Sxis] = Δσ

which gives

(519)#2s=1S[Xm,nss=s+1Si=mnxis+i=mnxiss=s+1Sxis] = Δσ

Since we need all data points i to evaluate this, in general this is not possible. We can then make an estimate of σm,nS using only the data points that are available using the left hand side of (512). While the average can be computed using all time steps in the simulation, the accuracy of the fluctuations is thus limited by the frequency with which energies are saved. Since this can be easily done with a program such as xmgr this is not built-in in GROMACS.