# Averages and fluctuations¶

## Formulae for averaging¶

**Note:** this section was taken from ref 179.

When analyzing a MD trajectory averages \(\left<x\right>\) and fluctuations

of a quantity \(x\) are to be computed. The variance \(\sigma_x\) of a series of N\(_x\) values, {\(x_i\)}, can be computed from

Unfortunately this formula is numerically not very accurate, especially when \(\sigma_x^{{\frac{1}{2}}}\) is small compared to the values of \(x_i\). The following (equivalent) expression is numerically more accurate

with

Using (2) and (4) one has to go through the series of \(x_i\) values twice, once to determine \(\left<x\right>\) and again to compute \(\sigma_x\), whereas (1) requires only one sequential scan of the series {\(x_i\)}. However, one may cast (2) in another form, containing partial sums, which allows for a sequential update algorithm. Define the partial sum

and the partial variance

It can be shown that

and

For \(n=1\) one finds

and for \(n=1\) and \(k=1\) (8) becomes

where we have used the relation

Using formulae (10) and (11) the average

and the fluctuation

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 \(\sigma_{1,m}\) and \(X_{1,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 (7) and (8) the average and standard deviation over part of the trajectory can be computed as:

or, more generally (with \(p \geq 1\) and \(q \geq p\)):

**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 \(X_{1,p-1}\) and \(\sigma_{1,p-1}\) from the
information at time \(p\) using (10) and
(11):

### 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 \(X_{1,n}^A\) from run A:

When we want to compute the partial variance from the two components we have to make a correction \(\Delta\sigma\):

if we define \(x_i^{AB}\) as the combined and renumbered set of data points we can write:

and thus

or

all the \(x_i^2\) terms drop out, and the terms independent of the summation counter \(i\) can be simplified:

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

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

### 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\):

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:

if we fill in (6):

which we can expand to:

the terms with \((x_i^s)^2\) cancel, so that we can simplify to:

or

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

which we can reformulate to:

or

which gives

Since we need all data points \(i\) to evaluate this, in general
this is not possible. We can then make an estimate of
\(\sigma_{m,n}^S\) using only the data points that are available
using the left hand side of (26).
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.